<?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['pho-title6'];
  $helppage = "";

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

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

  if(empty($_POST['mer_id'])) puterror2($TEXT['error-pho15']);
  if(empty($_POST['photo_id'])) puterror2($TEXT['error-pho17']);
  if(strlen($_POST['mer_id'])>11) puterror2($TEXT['error-pho13']);
  if(strlen($_POST['photo_id'])>11) puterror2($TEXT['error-pho18']);
  if(strlen($_POST['old_mer_id'])>11) puterror2($TEXT['error-pho21']);
  if(strlen($_POST['description'])>800) puterror2($TEXT['error-pho12']);
  if(strlen($_POST['name'])>300) puterror2($TEXT['error-pho9']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-pho4']);
  $_POST['mer_id'] = rep($_POST['mer_id']);
  $_POST['photo_id'] = rep($_POST['photo_id']);
  $_POST['old_mer_id'] = rep($_POST['old_mer_id']);
  $_POST['description'] = rep($_POST['description']);
  $_POST['name'] = rep($_POST['name']);
  $_POST['position'] = rep($_POST['position']);

  $new_mer = $_POST['mer_id'];

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

          $query3 = "SELECT * FROM `tbl_meropriyatiya` WHERE `mer_id`='".$_POST['mer_id']."' ";
          $result3 = mysql_query($query3);
          if (!$result3) puterror2($TEXT['error-tbl12']);
              $result4 = mysql_fetch_array($result3);

          $query4 = "SELECT * FROM `tbl_meropriyatiya` WHERE `mer_id`='".$_POST['old_mer_id']."' ";
          $result5 = mysql_query($query4);
          if (!$result5) puterror2($TEXT['error-tbl12']);
              $result6 = mysql_fetch_array($result5);

     if($result2['name'] !== $_POST['name']) $log_name = $TEXT['log-nd7'].$result2['name'].$TEXT['log-nd8'].$_POST['name'];
    else $log_name = "";

     if($result2['name'] !== $_POST['name']) $log_name_visible = $_POST['name'];
    else $log_name_visible = $result2['name'];

     if($new_mer !== $_POST['old_mer_id']) $log_mer = $TEXT['log-pho8'].$log_name_visible.$TEXT['log-pho9'].$result6['meropriyatie'].$TEXT['log-nd8'].$result4['meropriyatie'];
    else $log_mer = "";

     if($result2['description'] !== $_POST['description']) $log_des = $TEXT['log-pho8'].$log_name_visible.$TEXT['log-nd11'].$result2['description'].$TEXT['log-nd8'].$_POST['description'];
    else $log_des = "";

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

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-pho7'];
  $what = $log_name." ".$log_mer." ".$log_des." ".$log_pos.$TEXT['log-id'].$_POST['photo_id'];
  $who = $result2['fullname'];
  //*************************************
    $query = "UPDATE `tbl_photo` SET `name` = '".$_POST['name']."', `description` = '".$_POST['description']."', `position` = '".$_POST['position']."', `mer_id` = '$new_mer' WHERE `photo_id`='".$_POST['photo_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=photoindex.php?mer_id=".$_POST['old_mer_id']."' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
         } else puterror2($TEXT['error-pho22']);
  require_once "../util/bottom.php";
?>
