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

  require_once "utils/chlang_osn.php";

  $title = $TEXT['login-title4'];

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

if($result2['administrator'] == 0 && $result2['user'] == 0) {puterror2($result2['skin'],$TEXT['error-loginform2']); require_once "utils/bottom_film.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_film.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 co_film_id FROM $tbl_name");
//           $coun_log = mysqli_fetch_array($query_log_coun);
//            if($coun_log['co_film_id'] > 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_film.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_film.php"; exit();}
//             }
//             if($result_cl2['by_count'] > 0)
//             {
//             if($coun_log['co_film_id'] > $tbl_rows)
//             {
//             //удалить все если отклонение в количестве строк больше указанного
//             $del_rows = $coun_log['co_film_id'] - $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_film.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_film.data_postupleniya,
 tbl_film.film_name,
 tbl_film.film_id,
 tbl_film.vidano,
 YEAR(tbl_film.data_postupleniya) AS data_postupleniya_year,
 MONTH(tbl_film.data_postupleniya) AS data_postupleniya_month,
 DAY(tbl_film.data_postupleniya) AS data_postupleniya_day,
 tbl_film.cicle_id AS cicl_film,
 tbl_cicle.cicle
 FROM tbl_film
 LEFT JOIN tbl_cicle ON tbl_film.cicle_id=tbl_cicle.cicle_id
 WHERE tbl_film.visible = '1'
 ORDER BY tbl_film.data_postupleniya DESC
 LIMIT 6";
 $result_iskh = mysqli_query($dbcnx,$query_iskh);
  if(!$result_iskh) {puterror2($result2['skin'],$TEXT['error-tbl26']); require_once "utils/bottom_film.php"; exit();}
   while($result_iskh2 = mysqli_fetch_array($result_iskh))
    {
    if($result_iskh2['cicle'] !== '---') {$book_cicle = "<br />&nbsp;&nbsp;&nbsp;&nbsp;<a href='film-extsearch.php?cicle_id=".$result_iskh2['cicl_film']."&amp;cicle_ch=on' target=_blank title='".$TEXT['search-all-cicl']."'>(".$TEXT['book-cicle']." - ".$result_iskh2['cicle'].")</a>";}
     else {$book_cicle = "";}

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

      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='film-info.php?film_id=".$result_iskh2['film_id']."' target=_blank title='".$TEXT['aut-viewtit']."'>".rep_back($result_iskh2['film_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['film-myhand-tit']; ?>"><?php echo $TEXT['film-myhand']; ?></legend>
<table width="100%" border="0">
<?php
$query_iskh3 = "SELECT
 tbl_film.data_sdachi,
 tbl_film.film_name,
 tbl_film.film_id,
 tbl_film.vidano,
 YEAR(tbl_film.data_vidachi) AS data_vidachi_year,
 MONTH(tbl_film.data_vidachi) AS data_vidachi_month,
 DAY(tbl_film.data_vidachi) AS data_vidachi_day,
 YEAR(tbl_film.data_sdachi) AS data_sdachi_year,
 MONTH(tbl_film.data_sdachi) AS data_sdachi_month,
 DAY(tbl_film.data_sdachi) AS data_sdachi_day,
 tbl_film.cicle_id AS cicl_film,
 tbl_cicle.cicle
 FROM tbl_film
 LEFT JOIN tbl_cicle ON tbl_film.cicle_id=tbl_cicle.cicle_id
 WHERE tbl_film.vidano = '1' AND tbl_film.user_id = ".$result2['login_id']."
 ORDER BY tbl_film.data_sdachi, tbl_film.film_name LIMIT ".$lim_ogr."";
 $result_iskh3 = mysqli_query($dbcnx,$query_iskh3);
  if(!$result_iskh3) {puterror2($result2['skin'],$TEXT['error-tbl26']); require_once "utils/bottom_film.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='film-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 = "";}

       $query_zad = mysqli_query($dbcnx,"SELECT DATEDIFF(CURRENT_DATE(),data_sdachi) AS zad_date FROM tbl_film WHERE film_id = ".$result_iskh23['film_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'];
       $year = $TEXT['year2'];
       if($date_year1 == "0000" || $date_year1 == "1970") {$data_r3 = "---";}
        else {$data_r3 = (date("$date_day1 $month[$date_month1] $date_year1 $year"));}

       $date_day23 = $result_iskh23['data_sdachi_day'];
       $date_month23 = $result_iskh23['data_sdachi_month'];
       $date_year23 = $result_iskh23['data_sdachi_year'];
       if($date_year23 == "0000" || $date_year23 == "1970") {$data_r23 = "---";}
        else {$data_r23 = (date("$date_day23 $month[$date_month23] $date_year23 $year"));}

        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='film-info.php?film_id=".$result_iskh23['film_id']."' target=_blank title='".$TEXT['aut-viewtit']."'>".$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='film-info.php?film_id=".$result_iskh23['film_id']."' target=_blank title='".$TEXT['aut-viewtit']."'>".$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(film_id) AS c_film_id FROM tbl_film WHERE vidano = '1' AND user_id = ".$result2['login_id']."");
	       $coun_rows = mysqli_fetch_array($query_count);
               $pag = $coun_rows['c_film_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='film_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='film_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=film_main.php?k=".$k.">".$j."</option>";}
                       else {echo "<option value=film_main.php?k=".$k.">".$j."</option>";}
                    }
                    ?>
                    </select>
                    </td>
                   </tr>
                  </table>
                 <?php
                }
?>
</fieldset>
</td>
</tr>
</table>
<?php
  require_once "utils/bottom_film.php";
?>