<?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";
?>
<form action=index.php method=post>
<?php
  $title=$titlepage = $TEXT['ex-title'];
  $helppage = $TEXT['ex-help4']."<br>";

  require_once "../../config.php";
  require_once "../../util/top_layer2.php";
?>
  <input type=submit value='<?php echo $TEXT['btn_back']; ?>'>
<?php
  if($result2['user'] == 1) puterror3($TEXT['error-loginform4']);
  if($result2['master'] == 1) puterror3($TEXT['error-loginform4']);
  if($result2['moderator'] == 1) puterror3($TEXT['error-loginform4']);

  if(empty($_POST['org_id'])) $_POST['org_id'] = "";
  if(empty($_POST['old_org'])) $_POST['old_org'] = "off";

  if(strlen($_POST['org_id'])>300) puterror3($TEXT['error-org4']);
  if(strlen($_POST['typepar'])>2) puterror3($TEXT['search-err3']);
  if(strlen($_POST['old_org'])>3) puterror3($TEXT['ex-err1']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_POST['typepar'] = rep($_POST['typepar']);
  $_POST['old_org'] = rep($_POST['old_org']);

  if($_POST['old_org'] !== "on") $new_org = "";
    else $new_org = "AND organizaciya = '".$_POST['org_id']."'";

  $action = "support";
    switch($_POST['typepar'])
      {
      case 1:
      $action = "support";
      break;
      case 2:
      $action = "support_ch";
      break;
      }

     $exp_path = "../../../NormDoc/".$action."-".date("YMd-H-i-s",time()).".csv";

      $file = fopen($exp_path,"a");

      if($action == "support") 
      {
      $log_act = $TEXT['ex-log5'];
      $exp_head = $TEXT['tbl-org'].",".$TEXT['tbl-date'].",".$TEXT['login-manager'].",".$TEXT['login-master'].",".$TEXT['login-user'].",".$TEXT['supp-tema-title'].",".$TEXT['supp-zay'].",".$TEXT['supp-time']."\r\n";

      fwrite($file, $exp_head, strlen($exp_head));

     $query = "SELECT organizaciya, data, manager, master, user, tema, zay_id, time_isp FROM tbl_supp_archive WHERE arch_id != 0 ".$new_org." ORDER BY organizaciya, data";
     $result = mysql_query($query);
     if(!$result) puterror3($TEXT['error-tbl25']);
       if(mysql_num_rows($result)>0);
         {
         while($result_exps = mysql_fetch_array($result,MYSQL_ASSOC))
               {
        	$exp_string = $result_exps['organizaciya'].",".$result_exps['data'].",".$result_exps['manager'].",".$result_exps['master'].",".$result_exps['user'].",".$result_exps['tema'].",".$result_exps['zay_id'].",".$result_exps['time_isp']."\r\n";

  		fwrite($file, $exp_string, strlen($exp_string));
 		}
         }
  	fclose($file);
       }
      if($action == "support_ch") 
      {
      $log_act = $TEXT['ex-log4'];
      $exp_head = $TEXT['tbl-org'].",".$TEXT['tbl-date'].",".$TEXT['login-manager'].",".$TEXT['login-master'].",".$TEXT['login-user'].",".$TEXT['supp-tema-title'].",".$TEXT['supp-zay'].",".$TEXT['supp-time']."\r\n";

      fwrite($file, $exp_head, strlen($exp_head));

     $query = "SELECT organizaciya, data, manager, master, user, tema, zay_id, time_isp, pic_tr FROM tbl_supp_archive WHERE arch_id != 0 ".$new_org." ORDER BY organizaciya, data";
     $result = mysql_query($query);
     if(!$result) puterror3($TEXT['error-tbl25']);
       if(mysql_num_rows($result)>0);
         {
          while($result_exps = mysql_fetch_array($result,MYSQL_ASSOC))
               {
        	$exp_string = $result_exps['organizaciya'].",".$result_exps['data'].",".$result_exps['manager'].",".$result_exps['master'].",".$result_exps['user'].",".$result_exps['tema'].",".$result_exps['zay_id'].",".$result_exps['time_isp']."\r\n";
                if($result_exps['pic_tr'] !== "" && is_file("../../../".$result_exps['pic_tr'])) unlink("../../../".$result_exps['pic_tr']);

  		fwrite($file, $exp_string, strlen($exp_string));
 		}
         }
  	fclose($file);

  $query_delete = "DELETE FROM tbl_supp_archive WHERE arch_id != 0 ".$new_org."";
  if(!mysql_query($query_delete)) puterror3($TEXT['error-tbl25']);
  }
  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['ex-log3'];
  $what = $log_act;
  $who = $result2['fullname'];
  //*************************************
    $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=transfer.php?trans=".$exp_path."' />
                   </head></html>";
           } else puterror3($TEXT['error-log']);
?>
</form>
<?php
  require_once "../../util/bottom_exp.php";
?>