<?php
///////////////////////////////////////////////////
//2009 (C)
//Карпухин Павел
///////////////////////////////////////////////////

require_once "utils/chlang_osn.php";

$title=$TEXT['global-title'];

require_once "config.php";
require_once "utils/functions.php";
require_once "utils/top.php";

//// проверка на наличие обновлений - перенёс в bottom.php
//if($result2['chk_upd'] == "yes"){
// $vis_upd = update_check("http://tel-spr.narod.ru/library-upd.txt",$int_proxy);
//  if($vis_upd !== $TEXT['global-now-version']) {echo "<div id='formaupdate'>".$TEXT['global-new-version-yes']."<a href='http://tel-spr.narod.ru/library/changelog.html' target=_blank>".$vis_upd."</a></div>";}
//  if($vis_upd == "") {echo "<div id='formaupdate'>".$TEXT['global-new-version-no']."</div>";}
//}

if($result2['administrator'] == 0 && $result2['user'] == 0) {puterror2($result2['skin'],$TEXT['error-loginform2']); require_once "utils/bottom.php"; exit();}
if(strlen($_GET['k'])>8) {puterror($result2['skin'],$TEXT['error-paging1']); require_once "../utils/bottom_adm.php"; exit();}
$_GET['k'] = rep($_GET['k']);
if(empty($_GET['k'])) {$_GET['k']=0;}
$stop_r = $_GET['k'];
if($stop_r == 0) {$lim_ogr = "5";}
else {$lim_ogr = $stop_r.",5";}

$ktblvis = $stop_r+5;
$str = $ktblvis/5;

  $query_cl = "SELECT * FROM `tbl_clear`";
  $result_cl = mysqli_query($dbcnx,$query_cl);
    if(!$result_cl) {puterror2($result2['skin'],$TEXT['error-tbl20']); require_once "utils/bottom.php"; exit();}
     if(mysqli_num_rows($result_cl)>0)
         {
          while($result_cl2 = mysqli_fetch_array($result_cl))
           {
           $tbl_name = $result_cl2['col_tbl_name'];
           $tbl_col = $result_cl2['col_name'];
           $tbl_day = $result_cl2['by_data'];
           $tbl_rows = $result_cl2['by_count'];

           $query_log_coun = mysqli_query($dbcnx,"SELECT COUNT($tbl_col) AS c_tbl FROM $tbl_name");
           $coun_log = mysqli_fetch_array($query_log_coun);
            if($coun_log['c_tbl'] > 0)
           {
           $query10 = "SELECT ADDDATE((SELECT DATE((SELECT MAX($tbl_col) FROM $tbl_name))),-$tbl_day)";
           $result10_del_cl1 = mysqli_query($dbcnx,$query10);
            if(!$result10_del_cl1) {puterror2($result2['skin'],$TEXT['error-tbl20']); require_once "utils/bottom.php"; exit();}
             $r2 = mysqli_fetch_array($result10_del_cl1);
             if($result_cl2['by_data'] > 0)
             {
             //удалить всё если отклонение в днях больше указанного
              $query_del_cl1 = "DELETE FROM `$tbl_name` WHERE `".$tbl_col."` < '".$r2['0']."'";
               $result_del_cl1 = mysqli_query($dbcnx,$query_del_cl1);
                if (!$result_del_cl1) {puterror2($result2['skin'],$TEXT['error-tbl20']); require_once "utils/bottom.php"; exit();}
             }
             if($result_cl2['by_count'] > 0)
             {
             if($coun_log['c_tbl'] > $tbl_rows)
             {
             //удалить все если отклонение в количестве строк больше указанного
             $del_rows = $coun_log['c_tbl'] - $tbl_rows;
               $query_del_cl2 = "DELETE FROM `$tbl_name` ORDER BY data LIMIT $del_rows";
                $result_del_cl2 = mysqli_query($dbcnx,$query_del_cl2);
                if (!$result_del_cl2) {puterror2($result2['skin'],$TEXT['error-tbl20']); require_once "utils/bottom.php"; exit();}
              }
             }
           }
        }
      }


?>
<table align="center" width="98%" border="0">
<tr>
<td width="45%" valign="top" align="left">
<fieldset>
 <legend align="left" title="<?php echo $TEXT['book-new-tit']; ?>"><?php echo $TEXT['book-new']; ?></legend>
