<?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_film.php";

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

  if(empty($_POST['film_id'])) {puterror2($result2['skin'],$TEXT['error-film5']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['film_id'])>20) {puterror2($result2['skin'],$TEXT['error-film6']); require_once "utils/bottom_film.php"; exit();}
  if(empty($_POST['otz_id'])) {puterror2($result2['skin'],$TEXT['error-film26']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['otz_id'])>20) {puterror2($result2['skin'],$TEXT['error-film27']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['otziv'])>255) {puterror2($result2['skin'],$TEXT['error-film25']); require_once "utils/bottom_film.php"; exit();}
  if(empty($_POST['film_ocenka'])) {puterror2($result2['skin'],$TEXT['error-film24']); require_once "utils/bottom_film.php"; exit();}
  if(strlen($_POST['film_ocenka'])>1) {puterror2($result2['skin'],$TEXT['error-film24']); require_once "utils/bottom_film.php"; exit();}
  $_POST['otziv'] = rep($_POST['otziv']);
  $_POST['film_ocenka'] = rep($_POST['film_ocenka']);
  $_POST['film_id'] = rep($_POST['film_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_film.php"; exit();}
     $result_otziv = mysqli_fetch_array($result_otz);
     $film_ocenka = $result_otziv['usr_rating'];

  $query_iskh = "SELECT film_rating FROM tbl_film WHERE film_id = '".$_POST['film_id']."' ";
  $result_iskh = mysqli_query($dbcnx,$query_iskh);
  if(!$result_iskh) {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom_film.php"; exit();}
      $result_iskh2 = mysqli_fetch_array($result_iskh);
      $film_rating = ($result_iskh2['film_rating'] - $film_ocenka) + $_POST['film_ocenka'];
      $film_rating_del = $result_iskh2['film_rating'] - $film_ocenka;

  if(empty($_POST['otziv']) || $_POST['delotz'] == "on")
   {
   $query = "UPDATE `tbl_film` SET `film_rating`= ".$film_rating_del." WHERE `film_id`='".$_POST['film_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=film-otziv.php?film_id=".$_POST['film_id']."' />
            </head></html>";
          } else {puterror2($result2['skin'],$TEXT['error-tbl22']); require_once "utils/bottom_film.php"; exit();}
     } else {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom_film.php"; exit();}
    }
    else
    {
     $query = "UPDATE `tbl_film` SET `film_rating`= ".$film_rating." WHERE `film_id`='".$_POST['film_id']."' ";
      if(mysqli_query($dbcnx,$query))
       {
       $queryst = "UPDATE `tbl_otziv` SET `otziv`='".$_POST['otziv']."' , `otz_data` = CURRENT_DATE() , `usr_rating`=".$_POST['film_ocenka']." WHERE `otz_id`='".$_POST['otz_id']."' ";
        if(mysqli_query($dbcnx,$queryst))
          {
          echo "<html><head>
                <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=film-otziv.php?film_id=".$_POST['film_id']."' />
                </head></html>";
           } else {puterror2($result2['skin'],$TEXT['error-tbl22']); require_once "utils/bottom_film.php"; exit();}
       } else {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom_film.php"; exit();}
    }
  require_once "utils/bottom_film.php";
?>