<?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['zhurn-izm'];
  require_once "../config.php";
  require_once "top.php";
  ?>
  <title><?php echo $title; ?></title>
  <link rel="StyleSheet" type="text/css" href="../utils/grid.css" />
  </head>
  <body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" bottommargin="0" topmargin="0" >
  <?php
  if($result2['administrator'] == 0) puterror2($TEXT['error-supp11']);

  if(strlen($_GET['zh_str_id'])>11) puterror2($TEXT['error-zhurn2']);
  if(!empty($_GET['zh_str_id']))
   {
    $tmp_zh_str_id = "&amp;zh_str_id=".$_GET['zh_str_id']."";
    $old_zh_str_id = $_GET['zh_str_id'];
    if($_POST['koren'] == "on")
    {
      $koren = "";
      $koren_check = "checked";
      $ord = "tbl_zhurnal.position DESC, tbl_zhurnal.data DESC";
    }
     else
     {
       $koren = " AND tbl_zhurnal.zh_str_id='".$_GET['zh_str_id']."'";
       $ord = "tbl_zhurnal.position DESC, tbl_zhurnal.data DESC";
     }
   }
   elseif(!empty($_POST['zh_str_id']))
   {
    $tmp_zh_str_id = "&amp;zh_str_id=".$_GET['zh_str_id']."";
    $old_zh_str_id = $_POST['zh_str_id'];
    if($_POST['koren'] == "on")
    {
     $koren = "";
     $koren_check = "checked";
     $ord = "tbl_zhurnal.position DESC, tbl_zhurnal.data DESC";
    }
     else
     {
       $koren = " AND tbl_zhurnal.zh_str_id='".$_POST['zh_str_id']."'";
       $ord = "tbl_zhurnal.position DESC, tbl_zhurnal.data DESC";
     }
   }
   else
   {
     $koren = "";
     $ord = "tbl_zhurnal.data DESC";
     if($_GET['koren'] == "on" || $_POST['koren'] == "on") $koren_check = "checked";
   }

  if(strlen($_GET['k'])>8) puterror2($TEXT['error-paging1']);
  if(empty($_GET['k'])) $_GET['k']=0;
  $stop_r = $_GET['k'];
  if($stop_r == 0) $lim_ogr = "20";
   else $lim_ogr = $stop_r.",20";

  $ktblvis = $stop_r+20;
  $str = $ktblvis/20;

  if(empty($_POST['search'])) $_POST['search'] = $_GET['search'];
  if(empty($_POST['typepar'])) $_POST['typepar'] = $_GET['typepar'];

  if(!empty($_POST['search'])) {
   $tmp_srch = "&amp;search=".$_POST['search']."";
   $tmp_tppr = "&amp;typepar=".$_POST['typepar']."";
  } else {
     $tmp_srch = "";
     $tmp_tppr = "";
  }

  if(strlen($_POST['search'])>100) puterror2($TEXT['search-err2']);
  if(strlen($_POST['typepar'])>10) puterror2($TEXT['search-err3']);
  $_POST['search'] = rep($_POST['search']);
  $_POST['typepar'] = rep($_POST['typepar']);

    switch($_POST['typepar'])
      {
      case 1: //сквозной
      $action = " AND (LOWER(tbl_zhurnal.zh_tema) LIKE LOWER('%".$_POST['search']."%') OR LOWER(tbl_zhurnal.zh_soobschenie) LIKE LOWER('%".$_POST['search']."%'))";
      break;
      case 2: //тема
      $action = " AND LOWER(tbl_zhurnal.zh_tema) LIKE LOWER('%".$_POST['search']."%')";
      break;
      case 3: //сообщение
      $action = " AND LOWER(tbl_zhurnal.zh_soobschenie) LIKE LOWER('%".$_POST['search']."%')";
      break;
      }

  if($_POST['krit_ch'] == "on") {
      $krit_ch_tmp = "&amp;krit_ch=on";
      $krit_id = " AND tbl_zhurnal.krit_id='".$_POST['krit_id']."'";
      $krit_check = "checked";
      $krt_id = $_POST['krit_id'];
      $krt_str = "&amp;krit_id=".$krt_id."&amp;krit_ch=on";
    }
    elseif($_GET['krit_ch'] == "on") {
      $krit_id = " AND tbl_zhurnal.krit_id='".$_GET['krit_id']."'";
      $krit_check = "checked";
      $krt_id = $_GET['krit_id'];
      $krt_str = "&amp;krit_id=".$krt_id."&amp;krit_ch=on";
  }
   else   {
       $krit_id = "";
       $krt_str = "";
   }

   // Хлебные крошки Breadcrumbs
   if(!empty($_GET['zh_str_id']))
   {
   $resourcebr = "SELECT parent, zh_str_id, strukt_name FROM tbl_zhurn_strukt WHERE zh_str_id=".$_GET['zh_str_id']."";
    $resultbr=mysql_query($resourcebr);
     $result_bread=mysql_fetch_array($resultbr);

   $item_id = $_GET['zh_str_id'];
   $item_par = $result_bread['parent'];
   $item_name = "<a class='news3' href=zhurnal.php?zh_str_id=".$item_id.">".$result_bread['strukt_name']."</a>";

   while($item_par != 0)
    {
    $resourcebr2 = "SELECT parent, zh_str_id, strukt_name FROM tbl_zhurn_strukt WHERE zh_str_id=".$item_par."";
    $resultbr2=mysql_query($resourcebr2);
     $num_br = mysql_num_rows($resultbr2);
     if($num_br > 0)
     {
     $result_bread2=mysql_fetch_array($resultbr2);
     $item_id = $result_bread2['zh_str_id'];
     $item_par = $result_bread2['parent'];
     $item_name = "<a class='news3' href=zhurnal.php?zh_str_id=".$item_id.">".$result_bread2['strukt_name']."</a> &#187; ".$item_name;
     }
    }
   }
   //echo $item_name;
  // конец хлебных крошек

  if($_POST['typepar'] == 1) {
   $type_chk1 = "checked";
   $type_chk2 = "";
   $type_chk3 = "";
  }
  elseif($_POST['typepar'] == 2) {
   $type_chk1 = "";
   $type_chk2 = "checked";
   $type_chk3 = "";
  }
  elseif($_POST['typepar'] == 3) {
   $type_chk1 = "";
   $type_chk2 = "";
   $type_chk3 = "checked";
  }
  else {
   $type_chk1 = "checked";
   $type_chk2 = "";
   $type_chk3 = "";
  }