<table align="left" width="100%" border="0">
<?php
$query_iskh = "SELECT
 tbl_book.data_postupleniya,
 tbl_book.book_name,
 tbl_book.book_id,
 tbl_book.vidano,
 tbl_book.zakazano,
 YEAR(tbl_book.data_postupleniya) AS data_postupleniya_year,
 MONTH(tbl_book.data_postupleniya) AS data_postupleniya_month,
 DAY(tbl_book.data_postupleniya) AS data_postupleniya_day,
 tbl_book.cicle_id AS cicl_book,
 tbl_cicle.cicle
 FROM tbl_book
 LEFT JOIN tbl_cicle ON tbl_book.cicle_id=tbl_cicle.cicle_id
 WHERE tbl_book.visible = '1' AND tbl_book.vidano != '2'
 ORDER BY tbl_book.data_postupleniya DESC
 LIMIT 6";
 $result_iskh = mysqli_query($dbcnx,$query_iskh);
  if(!$result_iskh) {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}
   while($result_iskh2 = mysqli_fetch_array($result_iskh))
    {
    if($result_iskh2['cicle'] !== '---') {$book_cicle = "<br />&nbsp;&nbsp;&nbsp;&nbsp;<a href='extsearch.php?cicle_id=".$result_iskh2['cicl_book']."&amp;cicle_ch=on' target=_blank title='".$TEXT['search-all-cicl']."'>(".$TEXT['book-cicle']." - ".rep_back($result_iskh2['cicle']).")</a>";}
     else {$book_cicle = "";}
    $autor = "";
     $query_autor = "SELECT
     tbl_s_book_aut.*,
     tbl_autor.autor_id,
     tbl_autor.name,
     tbl_autor.familiya,
     tbl_autor.otchestvo
     FROM tbl_s_book_aut
     JOIN tbl_autor ON tbl_s_book_aut.autor_id=tbl_autor.autor_id
    WHERE book_id = '".$result_iskh2['book_id']."' ";
    $result_autor = mysqli_query($dbcnx,$query_autor);
     if(!$result_autor) {puterror2($result2['skin'],$TEXT['error-tbl16']); require_once "utils/bottom.php"; exit();}
      if(mysqli_num_rows($result_autor) > 0)
      {
       while($result_autor2 = mysqli_fetch_array($result_autor))
       {
        $autor .="<a href='extsearch.php?autor_id=".$result_autor2['autor_id']."&amp;autor_ch=on' target=_blank title='".$TEXT['search-all-aut']."'>".rep_back($result_autor2['name'])." ".rep_back($result_autor2['familiya'])."</a>, ";
       }
       $autors = substr($autor, 0,-2);
       $autors = "<br />&nbsp;&nbsp;".$autors;
      } else {$autors = "";}

       $date_day = $result_iskh2['data_postupleniya_day'];
       $date_month = $result_iskh2['data_postupleniya_month'];
       $date_year = $result_iskh2['data_postupleniya_year'];
       if($date_year == "0000" || $date_year == "1970") {$data_r = "---";}
        else {$data_r = $date_day." ".$month[$date_month]." ".$date_year." ".$TEXT['year2'];}

      if($result_iskh2['vidano'] == '3') {$zakaz = "<img align='top' alt='".$TEXT['book-show5']."' src='skins/".$result2['skin']."/zakupka.png' />";}
       else {$zakaz = "";}

      echo "<tr><td class='informlft'><a href='info.php?book_id=".$result_iskh2['book_id']."' target=_blank title='".$TEXT['aut-viewtit']."'>".rep_back($result_iskh2['book_name'])."</a> ".$zakaz." <span class='informlftdat'> &#040;".$data_r."&#041;</span>&nbsp;<span class='opis'>".$autors.$book_cicle."</span><hr align='left' width='55%' /></td></tr>";
      }

?>
</table>
</fieldset>
</td>
<td valign="top" align="right" width="55%">
 <fieldset>
 <legend align="left" title="<?php echo $TEXT['book-myhand-tit']; ?>"><?php echo $TEXT['book-myhand']; ?></legend>
