<?php
// === Просмотр списка уравнений ===
define('IN_EqWorld_AddEqs', true);
require_once('templates/header_prep.php');
$_SESSION['act']['action']='list'; // Тип дейстивия
$AddEqs_title_1 = 'List of Equations'; // Вторичный заголовок
$AddEqs_header_type = 'nav_top'; // Вид навигции
$AddEqs_form_submit = 'list.php'; // куда из форм
$_SESSION['links']['name'] = ''; // не может быть промежуточных родителей
$_SESSION['links']['url'] = ''; // не может быть промежуточных родителей
require_once('templates/header.php');

require_once($AddEqs_root_path.'core/latex.php');

// проверка входных параметров
$v_sect = ( isset($_REQUEST['secti']) && $_REQUEST['secti']!=='' && is_numeric($_REQUEST['secti']) ) ? (int) $_REQUEST['secti'] : -1;
$v_subsect = ( isset($_REQUEST['subsect_'.$v_sect]) && $_REQUEST['subsect_'.$v_sect]!=='' && is_numeric($_REQUEST['subsect_'.$v_sect]) ) ? (int) $_REQUEST['subsect_'.$v_sect] : -1;
$v_limit = ( isset($_REQUEST['limit']) && $_REQUEST['limit']!=='' && is_numeric($_REQUEST['limit']) ) ? (int) $_REQUEST['limit'] : 10;
$v_page = ( isset($_REQUEST['page']) && $_REQUEST['page']!=='' && is_numeric($_REQUEST['page']) ) ? (int) $_REQUEST['page'] : 0;
$v_sort1 = ( isset($_REQUEST['sort1']) ) ? (int) $_REQUEST['sort1'] : 1;
$v_order1 = ( isset($_REQUEST['order1']) ) ? (int) $_REQUEST['order1'] : 1;
$v_sort2 = ( isset($_REQUEST['sort2']) ) ? (int) $_REQUEST['sort2'] : 4;
$v_order2 = ( isset($_REQUEST['order2']) ) ? (int) $_REQUEST['order2'] : 1;
$v_activate = ( isset($_REQUEST['activate']) && $_REQUEST['activate']!=='' ) ? (int) $_REQUEST['activate'] : 0;

// проверка корректности входных параметров
if( $v_sect<-1 || $v_sect>=count($section) )
 $v_sect = -1;
if( $v_sect<0 || $v_subsect<-1 || $v_subsect>count($section[$v_sect][1]) )
 $v_subsect=-1;
if( $v_limit<0 )
 $v_limit=0;
if( $v_page<0 )
 $v_page=0;

// просмотр активированных только для админов
if( $_SESSION['act']['priv']!=='admin' )
 $v_activate=0;

// получаем данные из БД
$db_result = '';
$db_result = EWAE_DB_Data_GetBySect($v_sect,$v_subsect,$v_sort1,$v_order1,$v_sort2,$v_order2,$v_activate);
$n_page_rows = mysql_num_rows($db_result);
$v_real_limit = ( $v_limit<=0 ) ? $n_page_rows : $v_limit; // выводим все или заданное число

// число страниц с таким $v_limit
if( $v_limit<=0 )
{
 $n_pages=1;
 $v_start=0;
}
else
{
 $n_pages = ceil( $n_page_rows / $v_limit );
 // хватит ли данных на
 if( $v_page>=$n_pages )
  $v_page = $n_pages-1;
 // стартовая запись
 $v_start = $v_page*$v_limit;
}
?>
<script type="text/javascript" language="javascript"><!--
function select_section()
{
 n = <?php print count($section)?>;
 // получаем выбранный section
 s=document.fm.secti.selectedIndex-1;
 // скрываем все subsections
 for(i=0;i<n;i++)
 {
  str="document.fm.subsect_"+i+".style.display=";
  if(i==s)
   str=str+"''";
  else
   str=str+"'none'";
  eval(str);
 }
}
--></script>

