<?php
  ///////////////////////////////////////////////////
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////
  require_once "../utils/chlang.php";

  $title = $TEXT['film-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_film.php";
  require_once "../utils/photo_functions.php";

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

  if(empty($_POST['film_name'])) {puterror($result2['skin'],$TEXT['error-film1']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['film_name'])>255) {puterror($result2['skin'],$TEXT['error-film2']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['autors'])>255) {puterror($result2['skin'],$TEXT['error-film9']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['opisanie'])>2000) {puterror($result2['skin'],$TEXT['error-film10']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['oglavlenie'])>3000){puterror($result2['skin'],$TEXT['error-film11']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['zhanr_id'])>5){puterror($result2['skin'],$TEXT['error-zhanr6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['cicle_id'])>5) {puterror($result2['skin'],$TEXT['error-cicle6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['tip_izd_id'])>5) {puterror($result2['skin'],$TEXT['error-tip-izd6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['bibl_id'])>5) {puterror($result2['skin'],$TEXT['error-libr6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['photo_obr'])>1) {puterror($result2['skin'],$TEXT['error-pic2']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['zal_id'])>5) {puterror($result2['skin'],$TEXT['error-zal6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['shkaf_id'])>5) {puterror($result2['skin'],$TEXT['error-shkaf6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['polka_id'])>5) {puterror($result2['skin'],$TEXT['error-polka6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['visible'])>5) {puterror($result2['skin'],$TEXT['error-new11']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['data_izdaniya'])>4) {puterror($result2['skin'],$TEXT['error-film21']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['data_postupleniya'])>10) {puterror($result2['skin'],$TEXT['error-film22']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(empty($_POST['ser_id'])) {puterror($result2['skin'],$TEXT['error-ser5']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['ser_id'])>5) {puterror($result2['skin'],$TEXT['error-ser6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['film_num'])>3) {puterror($result2['skin'],$TEXT['error-film23']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(empty($_POST['bl_id'])) {puterror($result2['skin'],$TEXT['error-filmlang5']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['bl_id'])>5) {puterror($result2['skin'],$TEXT['error-filmlang6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(empty($_POST['vozr_id'])) {puterror($result2['skin'],$TEXT['error-filmlang5']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['vozr_id'])>5) {puterror($result2['skin'],$TEXT['error-filmlang6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['prod_min'])>3) {puterror($result2['skin'],$TEXT['error-film23']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['subtitle'])>1) {puterror($result2['skin'],$TEXT['error-film23']); require_once "../utils/bottom_adm_film.php"; exit();}

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

  $_POST['film_name'] = rep($_POST['film_name']);
  $_POST['autors'] = rep($_POST['autors']);
  $_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['film_num'] = rep($_POST['film_num']);
  $_POST['bl_id'] = rep($_POST['bl_id']);
  $_POST['vozr_id'] = rep($_POST['vozr_id']);
  $_POST['prod_min'] = rep($_POST['prod_min']);
  $_POST['subtitle'] = rep($_POST['subtitle']);

  if(empty($_POST['film_num'])) {$film_num = 0;}
   else
    {
     $bnt = strlen($_POST['film_num']);
      if($bnt == 1) {$film_num = "00".$_POST['film_num'];}
       elseif($bnt == 2) {$film_num = "0".$_POST['film_num'];}
        else {$film_num = $_POST['film_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['subtitle'] == "1") {$subtitle = "yes";}
    else {$subtitle = "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['film_name'], 0,1);
    if(preg_match("/[A-Z]/i", $first_bt)) {$first_b = substr($_POST['film_name'], 0,1);}
     elseif(preg_match("/[АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЫЭЮЯ]/i", $first_bt)) {$first_b = substr($_POST['film_name'], 0,2);}
      else {$first_b = substr($_POST['film_name'], 0,3);}

    if($_FILES['photo']['size'] > 1024*2*1024) {puterror($result2['skin'],$TEXT['error-pic3']); require_once "../utils/bottom_adm_film.php"; exit();}
     if(!empty($_FILES['photo']['tmp_name']))
      {
       $ext = strrchr($_FILES['photo']['name'], ".");
       $trans_name = translate($_POST['film_name']);
       $name = $trans_name;
       $name_ = "b_film_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, 130, 185)) {puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm_film.php"; exit();}
                if(is_file($image)) {unlink($image);}
            }
            if($_POST['photo_obr'] == '3') {
                if(!resizeimg($image, $smallimage, 185, 130)) {puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm_film.php"; exit();}
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '4') {
                if(!resizeimg($image, $smallimage, 130, 185)) {puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm_film.php"; exit();}
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '5') {
                if(!resizeimg($image, $smallimage, 185, 130)) {puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm_film.php"; exit();}
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '6') {
                if(!resizeimg_l_r($image, $smallimage, 130, 185)) {puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm_film.php"; exit();}
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '7') {
                if(!resizeimg_r($image, $smallimage, 185, 130)) {puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm_film.php"; exit();}
                if(is_file($image)) {unlink($image);}
            }
     }
      else {$smallimage = "";}

    if($_FILES['efilm']['size'] > 1024*15*1024) {puterror($result2['skin'],$TEXT['error-pic1']); require_once "../utils/bottom_adm_film.php"; exit();}

    if(!empty($_FILES['efilm']['tmp_name']))
      {
       $ext = strrchr($_FILES['efilm']['name'], ".");
       $trans_film = translate($_POST['film_name']);
       $name2 = "film_".$trans_film;
       $efilm = "../efilms/$name2".date("YmdHis",time())."$ext";

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

  $query = "INSERT INTO `tbl_film` (
  `film_id`,
  `bibl_id`,
  `tip_izd_id`,
  `cicle_id`,
  `zhanr_id`,
  `user_id`,
  `user_id_zak`,
  `data_vidachi`,
  `data_sdachi`,
  `data_zakaza`,
  `vidano`,
  `zakazano`,
  `film_name`,
  `photo_film`,
  `visible`,
  `napominalka`,
  `zal_id`,
  `shkaf_id`,
  `polka_id`,
  `data_izdaniya`,
  `data_postupleniya`,
  `e_film`,
  `prod_min`,
  `opisanie`,
  `oglavlenie`,
  `first_bt`,
  `prichina`,
  `ser_id`,
  `film_num`,
  `bl_id`,
  `vozr_id`,
  `subtitle`,
  `autors`)
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['film_name']."',
  '$smallimage',
  '$vis',
  '0',
  '".$_POST['zal_id']."',
  '".$_POST['shkaf_id']."',
  '".$_POST['polka_id']."',
  $data_izdaniya,
  $data_postupleniya,
  '$efilm',
  '".$_POST['prod_min']."',
  '".$_POST['opisanie']."',
  '".$_POST['oglavlenie']."',
  '$first_b',
  '',
  '".$_POST['ser_id']."',
  '$film_num',
  '".$_POST['bl_id']."',
  ".$_POST['vozr_id'].",  
  '".$subtitle."',
  '".$_POST['autors']."')";
  if(mysqli_query($dbcnx,$query))
  {
    $query3 = mysqli_query($dbcnx,"SELECT LAST_INSERT_ID() AS c_lid");
      $last = mysqli_fetch_array($query3);

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-film1'];
  $what = $_POST['film_name'].", ".$TEXT['log-id']." ".$last['c_lid'];
  $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_izd.php?film_id=".$last['c_lid']."' />
                   </head></html>";
           } else {puterror($result2['skin'],$TEXT['error-log']); require_once "../utils/bottom_adm_film.php"; exit();}
  } else {puterror($result2['skin'],$TEXT['error-film4']); require_once "../utils/bottom_adm_film.php"; exit();}
  require_once "../utils/bottom_adm_film.php";
?>