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

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

  $title=$titlepage=$TEXT['menu-btn17'];

  require_once "config.php";
  require_once "utils/top_zayavka_otv.php";
  if(empty($_POST['res_id'])) puterror($TEXT['error-res1']);
  if(strlen($_POST['res_id'])>11) puterror($TEXT['error-res2']);
  //$_POST['tema_id'] = substr($_POST['tema_id'], 24,52);
  //$_POST['tema_id'] = preg_replace('{&user_id=[\d]+?}','',$_POST['tema_id']);
  preg_match('/tema_id\\=([0-9]+)/i',$_POST['tema_id'], $tematmp);
  $_POST['tema_id'] = $tematmp['1'];
  if(strlen($_POST['sroch_id'])>11) puterror($TEXT['error-supp20']);
  if(strlen($_POST['tema_id'])>11) puterror($TEXT['error-supp1']);
  if(empty($_POST['kod'])) puterror($TEXT['error-supp9']);
  if(strlen($_POST['kod'])>300) puterror($TEXT['error-supp8']);
  if(strlen($_POST['org_id'])>11) puterror($TEXT['error-org']);
  $_POST['sroch_id'] = rep($_POST['sroch_id']);
  $_POST['tema_id'] = rep($_POST['tema_id']);
  $_POST['kod'] = rep($_POST['kod']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_POST['res_id'] = rep($_POST['res_id']);

  $org_id = $_POST['org_id'];
  $passwd = md5($_POST['kod']);
  $usr_tmp ="";

//function disabled by php > 5.5.3
////import_request_variables("gP", "in_");
// before using only ->  while(list($id,$value) = @each($in_usr))
 while(list($id2,$value2) = @each($_POST)) {
  if($id2 == "usr") {
   while(list($id,$value) = each($value2)) {
    if($id > 0 && $value == 'on')
     {
     $query_user = "SELECT
     tbl_users.*,
     tbl_organizaciya.org_id,
     tbl_organizaciya.organizaciya,
     tbl_upravlenie.upr_id,
     tbl_upravlenie.upravlenie,
     tbl_otdel.otd_id,
     tbl_otdel.otdel,
     tbl_dolzhnost.dol_id,
     tbl_dolzhnost.dolzhnost
     FROM tbl_users
     JOIN tbl_organizaciya ON tbl_users.org_id=tbl_organizaciya.org_id
     JOIN tbl_upravlenie ON tbl_users.upr_id=tbl_upravlenie.upr_id
     JOIN tbl_otdel ON tbl_users.otd_id=tbl_otdel.otd_id
     JOIN tbl_dolzhnost ON tbl_users.dol_id=tbl_dolzhnost.dol_id
     WHERE tbl_users.user_id = ".$id."
     ORDER BY tbl_users.familiya ";
     $result_user1 = mysql_query($query_user);
     if(!$result_user1) puterror($TEXT['error-tbl2']);
       if(mysql_num_rows($result_user1)>0);
        {
          while($result_zayavka = mysql_fetch_array($result_user1,MYSQL_ASSOC))
          {
           $usr_tmp .= $result_zayavka['familiya']." ".$result_zayavka['name']." ".$result_zayavka['otchestvo']." &#040;".$result_zayavka['organizaciya']." &#047; ".$result_zayavka['upravlenie']." &#047; ".$result_zayavka['otdel']." &#047; ".$result_zayavka['dolzhnost']."&#041; \n";
          }
        }
     }
   }
  }
 }
  $image = "";

  $query4 = "SELECT `sost_id`, `position` FROM `tbl_supp_sostoyanie` ORDER BY position LIMIT 1";
  $sost_tmp = mysql_query($query4);
   if(!$sost_tmp) puterror($TEXT['error-tbl23']);
     $sost = mysql_fetch_array($sost_tmp,MYSQL_ASSOC);
      $sostoyanie = $sost['sost_id'];

   $data_k_isp = "";
   $data_close = "";
   $man_id = "";
   $mast_id = "";
   $time_isp = "";
   $ob_type = "0";
   $otv = "1";

   $query_sh = "SELECT `shablon` FROM `tbl_supp_tema` WHERE tema_id='".$_POST['tema_id']."' ";
    $tema_tmp_sh = mysql_query($query_sh);
    if(!$tema_tmp_sh) puterror($TEXT['error-tbl22']);
     $tema_sh = mysql_fetch_array($tema_tmp_sh,MYSQL_ASSOC);
      $shablon = $tema_sh['shablon']."\n";

  $query_res = "SELECT `resource`, `ukk` FROM `tbl_resource` WHERE `res_id` = ".$_POST['res_id']." ";
  $res_tmp = mysql_query($query_res);
   if(!$res_tmp) puterror($TEXT['error-tbl16']);
   $res = mysql_fetch_array($res_tmp,MYSQL_ASSOC);
    $telo_soob = $TEXT['res-title1']." - ".$res['resource']." (".$TEXT['res-field5'].": ".$res['ukk'].") \n".$shablon." \n".$usr_tmp;

  $query = "INSERT INTO `tbl_supp_zayavka` (`zay_id` , `data_post` , `data_k_isp` , `data_close` , `man_id` , `mast_id` , `usr_id` , `sost_id` , `sroch_id` , `tema_id` , `problema` , `pic_tr` , `reshenie` , `kod` , `time_isp` , `org_id` , `ob_type`, `otv`, `mar_id`, `step`, `max_step`, `parent` ) VALUES (NULL , NOW() , '$data_k_isp' , '$data_close' , '$man_id' , '$mast_id' , '".$result2['usr_id']."' , '$sostoyanie' , '".$_POST['sroch_id']."' , '".$_POST['tema_id']."' , '".$telo_soob."' , '$image' , '".$TEXT['error-supp4']."' , '$passwd' , '$time_isp' , '".$_POST['org_id']."' , '$ob_type' , '$otv', '0', '0', '0', '0' )";
   if(mysql_query($query))
   {
   $query5 = "SELECT `zay_id`, `usr_id` FROM `tbl_supp_zayavka` WHERE `usr_id` = '".$result2['usr_id']."' ORDER BY zay_id DESC";
   $zay_tmp = mysql_query($query5);
    if(!$zay_tmp) puterror($TEXT['error-tbl26']);
     $zay = mysql_fetch_array($zay_tmp,MYSQL_ASSOC);
     $zay_num = $zay['zay_id'];

   $query6 = "SELECT `familiya`, `name`, `otchestvo` FROM `tbl_users` WHERE `user_id` = '".$result2['usr_id']."'";
   $usr_tmp = mysql_query($query6);
    if(!$usr_tmp) puterror($TEXT['error-tbl2']);
     $usr = mysql_fetch_array($usr_tmp,MYSQL_ASSOC);
     $usr_name = $usr['familiya']." ".$usr['name']." ".$usr['otchestvo'];

    //*************************************
    $ip = $_SERVER['REMOTE_ADDR'];
    $doing = $TEXT['error-supp5'];
    $what = $TEXT['support1']." ".$zay_num;
    $who = $usr_name;
    //*************************************
    $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=zayavka_otv.php' />
                   </head></html>";
           } else puterror($TEXT['error-log']);
   } else puterror($TEXT['error-tbl26']);
  require_once "utils/bottom_search.php";
?>
