<?php
  global $FORM, $TABLE_PREFIX;
  require_once "lib/menus/default/list_functions.php";

  global $menu, $tableName, $schema;
  list($listDetails, $listRecords) = listMatchingRecords($tableName, @$FORM['page'], @$FORM['perPage'], @$FORM['keyword']);
  $listFields = preg_split("/\s*,\s*/", $schema['listPageFields']);  // fields to show on list page

  global $ACCOUNT_SCHEMA;
  $ACCOUNT_SCHEMA = loadSchema('accounts');

  showHeader();
?>


<form method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>" autocomplete="off" style="display: inline: margin: 0px">
<input type="submit" style="width: 0px; height: 0px; position: absolute; border: none" /> <!-- bugfix: hitting enter in textfield submits first submit button on form -->
<input type="hidden" name="menu" id="menu" value="<?php echo htmlspecialchars($menu) ?>" />
<input type="hidden" name="_defaultAction" value="list" />
<input type="hidden" name="page" value="<?php echo $listDetails['page'] ?>" />

<table border="0" cellspacing="0" cellpadding="0" width="690">
 <tr>
  <td class="title">
   <a href="?menu=<?php echo $menu ?>" ><?php echo $schema['menuName'] ?></a>

  </td>
  <td align="right">
   <?php if (!@$schema['_disableAdd']): ?>
     <input type="submit" name="action=add"  value="Create" class="inputButton" />
   <?php endif ?>
   <input type="submit" name="action=list" value="Refresh" class="inputButton" />
  </td>
 </tr>
</table>

<div class="divider_line"></div>

<!-- search and results bar -->
<table border="0" cellspacing="0" cellpadding="0" width="690">
 <tr>
  <td>

   <table border="0" cellspacing="0" cellpadding="0">
    <tr>
     <td>Search for keyword</td>
     <td>perPage</td>
     <td>&nbsp;</td>
    </tr>
    <tr>
     <td><input type="text" name="keyword" value="<?php echo htmlspecialchars(@$listDetails['keyword']) ?>" size="25" />&nbsp;</td>
     <td>
       <select name="perPage">
       <option <?php selectedIf($listDetails['perPage'], '5')?>>5</option>
       <option <?php selectedIf($listDetails['perPage'], '10')?>>10</option>
       <option <?php selectedIf($listDetails['perPage'], '25')?>>25</option>
       <option <?php selectedIf($listDetails['perPage'], '50')?>>50</option>
       <option <?php selectedIf($listDetails['perPage'], '100')?>>100</option>
       </select>&nbsp;
      </td>
     <td>
      <input type="submit" name="search"      value="Search" class="inputButton" />
      <input type="submit" name="resetSearch" value="Reset" class="inputButton" /><br />
     </td>
    </tr>
   </table>

  </td>
  <td align="right">
   <table border="0" cellspacing="0" cellpadding="1">
    <tr>
     <td class="lSearchText" align="center">
      <?php if (@$listDetails['keyword'] == ''): ?>
        Total Records: <?php echo $listDetails['totalRecords'] ?>
      <?php else: ?>
        Matched: <?php echo $listDetails['totalMatches'] ?> of <?php echo $listDetails['totalRecords'] ?>
      <?php endif ?>
     </td>
    </tr>
    <tr>
     <td align="center">
      <table border="0" cellspacing="0" cellpadding="0">
       <tr>
        <td class="lSearchText">Page&nbsp;</td>
        <td>
          <!-- This field is renamed to "page" when changed by user -->
          <input type="text" size="4" class="lSearchBox" name="unused" value="<?php echo $listDetails['page'] ?>" onchange="this.name='page';"/><br/>
        </td>
        <td class="lSearchText">&nbsp;of <?php echo $listDetails['totalPages'] ?> </td>
       </tr>
      </table>
     </td>
    </tr>
    <tr><td class="lSearchText noBreak" align="center">
     <a href="?menu=<?php echo htmlspecialchars($menu) ?>&amp;action=list&amp;page=<?php echo $listDetails['prevPage']?>&amp;perPage=<?php echo $listDetails['perPage']?>&amp;keyword=<?php echo urlencode(@$listDetails['keyword']) ?>">&lt;&lt; Prev Page</a> |
     <a href="?menu=<?php echo htmlspecialchars($menu) ?>&amp;action=list&amp;page=<?php echo $listDetails['nextPage']?>&amp;perPage=<?php echo $listDetails['perPage']?>&amp;keyword=<?php echo urlencode(@$listDetails['keyword']) ?>">Next Page &gt;&gt;</a><br />
    </td></tr>
   </table>

  </td>
 </tr>
</table>

<div class="divider_line"></div>

<!-- list column headings -->
<table border="0" cellspacing="1" cellpadding="0" width="690" id="listTable">
 <tr class="nodrag nodrop">
  <?php
    // categories - add move up/down links
    if (@$schema['menuType'] == 'category') {
      print "<td class=\"listHeader\" align='center' width=\"60\">Order</td>\n";
    }

    //
    foreach ($listFields as $fieldname):
      if (empty($fieldname)) { continue; }

      // get field label
      $label = @$schema[$fieldname]['label'];
      if (preg_match("/^createdBy\.(\w+)/", $fieldname, $matches)) {
        $accountFieldname = $matches[1];
        $label = @$ACCOUNT_SCHEMA[$accountFieldname]['label'];
      }

      //
      $tdAttributes = '';
      if ($fieldname == 'dragSortOrder') {
        if (@$schema['dragSortOrder']['adminOnly'] && !$GLOBALS['hasAdminAccess']) { continue; }
        $tdAttributes = 'width="40" align="center"';
        $label = "Drag";
      }
    ?>
    <td class="listHeader" <?php echo $tdAttributes ?>><?php echo $label ?></td>
  <?php endforeach; ?>
  <td class="listHeader" style="padding: 0px;" align="center" colspan="2">Action</td>
 </tr>

