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

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

  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" >
  <script src="../utils/tree_toggle.js" type="text/javascript"></script>
  <?php
  if($result2['administrator'] == 0 && $result2['master'] == 0) puterror2($TEXT['error-supp11']);
  if($result2['master'] == 1) $vis_str = " AND tbl_pass_strukt.visible='show' ";
   else $vis_str = "";

  if($result2['master'] == 1) $Master = "1";
   else $Master = "0";

  if($result2['administrator'] == 1) $action_tree = "";
   else $action_tree = " AND tbl_pass_keep.visible = 'show' ";

  function ShowTree($ParentID,$Level, $Master){
    $Level++;
    echo("<ul class='Container'>");

    if($Master == "1") {
     $vis_str2 = " AND tbl_pass_strukt.visible='show' ";
     $action_tree2 = " AND tbl_pass_keep.visible = 'show' ";
     }
      else {
          $vis_str2 = "";
          $action_tree2 = "";
      }

    $query_tree="SELECT tbl_pass_strukt.*, (SELECT COUNT(tbl_pass_keep.pas_id) FROM tbl_pass_keep WHERE tbl_pass_keep.pas_str_id = tbl_pass_strukt.pas_str_id AND tbl_pass_keep.korzina = '0' $action_tree2 ) AS countzapp FROM tbl_pass_strukt WHERE tbl_pass_strukt.parent=".$ParentID." $vis_str2 ORDER BY tbl_pass_strukt.position, tbl_pass_strukt.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))
        {
        $ID1 = $row['pas_str_id'];
        if($row['pic_tr'] !=="") $img = "<img alt='pic' src='".$row['pic_tr']."' height='16' width='16' />&thinsp;";
         else $img = "";
        if($row['visible'] == "hide") $rtdir1 = "support25";
         else  $rtdir1 = "support24";
        $countzapp = "&thinsp;(".$row['countzapp'].")";

         $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("<div class='Expand'></div>");
             echo("<div class='Content'><table border='0' cellpadding='0' cellspacing='0'><tr><td>".$img."</td><td class='".$rtdir1."'><a class='".$rtdir1."' href=pass_keep.php?pas_str_id=".$ID1." target='right'>".$row['strukt_name']."</a><span class='data'>".$countzapp."</span></td></tr></table></div>");

             }
              else
              {
              echo("<li class='Node ExpandClosed'>");
              echo("<div class='Expand'></div>");
              echo("<div class='Content'><table border='0' cellpadding='0' cellspacing='0'><tr><td>".$img."</td><td class='".$rtdir1."'><a class='".$rtdir1."' href=pass_keep.php?pas_str_id=".$ID1." target='right'>".$row['strukt_name']."</a><span class='data'>".$countzapp."</span></td></tr></table></div>");
              }
            ShowTree($ID1, $Level, $Master);
            }
            else
            {
              if($kurent == $num_rows)
             {
             echo("<li class='Node ExpandLeaf IsLast'>");
             echo("<div class='Expand'></div>");
             echo("<div class='Content'><table border='0' cellpadding='0' cellspacing='0'><tr><td>".$img."</td><td class='".$rtdir1."'><a class='".$rtdir1."' href=pass_keep.php?pas_str_id=".$ID1." target='right'>".$row['strukt_name']."</a><span class='data'>".$countzapp."</span></td></tr></table></div>");
             }
              else
              {
              echo("<li class='Node ExpandLeaf'>");
              echo("<div class='Expand'></div>");
              echo("<div class='Content'><table border='0' cellpadding='0' cellspacing='0'><tr><td>".$img."</td><td class='".$rtdir1."'><a class='".$rtdir1."' href=pass_keep.php?pas_str_id=".$ID1." target='right'>".$row['strukt_name']."</a><span class='data'>".$countzapp."</span></td></tr></table></div>");
              }
            }
            echo("</li>");
            $kurent++;
       }
       $Level--;
     }
     echo("</ul>");
   }

     $query = "SELECT tbl_pass_strukt.*, (SELECT COUNT(tbl_pass_keep.pas_id) FROM tbl_pass_keep WHERE tbl_pass_keep.pas_str_id = tbl_pass_strukt.pas_str_id AND tbl_pass_keep.korzina = '0' $action_tree) AS countzap FROM tbl_pass_strukt WHERE tbl_pass_strukt.parent = 0 $vis_str ORDER BY tbl_pass_strukt.position, tbl_pass_strukt.strukt_name";
     $result = mysql_query($query);
     if(!$result) puterror2($TEXT['error-tbl51']);
     $num_row = mysql_num_rows($result);
      $kurent_row = 1;
      $Level = 0;
         if($num_row > 0){
         echo("&nbsp;&nbsp;<span class='support4'>".$TEXT['pass-title3']."</span>");
           while($result_kat = mysql_fetch_array($result,MYSQL_ASSOC))
           {
             $ID = $result_kat['pas_str_id'];
             if($result_kat['pic_tr'] !=="") $img = "<img alt='pic' src='".$result_kat['pic_tr']."' height='16' width='16' />&thinsp;";
               else $img = "";
             if($result_kat['visible'] == "hide") $rtdir = "support25";
               else  $rtdir = "support24";
             $countzap = "&thinsp;(".$result_kat['countzap'].")";

             echo("<div class='texttableusr' onclick='tree_toggle(arguments[0])'>");
             echo("<ul class='Container'>");

             $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("<div class='Expand'></div>");
             echo("<div class='Content'><table border='0' cellpadding='0' cellspacing='0'><tr><td>".$img."</td><td><a class='".$rtdir."' href=pass_keep.php?pas_str_id=".$ID." target='right'>".$result_kat['strukt_name']."</a><span class='data'>".$countzap."</span></td></tr></table></div>");
             }
              else
              {
              echo("<li class='Node ExpandClosed'>");
              echo("<div class='Expand'></div>");
              echo("<div class='Content'><table border='0' cellpadding='0' cellspacing='0'><tr><td>".$img."</td><td><a class='".$rtdir."' href=pass_keep.php?pas_str_id=".$ID." target='right'>".$result_kat['strukt_name']."</a><span class='data'>".$countzap."</span></td></tr></table></div>");
              }

             ShowTree($ID, $Level, $Master);
            }
             else
             {
              if($kurent_row == $num_row)
              {
              echo("<li class='Node ExpandLeaf IsLast'>");
              echo("<div class='Expand'></div>");
              echo("<div class='Content'><table border='0' cellpadding='0' cellspacing='0'><tr><td>".$img."</td><td><a class='".$rtdir."' href=pass_keep.php?pas_str_id=".$ID." target='right'>".$result_kat['strukt_name']."</a><span class='data'>".$countzap."</span></td></tr></table></div>");
              }
              else
               {
               echo("<li class='Node ExpandLeaf'>");
               echo("<div class='Expand'></div>");
               echo("<div class='Content'><table border='0' cellpadding='0' cellspacing='0'><tr><td>".$img."</td><td><a class='".$rtdir."' href=pass_keep.php?pas_str_id=".$ID." target='right'>".$result_kat['strukt_name']."</a><span class='data'>".$countzap."</span></td></tr></table></div>");
               }
            }
             $kurent_row++;
             //ShowTree($result_kat['pas_str_id'],0);
             echo("</li>");
             echo("</ul>");
             echo("</div>");
           }
         }
?>
</body>
</html>