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

  require_once "utils/chlang_osn.php";

  $title = $TEXT['menu-btn33-title'];

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

  if($result2['administrator'] == 0 && $result2['user'] == 0) {puterror2($result2['skin'],$TEXT['error-loginform2']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['oglavlenie'])>1500){puterror2($result2['skin'],$TEXT['error-film11']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['zhanr_id'])>5){puterror2($result2['skin'],$TEXT['error-zhanr6']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['cicle_id'])>5) {puterror2($result2['skin'],$TEXT['error-cicle6']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['tip_izd_id'])>5) {puterror2($result2['skin'],$TEXT['error-tip-izd6']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['bibl_id'])>5) {puterror2($result2['skin'],$TEXT['error-libr6']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['zal_id'])>5) {puterror2($result2['skin'],$TEXT['error-zal6']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['shkaf_id'])>5) {puterror2($result2['skin'],$TEXT['error-shkaf6']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['polka_id'])>5) {puterror2($result2['skin'],$TEXT['error-polka6']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['izd_id'])>5) {puterror2($result2['skin'],$TEXT['error-izd6']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['data_izdaniya'])>10) {puterror2($result2['skin'],$TEXT['error-film21']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['data_postupleniya'])>10) {puterror2($result2['skin'],$TEXT['error-film22']); require_once "utils/bottom_film.php"; exit();}
  if(empty($_POST['login_id'])) {puterror2($result2['skin'],$TEXT['error-login15']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['login_id'])>15) {puterror2($result2['skin'],$TEXT['error-login14']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['ser_id'])>5) {puterror2($result2['skin'],$TEXT['error-ser6']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['vidano'])>255) {puterror2($result2['skin'],$TEXT['param_err12']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['bl_id'])>5) {puterror($result2['skin'],$TEXT['error-filmlang6']); require_once "../utils/bottom_film_adm.php"; exit();}
  if(strlen($_POST['vozr_id'])>5) {puterror($result2['skin'],$TEXT['error-filmlang6']); require_once "../utils/bottom_film_film.php"; exit();}

  $_POST['vidano'] = rep($_POST['vidano']);
  $_POST['ser_id'] = rep($_POST['ser_id']);
  $_POST['bibl_id'] = rep($_POST['bibl_id']);
  $_POST['cicle_id'] = rep($_POST['cicle_id']);
  $_POST['cicle_ch'] = rep($_POST['cicle_ch']);
  $_POST['bibl_ch'] = rep($_POST['bibl_ch']);
  $_POST['zhanr_ch'] = rep($_POST['zhanr_ch']);
  $_POST['spisano'] = rep($_POST['spisano']);
  $_POST['oglavlenie'] = rep($_POST['oglavlenie']);
  $_POST['zhanr_id'] = rep($_POST['zhanr_id']);
  $_POST['cicle_id'] = rep($_POST['cicle_id']);
  $_POST['tip_izd_id'] = rep($_POST['tip_izd_id']);
  $_POST['bibl_id'] = rep($_POST['bibl_id']);
  $_POST['photo_obr'] = rep($_POST['photo_obr']);
  $_POST['zal_id'] = rep($_POST['zal_id']);
  $_POST['shkaf_id'] = rep($_POST['shkaf_id']);
  $_POST['polka_id'] = rep($_POST['polka_id']);
  $_POST['data_izdaniya'] = rep($_POST['data_izdaniya']);
  $_POST['data_postupleniya'] = rep($_POST['data_postupleniya']);
  $_POST['ser_id'] = rep($_POST['ser_id']);
  $_POST['izd_id'] = rep($_POST['izd_id']);
  $_POST['login_id'] = rep($_POST['login_id']);
  $_POST['bl_id'] = rep($_POST['bl_id']);
  $_POST['vozr_id'] = rep($_POST['vozr_id']);

  if(!empty($_POST['data_izdaniya'])) {$data_izdaniya = "AND tbl_film.data_izdaniya LIKE '%".$_POST['data_izdaniya']."%' ";}
    else {$data_izdaniya = "";}

  if(!empty($_POST['data_postupleniya'])) {$data_postupleniya = "AND tbl_film.data_postupleniya LIKE '%".$_POST['data_postupleniya']."%' ";}
    else {$data_postupleniya = "";}

  if(empty($_POST['izd_ch'])) {$_POST['izd_ch'] = "";}
  if(empty($_POST['login_ch'])) {$_POST['login_ch'] = "";}
  if(empty($_POST['ser_ch'])) {$_POST['ser_ch'] = "";}
  if(empty($_POST['zhanr_ch'])) {$_POST['zhanr_ch'] = "";}
  if(empty($_POST['cicle_ch'])) {$_POST['cicle_ch'] = "";}
  if(empty($_POST['tip_izd_ch'])) {$_POST['tip_izd_ch'] = "";}
  if(empty($_POST['bibl_ch'])) {$_POST['bibl_ch'] = "";}
  if(empty($_POST['zal_ch'])) {$_POST['zal_ch'] = "";}
  if(empty($_POST['shkaf_ch'])) {$_POST['shkaf_ch'] = "";}
  if(empty($_POST['polka_ch'])) {$_POST['polka_ch'] = "";}
  if(empty($_POST['vidano'])) {$_POST['vidano'] = "";}
  if(empty($_POST['oglavlenie'])) {$_POST['oglavlenie'] = "";}
  if(empty($_POST['bl_ch'])) {$_POST['bl_ch'] = "";}
  if(empty($_POST['vozr_ch'])) {$_POST['vozr_ch'] = "";}

  if($_POST['vidano'] !== '')
  {
    switch($_POST['vidano'])
      {
      case 1:
      $vidano = "AND tbl_film.vidano = '1' ";
      $vis_vid = "1";
      break;
      case 2:
      $vidano = "AND tbl_film.vidano = '2' ";
      $vis_vid = "2";
      break;
      case 3:
      $vidano = "AND tbl_film.vidano = '3' ";
      $vis_vid = "3";
      break;
      }
   } else {$vidano = "";}

  if($_POST['login_ch'] == "on") 
  {
   $login = "AND tbl_film.user_id = '".$_POST['login_id']."' ";
   $querylgn = "SELECT name, familiya, otchestvo FROM `tbl_logins` WHERE login_id = '".$_POST['login_id']."'  ";
     $resultlgn = mysqli_query($dbcnx,$querylgn);
       if(!$resultlgn) {puterror2($result2['skin'],$TEXT['error-tbl7']); require_once "utils/bottom_film.php"; exit();}
         $result_login = mysqli_fetch_array($resultlgn);
          $user_stat = $result_login['familiya']." ".$result_login['name']." ".$result_login['otchestvo'];
  }
   else {$login = "";}

  if($_POST['ser_ch'] == "on") {$ser = "AND tbl_film.ser_id = '".$_POST['ser_id']."' ";}
   else {$ser = "";}

  if($_POST['zhanr_ch'] == "on") {$zhanr = "AND tbl_film.zhanr_id = '".$_POST['zhanr_id']."' ";}
    else {$zhanr = "";}

  if($_POST['cicle_ch'] == "on") {$cicle = "AND tbl_film.cicle_id = '".$_POST['cicle_id']."' ";}
     else {$cicle = "";}

  if($_POST['tip_izd_ch'] == "on") {$tip_izd = "AND tbl_film.tip_izd_id = '".$_POST['tip_izd_id']."' ";}
     else {$tip_izd = "";}

  if($_POST['bibl_ch'] == "on") {$bibl = "AND tbl_film.bibl_id = '".$_POST['bibl_id']."' ";}
     else {$bibl = "";}

  if($_POST['zal_ch'] == "on") {$zal = "AND tbl_film.zal_id = '".$_POST['zal_id']."' ";}
    else {$zal = "";}

  if($_POST['shkaf_ch'] == "on") {$shkaf = "AND tbl_film.shkaf_id = '".$_POST['shkaf_id']."' ";}
     else {$shkaf = "";}

  if($_POST['polka_ch'] == "on") {$polka = "AND tbl_film.polka_id = '".$_POST['polka_id']."' ";}
    else {$polka = "";}

  if($_POST['bl_ch'] == "on") {$bl = "AND tbl_film.bl_id = '".$_POST['bl_id']."' ";}
     else {$bl = "";}

  if($_POST['vozr_ch'] == "on") {$vozr = "AND tbl_film.vozr_id = '".$_POST['vozr_id']."' ";}
     else {$vozr = "";}