?>
<form action=zhurnal.php method=post>
<table class="tabledata" border="0" cellpadding="2" cellspacing="0" width="100%">
<tr align="center">
  <td>
    <table width=100% class="topheader" border="0" cellspacing="3">
     <tr align="center" valign="middle">
     <td align="right"><input class="inputsearch" size=30 maxlength=30 type=text name=search title="<?php echo $TEXT['search-btn-title']; ?>" value="<?php echo $_POST['search']; ?>" >&nbsp;<input class="button" type=submit value='<?php echo $TEXT['search-btn']; ?>'></td>
     </tr>
     <tr>
       <td align="right" class="menu">
         <input type=hidden name=zh_str_id value='<?php echo $old_zh_str_id; ?>' />
         <input type=checkbox name=koren <?php echo $koren_check; ?> /><?php echo $TEXT['zhurn-search2']; ?>
         <input type=radio name=typepar value=1 <?php echo $type_chk1; ?> /><?php echo $TEXT['zhurn-search1']; ?>
         <input type=radio name=typepar value=2 <?php echo $type_chk2; ?> /><?php echo $TEXT['izm-tema']; ?>
         <input type=radio name=typepar value=3 <?php echo $type_chk3; ?> /><?php echo $TEXT['izm-soobsch']; ?>
         <input type=checkbox name=krit_ch />
         <select name=krit_id class="button">
    <?php
        $query4 = "SELECT * FROM `tbl_kritichnost` WHERE `krit_id` != '0' ORDER BY position, kritichnost";
        $kritichnost_tmp = mysql_query($query4);
          if(!$kritichnost_tmp) puterror2($TEXT['error-tbl39']);
              while($kritichnost = mysql_fetch_array($kritichnost_tmp,MYSQL_ASSOC))
                {
                 if($kritichnost['krit_id'] == $_POST['krit_id'])
                 {
                  $chk = "<option selected value=".$kritichnost['krit_id'].">".$kritichnost['kritichnost']."";
                 }
                 $chk = "<option value=".$kritichnost['krit_id'].">".$kritichnost['kritichnost']."";
                 echo $chk;
                }
    ?>
    </select>
       </td>
     </tr>
    </table>
<tr>
<tr>
  <td>
    <table border="0" width="86%" cellpadding="0" cellspacing="0" align="left" class="formainbread">
     <tr><td></td><td></td><td></td></tr>
     <tr>
       <td></td>
       <td class="newsdesch">&nbsp;&nbsp;<a class='news3' href=zhurnal.php><?php echo $TEXT['zhurn-path']; ?></a><?php echo $item_name; ?></td>
       <td></td>
     </tr>
     <tr><td></td><td></td><td></td></tr>
    </table>
    <table border="0" width="11%" cellpadding="0" cellspacing="0" align="right" class="formainbtn">
     <tr><td></td><td></td><td></td><td></td></tr>
     <tr>
       <td></td>
       <td>&nbsp;<img class="formainimg" alt="add" src="../img/add.png" /></td>
       <td class="newsdesch">&nbsp;<a href="zhurn_addform.php?zh_str_id=<?php echo $_GET['zh_str_id']; ?>" class="news3" title="<?php echo $TEXT['zhurn-izm-add-title']; ?>"><?php echo $TEXT['btn_add']; ?></a></td>
       <td></td>
     </tr>
     <tr><td></td><td></td><td></td><td></td></tr>
    </table>