<?php foreach ($listRecords as $record):
      $bgColorClass = (@$bgColorClass == "listRowOdd") ? 'listRowEven' : 'listRowOdd'; # rotate bgclass
?>
 <tr>
  <?php
    // categories - add move up/down links
    if (@$schema['menuType'] == 'category') {
      $upLink = "?menu=" .htmlspecialchars($menu). "&action=categoryMove&direction=up&num={$record['num']}";
      $dnLink = "?menu=" .htmlspecialchars($menu). "&action=categoryMove&direction=down&num={$record['num']}";

      print "<td class='$bgColorClass' align=\"center\">";
      print "&nbsp;<a href=\"$upLink\">UP</a>&nbsp;";
      print "&nbsp;<a href=\"$dnLink\">DN</a>&nbsp;";
      print "</td>";

    }


    foreach ($listFields as $fieldname):
      $value        = @$record[$fieldname];
      $tdAttributes = "class='$bgColorClass'";
      if(@$schema[$fieldname]['type'] == 'date' && @!$schema[$fieldname]['showTime']) {
        $value = date("Y-m-d", strtotime($record[$fieldname]));
      }
      if($fieldname == 'dragSortOrder') {
        if (@$schema['dragSortOrder']['adminOnly'] && !$GLOBALS['hasAdminAccess']) { continue; }
        $tdAttributes = "class='$bgColorClass dragger' width='40' align='center'";
        $value        = "<input type='hidden' name='recordNum' value='{$record['num']}' class='recordNum' />";
        $value       .= "<img src='images/drag.gif' height='6' width='19' class='dragger' alt='Order $record[$fieldname]' />";
      }

    // pad category names to their depth
    if (@$schema['menuType'] == 'category' && $fieldname == 'name' && @$record['depth']) {
      $padding = str_repeat("&nbsp; &nbsp; &nbsp;", @$record['depth']);
      $value   = $padding . " - " . $value;
    }

    // display first thumbnail for upload fields
    if (@$schema[$fieldname]['type'] == 'upload') {
      $query   = "SELECT * FROM `{$TABLE_PREFIX}uploads`\n";
      $query  .= " WHERE tableName = '$tableName' AND fieldName = '$fieldname' AND recordNum = {$record['num']}\n";
      $query  .= " ORDER BY `order`, num";
      $result  = mysql_query($query) or die("MySQL Error: ". htmlspecialchars(mysql_error()) . "\n");
      while ($upload = mysql_fetch_assoc($result)) {
        if (!$upload['thumbUrlPath']) { continue; }
        $value = "<img src='{$upload['thumbUrlPath']}' width='{$upload['thumbWidth']}'' height='{$upload['thumbHeight']}' alt='' /><br/>\n";
        break;
      }
      if (is_resource($result)) { mysql_free_result($result); }
    }

  ?>
    <td <?php echo $tdAttributes ?>><?php echo $value ?></td>
  <?php endforeach; ?>
  <?php
    if (!@$schema['_disableErase']) { $tableCellAttributes = "width='64' class='$bgColorClass' colspan='1' align='center'"; }
    else                            { $tableCellAttributes = "width='90' class='$bgColorClass' colspan='2' align='center'"; }

    // show modify link
    $modifyLink = "?menu=" .htmlspecialchars($menu). "&amp;action=edit&amp;num={$record['num']}";
    print "<td $tableCellAttributes><a href=\"$modifyLink\">modify</a></td>\n";

    // show erase link
    if (!@$schema['_disableErase']) {
      $eraseLink  = "javascript:confirmEraseRecord('" .htmlspecialchars($menu). "','{$record['num']}');";
      print "<td $tableCellAttributes><a href=\"$eraseLink\">erase</a></td>\n";
    }
  ?>
 </tr>
<?php endforeach ?>


<?php if (count($listRecords) == 0): ?>
  <tr>
   <td class="listRowOdd" colspan="<?php print count($listFields) + 2 ?>">
   <?php if (@$listDetails['keyword'] == ''): ?>  Sorry, no records were found!  <?php endif ?>
   <?php if (@$listDetails['keyword'] != ''): ?>  Sorry, the <b>search</b> returned no results! <?php endif ?>
   </td>
  </tr>
<?php endif ?>

</table>

<div class="divider_line"></div>

<table border="0" cellspacing="0" cellpadding="0" width="690">
 <tr>
  <td align="left">
  </td>
  <td align="right">
   <?php if (!@$schema['_disableAdd']): ?>
     <input type="submit" name="action=add"  value="Create" class="inputButton" />
   <?php endif ?>
   <input type="submit" name="action=list" value="Refresh" class="inputButton" />
  </td>
 </tr>
</table>

</form>

<script type="text/javascript" src="lib/jquery1.2.js"></script>
<script type="text/javascript" src="lib/jquery.tablednd.js"></script>
<script type="text/javascript" src="lib/menus/default/list_functions.js"></script>

<?php showFooter(); ?>