<table width="100%" border="0">
<?php
$query_iskh3 = "SELECT
 tbl_book.data_sdachi,
 tbl_book.book_name,
 tbl_book.book_id,
 tbl_book.vidano,
 tbl_book.zakazano,
 YEAR(tbl_book.data_vidachi) AS data_vidachi_year,
 MONTH(tbl_book.data_vidachi) AS data_vidachi_month,
 DAY(tbl_book.data_vidachi) AS data_vidachi_day,
 YEAR(tbl_book.data_sdachi) AS data_sdachi_year,
 MONTH(tbl_book.data_sdachi) AS data_sdachi_month,
 DAY(tbl_book.data_sdachi) AS data_sdachi_day,
 YEAR(tbl_book.data_zakaza) AS data_zakaza_year,
 MONTH(tbl_book.data_zakaza) AS data_zakaza_month,
 DAY(tbl_book.data_zakaza) AS data_zakaza_day,
 tbl_book.cicle_id AS cicl_book,
 tbl_cicle.cicle
 FROM tbl_book
 LEFT JOIN tbl_cicle ON tbl_book.cicle_id=tbl_cicle.cicle_id
 WHERE (tbl_book.vidano = '1' OR tbl_book.zakazano = '1') AND tbl_book.user_id = ".$result2['login_id']."
 ORDER BY tbl_book.data_sdachi, tbl_book.book_name LIMIT ".$lim_ogr."";
 $result_iskh3 = mysqli_query($dbcnx,$query_iskh3);
  if(!$result_iskh3) {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}
  if(mysqli_num_rows($result_iskh3) > 0)
  {
   while($result_iskh23 = mysqli_fetch_array($result_iskh3))
    {
    if($result_iskh23['cicle'] !== '---') {$book_cicle3 = "<br />&nbsp;&nbsp;&nbsp;&nbsp;<a href='extsearch.php?cicle_id=".$result_iskh23['cicl_book']."&amp;cicle_ch=on' target=_blank title='".$TEXT['search-all-cicl']."'>(".$TEXT['book-cicle']." - ".$result_iskh23['cicle'].")</a>";}
     else {$book_cicle3 = "";}
    $autor3 = "";
     $query_autor3 = "SELECT
     tbl_s_book_aut.*,
     tbl_autor.autor_id,
     tbl_autor.name,
     tbl_autor.familiya,
     tbl_autor.otchestvo
     FROM tbl_s_book_aut
     JOIN tbl_autor ON tbl_s_book_aut.autor_id=tbl_autor.autor_id
    WHERE book_id = '".$result_iskh23['book_id']."' ";
    $result_autor3 = mysqli_query($dbcnx,$query_autor3);
     if(!$result_autor3) {puterror2($result2['skin'],$TEXT['error-tbl16']); require_once "utils/bottom.php"; exit();}
      while($result_autor23 = mysqli_fetch_array($result_autor3))
       {
        $autor3 .="<a href='extsearch.php?autor_id=".$result_autor23['autor_id']."&amp;autor_ch=on' target=_blank title='".$TEXT['search-all-aut']."'>".rep_back($result_autor23['name'])." ".rep_back($result_autor23['familiya'])."</a>, ";
       }
       $autors3 =substr($autor3, 0,-2);

       $query_zad = mysqli_query($dbcnx,"SELECT DATEDIFF(CURRENT_DATE(),data_sdachi) AS zad_date FROM tbl_book WHERE book_id = ".$result_iskh23['book_id']."");
        $res_zad = mysqli_fetch_array($query_zad);

       $date_day1 = $result_iskh23['data_vidachi_day'];
       $date_month1 = $result_iskh23['data_vidachi_month'];
       $date_year1 = $result_iskh23['data_vidachi_year'];
       if($date_year1 == "0000" || $date_year1 == "1970") {$data_r3 = "---";}
        else {$data_r3 = $date_day1." ".$month[$date_month1]." ".$date_year1." ".$TEXT['year2'];}

       $date_day2 = $result_iskh23['data_sdachi_day'];
       $date_month2 = $result_iskh23['data_sdachi_month'];
       $date_year2 = $result_iskh23['data_sdachi_year'];
       if($date_year2 == "0000" || $date_year2 == "1970") {$data_r23 = "---";}
        else {$data_r23 = $date_day2." ".$month[$date_month2]." ".$date_year2." ".$TEXT['year2'];}

       $date_day3 = $result_iskh23['data_zakaza_day'];
       $date_month3 = $result_iskh23['data_zakaza_month'];
       $date_year3 = $result_iskh23['data_zakaza_year'];
       if($date_year3 == "0000" || $date_year3 == "1970") {$data_r33 = "---";}
        else {$data_r33 = $date_day3." ".$month[$date_month3]." ".$date_year3." ".$TEXT['year2'];}

        if($result_iskh23['zakazano'] == '1')
         {
          $book_sost_z = "<img alt='in rezerv' src='skins/".$result2['skin']."/rezerv.png' />";
          echo "<tr><td class='informlft'>".$book_sost_z."&nbsp;<a href='info.php?book_id=".$result_iskh23['book_id']."' target=_blank title='".$TEXT['aut-viewtit']."'>".rep_back($result_iskh23['book_name'])."</a>  <span class='informlftdat'> &#040;".$TEXT['log-book8']." - ".$data_r33."&#041;</span>&nbsp;<br />&nbsp;&nbsp;<span class='opis'>".$autors3.$book_cicle3."</span><hr align='left' width='55%' /></td></tr>";
         } 
        else 
           {
            if($res_zad['zad_date'] < 1)
             {
              $book_sost = "<img alt='ok' src='skins/".$result2['skin']."/active.png' />";
              echo "<tr><td class='informlft'>".$book_sost."&nbsp;<a href='info.php?book_id=".$result_iskh23['book_id']."' target=_blank title='".$TEXT['aut-viewtit']."'>".rep_back($result_iskh23['book_name'])."</a>  <span class='informlftdat'> &#040;".$data_r3." - ".$data_r23."&#041;</span>&nbsp;<br />&nbsp;&nbsp;<span class='opis'>".$autors3.$book_cicle3."</span><hr align='left' width='55%' /></td></tr>";
             }
             else
               {
                $book_sost = "<img alt='warning' src='skins/".$result2['skin']."/warn.png' />";
                echo "<tr><td class='informlft'>".$book_sost."&nbsp;<a href='info.php?book_id=".$result_iskh23['book_id']."' target=_blank title='".$TEXT['aut-viewtit']."'>".rep_back($result_iskh23['book_name'])."</a>  <span class='informlftdat'> &#040;".$data_r3." - ".$data_r23." &#124; ".$TEXT['book-zad']." - ".$res_zad['zad_date']." ".$TEXT['book-zad-days']."&#041;</span>&nbsp;<br />&nbsp;&nbsp;<span class='opis'>".$autors3.$book_cicle3."</span><hr align='left' width='55%' /></td></tr>";
               }
           }
    }
  } else {echo "<tr><td class='informcntr'>".$TEXT['book-myhand-clr']."</td></tr>";}

