<?php
  ///////////////////////////////////////////////////
  //Включить резервирование
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

 require_once "../utils/chlang.php";

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

  require_once "../config_a.php";
  require_once "../utils/functions.php";
  require_once "../utils/top_adm.php";

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

  if(empty($_POST['book_id'])) {puterror($result2['skin'],$TEXT['error-book5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['book_id'])>20) {puterror($result2['skin'],$TEXT['error-book6']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['user_id'])) {puterror($result2['skin'],$TEXT['error-login15']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['user_id'])>15) {puterror($result2['skin'],$TEXT['error-login14']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['data_zakaza'])) {puterror($result2['skin'],$TEXT['error-book12']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['data_zakaza'])>10) {puterror($result2['skin'],$TEXT['error-book13']); require_once "../utils/bottom_adm.php"; exit();}
  $_POST['book_id'] = rep($_POST['book_id']);
  $_POST['user_id'] = rep($_POST['user_id']);
  $_POST['data_zakaza'] = rep($_POST['data_zakaza']);

  //*************************************
  //Информация для лога
   $query_body = "SELECT `book_name` FROM tbl_book WHERE `book_id` = '".$_POST['book_id']."'";
     $result_body = mysqli_query($dbcnx,$query_body);
     if(!$result_body) {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "../utils/bottom_adm"; exit();}
      $result_body_book = mysqli_fetch_array($result_body);

   if($_POST['user_id'] !== $result2['login_id']) {
     $query_for_user = "SELECT `name`, `familiya`, `otchestvo` FROM `tbl_logins` WHERE `login_id`='".$_POST['user_id']."' LIMIT 1";
     $result_for_user = mysqli_query($dbcnx,$query_for_user);
     if(!$result_for_user) {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "../utils/bottom_adm"; exit();}
      $result_for_user_book = mysqli_fetch_array($result_for_user);
      $book_log_user = $TEXT['book-rez-for-user'].$result_for_user_book['familiya']."&nbsp;".$result_for_user_book['name']."&nbsp;".$result_for_user_book['otchestvo']."&#10099;";
   } else {$book_log_user = $TEXT['book-rez-for-me'];}
      
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-book8'].$book_log_user;
  $what = $result_body_book['book_name'].", ".$TEXT['log-id']." ".$_POST['book_id'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************

  $query = "UPDATE `tbl_book` SET `user_id`='".$_POST['user_id']."', `user_id_zak`='".$_POST['user_id']."', `data_zakaza`='".$_POST['data_zakaza']."', `zakazano`= '1' WHERE `book_id`='".$_POST['book_id']."' ";
  if(mysqli_query($dbcnx,$query))
  {
   $query_log = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
     if(mysqli_query($dbcnx,$query_log))
      {
       echo "<html><head>
             <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=info.php?book_id=".$_POST['book_id']."' />
             </head></html>";
      } else {puterror($result2['skin'],$TEXT['error-log']); require_once "../utils/bottom_adm"; exit();}
  } else {puterror($result2['skin'],$TEXT['error-tbl3']); require_once "../utils/bottom_adm.php"; exit();}
  require_once "../utils/bottom_adm.php";
?>