<?php
  ///////////////////////////////////////////////////
  //2007 (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['upr-title3'];
  $helppage = "";

  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['upr_id'])) puterror2($TEXT['error-upr9']);
  if(strlen($_POST['upr_id'])>11) puterror2($TEXT['error-upr2']);
  if(empty($_POST['upravlenie'])) puterror2($TEXT['error-upr3']);
  if(strlen($_POST['upravlenie'])>300) puterror2($TEXT['error-upr4']);
  if(empty($_POST['position'])) puterror2($TEXT['error-upr5']);
  if(strlen($_POST['position'])>5) puterror2($TEXT['error-upr6']);

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

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

  $_POST['upravlenie'] = rep($_POST['upravlenie']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['upr_id'] = rep($_POST['upr_id']);

  $query10 = "SELECT `upravlenie` FROM `tbl_upravlenie` WHERE `upr_id`='".$_POST['upr_id']."' ";
  $result10 = mysql_query($query10);
  if (!$result10) puterror2($TEXT['error-tbl9']);
      $result_lib = mysql_fetch_array($result10);
      if($_POST['upravlenie']!=$result_lib['upravlenie']) {
          $query9 = "SELECT `upravlenie` FROM `tbl_upravlenie` WHERE `upravlenie`='".$_POST['upravlenie']."' ";
            $result9 = mysql_query($query9);
              if (!$result9) puterror2($TEXT['error-tbl9']);
                 if (mysql_num_rows($result9)>0) puterror2($TEXT['global-est']);
                   else $name_upravlenie = "`upravlenie` = '".$_POST['upravlenie']."', ";
        }
       else $name_upravlenie = "";

  //*************************************
  $query1 = "SELECT * FROM `tbl_upravlenie` WHERE `upr_id`='".$_POST['upr_id']."' ";
  $result = mysql_query($query1);
  if (!$result) puterror2($TEXT['error-tbl9']);
   $result2 = mysql_fetch_array($result);

  if($result2['upravlenie'] !== $_POST['upravlenie']) $log_upr = $TEXT['log-upr7'].$result2['upravlenie'].$TEXT['log-nd8'].$_POST['upravlenie'];
    else $log_upr = "";

  if($result2['upravlenie'] !== $_POST['upravlenie']) $log_upr_visible = $_POST['upravlenie'];
    else $log_upr_visible = $result2['upravlenie'];

  if($result2['position'] !== $_POST['position']) $log_pos = $TEXT['log-upr6'].$log_upr_visible.$TEXT['log-nd12'].$result2['position'].$TEXT['log-nd8'].$_POST['position'];
    else $log_pos = "";

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-upr4'];
  $what = $log_upr." ".$log_pos.$TEXT['log-id'].$_POST['upr_id'];
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE `tbl_upravlenie` SET $name_upravlenie `position` = '".$_POST['position']."' WHERE `upr_id`='".$_POST['upr_id']."' ";
  if(mysql_query($query))
  {
   $query3 = "UPDATE `tbl_users` SET `upr_pos` = '".$_POST['position']."' WHERE `upr_id` = '".$_POST['upr_id']."' ";
    if(mysql_query($query3))
     {
    $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-user30']);
  } else puterror2($TEXT['error-upr11']);
  require_once "../util/bottom.php";
?>
