<?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(empty($_POST['otz_id'])) {puterror2($result2['skin'],$TEXT['error-book26']); require_once "utils/bottom.php"; exit();}
  if(strlen($_POST['otz_id'])>20) {puterror2($result2['skin'],$TEXT['error-book27']); 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['book_ocenka'] = rep($_POST['book_ocenka']);
  $_POST['book_id'] = rep($_POST['book_id']);
  $_POST['otz_id'] = rep($_POST['otz_id']);

  $query_otziv = "SELECT usr_rating FROM tbl_otziv WHERE otz_id = '".$_POST['otz_id']."' ";
   $result_otz = mysqli_query($dbcnx,$query_otziv);
    if(!$result_otz) {puterror2($result2['skin'],$TEXT['error-tbl22']); require_once "utils/bottom.php"; exit();}
     $result_otziv = mysqli_fetch_array($result_otz);
     $book_ocenka = $result_otziv['usr_rating'];

  $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 = ($result_iskh2['book_rating'] - $book_ocenka) + $_POST['book_ocenka'];
      $book_rating_del = $result_iskh2['book_rating'] - $book_ocenka;

  if(empty($_POST['otziv']) || $_POST['delotz'] == "on")
   {
   $query = "UPDATE `tbl_book` SET `book_rating`= ".$book_rating_del." WHERE `book_id`='".$_POST['book_id']."' ";
    if(mysqli_query($dbcnx,$query))
     {
     $queryst = "DELETE FROM `tbl_otziv` WHERE `otz_id`='".$_POST['otz_id']."' ";
        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 = "UPDATE `tbl_otziv` SET `otziv`='".$_POST['otziv']."' , `otz_data` = CURRENT_DATE() , `usr_rating`=".$_POST['book_ocenka']." WHERE `otz_id`='".$_POST['otz_id']."' ";
        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";
?>