<?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";

  $title=$titlepage = $TEXT['res-title4'];
  $helppage = $TEXT['res-help4'];

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

  if($result2['master'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['manager'] == 1) puterror2($TEXT['error-loginform4']);

  if(empty($_POST['ukk'])) $_POST['ukk'] = "";
  if(empty($_POST['org_id'])) $_POST['org_id'] = "";
  if(empty($_POST['user_id'])) $_POST['user_id'] = "";
  if(empty($_POST['visible'])) $_POST['visible'] = "";
  if(empty($_GET['onorg'])) $_GET['onorg'] = "";
  if(empty($_POST['onorg'])) $_POST['onorg'] = "";
  if(empty($_POST['onusr'])) $_POST['onusr'] = "";
  if(empty($_POST['onvis'])) $_POST['onvis'] = "";
  if(strlen($_POST['ukk'])>255) puterror2($TEXT['error-res18']);
  if(strlen($_POST['org_id'])>11) puterror2($TEXT['error-org1']);
  if(strlen($_POST['user_id'])>11) puterror2($TEXT['error-user2']);
  if(strlen($_POST['visible'])>4) puterror2($TEXT['error-res6']);
  $_POST['ukk'] = rep($_POST['ukk']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_POST['user_id'] = rep($_POST['user_id']);
  $_POST['visible'] = rep($_POST['visible']);

  $ukk=$_POST['ukk'];
  $visible=$_POST['visible'];
  $onchb = "";

  if($_GET['onorg'] == "on")
    {
   $_POST['org_id'] = $_GET['org_id'];
   $onchb = "checked";
    }
  else
  {
  if($_POST['onorg'] !== "on") $_POST['org_id'] = "";
    else
    {
    $_POST['org_id'] = $_POST['org_id'];
    $onchb = "checked";
    }
  }
  if($_POST['onusr'] !== "on")
  {
  $_POST['user_id'] = "";
  $onusr = "";
  }
    else
    {
    $_POST['user_id'] = $_POST['user_id'];
    $onusr = "checked";
    }

  if($_POST['onvis'] !== "on")
  {
  $_POST['visible'] = "";
  $onvis = "";
  }
    else
    {
    $_POST['visible'] = $_POST['visible'];
    $onvis = "checked";
    }

  if(!isset($button)) $button = $TEXT['btn_filter'];
  if(!isset($action)) $action = "index.php";

?>
<img alt="<?php echo $TEXT['res-add-new']; ?>" src= "../img/insert.png" height="15" align="top" /><a class="boldit" href="addform.php" title="<?php echo $TEXT['res-add-new-tit']; ?>"><?php echo $TEXT['res-add-new']; ?></a>
<form enctype='multipart/form-data' action=<?php echo $action; ?> method=post>
<table class="table" border="0" cellpadding="0" cellspacing="0" width="90%">
<tr><td>
<fieldset class="filterr">
<legend align="left" title="<?php echo $TEXT['log-tit7']; ?>"><?php echo $TEXT['btn_filter']; ?></legend>
<table class="table" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr class="zag2"><td><?php echo $TEXT['tbl-org']; ?></td><td><?php echo $TEXT['res-field2']; ?></td><td><?php echo $TEXT['tbl-visible']; ?></td><td><?php echo $TEXT['res-field5']; ?></td></tr>
<tr><td><select class="input" name="org_id">
    <?php
        $query4 = "SELECT * FROM `tbl_organizaciya` ORDER BY position";
        $organizaciya_tmp = mysql_query($query4);
          if(!$organizaciya_tmp) puterror2($TEXT['error-tbl7']);
              while($organizaciya = mysql_fetch_array($organizaciya_tmp,MYSQL_ASSOC))
                {
                 if($organizaciya['org_id'] == $_POST['org_id'])
                   {
                   $chk = "<option selected value=".$organizaciya['org_id'].">".$organizaciya['organizaciya']."";
                   }
                 else $chk = "<option value=".$organizaciya['org_id'].">".$organizaciya['organizaciya']."";
                 echo $chk;
                }
    ?>
    </select><input type=checkbox name=onorg <?php echo $onchb; ?>></td>
<td><select class="input" name="user_id">
    <?php
        $query5 = "SELECT DISTINCT 
        tbl_resource.user_id, 
        tbl_users.user_id, 
        tbl_users.familiya, 
        tbl_users.name, 
        tbl_users.otchestvo 
        FROM tbl_resource 
        JOIN tbl_users ON tbl_resource.user_id=tbl_users.user_id 
        ORDER BY tbl_users.familiya ";
        $user_tmp = mysql_query($query5);
          if(!$user_tmp) puterror2($TEXT['error-tbl2']);
              while($user = mysql_fetch_array($user_tmp,MYSQL_ASSOC))
                {
                 if($user['user_id'] == $_POST['user_id'])
                   {
                   $chk2 = "<option selected value=".$user['user_id'].">".$user['familiya']." ".$user['name']." ".$user['otchestvo'];
                   }
                 else $chk2 = "<option value=".$user['user_id'].">".$user['familiya']." ".$user['name']." ".$user['otchestvo'];
                 echo $chk2;
                }
    ?>
    </select><input type=checkbox name=onusr <?php echo $onusr; ?>></td>
<td><select class="input" name="visible">
    <?php
        $query6 = "SELECT DISTINCT visible FROM tbl_resource ";
        $vis_tmp = mysql_query($query6);
          if(!$vis_tmp) puterror2($TEXT['error-tbl2']);
              while($visible = mysql_fetch_array($vis_tmp,MYSQL_ASSOC))
                {
                 if($visible['visible']=='show') $res_vis_tmp=$TEXT['res-visible'];
                   else $res_vis_tmp=$TEXT['res-invisible'];
                 if($visible['visible'] == $_POST['visible'])
                   {
                   $chk3 = "<option selected value=".$visible['visible'].">".$res_vis_tmp;
                   }
                 else $chk3 = "<option value=".$visible['visible'].">".$res_vis_tmp;
                 echo $chk3;
                }
    ?>
    </select><input type=checkbox name=onvis <?php echo $onvis; ?>></td>
<td><input size="20" maxlength="255" type=text name=ukk title="<?php echo $TEXT['exusr-example2']; ?>" value="<?php echo $ukk; ?>"></td>
    <td><input class="button" type=submit value="<?php echo $button; ?>" /></td>
</tr>
</table>
</fieldset>
</td></tr>
</table>
</form>
 <br />
 <span class="tabledata"><?php echo $TEXT['res-text1']; ?></span>
<table class="table" border="1" cellpadding="5" cellspacing="0" width="100%">
<?php
   if(!empty($_POST['org_id'])) $organizaciya = "AND tbl_resource.org_id='".$_POST['org_id']."' ";
    else $organizaciya = "";

   if(!empty($_POST['user_id'])) $user = "AND tbl_resource.user_id='".$_POST['user_id']."' ";
    else $user = "";

   if(!empty($_POST['visible'])) $visible = "AND tbl_resource.visible='".$_POST['visible']."' ";
    else $visible = "";

   if(!empty($_POST['ukk'])) $ukk = "AND LOWER(tbl_resource.ukk) LIKE LOWER('%".$_POST['ukk']."%') ";
    else $ukk = "";

     $query = "SELECT 
     tbl_resource.*, 
     tbl_users.user_id, 
     tbl_users.familiya, 
     tbl_users.name, 
     tbl_users.otchestvo, 
     tbl_users.dom_id, 
     tbl_users.mail, 
     tbl_domain.dom_id, 
     tbl_domain.domain, 
     tbl_organizaciya.org_id, 
     tbl_organizaciya.organizaciya,
     tbl_tip_resurs.pic_tip,
     tbl_tip_resurs.prefix,
     tbl_tip_resurs.tip_name
     FROM tbl_resource 
     JOIN tbl_users ON tbl_resource.user_id=tbl_users.user_id 
     JOIN tbl_domain ON tbl_users.dom_id=tbl_domain.dom_id 
     JOIN tbl_organizaciya ON tbl_resource.org_id=tbl_organizaciya.org_id
     JOIN tbl_tip_resurs ON tbl_resource.tip_id=tbl_tip_resurs.tip_id
     WHERE tbl_resource.res_id !='' ".$organizaciya." ".$ukk." ".$user." ".$visible." 
     ORDER BY tbl_organizaciya.organizaciya, tbl_resource.position";
     $result = mysql_query($query);
     if(!$result) puterror2($TEXT['error-tbl16']);
       if(mysql_num_rows($result)>0);
         {
           ?>
           <tr class="tableheadercat">
             <td><?php echo $TEXT['res-field3']; ?></td>
             <td><?php echo $TEXT['tbl-org']; ?></td>
             <td><?php echo $TEXT['res-field2']; ?></td>
             <td><?php echo $TEXT['res-field4']; ?></td>
             <td><?php echo $TEXT['tbl-position']; ?></td>
             <td><?php echo $TEXT['tbl-visible']; ?></td>
             <td colspan=3><?php echo $TEXT['tbl-action']; ?></td>
           </tr>
           <?php
             while($result_resource = mysql_fetch_array($result,MYSQL_ASSOC))
               {
                $res_name = "<a href# onClick=window.open('resource.php?res_id=".$result_resource['res_id']."','popup','top=".$TEXT['resour-top'].",left=".$TEXT['resour-left'].",width=".$TEXT['resour-width'].",height=".$TEXT['resour-height'].",status=no,scrollbars=no,location=no,menubar=no,toolbar=no') title='".$TEXT['btn_test']."'>".$result_resource['familiya']." ".$result_resource['name']." ".$result_resource['otchestvo']."</a>";
                $res_dom = $result_resource['domain'];
                $res_org = $result_resource['organizaciya'];
                $email = "<a href=mailto:".$result_resource['mail']."@".$res_dom.">".$result_resource['mail']."@".$res_dom."</a>";

              if($result_resource['visible']=='show') $res_vis="<a href=hide.php?res_id=".$result_resource['res_id']." title='".$TEXT['tbl-hide-tit8']."'><img alt='".$TEXT['tbl-show']."' src='../img/show.png' height='16' width='16' align='top' /></a>";
                else $res_vis="<a href=show.php?res_id=".$result_resource['res_id']." title='".$TEXT['tbl-show-tit8']."'><img alt='".$TEXT['tbl-hide']."' src='../img/hide.png' height='16' width='16' align='top' /></a>";

               if($result_resource['prefix'] == "")
	       {
	       if($result_resource['res_name'] !== "") $nm = $result_resource['res_name'];
                 else $nm = $result_resource['resource'];
               $resource_path = "<a href=//".$result_resource['resource']." target=_blank title='".$result_resource['description']."''>".$nm."</a>";
	       }
               if($result_resource['prefix'] == "https")
	       {
	       if($result_resource['res_name'] !== "") $nm = $result_resource['res_name'];
                 else $nm = $result_resource['resource'];
               $resource_path = "<a href=https://".$result_resource['resource']." target=_blank title='".$result_resource['description']."''>".$nm."</a>";
	       }
	       if($result_resource['prefix'] == "http")
	       {
	       if($result_resource['res_name'] !== "") $nm = $result_resource['res_name'];
                 else $nm = $result_resource['resource'];
               $resource_path = "<a href=http://".$result_resource['resource']." target=_blank title='".$result_resource['description']."''>".$nm."</a>";
	       }
	       if($result_resource['prefix'] == "nfs")
	       {
	       if($result_resource['res_name'] !== "") $nm = $result_resource['res_name'];
                 else $nm = $result_resource['resource'];
               $resource_path = "<a href=nfs://".$result_resource['resource']." target=_blank title='".$result_resource['description']."''>".$nm."</a>";
	       }
	       if($result_resource['prefix'] == "smb")
	       {
	       if($result_resource['res_name'] !== "") $nm = $result_resource['res_name'];
                 else $nm = $result_resource['resource'];
               $resource_path = "<a href=smb://".$result_resource['resource']." target=_blank title='".$result_resource['description']."''>".$nm."</a>";
	       }
	       if($result_resource['prefix'] == "ftp")
	       {
	       if($result_resource['res_name'] !== "") $nm = $result_resource['res_name'];
                 else $nm = $result_resource['resource'];
               $resource_path = "<a href=ftp://".$result_resource['resource']." target=_blank title='".$result_resource['description']."''>".$nm."</a>";
	       }
	       if($result_resource['prefix'] == "mailto")
	       {
                if($result_resource['res_name'] !== "") $nm = $result_resource['res_name'];
                 else $nm = $result_resource['resource'];
               $resource_path = "<a href=mailto:".$result_resource['resource']." title='".$result_resource['description']."'>".$nm."</a>";
	       }
              $edit = "<a href=editform.php?res_id=".$result_resource['res_id']." title='".$TEXT['tbl-edit-tit-res']."'>".$TEXT['tbl-edit']."</a>";
              $delete = "<a href=deleteform.php?res_id=".$result_resource['res_id']." title='".$TEXT['tbl-delete-tit-res']."'>".$TEXT['tbl-delete']."</a>";
              $move_up = "<a href=up-down.php?res_id=".$result_resource['res_id']."&amp;org_id=".$result_resource['org_id']."&amp;position=".$result_resource['position']."&amp;moving=up title='".$TEXT['global-up-down-tit']."'><img alt='картинка' src='../img/up.png' height='16' width='16' align='top' /></a>";
              $move_down = "<a href=up-down.php?res_id=".$result_resource['res_id']."&amp;org_id=".$result_resource['org_id']."&amp;position=".$result_resource['position']."&amp;moving=down title='".$TEXT['global-up-down-tit']."'><img alt='картинка' src='../img/down.png' height='16' width='16' align='top' /></a>";
           ?>
           <tr class="tabledata" onmouseover="this.style.backgroundColor='#cccccc';" onmouseout="this.style.backgroundColor='#ffffff';">
              <td><?php echo $resource_path; ?></td>
              <td><?php echo $res_org; ?></td>
              <td><?php echo $res_name; ?></td>
              <td><?php echo $email; ?></td>
              <td><?php echo $result_resource['position']; ?></td>
              <td><?php echo $res_vis; ?></td>
              <td><?php echo $move_up; ?> <?php echo $move_down; ?></td>
              <td><?php echo $edit; ?></td>
              <td><?php echo $delete; ?></td>
            </tr>
            <?php
               }
         }
?>
</table>
<?php
  require_once "../util/bottom.php";
?>