<form action="<?php print $AddEqs_form_submit?>" method="post" name="fm">
<table class="EWae_form" cellpadding="0" cellspacing="1">
<tr><th class="hl">Selection options</th></tr>
<tr><td nowrap>Categories:&nbsp;<select name="secti" size="1" onChange="select_section()">
<option value="-1">--- All categories ---</option>
<?php
 for($i=0;$i<count($section);$i++)
  print "<option value=\"$i\"". ($v_sect==$i ? ' selected' : '') .'>'. $section[$i][0] ."</option>\n";
?>
</select><br>
Subcategories:&nbsp;<?php
 for($i=0;$i<count($section);$i++)
 {
  print "<select ". ($v_sect==$i ? '' : 'style="display:none" ') ."name=\"subsect_$i\" size=\"1\">\n"
    . "<option value=\"-1\">--- All subcategories for ". ($i+1) .". ---</option>\n";
  for($j=0;$j<count($section[$i][1]);$j++)
   print "<option value=\"$j\"". (($v_sect==$i && $v_subsect==$j) ? ' selected' : '') .">". $section[$i][1][$j] ."</option>\n";
  print "</select>";
 }
 print "\n";
?></td></tr>
<tr><td nowrap style="font-size:90%">Sorting&nbsp;1&nbsp;by:&nbsp;<select name="sort1" size="1">
<option value="0" <?php if($v_sort1<1 || $v_sort1>4) print ' selected'?>>-- no sorting --</option>
<option value="1" <?php if($v_sort1==1) print ' selected'?>>categories/subcategories</option>
<option value="2" <?php if($v_sort1==2) print ' selected'?>>last name</option>
<option value="3" <?php if($v_sort1==3) print ' selected'?>>date of submission</option>
<option value="4" <?php if($v_sort1==4) print ' selected'?>>date of update</option>
</select>&nbsp;&nbsp;&nbsp;&nbsp;Order:&nbsp;<select name="order1" size="1">
<option value="1" <?php if($v_order1>=0) print ' selected'?>>ascending</option>
<option value="-1" <?php if($v_order1<-0) print ' selected'?>>descending</option></select><br>
Sorting&nbsp;2&nbsp;by:&nbsp;<select name="sort2" size="1">
<option value="0" <?php if($v_sort2<1 || $v_sort2>4) print ' selected'?>>-- no sorting --</option>
<option value="1" <?php if($v_sort2==1) print ' selected'?>>categories/subcategories</option>
<option value="2" <?php if($v_sort2==2) print ' selected'?>>last name</option>
<option value="3" <?php if($v_sort2==3) print ' selected'?>>date of submission</option>
<option value="4" <?php if($v_sort2==4) print ' selected'?>>date of update</option>
</select>&nbsp;&nbsp;&nbsp;&nbsp;Order:&nbsp;<select name="order2" size="1">
<option value="1" <?php if($v_order2>=0) print ' selected'?>>ascending</option>
<option value="-1" <?php if($v_order2<-0) print ' selected'?>>descending</option></select><br>
Equations per page (0-all):&nbsp;<input name="limit" type="text" value="<?php print $v_limit?>" size="5" maxlength="5">
<?php
if( $_SESSION['act']['priv']==='admin' )
{
 print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="EWae_red">Activated:&nbsp;<select name="activate" size="1">'
    . '<option value="1" '. (($v_activate>0) ? ' selected' : '') .'>all</option>'
    . '<option value="0" '. (($v_activate==0) ? ' selected' : '') .'>active</option>'
    . '<option value="-1" '. (($v_activate<0) ? ' selected' : '') .'>non active</option>'
    . '</select></span>';
}
?></td></tr>
<tr><td class="hl" align="center"><input name="submit" type="submit" value="Submit"></td></tr>
<tr><td align="center">See also: <a href="list_sec.php">Categorized List of Equations</a></td></tr>
</table></form>

<?php print (($v_sect>=0) ? '<h3>'.$section[$v_sect][0].'</h3>' : '')//'<i>--- All categories ---</i>')?>
<?php print (($v_sect>=0 && $v_subsect>=0) ? '<h4>'.$section[$v_sect][1][$v_subsect].'</h4>' : '')//'<i>--- All subcategories ---</i>')?>

