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

  $title = $TEXT['book-otziv-add2'];

  require_once "config_a.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($_POST['book_id'])) {puterror2($result2['skin'],$TEXT['error-book5']); require_once "utils/bottom.php"; exit();}
  if(strlen($_POST['book_id'])>20) {puterror2($result2['skin'],$TEXT['error-book6']); require_once "utils/bottom.php"; exit();}
  if(strlen($_POST['otziv'])>255) {puterror2($result2['skin'],$TEXT['error-book25']); require_once "utils/bottom.php"; exit();}
  if(empty($_POST['book_ocenka'])) {puterror2($result2['skin'],$TEXT['error-book24']); require_once "utils/bottom.php"; exit();}
  if(strlen($_POST['book_ocenka'])>1) {puterror2($result2['skin'],$TEXT['error-book24']); require_once "utils/bottom.php"; exit();}
  $_POST['otziv'] = rep($_POST['otziv']);
  $_POST['otziv'] = rep($_POST['otziv']);
  $_POST['book_id'] = rep($_POST['book_id']);

  $query_iskh = "SELECT book_rating FROM tbl_book WHERE book_id = '".$_POST['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);
      $book_rating = $_POST['book_ocenka'] + $result_iskh2['book_rating'];

  if(empty($_POST['otziv']))
   {
   $query = "UPDATE `tbl_book` SET `book_rating`= ".$book_rating." WHERE `book_id`='".$_POST['book_id']."'";
    if(mysqli_query($dbcnx,$query))
     {
     $queryst = "INSERT INTO tbl_otziv ( `otz_id` , `login_id` , `book_id` , `otziv` , `otz_data` , `usr_rating`, `use_prog` )
                 VALUES ( NULL,'".$result2['login_id']."','".$_POST['book_id']."','',CURRENT_DATE(),'".$_POST['book_ocenka']."','1')";
        if(mysqli_query($dbcnx,$queryst))
        {
         echo "<html><head>
            <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=otziv.php?book_id=".$_POST['book_id']."' />
            </head></html>";
         } else {puterror2($result2['skin'],$TEXT['error-tbl22']); require_once "utils/bottom.php"; exit();}
     } else {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}
    }
    else
    {
     $query = "UPDATE `tbl_book` SET `book_rating`= ".$book_rating." WHERE `book_id`='".$_POST['book_id']."'";
      if(mysqli_query($dbcnx,$query))
       {
       $queryst = "INSERT INTO tbl_otziv ( `otz_id` , `login_id` , `book_id` , `otziv` , `otz_data` , `usr_rating`, `use_prog` )
                   VALUES ( NULL , '".$result2['login_id']."' , '".$_POST['book_id']."' , '".$_POST['otziv']."' , CURRENT_DATE(), '".$_POST['book_ocenka']."','1')";
        if(mysqli_query($dbcnx,$queryst))
          {
          echo "<html><head>
                <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=otziv.php?book_id=".$_POST['book_id']."' />
                </head></html>";
           } else {puterror2($result2['skin'],$TEXT['error-tbl22']); require_once "utils/bottom.php"; exit();}
       } else {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}
    }
  require_once "utils/bottom.php";
?>