?>
</table>
 <?php
$query_count = mysqli_query($dbcnx,"SELECT COUNT(book_id) AS c_book_id FROM tbl_book WHERE vidano = '1' AND user_id = ".$result2['login_id']."");
	       $coun_rows = mysqli_fetch_array($query_count);
               $pag = $coun_rows['c_book_id']/5;
               if($pag<=1) {$pag = ceil($pag);}
                else
                {
                 ?>
                  <table align="center" border="0" cellpadding="0" cellspacing="0" width="90%" >
                   <tr align="center">
                    <td><hr size="3" width="98%" />
                <?php
                $pag=ceil($pag);
                $strn = $str-5;
                if($strn<1) {$strn=1;}
                $strk = $str+5;
                for($i=$strn; $i<=$pag; $i++)
                 {
                  if($i<=$strk)
                  {
                    if($i==$str)
                    {
                      $k = $i*5-5;
                      $kvis=$k+5;
                      $nvis=$k;
                      echo $pg = " <a class='zagolovok' href='main.php?k=".$k."' title='".$TEXT['tbl-details4'].$nvis.$TEXT['tbl-details3'].$kvis."'>".$i."</a>";
                    }
                    else
                    {
                    $k = $i*5-5;
                    $kvis=$k+5;
                    $nvis=$k;
                    echo $pg = " <a href='main.php?k=".$k."' title='".$TEXT['tbl-details4'].$nvis.$TEXT['tbl-details3'].$kvis."'>[".$i."]</a>";
                    }
                  }
                 }
                 ?>
                    <select name='k' onchange=window.location.href=this.options[this.selectedIndex].value;>
                    <?php
                    for($j=1; $j<=$pag; $j++)
                    {
                     $k = $j*5-5;
                     if($k == $_GET['k']) {echo "<option selected value=main.php?k=".$k.">".$j."</option>";}
                       else {echo "<option value=main.php?k=".$k.">".$j."</option>";}
                    }
                    ?>
                    </select>
                    </td>
                   </tr>
                  </table>
                 <?php
                }
?>
</fieldset>
</td>
</tr>
</table>
<?php
  require_once "utils/bottom.php";
?>