<?php
  ///////////////////////////////////////////////////
  //Выдать книгу
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

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

  $title = $TEXT['aut-title-info'];

  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_vidachi'])) {puterror($result2['skin'],$TEXT['error-book14']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['data_vidachi'])>10) {puterror($result2['skin'],$TEXT['error-book15']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['data_sdachi'])) {puterror($result2['skin'],$TEXT['error-book16']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['data_sdachi'])>10) {puterror($result2['skin'],$TEXT['error-book17']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['napominalka'])>3) {puterror($result2['skin'],$TEXT['error-book18']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['naloff'])>3) {puterror($result2['skin'],$TEXT['error-book19']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['prichina'])>255) {puterror($result2['skin'],$TEXT['error-book20']); require_once "../utils/bottom_adm.php"; exit();}
  $_POST['book_id'] = rep($_POST['book_id']);
  $_POST['user_id'] = rep($_POST['user_id']);
  $_POST['data_vidachi'] = rep($_POST['data_vidachi']);
  $_POST['data_sdachi'] = rep($_POST['data_sdachi']);
  $_POST['napominalka'] = rep($_POST['napominalka']);
  $_POST['naloff'] = rep($_POST['naloff']);
  $_POST['prichina'] = rep($_POST['prichina']);

  if($_POST['napominalka'] == "on") {$napominalka = 1;}
   else {$napominalka =0;}

$query_iskh = "SELECT `book_name`, `zakazano` FROM `tbl_book` WHERE `book_id` = '".$_POST['book_id']."' ";
  $result_iskh = mysqli_query($dbcnx,$query_iskh);
  if(!$result_iskh) {puterror($result2['skin'],$TEXT['error-tbl3']); require_once "../utils/bottom_adm.php"; exit();}
      $result_iskh2 = mysqli_fetch_array($result_iskh);

  if($_POST['naloff'] == "on")
  {
  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-book6'];
  $what = $result_iskh2['book_name'].", ".$TEXT['log-id']." ".$_POST['book_id'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************
  $query = "UPDATE `tbl_book` SET `napominalka`='0', `user_id`='".$_POST['user_id']."', `data_vidachi`=CURRENT_DATE(), `data_sdachi`='1970-01-01', `vidano`='2', `prichina`='".$_POST['prichina']."', `zakazano`='0', `data_zakaza`='1970-01-01', `user_id_zak`='0' WHERE `book_id`='".$_POST['book_id']."' ";
  if(mysqli_query($dbcnx,$query))
  {
      $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysqli_query($dbcnx,$query2))
           {
           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.php"; exit();}
  } else {puterror($result2['skin'],$TEXT['error-tbl3']); require_once "../utils/bottom_adm.php"; exit();}
  }
   else
  {
  //*************************************
  //Информация для лога

   if($result_iskh2['zakazano'] == '1') {$been_zak = $TEXT['log-book9'].", ";}
     else {$been_zak = "";}

   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.php"; exit();}
      $result_for_user_book = mysqli_fetch_array($result_for_user);
      $book_log_user = $TEXT['book-nal-for-user2'].$result_for_user_book['familiya']."&nbsp;".$result_for_user_book['name']."&nbsp;".$result_for_user_book['otchestvo']."&#10099;";
    } else {$book_log_user = $TEXT['book-nal-for-me'];}

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $been_zak.$TEXT['log-book4'].$book_log_user;
  $what = $result_iskh2['book_name'].", ".$TEXT['log-id']." ".$_POST['book_id'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************
  $query = "UPDATE `tbl_book` SET `napominalka`='".$napominalka."', `user_id`='".$_POST['user_id']."', `data_vidachi`='".$_POST['data_vidachi']."', `data_sdachi`='".$_POST['data_sdachi']."', `vidano`='1', `zakazano`='0', `data_zakaza`='1970-01-01', `user_id_zak`='0' WHERE `book_id`='".$_POST['book_id']."' ";
  if(mysqli_query($dbcnx,$query))
  {
      $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysqli_query($dbcnx,$query2))
           {
           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.php"; exit();}
  } else {puterror($result2['skin'],$TEXT['error-tbl3']); require_once "../utils/bottom_adm.php"; exit();}
 }
  require_once "../utils/bottom_adm.php";
?>