<?php
  $query4 = "SELECT 
    tbl_zhurnal.*,
    YEAR(tbl_zhurnal.data) AS zhurnal_year,
    MONTH(tbl_zhurnal.data) AS zhurnal_month,
    DAY(tbl_zhurnal.data) AS zhurnal_day,
    TIME(tbl_zhurnal.data) AS zhurnal_hour,
    tbl_logins.fullname,
    tbl_kritichnost.pic_krit,
    tbl_kritichnost.kritichnost
    FROM tbl_zhurnal
    JOIN tbl_logins ON tbl_zhurnal.login_id=tbl_logins.login_id
    JOIN tbl_kritichnost ON tbl_zhurnal.krit_id=tbl_kritichnost.krit_id
    WHERE tbl_zhurnal.zhurn_id != 0 ".$koren." ".$krit_id." ".$action."
    ORDER BY ".$ord." LIMIT $lim_ogr";
    $tmp4 = mysql_query($query4);
     if(!$tmp4) puterror2(mysql_error());
      $num_rows = mysql_num_rows($tmp4);
        if($num_rows == 0)
          {
           echo "<tr><td align='center' class='zagolovok'>";
           echo "<br />".$TEXT['zhurn-nodata']."";
           echo "</td></tr>";
          }
           else
           {
 ?>
  <tr>
    <td align="center">
      <table border="0" width="95%" cellpadding="0" cellspacing="0" class="formain">
       <tr>
         <td></td>
         <td></td>
         <td></td>
         <td></td>
         <td></td>
       </tr>
       <?php
           while($sost4 = mysql_fetch_array($tmp4,MYSQL_ASSOC))
           {
            if(!empty($sost4['pic_krit']) && $sost4['pic_krit']!="-") $krit_image = "<img alt='".$sost4['kritichnost']."' title='".$sost4['kritichnost']."' src='".$sost4['pic_krit']."' />";
              else $krit_image = $sost4['kritichnost'];

           $date_day = $sost4['zhurnal_day'];
           $date_month = $sost4['zhurnal_month'];
           $date_year = $sost4['zhurnal_year'];
           $date_time = $sost4['zhurnal_hour'];
           if($sost4['zhurnal_year'] == "" || $sost4['zhurnal_year'] == "0000") $data_r = "---";
            else $data_r = $date_day." ".$month[$date_month]." ".$date_year." ".$TEXT['year']." ".$date_time;

          $edit = "<a href=zhurn_editform.php?zhurn_id=".$sost4['zhurn_id']."&amp;zh_str_id=".$sost4['zh_str_id']."&amp;k=".$stop_r." title='".$TEXT['kat-edit-tit']."'><img alt='".$TEXT['tbl-edit']."' src='../img/edit.png' height='16' width='16' align='top' /></a>";
          $delete = "<a href=zhurn_deleteform.php?zhurn_id=".$sost4['zhurn_id']."&amp;zh_str_id=".$sost4['zh_str_id']."&amp;k=".$stop_r." title='".$TEXT['kat-del-tit']."'><img alt='".$TEXT['tbl-delete']."' src='../img/drop.png' height='16' width='16' align='top' /></a>";
          $move_up = "<a href=zhurn_up_down.php?zhurn_id=".$sost4['zhurn_id']."&amp;zh_str_id=".$sost4['zh_str_id']."&amp;k=".$stop_r."&amp;position=".$sost4['position']."&amp;moving=down title='".$TEXT['global-up-down-tit']."'><img alt='up' src='../img/up.png' height='16' width='16' align='top' /></a>";
          $move_down = "<a href=zhurn_up_down.php?zhurn_id=".$sost4['zhurn_id']."&amp;zh_str_id=".$sost4['zh_str_id']."&amp;k=".$stop_r."&amp;position=".$sost4['position']."&amp;moving=up title='".$TEXT['global-up-down-tit']."'><img alt='down' src='../img/down.png' height='16' width='16' align='top' /></a>";
          $print = "<a href=zhurn_printform.php?zhurn_id=".$sost4['zhurn_id']."&amp;zh_str_id=".$sost4['zh_str_id']."&amp;k=".$stop_r." title='".$TEXT['kat-edit-tit']."'><img alt='".$TEXT['print-vers']."' src='../img/printer.png' height='16' width='16' align='top' /></a>";
          $sost4['zh_soobschenie'] = str_replace("  ","&nbsp;&nbsp;",$sost4['zh_soobschenie']);
          $sost4['zh_soobschenie'] = rep_back($sost4['zh_soobschenie']);

            if($_GET['zhurn_id'] == $sost4['zhurn_id'])
            {
             echo "<tr>
              <td></td>
              <td colspan=3 >";
              ?>
             <fieldset class='zayavka' >
              <legend align="left" ><?php echo $krit_image; ?>&nbsp;<?php echo $sost4['zh_tema']; ?></legend>
               <table align='center' border='0' width='100%' cellpadding='0' cellspacing='0'>
                <tr>
                  <td>&nbsp;</td><td class="data"><?php echo $print.$move_up.$move_down.$edit.$delete; ?></td>
                </tr>
                <tr>
                  <td colspan="2" class="support" width="100%"><?php echo nl2br($sost4['zh_soobschenie']); ?></td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td class="autor"><?php echo $TEXT['zhurn-autor'].$sost4['fullname']; ?></td>
                  <td class="data"><?php echo $data_r; ?></td>
                </tr>
              </table>
            </fieldset>
       <?php
              echo "</td><td></td>
             </tr>";
            }
            else
             {
             echo "<tr>
              <td>&nbsp;</td>
              <td width= '16px' class='newsdesch'>".$krit_image."</td>
              <td class='newsdesch'>&nbsp;<a href='zhurnal.php?zhurn_id=".$sost4['zhurn_id'].$tmp_zh_str_id.$koren_tmp.$krt_str.$tmp_srch.$tmp_tppr."&amp;k=".$stop_r."' class='news3' title='".$TEXT['support14']."'>".$sost4['zh_tema']."&nbsp;<span class='data'>&#040;".$data_r."&#041;</span></a></td>
              <td width= '80px' class='newsdesch'>".$move_up.$move_down.$edit.$delete."</td>
              <td></td>
             </tr>";

             }
            }
            ?>
             <tr>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
             </tr>
            </table>
           <?php
         }
            ?>
   </td>
  </tr>
