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

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang."/".$lang.".php")) include "../lang/".$lang."/".$lang.".php";
     else include "../lang/ru/ru.php";

  $title=$titlepage = $TEXT['kat-title3'];
  $helppage = $TEXT['kat-help3'];

  require_once "../config.php";
  require_once "../util/top.php";

  if($result2['user'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['master'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['manager'] == 1) puterror2($TEXT['error-loginform4']);

  if(empty($_POST['zh_str_id'])) puterror2($TEXT['error-kat8']);
  if(strlen($_POST['zh_str_id'])>11) puterror2($TEXT['error-kat9']);
  if(empty($_POST['strukt_name'])) puterror2($TEXT['error-kat2']);
  if(strlen($_POST['strukt_name'])>255) puterror2($TEXT['error-kat3']);
  if(empty($_POST['position'])) puterror2($TEXT['error-kat4']);
  if(strlen($_POST['position'])>6) puterror2($TEXT['error-kat5']);
  if(empty($_POST['parent'])) $_POST['parent'] = 0;
  if(strlen($_POST['parent'])>20) puterror2($TEXT['error-kat18']);

  if(isset($_POST['strukt_name'])) $strukt_name=trim($_POST['strukt_name']);
    else $strukt_name="";
     if(empty($strukt_name)) puterror2($TEXT['error-kat6']);

  if(isset($_POST['position'])) $position=trim($_POST['position']);
    else $position="";
     if(empty($position)) puterror2($TEXT['error-otd7']);

  $_POST['strukt_name'] = rep($_POST['strukt_name']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['zh_str_id'] = rep($_POST['zh_str_id']);

  if($_POST['movechb'] == "on") $parnt = ", `parent` = '".$_POST['parent']."' ";
   else $parnt = "";
  //*************************************
  $query_kat = "SELECT * FROM `tbl_zhnaniy_strukt` WHERE `zh_str_id`='".$_POST['zh_str_id']."' ";
  $result_kat = mysql_query($query_kat);
  if(!$result_kat) puterror2($TEXT['error-tbl45']);
   $result_log = mysql_fetch_array($result_kat);

  if($result_log['strukt_name'] !== $_POST['strukt_name']) $log_strukt_name = $TEXT['log-otd5'].$result_log['strukt_name'].$TEXT['log-nd8'].$_POST['strukt_name']."<br />";
    else $log_strukt_name = "";

  if($result_log['strukt_name'] !== $_POST['strukt_name']) $log_strukt_name_visible = $_POST['strukt_name'];
    else $log_strukt_name_visible = $result_log['strukt_name'];

  if($result_log['position'] !== $_POST['position']) $log_pos = $TEXT['log-kat4'].$log_strukt_name_visible.$TEXT['log-nd12'].$result_log['position'].$TEXT['log-nd8'].$_POST['position']."<br />";
    else $log_pos = "";

  if($result_log['parent'] !== $_POST['parent']) $log_par = $TEXT['log-kat4'].$log_strukt_name_visible.$TEXT['log-nd12'].$result_log['parent'].$TEXT['log-nd8'].$_POST['parent']."<br />";
    else $log_par = "";

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-kat3'];
  $what = $log_strukt_name." ".$log_pos.$log_par.$TEXT['log-id'].$_POST['zh_str_id'];
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE `tbl_zhnaniy_strukt` SET `strukt_name` = '".$_POST['strukt_name']."', `position` = ".$_POST['position']." $parnt WHERE `zh_str_id`='".$_POST['zh_str_id']."' ";
  if(mysql_query($query))
  {
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
  } else puterror2($TEXT['error-kat11']);
  require_once "../util/bottom.php";
?>