<?php
  ///////////////////////////////////////////////////
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////
  require_once "utils/chlang_osn.php";

  $title = $TEXT['book-otziv2'];

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

  if($result2['administrator'] == 0 && $result2['user'] == 0) {puterror2($result2['skin'],$TEXT['error-loginform2']); require_once "utils/bottom.php"; exit();}

  if(empty($_GET['book_id'])) {puterror2($result2['skin'],$TEXT['error-book5']); require_once "utils/bottom.php"; exit();}
  if(strlen($_GET['book_id'])>255) {puterror2($result2['skin'],$TEXT['error-book6']); 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['book_id'] = rep($_GET['book_id']);
  $_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_iskh = "SELECT book_name, photo_book FROM tbl_book WHERE book_id = '".$_GET['book_id']."' ";
 $result_iskh = mysqli_query($dbcnx,$query_iskh);
  if(!$result_iskh) {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}
      $result_iskh2 = mysqli_fetch_array($result_iskh);

     $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 = '".$_GET['book_id']."' ";
    $result_autor = mysqli_query($dbcnx,$query_autor);
     if(!$result_autor) {puterror2($result2['skin'],$TEXT['error-tbl16']); require_once "utils/bottom.php"; exit();}
      while($result_autor2 = mysqli_fetch_array($result_autor))
       {
        $autor .="<a href='info_autor.php?autor_id=".$result_autor2['autor_id']."' target=_blank title='".$TEXT['aut-about']."'>".$result_autor2['name']." ".$result_autor2['otchestvo']." ".$result_autor2['familiya']."</a>, ";
       }
       $autors =substr($autor, 0,-2);

      $sm = preg_replace("/\.\.+./","",$result_iskh2['photo_book']);
      if(!empty($result_iskh2['photo_book']) && file_exists($sm)) {$result_image = "<div id='author-container'><img alt='".$TEXT['global-alt']."' src='".$sm."' /></div>";}
        else {$result_image =  "<img alt='".$TEXT['global-alt']."' src='skins/".$result2['skin']."/nophoto.png' />";}

// rating range //
  $query_otziv1 = "SELECT COUNT(otz_id) AS v_bad_books FROM tbl_otziv WHERE
           tbl_otziv.book_id = '".$_GET['book_id']."'
           AND tbl_otziv.use_prog = '1' 
           AND tbl_otziv.usr_rating = '1'";
   $result_otz1 = mysqli_query($dbcnx,$query_otziv1);
    if(!$result_otz1) {puterror2($result2['skin'],$TEXT['error-tbl22']); require_once "utils/bottom.php"; exit();}
     $result_otziv1 = mysqli_fetch_array($result_otz1);
     $v_bad_books = $result_otziv1['v_bad_books'];

  $query_otziv2 = "SELECT COUNT(otz_id) AS bad_books FROM tbl_otziv WHERE
           tbl_otziv.book_id = '".$_GET['book_id']."'
           AND tbl_otziv.use_prog = '1' 
           AND tbl_otziv.usr_rating = '2'";
   $result_otz2 = mysqli_query($dbcnx,$query_otziv2);
    if(!$result_otz2) {puterror2($result2['skin'],$TEXT['error-tbl22']); require_once "utils/bottom.php"; exit();}
     $result_otziv2 = mysqli_fetch_array($result_otz2);
     $nv_bad_books = $result_otziv2['bad_books'];

  $query_otziv3 = "SELECT COUNT(otz_id) AS v_good_books FROM tbl_otziv WHERE
           tbl_otziv.book_id = '".$_GET['book_id']."'
           AND tbl_otziv.use_prog = '1' 
           AND tbl_otziv.usr_rating = '5'";
   $result_otz3 = mysqli_query($dbcnx,$query_otziv3);
    if(!$result_otz3) {puterror2($result2['skin'],$TEXT['error-tbl22']); require_once "utils/bottom.php"; exit();}
     $result_otziv3 = mysqli_fetch_array($result_otz3);
     $v_good_books = $result_otziv3['v_good_books'];

  $query_otziv4 = "SELECT COUNT(otz_id) AS nv_good_books FROM tbl_otziv WHERE
           tbl_otziv.book_id = '".$_GET['book_id']."'
           AND tbl_otziv.use_prog = '1' 
           AND tbl_otziv.usr_rating = '4'";
   $result_otz4 = mysqli_query($dbcnx,$query_otziv4);
    if(!$result_otz4) {puterror2($result2['skin'],$TEXT['error-tbl22']); require_once "utils/bottom.php"; exit();}
     $result_otziv4 = mysqli_fetch_array($result_otz4);
     $nv_good_books = $result_otziv4['nv_good_books'];

  $query_otziv5 = "SELECT COUNT(otz_id) AS srd_books FROM tbl_otziv WHERE
           tbl_otziv.book_id = '".$_GET['book_id']."'
           AND tbl_otziv.use_prog = '1' 
           AND tbl_otziv.usr_rating = '3'";
   $result_otz5 = mysqli_query($dbcnx,$query_otziv5);
    if(!$result_otz5) {puterror2($result2['skin'],$TEXT['error-tbl22']); require_once "utils/bottom.php"; exit();}
     $result_otziv5 = mysqli_fetch_array($result_otz5);
     $srd_books = $result_otziv5['srd_books'];

   $all_books_cnt = $v_bad_books + $nv_bad_books + $v_good_books + $nv_good_books + $srd_books;
   $good_books = $v_good_books + $nv_good_books;
   $bad_books = $v_bad_books + $nv_bad_books;
   
   if($all_books_cnt > 0) {
       $prcnt_v_bad = 100 * $v_bad_books / $all_books_cnt;
       $prcnt_bad = 100 * $nv_bad_books / $all_books_cnt;
       $prcnt_srd = 100 * $srd_books / $all_books_cnt;
       $prcnt_nv_good = 100 * $nv_good_books / $all_books_cnt;
       $prcnt_v_good = 100 * $v_good_books / $all_books_cnt;
       $start_dl = "<dl id='chart'>";
       if($prcnt_v_bad > 0) {$prcnt_v_bad_print = "<dt class='opis'>".$TEXT['book-ocenka1']."</dt><dd><span style='width':".$prcnt_v_bad."%>".$prcnt_v_bad."%</span></dd>";}
       if($prcnt_bad > 0)   {$prcnt_bad_print = "<dt class='opis'>".$TEXT['book-ocenka2']."</dt><dd><span style='width':".$prcnt_bad."%>".$prcnt_bad."%</span></dd>";}
       if($prcnt_srd > 0)   {$prcnt_srd_print = "<dt class='opis'>".$TEXT['book-ocenka3']."</dt><dd><span style='width':".$prcnt_srd."%>".$prcnt_srd."%</span></dd>";}
       if($prcnt_nv_good > 0)  {$prcnt_good_print = "<dt class='opis'>".$TEXT['book-ocenka4']."</dt><dd><span style='width':".$prcnt_nv_good."%>".$prcnt_nv_good."%</span></dd>";}
       if($prcnt_v_good > 0){$prcnt_v_good_print = "<dt class='opis'>".$TEXT['book-ocenka5']."</dt><dd><span style='width':".$prcnt_v_good."%>".$prcnt_v_good."%</span></dd>";}
       $stop_dl = "</dl>";
   } else {
       $start_dl = "";
       $prcnt_v_bad_print = "";
       $prcnt_bad_print = "";
       $prcnt_srd_print = "";
       $prcnt_good_print = "";
       $prcnt_v_good_print = "";
       $stop_dl = "";
   }
//    if($result_iskh2['book_rating'] !== "0") {$book_rat = "<span class='opis'>&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;".$TEXT['book-rating']."&#040;".$result_iskh2['book_rating']."&#041;&nbsp;</span>";}
    if($result_iskh2['book_rating'] !== "0") {$book_rat = $TEXT['book-rating']."&#040;".$result_iskh2['book_rating']."&#041;";}

    if($srd_books > $good_books || $srd_books > $bad_books) {$rating_pic = "&nbsp;&nbsp;<img alt='".$book_rat."' src='skins/".$result2['skin']."/boredom.gif' />";}
    else {
    if($good_books > $bad_books) {$rating_pic = "&nbsp;&nbsp;<img alt='".$book_rat."' src='skins/".$result2['skin']."/yes.gif' />";
     if($v_good_books > $nv_good_books) {$rating_pic = "&nbsp;&nbsp;<img alt='".$book_rat."' src='skins/".$result2['skin']."/good2.gif' />";}
      else {$rating_pic = "&nbsp;&nbsp;<img alt='".$book_rat."' src='skins/".$result2['skin']."/yes.gif' />";}
    }
    elseif($bad_books > $good_books) {$rating_pic = "&nbsp;&nbsp;<img alt='".$book_rat."' src='skins/".$result2['skin']."/bad.gif' />";}
     else {$rating_pic = "&nbsp;&nbsp;<img alt='".$book_rat."' src='skins/".$result2['skin']."/boredom.gif' />";}
    }
// end rating range //
?>
<table align="center" width="98%" border="0">
 <tr><td>&nbsp;<a class="zagolovok" href='info.php?book_id=<?php echo $_GET['book_id']; ?>' title='<?php echo $TEXT['aut-title-info']; ?>'><?php echo rep_back($result_iskh2['book_name']); ?></a>&nbsp;<span class="opis">&#040;<?php echo $autors; ?>&#041;</span></td></tr>
</table>
<hr width="98%" />
<table align="center" width="98%" border="0">
<tr>
<td width="5%" valign="top">
 <table align="left" border="0" width="100%">
  <tr><td><?php echo $result_image; ?></td></tr>
 </table>
</td>
<td width="65%" valign="top" align="right">
     <table align="left" border="0" width="100%">
       <tr><td>
          <table align="left" border="0" width="100%">
            <tr>
             <td>
              <?php echo $start_dl; ?>
              <?php echo $prcnt_v_good_print; ?>
              <?php echo $prcnt_good_print; ?>
              <?php echo $prcnt_srd_print; ?>
              <?php echo $prcnt_bad_print; ?>
              <?php echo $prcnt_v_bad_print; ?>
              <?php echo $stop_dl; ?>
             </td>
            </tr>
     </table>
</td>
<td valign="bottom" align="right" width="20%">
   <?php
 $query_b1 = mysqli_query($dbcnx,"SELECT COUNT(otz_id) AS co_otz_id FROM tbl_otziv WHERE book_id='".$_GET['book_id']."' AND login_id='".$result2['login_id']."' AND use_prog = '1'");
  $coun_b = mysqli_fetch_array($query_b1);
    if($coun_b['co_otz_id'] > 0) {echo "<img alt='".$TEXT['edit-tit']."' src='skins/".$result2['skin']."/edit.png' /><a class='bold_it' href='editform_otz.php?book_id=".$_GET['book_id']."' title='".$TEXT['book-otziv-edit']."'>".$TEXT['book-otziv-edit']."</a>";}
     else {echo "<img alt='".$TEXT['add-new']."' src='skins/".$result2['skin']."/add.png' /><a class='bold_it' href='addform_otz.php?book_id=".$_GET['book_id']."' title='".$TEXT['book-otziv-add']."'>".$TEXT['book-otziv-add']."</a>";}
 ?>
</td>
</tr>
</table>
<fieldset>
 <legend align="left" title="<?php echo $TEXT['book-otziv2']; ?>"><?php echo $TEXT['book-otziv2']; ?></legend>
<table align="left" width="100%" border="0">
<?php
$query_otziv = "SELECT 
 tbl_otziv.*,
 YEAR(tbl_otziv.otz_data) AS otz_data_year,
 MONTH(tbl_otziv.otz_data) AS otz_data_month,
 DAY(tbl_otziv.otz_data) AS otz_data_day,
 tbl_logins.familiya,
 tbl_logins.name,
 tbl_logins.otchestvo
 FROM tbl_otziv
 JOIN tbl_logins ON tbl_otziv.login_id=tbl_logins.login_id
 WHERE tbl_otziv.book_id = '".$_GET['book_id']."' AND tbl_otziv.use_prog = '1' LIMIT $lim_ogr";
 $result_otz = mysqli_query($dbcnx,$query_otziv);
  if(!$result_otz) {puterror2($result2['skin'],$TEXT['error-tbl22']); require_once "utils/bottom.php"; exit();}
    $otziv_num = mysqli_num_rows($result_otz);
    if($otziv_num == 0) {echo "<tr><td class='informcntr'>".$TEXT['book-otziv3']."&nbsp;</td></tr>";}
     else
     {
      while($result_otziv = mysqli_fetch_array($result_otz))
       {
       $date_day = $result_otziv['otz_data_day'];
       $date_month = $result_otziv['otz_data_month'];
       $date_year = $result_otziv['otz_data_year'];
       if($date_year == "0000" || $date_year == "1970") {$data_r = "---";}
        else {$data_r = $date_day." ".$month[$date_month]." ".$date_year." ".$TEXT['year2'];}
        
       $result_otziv['otziv'] = rep_back($result_otziv['otziv']);

       if($result_otziv['usr_rating'] == "1") {$otz_rating = $TEXT['book-ocenka1'];}
       if($result_otziv['usr_rating'] == "2") {$otz_rating = $TEXT['book-ocenka2'];}
       if($result_otziv['usr_rating'] == "3") {$otz_rating = $TEXT['book-ocenka3'];}
       if($result_otziv['usr_rating'] == "4") {$otz_rating = $TEXT['book-ocenka4'];}
       if($result_otziv['usr_rating'] == "5") {$otz_rating = $TEXT['book-ocenka5'];}
        
       if($result_otziv['otziv'] == "" && $result2['login_id'] == $result_otziv['login_id']) {$otziv = $TEXT['book-otziv6']."<a class='bold_it' href='editform_otz.php?book_id=".$_GET['book_id']."' title='".$TEXT['book-otziv-edit']."'>".$TEXT['book-otziv-edit']."</a>&raquo;";}
        else {$otziv = "<br />&nbsp;<span class='opis'>".nl2br($result_otziv['otziv'])."</span>";}

      if($result_otziv['otziv'] == "" && $result2['login_id'] == $result_otziv['login_id']) {echo "<tr><td class='informlft'>".$result_otziv['familiya']." ".$result_otziv['name']." ".$result_otziv['otchestvo']."<span class='informlftdat'> &#040;".$data_r." &#124; ".$otz_rating." &#041;</span>".$otziv."<hr align='left' width='55%' /></td></tr>";}
       elseif($result_otziv['otziv'] !== "" && $result2['login_id'] == $result_otziv['login_id']) {echo "<tr><td class='informlft'>".$result_otziv['familiya']." ".$result_otziv['name']." ".$result_otziv['otchestvo']."<span class='informlftdat'> &#040;".$data_r." &#124; ".$otz_rating." &#041;</span>".$otziv."<hr align='left' width='55%' /></td></tr>";}
        elseif($result_otziv['otziv'] !== "" && $result2['login_id'] !== $result_otziv['login_id']) {echo "<tr><td class='informlft'>".$result_otziv['familiya']." ".$result_otziv['name']." ".$result_otziv['otchestvo']."<span class='informlftdat'> &#040;".$data_r." &#124; ".$otz_rating." &#041;</span>".$otziv."<hr align='left' width='55%' /></td></tr>";}
       else {echo "";}
      }
     }
?>
</table>
</fieldset>
</td></tr>
</table>
     <?php
       $query_count = mysqli_query($dbcnx,"SELECT COUNT(otz_id) AS c_otz_id FROM tbl_otziv WHERE book_id = '".$_GET['book_id']."' AND use_prog = '1'");
	       $coun_rows = mysqli_fetch_array($query_count);
               $pag = $coun_rows['c_otz_id']/5;
               if($pag<1) {$pag = ceil($pag);}
                else
                {
                 ?>
                  <hr size="3" width="98%" />
                  <table align="center" border="0" cellpadding="0" cellspacing="0" width="60%" >
                   <tr align="center">
                    <td>
                <?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='otziv.php?k=".$k."&amp;book_id=".$_GET['book_id']."' 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='otziv.php?k=".$k."&amp;book_id=".$_GET['book_id']."' 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=otziv.php?k=".$k."&amp;book_id=".$_GET['book_id'].">".$j."</option>";}
                       else {echo "<option value=otziv.php?k=".$k."&amp;book_id=".$_GET['book_id'].">".$j."</option>";}
                    }
                    ?>
                    </select>
                    </td>
                   </tr>
                  </table>
                 <?php
                }
  require_once "utils/bottom.php";
?>