if($_POST['org_viv'] == "on")
{
if($_POST['izd_ch'] == "on")
{
$izdatelstvo = " AND tbl_s_film_izd.izd_id = '".$_POST['izd_id']."' ";
$query_iskh = "SELECT
tbl_s_film_izd.*,
tbl_film.film_id,
tbl_film.oglavlenie,
tbl_film.isbn,
tbl_film.film_num,
tbl_film.vidano,
tbl_film.film_name,
tbl_film.prichina,
tbl_film.cicle_id AS cicl_film,
tbl_cicle.cicle,
tbl_seriya.seriya,
tbl_zhanr.zhanr
FROM tbl_s_film_izd
JOIN tbl_film ON tbl_s_film_izd.film_id=tbl_film.film_id
LEFT JOIN tbl_cicle ON tbl_film.cicle_id=tbl_cicle.cicle_id
LEFT JOIN tbl_seriya ON tbl_film.ser_id=tbl_seriya.ser_id
LEFT JOIN tbl_zhanr ON tbl_film.zhanr_id=tbl_zhanr.zhanr_id
WHERE tbl_film.film_id != '0' $izdatelstvo $data_izdaniya $data_postupleniya $ser $vozr $zhanr $cicle $tip_izd $bibl $zal $shkaf $polka $bl $vidano $login
ORDER BY tbl_film.film_name";
}
else
{
$query_iskh = "SELECT
tbl_film.film_id,
tbl_film.oglavlenie,
tbl_film.film_num,
tbl_film.vidano,
tbl_film.film_name,
tbl_film.prichina,
tbl_film.autors,
tbl_film.cicle_id AS cicl_film,
tbl_cicle.cicle,
tbl_seriya.seriya,
tbl_zhanr.zhanr
FROM tbl_film
LEFT JOIN tbl_cicle ON tbl_film.cicle_id=tbl_cicle.cicle_id
LEFT JOIN tbl_seriya ON tbl_film.ser_id=tbl_seriya.ser_id
LEFT JOIN tbl_zhanr ON tbl_film.zhanr_id=tbl_zhanr.zhanr_id
WHERE tbl_film.film_id != '0' $data_izdaniya $data_postupleniya $ser $vozr $zhanr $cicle $tip_izd $bibl $zal $shkaf $polka $bl $vidano $login
ORDER BY tbl_film.film_name";
}
$result_iskh = mysqli_query($dbcnx,$query_iskh);
//print $query_iskh;
  if(!$result_iskh) {puterror2($result2['skin'],$TEXT['error-tbl26']); require_once "utils/bottom_film.php"; exit();}
    $res_rows = mysqli_num_rows($result_iskh);
     if($res_rows == 0) {puterror2($result2['skin'],$TEXT['search-err4']); require_once "utils/bottom_film.php"; exit();}
      elseif($res_rows > 0) {echo "<div class='logo' valign='top'><img alt='Picture' src='skins/".$result2['skin']."/library.png' />".$TEXT['global-title']."&nbsp;<span class='logover'>".$TEXT['global-vertion']."</span></div>";}
       if($_POST['login_ch'] == "on") {echo "<hr width='250px' align='right' /><div class='logo'>".$TEXT['film-user-stat']."<span class='logouser'>".$user_stat."</span></div><br />";}
       if($vis_vid == "1") {echo "<div class='logouser'>".$TEXT['film-vidano']."</div><hr width='250px' align='left' />";}
       elseif($vis_vid == "2") {echo "<div class='logouser'>".$TEXT['film-show4']."</div><hr width='250px' align='left' />";}
       elseif($vis_vid == "3") {echo "<div class='logouser'>".$TEXT['film-show5']."</div><hr width='250px' align='left' />";}
       echo "<div>";
       echo "<ul id='prn'>";
       while($result_body_film = mysqli_fetch_array($result_iskh))
       {
        $izd = "";
        $result_body_film['oglavlenie'] = rep_back($result_body_film['oglavlenie']);
        $result_body_film['film_name'] = rep_back($result_body_film['film_name']);
        $result_body_film['autors'] = rep_back($result_body_film['autors']);

        if($result_body_film['vidano'] == 0) {$film_name = "<span>".$result_body_film['film_name']."</span>";}
         elseif($result_body_film['vidano'] == 1) {$film_name = "<span class='vidano'>".$result_body_film['film_name']."</span>";}
         elseif($result_body_film['vidano'] == 2) {$film_name = "<span class='spisano'>".$result_body_film['film_name']."</span>&nbsp;&#187;&nbsp;<span class='prichina'>".$result_body_film['prichina']."</span> ";}
         elseif($result_body_film['vidano'] == 3) {$film_name = "<span class='zakupka'>".$result_body_film['film_name']."</span>";}

       echo "<li>".$film_name."</li>";
       echo "<ul>";

       $autors = $TEXT['film-rezhiser']." - ".$result_body_film['autors'];

       $query_izd = "SELECT
       tbl_s_film_izd.*,
       tbl_izdatelstvo.*
       FROM tbl_s_film_izd
       JOIN tbl_izdatelstvo ON tbl_s_film_izd.izd_id=tbl_izdatelstvo.izd_id
       WHERE film_id = '".$result_body_film['film_id']."' ";
       $result_izd = mysqli_query($dbcnx,$query_izd);
        if(!$result_izd) {puterror2($result2['skin'],$TEXT['error-tbl17']); require_once "utils/bottom_film.php"; exit();}
        while($result_izd2 = mysqli_fetch_array($result_izd))
        {
        $izd .= $result_izd2['izdatelstvo'].", ";
        }
        $izds = $TEXT['menu-btn22-film']." - ".substr($izd, 0,-2);

        if($result_body_film['film_num'] == 0) {$film_num = "";}
         else
            {
            $bnt = substr($result_body_film['film_num'],0,1);
             if($bnt == '0')
             {
             $film_n = substr($result_body_film['film_num'],1,1);
              if($film_n == '0')
               {
               $film_num = "&#8470;&nbsp;".$TEXT['global-now-film2']." - ".substr($result_body_film['film_num'],2,3);
               } else {$film_num = "&#8470;&nbsp;".$TEXT['global-now-film2']." - ".substr($result_body_film['film_num'],1,3);}
             } else {$film_num = "&#8470;&nbsp;".$TEXT['global-now-film2']." - ".$result_body_film['film_num'];}
           }

           $zhn = $TEXT['book-zhanr']." - ".$result_body_film['zhanr'];
           $cicle = $TEXT['book-cicle']." - ".$result_body_film['cicle'];
           $seriya = $TEXT['film-seriya']." - ".$result_body_film['seriya'];
           $oglavlenie = $TEXT['film-soderzh'].":<br />".nl2br($result_body_film['oglavlenie']);
           echo "<li>".$autors."</li>";
           echo "<ul id='prin'>";
           echo "<li><span class='oglavlenie'>".$izds."</span></li>";
           if($result_body_film['seriya'] !== "---") {echo "<li><span class='oglavlenie'>".$seriya."</span></li>";}
           if($result_body_film['cicle'] !== "---") {echo "<li><span class='oglavlenie'>".$cicle."</span></li>";}
           if($result_body_film['zhanr'] !== "---") {echo "<li><span class='oglavlenie'>".$zhn."</span></li>";}
           echo "<li><span class='oglavlenie'>".$film_num."</span></li>";
           if($_POST['oglavlenie'] == "on" && $result_body_film['oglavlenie'] !== "") {echo "<li><hr width='50px' align='left' /><span class='oglavlenie'>".$oglavlenie."</span><hr width='150px' align='left' /></li>";}
            else {echo "<li><hr width='150px' align='left' /></li>";}
           echo "</ul>";
           echo "</ul>";
      }
      echo "</ul>";
      echo "<div>";
}
else
{
echo "<div class='logo' valign='top'><img alt='Picture' src='skins/".$result2['skin']."/library.png' />".$TEXT['global-title']."&nbsp;<span class='logover'>".$TEXT['global-vertion']."</span></div>";
if($_POST['login_ch'] == "on") {echo "<hr width='250px' align='right' /><br /><div class='logo'>".$TEXT['film-user-stat']."<span class='logouser'>".$user_stat."</span></div><br />";}
if($vis_vid == "1") {echo "<div class='logouser'>".$TEXT['film-vidano']."</div><hr width='250px' align='left' />";}
elseif($vis_vid == "2") {echo "<div class='logouser'>".$TEXT['film-show4']."</div><hr width='250px' align='left' />";}
elseif($vis_vid == "3") {echo "<div class='logouser'>".$TEXT['film-show5']."</div><hr width='250px' align='left' />";}
echo "<div>";
echo "<ul id='prn'>";

if($_POST['izd_ch'] == "on")
 {
 $query_iskh2 = "SELECT
 tbl_s_film_izd.*,
 tbl_s_film_aut.*,
 tbl_film.film_id,
 tbl_film.oglavlenie,
 tbl_film.cicle,
 tbl_film.film_num,
 tbl_film.vidano,
 tbl_film.film_name,
 tbl_film.prichina,
 tbl_film.autors,
 tbl_cicle.*
 FROM tbl_s_film_izd
 JOIN tbl_film ON tbl_s_film_izd.film_id=tbl_film.film_id
 JOIN tbl_s_film_aut ON tbl_s_film_izd.film_id=tbl_s_film_aut.film_id
 LEFT JOIN tbl_cicle ON tbl_film.cicle_id=tbl_cicle.cicle_id
 WHERE tbl_s_film_izd.izd_id = '".$_POST['izd_id']."' $data_izdaniya $data_postupleniya $ser $zhanr $cicle $tip_izd $bibl $zal $shkaf $polka $bl $vidano $login
 ORDER BY tbl_film.cicle, tbl_film.film_num, tbl_film.film_name ";
 }
 else
  {
  $query_iskh2 = "SELECT
  tbl_film.film_id,
  tbl_film.oglavlenie,
  tbl_film.cicle,
  tbl_film.film_num,
  tbl_film.vidano,
  tbl_film.film_name,
  tbl_film.prichina,
  tbl_film.autors,
  tbl_film.cicle_id AS cicl_film,
  tbl_cicle.cicle,
  tbl_seriya.seriya,
  tbl_zhanr.zhanr
  FROM tbl_film
  LEFT JOIN tbl_cicle ON tbl_film.cicle_id=tbl_cicle.cicle_id
  LEFT JOIN tbl_seriya ON tbl_film.ser_id=tbl_seriya.ser_id
  LEFT JOIN tbl_zhanr ON tbl_film.zhanr_id=tbl_zhanr.zhanr_id
  WHERE tbl_film.film_id != '0' $data_izdaniya $data_postupleniya $ser $vozr $zhanr $cicle $tip_izd $bibl $zal $shkaf $polka $bl $vidano $login
  ORDER BY tbl_film.cicle, tbl_film.film_num, tbl_film.film_name";
  }
  $result_iskh3 = mysqli_query($dbcnx,$query_iskh2);
  if(!$result_iskh3) {puterror2($result2['skin'],$TEXT['error-tbl26']); require_once "utils/bottom_film.php"; exit();}
   $res_rows2 = mysqli_num_rows($result_iskh3);
    if($res_rows2 > 0)
    {
     while($result_iskh4 = mysqli_fetch_array($result_iskh3))
       {
        $result_iskh4['oglavlenie'] = rep_back($result_iskh4['oglavlenie']);
        $result_iskh4['film_name'] = rep_back($result_iskh4['film_name']);
        $result_iskh4['autors'] = rep_back($result_iskh4['autors']);

        if($result_iskh4['vidano'] == 0) {$film_name = "<span>".$result_iskh4['film_name']."</span>";}
         elseif($result_iskh4['vidano'] == 1) {$film_name = "<span class='vidano'>".$result_iskh4['film_name']."</span>";}
         elseif($result_iskh4['vidano'] == 2) {$film_name = "<span class='spisano'>".$result_iskh4['film_name']."</span>&nbsp;&#187;&nbsp;<span class='prichina'>".$result_iskh4['prichina']."</span> ";}
         elseif($result_iskh4['vidano'] == 3) {$film_name = "<span class='zakupka'>".$result_iskh4['film_name']."</span>";}
           echo "<li>".$film_name."</li>";
           echo "<ul>";
       $izd4 = "";
       $query_izd2 = "SELECT
       tbl_s_film_izd.*,
       tbl_izdatelstvo.*
       FROM tbl_s_film_izd
       JOIN tbl_izdatelstvo ON tbl_s_film_izd.izd_id=tbl_izdatelstvo.izd_id
       WHERE film_id = '".$result_iskh4['film_id']."' ";
       $result_izd2 = mysqli_query($dbcnx,$query_izd2);
        if(!$result_izd2) {puterror2($result2['skin'],$TEXT['error-tbl17']); require_once "utils/bottom_film.php"; exit();}
        while($result_izd4 = mysqli_fetch_array($result_izd2))
        {
        $izd4 .= $result_izd4['izdatelstvo'].", ";
        }
        $izds2 = $TEXT['menu-btn22-film']." - ".substr($izd4, 0,-2);

        if($result_iskh4['film_num'] == "" || $result_iskh4['film_num'] == "0" || $result_iskh4['film_num'] == NULL) {$film_num = "";}
         else
         {
          $bnt = substr($result_iskh4['film_num'],0,1);
           if($bnt == '0')
            {
            $film_n = substr($result_iskh4['film_num'],1,1);
             if($film_n == '0')
             {
             $film_num = "&#8470;&nbsp;".$TEXT['global-now-film2']." - ".substr($result_iskh4['film_num'],2,3);
             } else {$film_num = "&#8470;&nbsp;".$TEXT['global-now-film2']." - ".substr($result_iskh4['film_num'],1,3);}
            } else {$film_num = "&#8470;&nbsp;".$TEXT['global-now-film2']." - ".$result_iskh4['film_num'];}
          }

        $autors2 = $TEXT['film-rezhiser']." - ".$result_iskh4['autors'];
        $zhn2 = $TEXT['book-zhanr']." - ".$result_iskh4['zhanr'];
        $cicle2 = $TEXT['book-cicle']." - ".$result_iskh4['cicle'];
        $seriya2 = $TEXT['film-seriya']." - ".$result_iskh4['seriya'];
        $oglavlenie2 = $TEXT['film-soderzh'].":<br />".nl2br($result_iskh4['oglavlenie']);
        echo "<li>".$autors2."</li>";
        echo "<ul id='prin'>";
        echo "<li><span class='oglavlenie'>".$izds2."</span></li>";
        if($result_iskh4['seriya'] !== "---") {echo "<li><span class='oglavlenie'>".$seriya2."</span></li>";}
        if($result_iskh4['cicle'] !== "---") {echo "<li><span class='oglavlenie'>".$cicle2."</span></li>";}
        if($result_iskh4['zhanr'] !== "---") {echo "<li><span class='oglavlenie'>".$zhn2."</span></li>";}
        echo "<li><span class='oglavlenie'>".$film_num."</span></li>";
        if($_POST['oglavlenie'] == "on" && $result_iskh4['oglavlenie'] !== "") {echo "<li><hr width='50px' align='left' /><span class='oglavlenie'>".$oglavlenie2."</span><hr width='150px' align='left' /></li>";}
         else {echo "<li><hr width='150px' align='left' /></li>";}
        echo "</ul>";
        echo "</ul>";
       }
      }
      echo "</ul>";
      echo "<div>";
  }
?>
</body>
</html>