</table>
</form>
<form>
     <?php
       $query_count = mysql_query("SELECT COUNT(tbl_zhurnal.zhurn_id) FROM tbl_zhurnal WHERE tbl_zhurnal.zhurn_id != 0 ".$koren." ".$krit_id." ".$action." ");
       $coun_rows = mysql_result($query_count, 0);
               $pag = $coun_rows/20;
               if($pag<=1) $pag = ceil($pag);
                else
                {
                 ?>
                  <table align="center" border="0" cellpadding="0" cellspacing="0" width="60%" >
                   <tr align="center">
                    <td>
                <?php
                $pag=ceil($pag);
                $strn = $str-5;
                if($strn<1) $strn=1;
                $strk = $str+5;
                for($i=$strn; $i<=$pag; $i++)
                 {
                  if($i<=$strk)
                  {
                    if($i==$str)
                    {
                      $k = $i*20-20;
                      $kvis=$k+20;
                      $nvis=$k;
                      echo $pg = " <a class='zagolovok' href='zhurnal.php?k=".$k.$tmp_zh_str_id.$koren_tmp.$krt_str.$tmp_srch.$tmp_tppr."' title='".$TEXT['zhurn-details2'].$nvis.$TEXT['zhurn-details1'].$kvis."'>".$i."</a>";
                    }
                    else
                    {
                    $k = $i*20-20;
                    $kvis=$k+20;
                    $nvis=$k;
                    echo $pg = " <a href='zhurnal.php?k=".$k.$tmp_zh_str_id.$koren_tmp.$krt_str.$tmp_srch.$tmp_tppr."' title='".$TEXT['zhurn-details2'].$nvis.$TEXT['zhurn-details1'].$kvis."'>[".$i."]</a>";
                    }
                  }
                 }
                 ?>
                    <select name='k' onchange=window.location.href=this.options[this.selectedIndex].value;>
                    <?php
                    for($j=1; $j<=$pag; $j++)
                    {
                     $k = $j*20-20;
                     if($k == $_GET['k']) echo "<option selected value=zhurnal.php?k=".$k.$tmp_zh_str_id.$koren_tmp.$krt_str.$tmp_srch.$tmp_tppr.">".$j."</option>";
                       else echo "<option value=zhurnal.php?k=".$k.$tmp_zh_str_id.$koren_tmp.$krt_str.$tmp_srch.$tmp_tppr.">".$j."</option>";
                    }
                    ?>
                    </select>
                    </td>
                   </tr>
                  </table>
                 <?php
                }
               ?>
</form>
</body>
</html>