<?php
  ///////////////////////////////////////////////////
  //2007 (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['global-res'];

  require_once "config.php";
  require_once "utils/top_res.php";

  if(strlen($_POST['usr_id'])>11) puterror($TEXT['error-usr2']);
  if(strlen($_POST['tip_id'])>11) puterror($TEXT['error-tip1']);
  if(strlen($_GET['k'])>8) puterror($TEXT['error-paging1']);
  if(empty($_GET['k'])) $_GET['k']=0;
  $stop_r = $_GET['k'];
  if($stop_r == 0) $lim_ogr = "15";
   else $lim_ogr = $stop_r.",15";

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

  if(!empty($_GET['org_id'])) 
  {
  $org_resource = " AND tbl_resource.org_id = ".$_GET['org_id']." ";
  $old_org_id = $_GET['org_id'];
  }
   else {
         $query_org_no = "SELECT `org_id` FROM `tbl_organizaciya` WHERE `organizaciya` = '-' ";
           $org_no = mysql_query($query_org_no);
            if(!$org_no) puterror($TEXT['error-tbl7']);
              $result_org_no = mysql_fetch_array($org_no,MYSQL_ASSOC);
              $org_resource = " AND tbl_resource.org_id = ".$result_org_no['org_id']." ";
              $old_org_id = $result_org_no['org_id'];
        }


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

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

  if($_POST['typepar'] !== "")
   {
    switch($_POST['typepar'])
      {
      case 1: //УКК
      $action = " AND LOWER(tbl_resource.ukk) LIKE LOWER('%".$_POST['search']."%')";
      break;
     case 2: //сквозной - имя - описание - путь
      $action = " AND (LOWER(tbl_resource.res_name) LIKE LOWER('%".$_POST['search']."%') OR LOWER(tbl_resource.description) LIKE LOWER('%".$_POST['search']."%') OR LOWER(tbl_resource.resource) LIKE LOWER('%".$_POST['search']."%'))";
      break;
      }
  }
  if($_POST['usr_ch'] == "on") $usr_id = " AND tbl_resource.user_id='".$_POST['usr_id']."'";
   else $usr_id = "";

  if($_POST['tip_ch'] == "on") $tip_id = " AND tbl_resource.tip_id='".$_POST['tip_id']."'";
   else $tip_id = "";

