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

  $title=$titlepage=$TEXT['meet-title'];

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

  if(empty($_GET['room_id'])) puterror($TEXT['meet-err-edit2']);
  if(strlen($_GET['room_id'])>11) puterror($TEXT['meet-err-edit3']);
  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;

  $room_page = "&amp;room_id=".$_GET['room_id']."";

  if(!empty($_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);
              $old_org_id = $result_org_no['org_id'];
        }

if(empty($_POST['data'])) {
     $data_srch = "AND tbl_meet_event.meet_data > DATE_ADD(NOW(), INTERVAL -1 DAY)";
     $data_page = "";
     }
  else {
      $data_srch = "AND tbl_meet_event.meet_data = '".$_POST['data']."'";
      $data_page = "&amp;data=".$_POST['data']."";

      $date_par = date_parse($_POST['data']);
      $date_day2 = $date_par['day'];
      $date_month2 = $date_par['month'];
      $date_year2 = $date_par['year'];
      $year2 = $TEXT['year'];
      if($_POST['data'] == "" || $_POST['data'] == "0") $data_r2 = "---";
       else $data_r2 = $date_day2." ".$month[$date_month2]." ".$date_year2." ".$year2;
      }

  $query1 = "SELECT
      tbl_meet_room.*
      FROM tbl_meet_room
      WHERE tbl_meet_room.room_id = '".$_GET['room_id']."' ";
  $result = mysql_query($query1);
    if(!$result) puterror(mysql_error());
      {
       if(mysql_num_rows($result)>0);
        {
           while($razd = mysql_fetch_array($result))
           {
            $sm = preg_replace("/\.\.+./","",$razd['room_photo']);
             if(!empty($razd['room_photo']) && file_exists($sm)) $ima = "<td width='10%'><img alt='".$razd['room_photo']."' width='185' heigh='130' src='".$sm."' /></td>";
               else $ima = "<td></td>";

            if($razd['countevent'] > 0) $meet_evnts = "<a class='news3' href=room_events.php  title='".$TEXT['meet-events-help']."'>".$TEXT['meet-events']."(".$razd['countevent'].")</a>";
              else $meet_evnts = "";

           $razd['room_descr'] = str_replace(":smile:","<img alt='картинка' src='img/smile.png' align=middle border=0>",$razd['room_descr']);
           $razd['room_descr'] = str_replace(":tongue:","<img alt='картинка' src='img/tongue.png' align=middle border=0>",$razd['room_descr']);
           $razd['room_descr'] = str_replace(":wink:","<img alt='картинка' src='img/wink.png' align=middle border=0>",$razd['room_descr']);
           $razd['room_descr'] = str_replace(":wassat:","<img alt='картинка' src='img/wassat.png' align=middle border=0>",$razd['room_descr']);
           $razd['room_descr'] = str_replace(":sad:","<img alt='картинка' src='img/sad.png' align=middle border=0>",$razd['room_descr']);
           $razd['room_descr'] = str_replace(":crying:","<img alt='картинка' src='img/crying.png' align=middle border=0>",$razd['room_descr']);
           $razd['room_descr'] = str_replace(":angry:","<img alt='картинка' src='img/angry.png' align=middle border=0>",$razd['room_descr']);

           echo "<table border='0' width='80%' cellpadding='0' cellspacing='0' align='left' class='formainmeet'>
                        <tr><td></td><td></td></tr>
                        <tr>
                        ".$ima."
                        <td>
                        <table border='0' width='95%' cellpadding='0' cellspacing='0' align='left'>
                            <tr><td>&nbsp;</td><td><span class='meetname'>".$razd['room_name']."</span></td><td></td></tr>
                            <tr><td>&nbsp;</td><td><br /><span class='news3'>".$TEXT['search-descr']."&#58;</span></td><td></td></tr>
                            <tr><td>&nbsp;</td><td>".nl2br($razd['room_descr'])."</td><td></td></tr>
                            <tr><td colspan=3 class='support2'>".$meet_evnts."</td></tr>
                        </table>
                        </td></tr>
                        <tr><td></td><td></td></tr>
                        </table><br /><br />";

     echo "<table border='0' width='80%' cellpadding='0' cellspacing='0' align='left' class='meetrows'>
     <tr align='left'>
     <td width='100%' class='support'><br />".$TEXT['meet-events3']." ".$TEXT['meet-na'].$data_r2."</td>
     </tr>
     <tr align='left'>
     <td width='100%' ><hr width='90%' align='left' />";

      $query_z = "SELECT
          tbl_meet_event.evnt_id,
          tbl_meet_event.room_id,
          tbl_meet_event.meet_name,
          tbl_meet_event.usr_id,
          tbl_meet_event.meet_visible,
          tbl_meet_event.meet_color,
          YEAR(tbl_meet_event.meet_data) AS meet_year,
          MONTH(tbl_meet_event.meet_data) AS meet_month,
          DAY(tbl_meet_event.meet_data) AS meet_day,
          tbl_meet_event.time_start_min,
          tbl_meet_event.time_stop_min,
          tbl_meet_event.time_start,
          tbl_meet_event.time_stop
          FROM tbl_meet_event
          WHERE tbl_meet_event.room_id = '".$razd['room_id']."' ".$data_srch." AND tbl_meet_event.meet_visible = 'yes' 
          ORDER BY tbl_meet_event.meet_data, tbl_meet_event.time_start LIMIT $lim_ogr";
      $zayavka = mysql_query($query_z);
       if(!$zayavka) puterror($TEXT['error-tbl49']);
        if(mysql_num_rows($zayavka) > 0)
        {

        if(!empty($_POST['data'])) echo "<table border='0' width='100%' cellpadding='0' cellspacing='0' align='left' class='meetdesc'>
        <tr class='tableheadercat'>
        <td>".$TEXT['support30']."</td>
        <td>".$TEXT['meet-name']."</td>
        <td>0</td>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
        <td>5</td>
        <td>6</td>
        <td>7</td>
        <td>8</td>
        <td>9</td>
        <td>10</td>
        <td>11</td>
        <td>12</td>
        <td>13</td>
        <td>14</td>
        <td>15</td>
        <td>16</td>
        <td>17</td>
        <td>18</td>
        <td>19</td>
        <td>20</td>
        <td>21</td>
        <td>22</td>
        <td>23</td>
        </tr>";

         while($zay = mysql_fetch_array($zayavka,MYSQL_ASSOC))
         {
          $tema_full = $zay['meet_name'];
          $date_day = $zay['meet_day'];
          $date_month = $zay['meet_month'];
          $date_year = $zay['meet_year'];
          $year = $TEXT['year'];
          if($zay['meet_year'] == "" || $zay['meet_year'] == "0") $data_r = "---";
            else $data_r = $date_day." ".$month[$date_month]." ".$date_year." ".$year." (".$TEXT['meet-with'].$zay['time_start']."&#58;".$zay['time_start_min'].$TEXT['meet-to'].$zay['time_stop']."&#58;".$zay['time_stop_min']." )";

          $tema_meet =  "<a class='support23'  target='_blank' href='meet_detail.php?evnt_id=".$zay['evnt_id']."&amp;room_id=".$zay['room_id']."&amp;org_id=".$old_org_id."' title='".$TEXT['support14']."'> ".$tema_full."</a>";
          $time_start = $zay['time_start'];
          $time_stop = $zay['time_stop'];

          if($zay['meet_color'] == "black") $class_td = " class='meetbcgblack'";
           elseif($zay['meet_color'] == "green") $class_td = " class='meetbcggreen'";
           elseif($zay['meet_color'] == "blue") $class_td = " class='meetbcgblue'";
           elseif($zay['meet_color'] == "grey") $class_td = " class='meetbcggrey'";
           elseif($zay['meet_color'] == "brown") $class_td = " class='meetbcgbrown'";
           elseif($zay['meet_color'] == "red") $class_td = " class='meetbcgred'";
           elseif($zay['meet_color'] == "yellow") $class_td = " class='meetbcgyellow'";
           else $class_td = " class='meetbcgblack'";

         if(empty($_POST['data']))
          {
            echo "<a class='support_search'  target='_blank' href='meet_detail.php?evnt_id=".$zay['evnt_id']."&amp;room_id=".$zay['room_id']."&amp;org_id=".$old_org_id."' title='".$TEXT['support14']."'> ".$data_r.", ".$TEXT['meet-name'].": ".$tema_full."</a><hr width='80%' align=left />";
           }
           else
             {
               $query_z2 = "SELECT familiya, name, otchestvo, user_id FROM tbl_users WHERE user_id = '".$zay['usr_id']."' ";
                $zayavka2 = mysql_query($query_z2);
                  if(!$zayavka2) puterror($TEXT['error-tbl2']);
                   if(mysql_num_rows($zayavka2) > 0)
                    {
                     while($zay2 = mysql_fetch_array($zayavka2,MYSQL_ASSOC))
                     {
                      $full_name = "<a class='support23' href=# onClick=\"window.open('user.php?user_id=".$zay2['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']."'>".$zay2['familiya']." ".$zay2['name']." ".$zay2['otchestvo']."</a>";
                     }
                    }

             echo "
                <tr  class='support23'>
                <td>". $full_name."</td>
                <td>".$tema_meet."</td>";

                for($i=0; $i<$time_start; $i++)
               {
                 echo "<td>&nbsp;</td>";
               }

               for($i=$time_start; $i<=$time_stop; $i++)
               {
                 echo "<td ".$class_td." title='".$TEXT['meet-with'].$zay['time_start']."&#58;".$zay['time_start_min'].$TEXT['meet-to'].$zay['time_stop']."&#58;".$zay['time_stop_min']."'>&nbsp;</td>";
               }

               for($i=$time_stop; $i<23; $i++)
               {
                 echo "<td>&nbsp;</td>";
               }
               echo "</tr>";
             }
         }
         if(!empty($_POST['data'])) echo "</table>";
        } else echo "<table border='0' width='80%' cellpadding='0' cellspacing='0' align='left' class='meetrows'><tr><td>&nbsp;</td></tr></table><table border='0' width='80%' cellpadding='0' cellspacing='0' align='left' class='meetrows'><tr><td>".$TEXT['meet-events4']."</td></tr></table>";
         
         echo "</td>
                   </tr>
                   </table>";
           }
        }
      }
?>

     <?php
       $query_count = mysql_query("SELECT COUNT(evnt_id) FROM tbl_meet_event WHERE tbl_meet_event.meet_visible = 'yes' AND tbl_meet_event.room_id = '".$_GET['room_id']."' ".$data_srch."");
       $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='meet_room.php?k=".$k."&amp;org_id=".$old_org_id."".$data_page."".$room_page."' 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='meet_room.php?k=".$k."&amp;org_id=".$old_org_id."".$data_page."".$room_page."' 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=meet_room.php?k=".$k."&amp;org_id=".$old_org_id."".$data_page."".$room_page.">".$j."</option>";
                       else echo "<option value=meet_room.php?k=".$k."&amp;org_id=".$old_org_id."".$data_page."".$room_page.">".$j."</option>";
                    }
                    ?>
                    </select>
                    </td>
                   </tr>
                  </table>
                 <?php
                }
               ?>
</div>
<?php
  require_once "utils/bottom.php";
?>