<?php
  ///////////////////////////////////////////////////
  //2011 (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['prn-title'];
  require_once "../config.php";
  require_once "top.php";
?>
<title><?php echo $title; ?></title>
  <link rel="StyleSheet" type="text/css" href="../utils/reader.css" />
  <link rel="StyleSheet" type="text/css" href="../utils/grid.css" />
  </head>
  <body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" bottommargin="0" topmargin="0" >
<form action=prn_main.php method=post>
<?php
  $month["01"] = $TEXT['month01'];
  $month["02"] = $TEXT['month02'];
  $month["03"] = $TEXT['month03'];
  $month["04"] = $TEXT['month04'];
  $month["05"] = $TEXT['month05'];
  $month["06"] = $TEXT['month06'];
  $month["07"] = $TEXT['month07'];
  $month["08"] = $TEXT['month08'];
  $month["09"] = $TEXT['month09'];
  $month["10"] = $TEXT['month10'];
  $month["11"] = $TEXT['month11'];
  $month["12"] = $TEXT['month12'];

    switch($_GET['typepar'])
      {
      case 1: //кол-во страниц
      $action = " AND tbl_prn_logs.pages >= '".$_GET['search']."'";
      break;
      case 2: //дата
      $delimiter = ",";
      $srch_trim = explode($delimiter, $_GET['search'], 2);
      $dt = trim($srch_trim[0]);
      $lst = trim($srch_trim[1]);
      if($lst !== "") $action = " AND tbl_prn_logs.prn_data LIKE '%".$dt."%' AND tbl_prn_logs.pages >= '".$lst."'";
       else $action = " AND tbl_prn_logs.prn_data LIKE '%".$dt."%'";
      break;
      }

  if($_GET['srv_name_ch'] == "on") $srv_comp_id = " AND tbl_prn_logs.srv_comp_id='".$_GET['srv_comp_id']."'";
   else $srv_comp_id = "";

  if($_GET['port_name_ch'] == "on") $port_id = " AND tbl_prn_logs.port_id='".$_GET['port_id']."'";
   else $port_id = "";

  if($_GET['prn_name_ch'] == "on") $prn_id = " AND tbl_prn_logs.prn_id='".$_GET['prn_id']."'";
   else $prn_id = "";

  if($_GET['usr_name_ch'] == "on") $usr_name_id = " AND tbl_prn_logs.usr_name_id='".$_GET['usr_name_id']."'";
   else $usr_name_id = "";

  if($_GET['login_name_ch'] == "on") $usr_login_id = " AND tbl_prn_logs.usr_login_id='".$_GET['usr_login_id']."'";
   else $usr_login_id = "";

  if($_GET['pc_name_ch'] == "on") $pc_id = " AND tbl_prn_logs.pc_id='".$_GET['prn_pc_id']."'";
   else $pc_id = "";

  $fl_name = "export-print-logs";
  $exp_path = "../print-logs/".$fl_name."-".date("YMd-H-i-s",time()).".csv";

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

      $log_act = $TEXT['ex-log7'];
      $exp_head = $TEXT['search-date'].";".$TEXT['support34'].";".$TEXT['search-name'].";".$TEXT['support45'].";".$TEXT['prn-size2'].";".$TEXT['prn-pages']."\r\n";

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

     $query = "SELECT
    tbl_prn_logs.*,
    UNIX_TIMESTAMP(tbl_prn_logs.prn_data) AS data,
    tbl_prn_pc.pc_name,
    tbl_prn_usr_login.login_name,
    tbl_prn_usr_name.usr_name,
    tbl_prn_name.prn_name,
    tbl_prn_port.port_name,
    tbl_prn_srv_comp.srv_name
    FROM tbl_prn_logs
    JOIN tbl_prn_pc ON tbl_prn_logs.pc_id=tbl_prn_pc.prn_pc_id
    JOIN tbl_prn_usr_login ON tbl_prn_logs.usr_login_id=tbl_prn_usr_login.usr_login_id
    JOIN tbl_prn_usr_name ON tbl_prn_logs.usr_name_id=tbl_prn_usr_name.usr_name_id
    JOIN tbl_prn_name ON tbl_prn_logs.prn_id=tbl_prn_name.prn_id
    JOIN tbl_prn_port ON tbl_prn_logs.port_id=tbl_prn_port.port_id
    JOIN tbl_prn_srv_comp ON tbl_prn_logs.srv_comp_id=tbl_prn_srv_comp.srv_comp_id
    WHERE tbl_prn_logs.prn_log_id != 0 ".$srv_comp_id." ".$port_id." ".$prn_id." ".$usr_name_id." ".$usr_login_id." ".$pc_id." ".$action."
    ORDER BY tbl_prn_logs.prn_data DESC";
     $result = mysql_query($query);
     if(!$result) puterror2(mysql_error());
       if(mysql_num_rows($result)>0);
         {
          while($sost4 = mysql_fetch_array($result,MYSQL_ASSOC))
               {

           $date_day = date("d",$sost4['data']);
           $date_month = date("m",$sost4['data']);
           $date_year = date("Y",$sost4['data']);
           $date_time = date("H:i:s",$sost4['data']);
           $year = $TEXT['year'];
           if($sost4['data'] == "" || $sost4['data'] == "0") $data_r = "---";
            else $data_r = (date("$date_day $month[$date_month] $date_year $year $date_time"));

           $result_tb = $sost4['size'];
           if($result_tb > 1024)
              {
              $result_tb = (float)($result_tb/1024);
              if($result_tb > 1024)
                {
                 $result_tb = (float)($result_tb/1024);
                 if($result_tb > 1024)
                 {
                     $result_tb = (float)($result_tb/1024);
                     $result_tb = round($result_tb, 2);
                     $result_tb = $result_tb.$TEXT['size-gb'];
                 } else
                    {
                       $result_tb = (float)($result_tb);
                       $result_tb = round($result_tb, 2);
                       $result_tb = $result_tb.$TEXT['size-mb'];
                    }
                }
                 else
                  {
                  $result_tb = round($result_tb, 2);
                  $result_tb = $result_tb.$TEXT['size-kb'];
                  }
              }
              else $result_tb = $result_tb.$TEXT['size-b'];

        	$exp_string = $data_r.";".$sost4['login_name'].";".$sost4['usr_name'].";".$sost4['document'].";".$result_tb.";".$sost4['pages']."\r\n";

  		fwrite($file, $exp_string, strlen($exp_string));
 		}
         }
  	fclose($file);
 
  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = "Export print logs";
  $what = "Export print logs";
  $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=prn_transfer.php?trans=".$exp_path."' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