<?php
$pages_links_txt = "Found <b>$n_page_rows</b> equations";
$pages_links_urlparm = 'secti='. htmlentities(urlencode($v_sect))
    .'&subsect_'.$v_sect.'='. htmlentities(urlencode($v_subsect))
    .'&limit='. htmlentities(urlencode($v_limit))
    .'&sort1='. htmlentities(urlencode($v_sort1))
    .'&order1='. htmlentities(urlencode($v_order1))
    .'&sort2='. htmlentities(urlencode($v_sort2))
    .'&order2='. htmlentities(urlencode($v_order2));
if( $_SESSION['act']['priv']==='admin' )
 $pages_links_urlparm .= '&activate='.  htmlentities(urlencode($v_activate));
if( $n_pages>1 )
{
 $pages_links_txt .= ", <b>$n_pages</b> pages (<b>$v_limit</b> eqs. per page)";
 $pages_links_txt .= ': <span style="font-size:90%">';
 if( $v_page>0)
  $pages_links_txt .= '<a href="'.$AddEqs_form_submit.'?'.$pages_links_urlparm.'&page='.htmlentities(urlencode($v_page-1)).'" title="previouse page">&lt;&lt;</a> ';
 else
  $pages_links_txt .= '<span title="previouse page" style="color:#666666">&lt;&lt;</span> ';
 for($i=0;$i<$n_pages; $i++)
  $pages_links_txt .= ($i!=$v_page)
    ? '<a href="'.$AddEqs_form_submit.'?'.$pages_links_urlparm.'&page='.htmlentities(urlencode($i)).'">'.($i+1).'</a> '
    : '<b>'.($i+1).'</b> ';
 if( $v_page<$n_pages-1)
  $pages_links_txt .= '<a href="'.$AddEqs_form_submit.'?'.$pages_links_urlparm.'&page='.htmlentities(urlencode($v_page+1)).'" title="next page">&gt;&gt;</a> ';
 else
  $pages_links_txt .= '<span title="next page" style="color:#666666">&gt;&gt;</span> ';
 $pages_links_txt .= "</span>\n";
}
$pages_links_urlparm .= '&page='.htmlentities(urlencode($v_page)); // теперь часть ссылки на текущую страницы
print $pages_links_txt;

$_SESSION['links']['name'] = $AddEqs_title_1; // для потомков мы промежуточные родители
$_SESSION['links']['url'] = $AddEqs_form_submit.'?'.$pages_links_urlparm; // для потомков мы промежуточные родители


