<?php
  ///////////////////////////////////////////////////
  //2010 (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['pas-title'];
  $helppage = $TEXT['pas-help'];

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

  if($result2['user'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['master'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['manager'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['otvetstvenniy'] == 1) puterror2($TEXT['error-loginform4']);
  $Pos = $TEXT['tbl-position'];

  function ShowTree($ParentID,$Level,$Pos){
    $Level++;
    echo("<ul class='Container'>");
 
    $query_tree="SELECT * FROM tbl_pass_strukt WHERE parent=".$ParentID." ORDER BY position, strukt_name";
     $result4=mysql_query($query_tree);
      $num_rows = mysql_num_rows($result4);
       $kurent = 1;
       if($num_rows > 0)
       {
        while($row = mysql_fetch_array($result4,MYSQL_ASSOC))
        {
        if($row['visible']=='show') $otd_vis="<a href=hide.php?pas_str_id=".$row['pas_str_id']." title='".$TEXT['tbl-hide']."'><img alt='".$TEXT['tbl-show']."' src='../img/show.png' height='16' width='16' align='top' /></a>";
         else $otd_vis="<a href=show.php?pas_str_id=".$row['pas_str_id']." title='".$TEXT['tbl-show']."'><img alt='".$TEXT['tbl-hide']."' src='../img/hide.png' height='16' width='16' align='top' /></a>";

        $edit = "<a href=editform.php?pas_str_id=".$row['pas_str_id']." title='".$TEXT['kat-edit-tit']."'><img alt='".$TEXT['tbl-edit']."' src='../img/edit.png' height='16' width='16' align='top' /></a>";
        $delete = "<a href=deleteform.php?pas_str_id=".$row['pas_str_id']." 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=up-down.php?pas_str_id=".$row['pas_str_id']."&amp;position=".$row['position']."&amp;parent=".$row['parent']."&amp;moving=up title='".$TEXT['global-up-down-tit']."'><img alt='up' src='../img/up.png' height='16' width='16' align='top' /></a>";
        $move_down = "<a href=up-down.php?pas_str_id=".$row['pas_str_id']."&amp;position=".$row['position']."&amp;parent=".$row['parent']."&amp;moving=down title='".$TEXT['global-up-down-tit']."'><img alt='down' src='../img/down.png' height='16' width='16' align='top' /></a>";
        if($row['pic_tr'] !=="") $img = "&nbsp;<img alt='pic' src='../".$row['pic_tr']."' height='16' width='16' align='top' />&nbsp;";
         else $img = "";
  
        $ID1 = $row['pas_str_id'];

        //echo ("kurent=".$kurent."num_rows=".$num_rows);
         $query_check=mysql_query("SELECT COUNT(pas_str_id) FROM tbl_pass_strukt WHERE `parent`=".$ID1."");
          $result_check=mysql_result($query_check, 0);
            if($result_check > 0)
            {
             if($kurent == $num_rows)
             {
             //echo("<li class='Node ExpandClosed IsLast'>");
             echo("<li class='Node ExpandOpen IsLast'>");
             echo("<div class='Expand'></div>");
             echo("<div class='Content'>".$img."<a align='left' href=addform.php?pas_str_id=".$ID1." title='".$Pos." &rArr; ".$row['position']."'>".$row['strukt_name']."</a>".$otd_vis.$move_up.$move_down.$edit.$delete."</div>");

             }
              else
              {
              //echo("<li class='Node ExpandClosed'>");
              echo("<li class='Node ExpandOpen'>");
              echo("<div class='Expand'></div>");
              echo("<div class='Content'>".$img."<a align='left' href=addform.php?pas_str_id=".$ID1." title='".$Pos." &rArr; ".$row['position']."'>".$row['strukt_name']."</a>".$otd_vis.$move_up.$move_down.$edit.$delete."</div>");
              }
            ShowTree($ID1,$Level,$Pos);
            }
            else
            {
              if($kurent == $num_rows)
             {
             echo("<li class='Node ExpandLeaf IsLast'>");
             echo("<div class='Expand'></div>");
             echo("<div class='Content'>".$img."<a align='left' href=addform.php?pas_str_id=".$ID1." title='".$Pos." &rArr; ".$row['position']."'>".$row['strukt_name']."</a>".$otd_vis.$move_up.$move_down.$edit.$delete."</div>");
             }
              else
              {
              echo("<li class='Node ExpandLeaf'>");
              echo("<div class='Expand'></div>");
              echo("<div class='Content'>".$img."<a align='left' href=addform.php?pas_str_id=".$ID1." title='".$Pos." &rArr; ".$row['position']."'>".$row['strukt_name']."</a>".$otd_vis.$move_up.$move_down.$edit.$delete."</div>");
              }
            }
            echo("</li>");
            $kurent++;
       }
       $Level--;
     }
     echo("</ul>");
   }

?>
<table class="table" border="1" cellpadding="0" cellspacing="0" width="40%" >
  <tr><td>
<?php
echo("<div class='texttableusr'><img alt='".$TEXT['kat-add-new']."' src= '../img/insert.png' height='15' align='top' /><a class='boldit' href='addform.php' title='".$TEXT['kat-add-new-tit']."'>".$TEXT['kat-add-new']."</a></div>");
     $query = "SELECT * FROM `tbl_pass_strukt` WHERE `parent` = 0 ORDER BY position, strukt_name";
     $result = mysql_query($query);
     if(!$result) puterror2($TEXT['error-tbl51']);
     $num_row = mysql_num_rows($result);
      $kurent_row = 1;
         if($num_row > 0){
           while($result_kat = mysql_fetch_array($result,MYSQL_ASSOC))
           {
             if($result_kat['visible']=='show') $otd_vis1="<a href=hide.php?pas_str_id=".$result_kat['pas_str_id']." title='".$TEXT['tbl-hide']."'><img alt='".$TEXT['tbl-show']."' src='../img/show.png' height='16' width='16' align='top' /></a>";
               else $otd_vis1="<a href=show.php?pas_str_id=".$result_kat['pas_str_id']." title='".$TEXT['tbl-show']."'><img alt='".$TEXT['tbl-hide']."' src='../img/hide.png' height='16' width='16' align='top' /></a>";

             $edit1 = "<a href=editform.php?pas_str_id=".$result_kat['pas_str_id']." title='".$TEXT['kat-edit-tit']."'><img alt='".$TEXT['tbl-edit']."' src='../img/edit.png' height='16' width='16' align='top' /></a>";
             $delete1 = "<a href=deleteform.php?pas_str_id=".$result_kat['pas_str_id']." title='".$TEXT['kat-del-tit']."'><img alt='".$TEXT['tbl-delete']."' src='../img/drop.png' height='16' width='16' align='top' /></a>";
             $move_up1 = "<a href=up-down.php?pas_str_id=".$result_kat['pas_str_id']."&amp;position=".$result_kat['position']."&amp;parent=".$result_kat['parent']."&amp;moving=up title='".$TEXT['global-up-down-tit']."'><img alt='up' src='../img/up.png' height='16' width='16' align='top' /></a>";
             $move_down1 = "<a href=up-down.php?pas_str_id=".$result_kat['pas_str_id']."&amp;position=".$result_kat['position']."&amp;parent=".$result_kat['parent']."&amp;moving=down title='".$TEXT['global-up-down-tit']."'><img alt='down' src='../img/down.png' height='16' width='16' align='top' /></a>";
             $ID = $result_kat['pas_str_id'];
             if($result_kat['pic_tr'] !=="") $img = "&nbsp;<img alt='pic' src='../".$result_kat['pic_tr']."' height='16' width='16' align='top' />&nbsp;";
               else $img = "";
             
             echo("<div class='texttableusr' onclick='tree_toggle(arguments[0])'>");
             echo("<ul class='Container'>");
             //echo ("kurent=".$kurent_row."num_rows=".$num_row);
             $query_chk=mysql_query("SELECT COUNT(pas_str_id) FROM tbl_pass_strukt WHERE `parent`= ".$ID."");
             $result_chk=mysql_result($query_chk, 0);
            if($result_chk > 0)
            {
             if($kurent_row == $num_row)
             {
             //echo("<li class='Node ExpandClosed IsLast'>");
             echo("<li class='Node ExpandOpen IsLast'>");
             echo("<div class='Expand'></div>");
             echo("<div class='Content'>".$img."<a align='left' href=addform.php?pas_str_id=".$ID." title='".$TEXT['tbl-position']." &rArr; ".$result_kat['position']."'>".$result_kat['strukt_name']."</a>".$otd_vis1.$move_up1.$move_down1.$edit1.$delete1."</div>");

             }
              else
              {
              //echo("<li class='Node ExpandClosed'>");
              echo("<li class='Node ExpandOpen'>");
              echo("<div class='Expand'></div>");
              echo("<div class='Content'>".$img."<a align='left' href=addform.php?pas_str_id=".$ID." title='".$TEXT['tbl-position']." &rArr; ".$result_kat['position']."'>".$result_kat['strukt_name']."</a>".$otd_vis1.$move_up1.$move_down1.$edit1.$delete1."</div>");
              }

             ShowTree($ID,$Level,$Pos);
            }
             else
             {
              if($kurent_row == $num_row)
              {
              echo("<li class='Node ExpandLeaf IsLast'>");
              echo("<div class='Expand'></div>");
              echo("<div class='Content'>".$img."<a align='left' href=addform.php?pas_str_id=".$ID." title='".$TEXT['tbl-position']." &rArr; ".$result_kat['position']."'>".$result_kat['strukt_name']."</a>".$otd_vis1.$move_up1.$move_down1.$edit1.$delete1."</div>");
              }
              else
               {
               echo("<li class='Node ExpandLeaf'>");
               echo("<div class='Expand'></div>");
               echo("<div class='Content'>".$img."<a align='left' href=addform.php?pas_str_id=".$ID." title='".$TEXT['tbl-position']." &rArr; ".$result_kat['position']."'>".$result_kat['strukt_name']."</a>".$otd_vis1.$move_up1.$move_down1.$edit1.$delete1."</div>");
               }
            }
             $kurent_row++;
             //ShowTree($result_kat['pas_str_id'],0);
             echo("</li>");
             echo("</ul>");
             echo("</div>");
           }
         }
   echo("</td></tr></table>");
  require_once "../util/bottom.php";
?>