?>
<input type="hidden" name="srv_comp_id" value='<?php echo $_GET['srv_comp_id']; ?>'>
<input type="hidden" name="srv_name_ch" value='<?php echo $_GET['srv_name_ch']; ?>'>
<input type="hidden" name="port_id" value='<?php echo $_GET['port_id']; ?>'>
<input type="hidden" name="port_name_ch" value='<?php echo $_GET['port_name_ch']; ?>'>
<input type="hidden" name="prn_id" value='<?php echo $_GET['prn_id']; ?>'>
<input type="hidden" name="prn_name_ch" value='<?php echo $_GET['prn_name_ch']; ?>'>
<input type="hidden" name="usr_name_id" value='<?php echo $_GET['usr_name_id']; ?>'>
<input type="hidden" name="usr_name_ch" value='<?php echo $_GET['usr_name_ch']; ?>'>
<input type="hidden" name="usr_login_id" value='<?php echo $_GET['usr_login_id']; ?>'>
<input type="hidden" name="login_name_ch" value='<?php echo $_GET['login_name_ch']; ?>'>
<input type="hidden" name="prn_pc_id" value='<?php echo $_GET['prn_pc_id']; ?>'>
<input type="hidden" name="pc_name_ch" value='<?php echo $_GET['pc_name_ch']; ?>'>
<input type="hidden" name="k" value='<?php echo $_GET['k']; ?>'>
<input type="hidden" name="typepar" value='<?php echo $_GET['typepar']; ?>'>
<input type="hidden" name="search" value='<?php echo $_GET['search']; ?>'>
<input type=submit value='<?php echo $TEXT['menu-btn10']; ?>'>
</form>
</body>
</html>