?>
<!--<table class="table" border="0" cellpadding="5" cellspacing="0" width="87%">-->
<?php
  $query1 = "SELECT 
  tbl_resource.*,
  tbl_users.familiya,
  tbl_users.name,
  tbl_users.otchestvo,
  tbl_users.mail,
  tbl_users.dom_id,
  tbl_domain.domain,
  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_tip_resurs ON tbl_resource.tip_id=tbl_tip_resurs.tip_id 
  WHERE tbl_resource.visible = 'show' ".$org_resource." ".$tip_id." ".$usr_id." ".$action." ORDER BY position LIMIT $lim_ogr ";
  $result = mysql_query($query1);
    if(!$result) puterror(mysql_error());
      {
       if(mysql_num_rows($result)>0);
        {
//           echo "<tr class='res'>
//                     <td></td>
//                     <td>".$TEXT['res-field5']."</td>
//                     <td>".$TEXT['res-title1']."</td>
//                     <td>".$TEXT['res-title2']."</td>
//                     <td>".$TEXT['res-title3']."</td>
//                 </tr>";
           while($razd = mysql_fetch_array($result))
           {
            $user_name = "<a href=# onClick=window.open('user.php?user_id=".$razd['user_id']."','popup','top=".$TEXT['user-top'].",left=".$TEXT['user-left'].",width=".$TEXT['user-width'].",height=".$TEXT['user-height'].",status=no,scrollbars=no,location=no,menubar=no,toolbar=no'); title = '".$TEXT['global-usr']."'>".$razd['familiya']." ".$razd['name']." ".$razd['otchestvo']."</a>";
            $dom = $razd['domain'];
            $e_mail = $razd['mail']."@".$dom;
            $sm = preg_replace("/\.\.+./","",$razd['pic_tip']);
             if(!empty($razd['pic_tip']) && file_exists($sm)) $ima = "<img alt='".$razd['tip_name']."' src='".$sm."' />";
               else $ima = "<img alt='".$razd['tip_name']."' src='img/nophoto.png' />";

               if($razd['prefix'] == "")
	       {
	       if($razd['res_name'] !== "") $nm = $razd['res_name'];
                 else $nm = $razd['resource'];
               $resource_path = "<a href=//".$razd['resource']." target=_blank title='".$razd['description']."''>".$nm."</a>";
	       }
               if($razd['prefix'] == "https")
	       {
	       if($razd['res_name'] !== "") $nm = $razd['res_name'];
                 else $nm = $razd['resource'];
               $resource_path = "<a href=https://".$razd['resource']." target=_blank title='".$razd['description']."''>".$nm."</a>";
	       }
	       if($razd['prefix'] == "http")
	       {
	       if($razd['res_name'] !== "") $nm = $razd['res_name'];
                 else $nm = $razd['resource'];
               $resource_path = "<a href=http://".$razd['resource']." target=_blank title='".$razd['description']."''>".$nm."</a>";
	       }
	       if($razd['prefix'] == "nfs")
	       {
	       if($razd['res_name'] !== "") $nm = $razd['res_name'];
                 else $nm = $razd['resource'];
               $resource_path = "<a href=nfs://".$razd['resource']." target=_blank title='".$razd['description']."''>".$nm."</a>";
	       }
	       if($razd['prefix'] == "smb")
	       {
	       if($razd['res_name'] !== "") $nm = $razd['res_name'];
                 else $nm = $razd['resource'];
               $resource_path = "<a href=smb://".$razd['resource']." target=_blank title='".$razd['description']."''>".$nm."</a>";
	       }
	       if($razd['prefix'] == "ftp")
	       {
	       if($razd['res_name'] !== "") $nm = $razd['res_name'];
                 else $nm = $razd['resource'];
               $resource_path = "<a href=ftp://".$razd['resource']." target=_blank title='".$razd['description']."''>".$nm."</a>";
	       }
	       if($razd['prefix'] == "mailto")
	       {
                if($razd['res_name'] !== "") $nm = $razd['res_name'];
                 else $nm = $razd['resource'];
               $resource_path = "<a href=mailto:".$razd['resource']." title='".$razd['description']."'>".$nm."</a>";
	       }
                echo "<tr align='center'>
                          <td class='resources' width='5%'>".$ima."</td>
                          <td class='resources'>".$razd['ukk']." </td>
                          <td class='resources'>".$resource_path."</td>
                          <td class='resources'>$user_name</td>
                          <td class='resources'><a href=mailto:$e_mail>$e_mail</a></td>
                     </tr>";
           }
        }
      }
?>
</table>
     <?php
       $query_count = mysql_query("SELECT COUNT(res_id) FROM tbl_resource WHERE `visible` = 'show' ".$org_resource." ".$tip_id." ".$usr_id." ".$action."");
       $coun_rows = mysql_result($query_count, 0);
               $pag = $coun_rows/15;
               if($pag<=1) $pag = ceil($pag);
                else
                {
                 ?>
                  <hr align="center" width="90%"/>
                  <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*15-15;
                      $kvis=$k+15;
                      $nvis=$k;
                      echo $pg = " <a class='zagolovok' href='resources.php?k=".$k."&amp;org_id=".$old_org_id."' title='".$TEXT['zhurn-details2'].$nvis.$TEXT['zhurn-details1'].$kvis."'>".$i."</a>";
                    }
                    else
                    {
                    $k = $i*15-15;
                    $kvis=$k+15;
                    $nvis=$k;
                    echo $pg = " <a href='resources.php?k=".$k."&amp;org_id=".$old_org_id."' 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*15-15;
                     if($k == $_GET['k']) echo "<option selected value=resources.php?k=".$k."&amp;org_id=".$old_org_id.">".$j."</option>";
                       else echo "<option value=resources.php?k=".$k."&amp;org_id=".$old_org_id.">".$j."</option>";
                    }
                    ?>
                    </select>
                    </td>
                   </tr>
                  </table>
                 <?php
                }
               ?>
</div>
<?php
  require_once "utils/bottom.php";
?>