<?php
  ///////////////////////////////////////////////////
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

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

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

  if(is_file ("../lang/".$lang."/trans.php")) {require_once "../lang/".$lang."/trans.php";}
   else {require_once "../lang/ru/trans.php";}

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

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

  if(empty($_POST['book_name'])) {puterror($result2['skin'],$TEXT['error-book1']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['book_name'])>255) {puterror($result2['skin'],$TEXT['error-book2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['isbn'])>255) {puterror($result2['skin'],$TEXT['error-book9']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['opisanie'])>3000) {puterror($result2['skin'],$TEXT['error-book10']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['oglavlenie'])>30000){puterror($result2['skin'],$TEXT['error-book11']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['zhanr_id'])>5){puterror($result2['skin'],$TEXT['error-zhanr6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['cicle_id'])>5) {puterror($result2['skin'],$TEXT['error-cicle6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['tip_izd_id'])>5) {puterror($result2['skin'],$TEXT['error-tip-izd6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['bibl_id'])>5) {puterror($result2['skin'],$TEXT['error-libr6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['photo_obr'])>1) {puterror($result2['skin'],$TEXT['error-pic2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['zal_id'])>5) {puterror($result2['skin'],$TEXT['error-zal6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['shkaf_id'])>5) {puterror($result2['skin'],$TEXT['error-shkaf6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['polka_id'])>5) {puterror($result2['skin'],$TEXT['error-polka6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['visible'])>5) {puterror($result2['skin'],$TEXT['error-new11']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['data_izdaniya'])>4) {puterror($result2['skin'],$TEXT['error-book21']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['data_postupleniya'])>10) {puterror($result2['skin'],$TEXT['error-book22']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['ser_id'])) {puterror($result2['skin'],$TEXT['error-ser5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['ser_id'])>5) {puterror($result2['skin'],$TEXT['error-ser6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['book_num'])>3) {puterror($result2['skin'],$TEXT['error-book23']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['bl_id'])) {puterror($result2['skin'],$TEXT['error-booklang5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['bl_id'])>5) {puterror($result2['skin'],$TEXT['error-booklang6']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['bl2_id'])) {puterror($result2['skin'],$TEXT['error-booklang5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['bl2_id'])>5) {puterror($result2['skin'],$TEXT['error-booklang6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['paper_copy'])>5) {puterror($result2['skin'],$TEXT['error-new13']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['remote_copy'])>5) {puterror($result2['skin'],$TEXT['error-new13']); require_once "../utils/bottom_adm.php"; exit();}

  if(isset($_POST['book_name'])) {$book_name=trim($_POST['book_name']);}
    else {$book_name="";}
     if(empty($book_name)) {puterror($result2['skin'],$TEXT['error-book3']); require_once "../utils/bottom_adm.php"; exit();}

  $_POST['book_name'] = rep($_POST['book_name']);
  $_POST['isbn'] = rep($_POST['isbn']);
  $_POST['opisanie'] = rep($_POST['opisanie']);
  $_POST['oglavlenie'] = rep($_POST['oglavlenie']);
  $_POST['zhanr_id'] = rep($_POST['zhanr_id']);
  $_POST['cicle_id'] = rep($_POST['cicle_id']);
  $_POST['tip_izd_id'] = rep($_POST['tip_izd_id']);
  $_POST['bibl_id'] = rep($_POST['bibl_id']);
  $_POST['photo_obr'] = rep($_POST['photo_obr']);
  $_POST['zal_id'] = rep($_POST['zal_id']);
  $_POST['shkaf_id'] = rep($_POST['shkaf_id']);
  $_POST['polka_id'] = rep($_POST['polka_id']);
  $_POST['data_izdaniya'] = rep($_POST['data_izdaniya']);
  $_POST['data_postupleniya'] = rep($_POST['data_postupleniya']);
  $_POST['ser_id'] = rep($_POST['ser_id']);
  $_POST['book_num'] = rep($_POST['book_num']);
  $_POST['bl_id'] = rep($_POST['bl_id']);
  $_POST['bl2_id'] = rep($_POST['bl2_id']);

  if(empty($_POST['book_num'])) {$book_num = 0;}
   else
    {
     $bnt = strlen($_POST['book_num']);
      if($bnt == 1) {$book_num = "00".$_POST['book_num'];}
       elseif($bnt == 2) {$book_num = "0".$_POST['book_num'];}
        else {$book_num = $_POST['book_num'];}
   }

  if($_POST['e_book_10'] == "on" && !empty($_POST['book_num']) && $_POST['book_num'] !== 0) {$book_num_up10 = "0".$_POST['book_num'];}
   else {$book_num_up10 = $_POST['book_num'];}

  if(empty($_POST['data_postupleniya'])) {$data_postupleniya = "CURRENT_DATE()";}
   else {$data_postupleniya = "'".$_POST['data_postupleniya']."'";}

  if(empty($_POST['data_izdaniya'])) {$data_izdaniya = "DATE_FORMAT(NOW(),'%Y')";}
   else {$data_izdaniya = $_POST['data_izdaniya'];}

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

  if($_POST['paper_copy'] == "on") {$papcopy = "yes";}
    else {$papcopy = "no";}

  if($_POST['remote_copy'] == "on") {$remote_copy = "yes";}
    else {$remote_copy = "no";}

  if($_POST['zakupka'] == "on") {
   $zakupka = 3;
   $user_id = $result2['login_id'];
   $data = "CURRENT_DATE()";
   $data_postupleniya = "'1970-01-01'";
  }
   else
   {
    $zakupka = 0;
    $user_id = 0;
    $data = "'1970-01-01'";
   }

  $first_bt = substr($_POST['book_name'], 0,1);
    if(preg_match("/[A-Z]/i", $first_bt)) {$first_b = substr($_POST['book_name'], 0,1);}
       else {$first_b = substr($_POST['book_name'], 0,2);}

    if($_FILES['photo']['size'] > 1024*2*1024) {puterror($result2['skin'],$TEXT['error-pic3']); require_once "../utils/bottom_adm.php"; exit();}
     if(!empty($_FILES['photo']['tmp_name']))
      {
       $ext = strrchr($_FILES['photo']['name'], ".");
       $trans_name = translate($_POST['book_name']);
       $name = $trans_name;
       $name_ = "b_pho_".$trans_name;
       $image = "../photo/$name".date("YmdHis",time())."$ext";
       $smallimage = "../photo/$name_".date("YmdHis",time())."$ext";

          if(copy($_FILES['photo']['tmp_name'], $image))
            {
            unlink($_FILES['photo']['tmp_name']);
            chmod($image, 0644);
            }
            if($_POST['photo_obr'] == '1') {
                 $smallimage = $image;
            }
            if($_POST['photo_obr'] == '2') {
                 if(!resizeimg($image, $smallimage, 300, 500))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
            if($_POST['photo_obr'] == '3') {
                 if(!resizeimg($image, $smallimage, 500, 300))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '4') {
                 if(!resizeimg($image, $smallimage, 300, 500))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '5') {
                 if(!resizeimg($image, $smallimage, 500, 300))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '6') {
                 if(!resizeimg_l_r($image, $smallimage, 300, 500))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '7') {
                 if(!resizeimg_r($image, $smallimage, 500, 300))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
     }
      else {$smallimage = "";}

    $query11 = "SELECT `cicle` FROM `tbl_cicle` WHERE `cicle_id` = '".$_POST['cicle_id']."' LIMIT 1 ";
    $result11 = mysqli_query($dbcnx,$query11);
        if(!$result11) {puterror($result2['skin'],$TEXT['error-tbl5']); require_once "../utils/bottom_adm.php"; exit();}
        while($result_cicle = mysqli_fetch_array($result11))
            {
            if($result_cicle['cicle'] != '' && $result_cicle['cicle'] != '---') {$book_cicle_name = translate("_".rep_back($result_cicle['cicle'])."_".$book_num_up10."_");}
            else {$book_cicle_name = "_";}
            }

    if($_FILES['ebook']['size'] > 1024*200*1024) {puterror($result2['skin'],$TEXT['error-pic1']); require_once "../utils/bottom_adm.php"; exit();}
    if(!empty($_FILES['ebook']['tmp_name']))
      {
       $ext = strrchr($_FILES['ebook']['name'], ".");
       $trans_book = translate($_POST['book_name']);
       $name2 = "book".$book_cicle_name.$trans_book;
       $ebook = "../ebooks/".$name2.".".$ext;

          if(copy($_FILES['ebook']['tmp_name'], $ebook))
            {
            unlink($_FILES['ebook']['tmp_name']);
            chmod($ebook, 0644);
            }
     }
      else {$ebook = "";}

    if($_FILES['abook']['size'] > 1024*700*1024) {puterror($result2['skin'],$TEXT['error-pic7']); require_once "../utils/bottom_adm.php"; exit();}
    if(!empty($_FILES['abook']['tmp_name']))
      {
       $exta = strrchr($_FILES['abook']['name'], ".");
       $trans_abook = translate($_POST['book_name']);
       $namea2 = "audio_book".$book_cicle_name.$trans_abook;
       $abook = "../abooks/".$namea2.".".$exta;

          if(copy($_FILES['abook']['tmp_name'], $abook))
            {
            unlink($_FILES['abook']['tmp_name']);
            chmod($abook, 0644);
            }
     }
      else {$abook = "";}

    if($_FILES['cdbook']['size'] > 1024*700*1024) {puterror($result2['skin'],$TEXT['error-pic7']); require_once "../utils/bottom_adm.php"; exit();}
    if(!empty($_FILES['cdbook']['tmp_name']))
      {
       $extcd = strrchr($_FILES['cdbook']['name'], ".");
       $trans_cdbook = translate($_POST['book_name']);
       $namecd2 = "cdrom_book".$book_cicle_name.$trans_cdbook;
       $cdbook = "../cdbooks/".$namecd2.".".$extcd;

          if(copy($_FILES['cdbook']['tmp_name'], $cdbook))
            {
            unlink($_FILES['cdbook']['tmp_name']);
            chmod($cdbook, 0644);
            }
     }
      else {$cdbook = "";}

  $query = "INSERT INTO `tbl_book` (
  `book_id`,
  `bibl_id`,
  `tip_izd_id`,
  `cicle_id`,
  `zhanr_id`,
  `user_id`,
  `user_id_zak`,
  `data_vidachi`,
  `data_sdachi`,
  `data_zakaza`,
  `vidano`,
  `zakazano`,
  `book_name`,
  `photo_book`,
  `visible`,
  `napominalka`,
  `zal_id`,
  `shkaf_id`,
  `polka_id`,
  `data_izdaniya`,
  `data_postupleniya`,
  `e_book`,
  `isbn`,
  `opisanie`,
  `oglavlenie`,
  `first_bt`,
  `prichina`,
  `ser_id`,
  `book_num`,
  `bl_id`,
  `bl2_id`,
  `paper_copy`,
  `remote_copy`,
  `a_book`,
  `cd_book`,
  `prev_books`)
VALUES ( 
  NULL ,
  '".$_POST['bibl_id']."',
  '".$_POST['tip_izd_id']."',
  '".$_POST['cicle_id']."',
  '".$_POST['zhanr_id']."',
  '$user_id',
  '0',
  $data,
  '1970-01-01',
  '1970-01-01',
  '$zakupka',
  '0',
  '".$_POST['book_name']."',
  '$smallimage',
  '$vis',
  '0',
  '".$_POST['zal_id']."',
  '".$_POST['shkaf_id']."',
  '".$_POST['polka_id']."',
  $data_izdaniya,
  $data_postupleniya,
  '$ebook',
  '".$_POST['isbn']."',
  '".$_POST['opisanie']."',
  '".$_POST['oglavlenie']."',
  '$first_b',
  '',
  '".$_POST['ser_id']."',
  '$book_num',
  '".$_POST['bl_id']."',
  '".$_POST['bl2_id']."',
  '$papcopy',
  '$remote_copy',
  '$abook',
  '$cdbook',
  '0')";
  if(mysqli_query($dbcnx,$query))
  {
    $query3 = mysqli_query($dbcnx,"SELECT LAST_INSERT_ID() AS c_lst_id");
      $last = mysqli_fetch_array($query3);

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-book1'];
  $what = $_POST['book_name'].", ".$TEXT['log-id']." ".$last['c_lst_id'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************
    $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=add_autor.php?book_id=".$last['c_lst_id']."' />
                   </head></html>";
           } else {puterror($result2['skin'],$TEXT['error-log']); require_once "../utils/bottom_adm.php"; exit();}
  } else {puterror($result2['skin'],$TEXT['error-book4']); require_once "../utils/bottom_adm.php"; exit();}
  require_once "../utils/bottom_adm.php";
?>