<?php

  global $tableNameWithoutPrefix, $libDirPath, $schema, $menuName;
  $tableNameWithoutPrefix = getTableNameWithoutPrefix($FORM['tableName']);
  $schema                 = loadSchema($tableNameWithoutPrefix);
  $libDirPath             = pathinfo($_SERVER["SCRIPT_FILENAME"], PATHINFO_DIRNAME) . "/lib/";
  $libDirPath             = preg_replace('/[\\\\\/]+/', '/', $libDirPath); // replace and collapse slashes
  $menuName               = @$schema['menuName'] ? $schema['menuName'] : $tableNameWithoutPrefix;

//
function showHtmlHeader() {
?>&lt;?php echo "&lt;?xml version='1.0'?>\n"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  <style type="text/css">
    body          { font-family: arial; }
    .instructions { border: 3px solid #000; background-color: #EEE; padding: 10px; text-align: left; margin: 25px}
  </style>
 </head>
<body><?php
}


//
function showHtmlFooter() { ?>



<div style="float: right; font-size: 10px; color: #999999">
  <?php echo @$GLOBALS['SETTINGS']['vendorPoweredBy']; ?>

</div>

</body>
</html>
<?php
}

//
function showCodeForListViewer() { ?>


  <!-- INSTRUCTIONS -->
    <div class="instructions">
      <b>Sample List Viewer - Instructions:</b>
      <ol>
        <li><b>Remove any fields you don't want displayed.</b> (Most list pages only have title and link fields.)</li>
        <li>Rearrange remaining fields to suit your needs.</li>
        <li>Copy and paste code into previously designed page (or add design to this page).</li>
      </ol>
    </div>
  <!-- /INSTRUCTIONS -->



  <!-- STEP1: Load Record List (Paste this above other steps) -->
    &lt;?php
      require_once "<?php echo $GLOBALS['libDirPath']; ?>viewer_functions.php";
      $options = array();                  // NOTE: see online documentation for more details on these options
      $options['tableName']  = '<?php echo $GLOBALS['tableNameWithoutPrefix']; ?>';  // (REQUIRED) MySQL tablename to list record from. Example: 'article';
      $options['titleField'] = '<?php if (@$GLOBALS['schema']['title']) { echo "title"; } ?>';    // (optional) MySQL fieldname used in viewer url for search engines. Example: 'title' would display: viewer.php/article_title_here-123
      $options['viewerUrl']  = '<?php echo $GLOBALS['tableNameWithoutPrefix']; ?>Page.php'; // (optional) URL of viewer page. Example: '/articles/view.php';
      $options['perPage']    = '';         // (optional) The number of records to display per page. Example: '5'; Defaults to 10.
      $options['orderBy']    = '<?php echo @$GLOBALS['schema']['listPageOrder']; ?>';         // (optional) Fieldnames to sort by. Example: 'field1, field2 DESC, field3';
      $options['pageNum']    = '';         // (optional) Page number of results to display. Example: '1'; Defaults to number on end of url, then 1
      $options['where']      = '';         // (ADVANCED) Additional MySQL WHERE conditions. Example: 'fieldname = "value"'
      $options['useSeoUrls'] = '';         // (ADVANCED) Set this to '1' for search engine friendly urls: view.php/123 instead of view.php?123 (not supported on all web servers, and viewerUrl must be absolute path such as '/path/viewer.php')
      list($listRows, $listDetails) = getListRows($options);
    ?>
  <!-- /STEP1: Load Record List -->



  <!-- STEP2: Display Record List (Paste this where you want your records to be listed) -->
    <h1><?php echo $GLOBALS['menuName']; ?> List Viewer</h1>
    &lt;?php foreach ($listRows as $record): ?>
    <?php foreach (_getFieldCodeArray() as $fieldCode): ?>
  <?php echo $fieldCode; ?>
    <?php endforeach ?>
  link : <a href="&lt;?php echo $record['_link'] ?>">&lt;?php echo $record['_link'] ?></a><br/>
    <hr/>
    &lt;?php endforeach ?>

    &lt;?php if ($listDetails['noRecordsFound']): ?>
      No records were found!<br/>
    &lt;?php endif ?>

    &lt;?php if ($listDetails['invalidPageNum']): ?>
      Results page not found, <a href="&lt;?php echo $listDetails['firstPageLink'] ?>">start over &gt;&gt;</a>.<br/>
    &lt;?php endif ?><br/>
  <!-- /STEP2: Display Record List -->




  <!-- STEP3: Display Page Links (Paste anywhere below "Load Record List") -->
    &lt;?php if ($listDetails['prevPage']): ?>
      <a href="&lt;?php echo $listDetails['prevPageLink'] ?>">&lt;&lt; prev</a>
    &lt;?php else: ?>
      &lt;&lt; prev
    &lt;?php endif ?>

    - page &lt;?php echo $listDetails['page'] ?> of &lt;?php echo $listDetails['totalPages'] ?> -

    &lt;?php if ($listDetails['nextPage']): ?>
      <a href="&lt;?php echo $listDetails['nextPageLink'] ?>">next &gt;&gt;</a>
    &lt;?php else: ?>
      next &gt;&gt;
    &lt;?php endif ?>
  <!-- /STEP3: Display Page Links -->
<?php
}


//
function showCodeForPageViewer() { ?>


  <!-- INSTRUCTIONS -->
    <div class="instructions">
      <b>Sample Page Viewer - Instructions:</b>
      <ol>
        <li>Remove any fields you don't want displayed.</li>
        <li>Rearrange remaining fields to suit your needs.</li>
        <li>Copy and paste code into previously designed page (or add design to this page).</li>
      </ol>
    </div>
  <!-- /INSTRUCTIONS -->



  <!-- STEP1: Load Record (Paste this above other step) -->
    &lt;?php
      require_once "<?php echo $GLOBALS['libDirPath']; ?>viewer_functions.php";
      $options = array();                // NOTE: see online documentation for more details on these options
      $options['tableName'] = '<?php echo $GLOBALS['tableNameWithoutPrefix']; ?>'; // (REQUIRED) MySQL tablename to list record from. Example: "article";
      $options['recordNum'] = '';        // (optional) Record number to display.  Example: "1"; Defaults to number on end of url, then 1
      $options['where']     = '';        // (ADVANCED) MySQL WHERE conditions to use INSTEAD of recordNum to look up record. Example: "fieldname = 'value'"
      $record = getRecord($options);
    ?>
  <!-- /STEP1: Load Record -->



  <!-- STEP2: Display Record (Paste this where you want your record displayed and re-arrange fields) -->
  <h1><?php echo $GLOBALS['menuName']; ?> Page Viewer</h1>
    &lt;?php if ($record): ?>
    <?php foreach (_getFieldCodeArray() as $fieldCode): ?>
  <?php echo $fieldCode; ?>
    <?php endforeach ?>
&lt;?php endif ?>

    &lt;?php if (empty($record)): ?><!-- Display "No Records Found" Message -->
      No record found!<br/><br/>
    &lt;?php endif ?>
  <!-- STEP2: /Display Record -->
<?php // NOTE: THE SPACING ABOVE IS OFF HERE, BUT CORRECT IN GENERATED CODE!!!
}



//
function showCodeForDisplayUploads() {
  $fieldCode  = "";

  // get code for each schema fields
  foreach ($GLOBALS['schema'] as $fieldname => $fieldSchema) {
    if (@$fieldSchema['type'] != 'upload') { continue; } // skip all but upload fields
    $labelOrName = @$fieldSchema['label'] ? $fieldSchema['label'] : $fieldname;
?>



  <!-- STEP3: Display Uploads from '<?php echo $fieldname; ?>' (Paste this where you want your uploads displayed) -->
    <!-- Upload Program Fields : num, createdTime, tableName, fieldName, recordNum, preSaveTempId, filePath, filename, extension, thumbFilePath -->
    <!-- Upload Image Fields   : isImage, hasThumbnail, urlPath, width, height, thumbUrlPath, thumbWidth, thumbHeight -->
    <!-- Upload Info Fields    : info1, info2, info3, info4, info5 -->
    &lt;?php foreach (getUploads($options['tableName'], '<?php echo $fieldname; ?>', $record['num']) as $upload): ?>
      &lt;?php if ($upload['hasThumbnail']): ?>
        <img src="&lt;?php echo $upload['thumbUrlPath'] ?>" width="&lt;?php echo $upload['thumbWidth'] ?>" height="&lt;?php echo $upload['thumbHeight'] ?>" /><br/>

      &lt;?php elseif ($upload['isImage']): ?>
        <img src="&lt;?php echo $upload['urlPath'] ?>" width="&lt;?php echo $upload['width'] ?>" height="&lt;?php echo $upload['height'] ?>" /><br/>

      &lt;?php else: ?>
        <a href="&lt;?php echo $upload['urlPath'] ?>">Download &lt;?php echo $upload['filename'] ?></a><br/>

      &lt;?php endif ?>
    &lt;?php endforeach ?>
  <!-- STEP3: /Display Uploads from '<?php echo $fieldname; ?>' -->
<?php
  }
}


//
function _getFieldCodeArray() {
  $fieldCode         = array();
  $fieldTypesSkipped = array('none', 'upload', 'separator');
  $fieldNamesSkipped = array('hidden', 'autoPublishStartDate', 'autoPublishEndDate', 'autoPublishNeverExpires');

  // get code for each schema fields
  foreach ($GLOBALS['schema'] as $fieldname => $fieldSchema) {
    $skipField = !is_array($fieldSchema) ||                             // not a field definition, table metadata field
                 in_array($fieldSchema['type'], $fieldTypesSkipped) ||  // skip field types that aren't displayed
                 in_array($fieldname, $fieldNamesSkipped);              // skip internal field names that aren't displayed
    if ($skipField && $fieldname != 'num') { continue; }

    // get field code
    $labelOrName = @$fieldSchema['label'] ? $fieldSchema['label'] : $fieldname;
    if ($fieldSchema['type'] == 'date') {
      array_push($fieldCode, "$labelOrName: <?php echo date(\"D, M jS, Y G:H:s a\", strtotime(\$record['$fieldname'])) ?><br/>\n");
      array_push($fieldCode, "      <!-- For date formatting codes see: http://www.php.net/date -->\n");
    }
    else {
      array_push($fieldCode, "$labelOrName: <?php echo \$record['$fieldname'] ?><br/>\n");
    }
  }

  return $fieldCode;
}

?>