include($AddEqs_root_path.'core/please_wait_on.php');
?>
<table border="0" class="EWae_form" cellspacing="1">
<tr><th class="hl">&nbsp;</th><?php if($v_sect<0 || $v_subsect<0) print '<th class="hl">Category</th>' ?><th class="hl">Equation(s)</th><th class="hl">Author/<br>Contributor</th><th class="hl">Actions</th><?php if( $_SESSION['act']['priv']==='admin' ) print '<th class="hl">(ID)<br>Active</th>';?></tr>
<?php
 // выводим извинения, если нет уравнений
 if( $n_page_rows<=0 )
 {
  // число столбцов
  $n = 4; // No + Equation + Author/Contributor + Actions
  if($v_sect<0 || $v_subsect<0)
   $n += 1; // Category
  if( $_SESSION['act']['priv']==='admin' )
   $n += 1; // ID/Active
  print "<tr><td colspan=\"$n\" align=\"center\">&nbsp;&nbsp;&nbsp;No equations found for the selection criteria specified&nbsp;&nbsp;&nbsp;</td></tr>\n";
 }
 // выводим уравнения, если их больше нуля
 else
 {
  // пропускаем предыдущие страницы
  for($n_row=1;$n_row<=$v_start;$n_row++)
   $row = @mysql_fetch_assoc($db_result);
  // выводим текущую страницу
  for( ; ($n_row<=$v_start+$v_real_limit) && ($row=@mysql_fetch_assoc($db_result)); $n_row++ )
  {
   $tmp = ($n_row % 2 != 0) ? '' : ' class="hl2" ';
   print '<tr>';
   // No
   print "<td $tmp align=\"right\">$n_row</td>\n";
   //  Category
   if( $v_sect<0 || $v_subsect<0 )
    print "<td $tmp style=\"font-size:80%\">"
        . '<b style="white-space: nowrap">'. $section[ $row['eqSect'] ][0] .'</b><br>'
        . $section[ $row['eqSect'] ][1][ $row['eqSubSect'] ]
        . "</td>\n";
   // Equation
   print "<td nowrap $tmp>";
   print EWAE_latexrender($row['eqForm']);
   print "</td>\n";
   // Author/Contributor
   print "<td align=\"center\" nowrap $tmp>"
        . '<span style="font-size:80%">'
        . '<span title="First Name">'.htmlspecialchars($row['uNameFirst']) .'</span>'
        . ' <span title="Middle Name(s)">'. htmlspecialchars($row['uNameMid']) .'</span>'
        . ' <b title="Last Name">'. htmlspecialchars($row['uNameLast']) .'</b>'
        . '</span>';
   if( $_SESSION['act']['priv']==='admin' )
    print '<br><a href="mailto:'. htmlspecialchars($row['uEmail']) .'" style="font-size:70%">'. $row['uEmail']  .'</a>';
   print '<br><span style="font-size:70%" title="Date of first submission">'
        . 'Submitted: '. date('d M y H:i', $row['tmCreate'])
        . '</span>';
   if( $row['numEditAuth']>0 && $row['tmEditAuth']>0 )
    print '<br><span style="font-size:70%" title="Date of last revision by author">'
        . 'Edited (author): '. date('d M y H:i', $row['tmEditAuth'])
        . '</span>';
   if( $row['numEditAdmin']>0 && $row['tmEditAdmin']>0 )
    print '<br><span style="font-size:70%" title="Date of last modification by admin">'
        . 'Edited (admin): '. date('d M y H:i', $row['tmEditAdmin'])
        . '</span>';
   print "</td>\n";
   // Actions
   print "<td align=\"center\" nowrap $tmp style=\"font-size:80%\">"
        . '<a href="view.php?id='. htmlentities(urlencode($row['id'])) .'" title="For all">Details</a><br>'
        . '<a href="edit.php?id='. htmlentities(urlencode($row['id'])) .'" title="Only for Author/Contributor">Edit</a><br>';
   if( $_SESSION['act']['priv']==='admin' )
   {
    print '<a href="'.$AddEqs_form_submit.'?adminto='. htmlentities(urlencode('delete')) .'&id='. htmlentities(urlencode($row['id'])) .'&'.$pages_links_urlparm."\" onClick=\"return window.confirm('Do you want to DELETE equation with id=".$row['id']."?');\">Delete</a><br>";
    if( empty($row['uActivate']) )
     print '<a href="'.$AddEqs_form_submit.'?adminto='. htmlentities(urlencode('activate')) .'&id='. htmlentities(urlencode($row['id'])) .'&'.$pages_links_urlparm.'">Активировать</a><br>';
    else
     print '<a href="'.$AddEqs_form_submit.'?adminto='. htmlentities(urlencode('deactivate')) .'&id='. htmlentities(urlencode($row['id'])) .'&'.$pages_links_urlparm.'">Деактивировать</a><br>';
   }
   print '</td>';
   // Активирован
   if( $_SESSION['act']['priv']==='admin' )
    print "<td align=\"center\" nowrap $tmp><span title=\"id\">(".$row['id'].')</span><br><span title="Active">'. (empty($row['uActivate']) ? '<span class="EWae_red">none</span>' : 'done') .'</span></td>';
   print "</tr>\n";
  } // for(...)
 } // else...
 mysql_free_result($db_result);
?>
</table>
<?php
print $pages_links_txt;

include($AddEqs_root_path.'core/please_wait_off.php');
require_once('templates/footer.php');
?>
