<?php

if(!mysql_pconnect('localhost','root',''))
  die('<p>Server connection failed</p>');
  if(!mysql_select_db('bcdata'))
  die('<p>Database connection failed</p>');
  @mysql_query("SET CHARACTER SET utf8");
  @mysql_query("SET NAMES 'utf8'");




include("MPDF54/mpdf.php");
$mpdf=new mPDF('C','A4-L');
$stylesheet = file_get_contents('pdfstylus.css');
  
$form_key = $_POST['form_key'];
$form_val = $_POST['form_val'];


$print_all = '<html><head></head><body>';

switch($form_key) {
  case 'ucitel':
  
  $sqlbc="SELECT DISTINCT *
    FROM timetable
    LEFT JOIN course ON timetable_code=course_code  
    WHERE timetable_lector = '".$form_val."'";
    $vbc = mysql_query($sqlbc);
    while($rbc = mysql_fetch_array($vbc)){
          $tc[intval($rbc['timetable_start'])][$rbc['timetable_day']]=$rbc['course_title_sk'];
          $tca[intval($rbc['timetable_start'])][$rbc['timetable_day']]=$rbc['timetable_group'];
          $tcb[intval($rbc['timetable_start'])][$rbc['timetable_day']]=$rbc['timetable_room'];
          
          $lc=$rbc['timetable_stop']-$rbc['timetable_start'];
          $hc[intval($rbc['timetable_start'])][$rbc['timetable_day']]=$lc;
            for($i=1;$i<$lc;$i++){
                                  $hc[intval($rbc['timetable_start'])+$i][$rbc['timetable_day']]='x';
                                  }
          }


$print_all .= "<table border='1' vertical-align: bottom class=center>";
$print_all .= "<tr><th></th><th>PONDELOK</th><th>UTOROK</th><th>STREDA</th><th>ŠTVRTOK</th><th>PIATOK</th><th>SOBOTA</th></tr>";
for($i=7;$i<=19;$i++)
{
  $print_all .= "<tr>\n";
  $print_all .= "<td>$i</td>\n";
  for($j=1;$j<=6;$j++)
  {
    if(!isset($hc[$i][$j]))
    {
      $print_all .= "<td></td>\n";
    }
    elseif($hc[$i][$j]=='x')
    {
      $print_all .= "";
    }
    elseif($hc[$i][$j]>1)
    {
      $print_all .= "<td rowspan='".$hc[$i][$j]."'>".$tc[$i][$j]."<br />".$tca[$i][$j]."<br />".$tcb[$i][$j]."</td>\n";
    } 
    else
      $print_all .= "<td> ".$tc[$i][$j]."<br />".$tca[$i][$j]."<br />".$tcb[$i][$j]."</td>\n";
  } 
  $print_all .= "</tr>\n";
}
$print_all .= "</table>";


  
  
 break; 
  
  
  
  

  case 'miestnost':  

  
    

    $sqlba="SELECT DISTINCT *,CONCAT(person_fchpt_name,' ',person_fchpt_surname) AS meno FROM timetable 
    LEFT JOIN course ON timetable_code=course_code
    LEFT JOIN person_fchpt ON timetable_lector=person_fchpt_id 
    WHERE timetable_room = '".$form_val."'";
    
    $vba = mysql_query($sqlba);
    while($rba = mysql_fetch_array($vba)){
      $t[intval($rba['timetable_start'])][$rba['timetable_day']]=$rba['course_title_sk'];
      $tx[intval($rba['timetable_start'])][$rba['timetable_day']]=$rba['meno'];
      $txa[intval($rba['timetable_start'])][$rba['timetable_day']]=$rba['timetable_group']; 
      $l=$rba['timetable_stop']-$rba['timetable_start'];
      $h[intval($rba['timetable_start'])][$rba['timetable_day']]=$l;
      for($i=1;$i<$l;$i++){
        $h[intval($rba['timetable_start'])+$i][$rba['timetable_day']]='x';
      }
    }

         
  $print_all .= "<table border='1' vertical-align: bottom; class=center>";
$print_all .= "<tr><th></th><th>PONDELOK</th><th>UTOROK</th><th>STREDA</th><th>ŠTVRTOK</th><th>PIATOK</th><th>SOBOTA</th></tr>";
for($i=7;$i<=19;$i++)
{
  $print_all .=  "<tr>\n";
  $print_all .=  "<td>$i</td>\n";
  for($j=1;$j<=6;$j++)
  {
    if(!isset($h[$i][$j]))
    {
      $print_all .=  "<td></td>\n";
    }
    else if($h[$i][$j]=='x')
    {
      $print_all .=  "";
    }
    else if($h[$i][$j]>1)
    {
      $print_all .=  "<td rowspan='".$h[$i][$j]."'>".$t[$i][$j]."<br />".$tx[$i][$j]."<br />".$txa[$i][$j]."</td>\n";
    } 
    else
      $print_all .=  "<td> ".$t[$i][$j]."<br />".$tx[$i][$j]."<br />".$txa[$i][$j]."</td>\n";
  } 
  $print_all .=  "</tr>\n";
}

$print_all .=  "</table>";
    
       
  
  break;
 
  
  case 'kurz': 
  
  
  $sqlbd="SELECT DISTINCT *,CONCAT(person_fchpt_name,' ',person_fchpt_surname) AS meno 
      FROM timetable 
      LEFT JOIN course ON timetable_code=course_code
      LEFT JOIN person_fchpt ON timetable_lector=person_fchpt_id  
      WHERE timetable_code = '".$form_val."'";
      $vbd = mysql_query($sqlbd);
      while($rbd = mysql_fetch_array($vbd)){
            $td[intval($rbd['timetable_start'])][$rbd['timetable_day']]=$rbd['course_title_sk'];
            $tda[intval($rbd['timetable_start'])][$rbd['timetable_day']]=$rbd['meno'];
            $tdb[intval($rbd['timetable_start'])][$rbd['timetable_day']]=$rbd['timetable_group'];
            $tdc[intval($rbd['timetable_start'])][$rbd['timetable_day']]=$rbd['timetable_room'];
            
            $ld=$rbd['timetable_stop']-$rbd['timetable_start'];
            $hd[intval($rbd['timetable_start'])][$rbd['timetable_day']]=$ld;
            for($i=1;$i<$ld;$i++){
                                $hd[intval($rbd['timetable_start'])+$i][$rbd['timetable_day']]='x';
                                  }
            }
    
    
$print_all .=  "<table border='1' vertical-align: bottom; class=center>";
$print_all .= "<tr><th></th><th>PONDELOK</th><th>UTOROK</th><th>STREDA</th><th>ŠTVRTOK</th><th>PIATOK</th><th>SOBOTA</th></tr>";
for($i=7;$i<=19;$i++)
{
  $print_all .=  "<tr>\n";
  $print_all .=  "<td>$i</td>\n";
  for($j=1;$j<=6;$j++)
  {
    if(!isset($hd[$i][$j]))
    {
      $print_all .=  "<td></td>\n";
    }
    elseif($hd[$i][$j]=='x')
    {
      $print_all .=  "";
    }
    elseif($hd[$i][$j]>1)
    {
      $print_all .=  "<td rowspan='".$hd[$i][$j]."'>".$td[$i][$j]."<br />".$tda[$i][$j]."<br />".$tdb[$i][$j]."<br />".$tdc[$i][$j]."</td>\n";
    } 
    else
      $print_all .=  "<td> ".$td[$i][$j]."<br />".$tda[$i][$j]."<br />".$tdb[$i][$j]."<br />".$tdc[$i][$j]."</td>\n";
  } 
  $print_all .=  "</tr>\n";
}
$print_all .=  "</table>";


break;
  
  
  
  
  case 'kruzok':
  
      $fdp = explode("|",$form_val);
            $sqlbb="SELECT DISTINCT *,CONCAT(person_fchpt_name,' ',person_fchpt_surname) AS meno FROM timetable
             LEFT JOIN course ON timetable_code=course_code
              LEFT JOIN person_fchpt ON timetable_lector=person_fchpt_id 
            WHERE timetable_group LIKE '".$fdp[0]."%' AND timetable_class = '".$fdp[1]."'";
      $vbb = mysql_query($sqlbb);
      
while($rbb = mysql_fetch_array($vbb)){
        $tb[intval($rbb['timetable_start'])][$rbb['timetable_day']]=$rbb['course_title_sk'];
        $txba[intval($rbb['timetable_start'])][$rbb['timetable_day']]=$rbb['meno'];
        $txbb[intval($rbb['timetable_start'])][$rbb['timetable_day']]=$rbb['timetable_room'];
        $lb=$rbb['timetable_stop']-$rbb['timetable_start'];
        $hb[intval($rbb['timetable_start'])][$rbb['timetable_day']]=$lb;
  for($i=1;$i<$lb;$i++){
                        $hb[intval($rbb['timetable_start'])+$i][$rbb['timetable_day']]='x';
                        }
        }

$print_all .= "<table border='1' vertical-align: bottom; class=center>";
$print_all .= "<tr><th></th><th>PONDELOK</th><th>UTOROK</th><th>STREDA</th><th>ŠTVRTOK</th><th>PIATOK</th><th>SOBOTA</th></tr>";
for($i=7;$i<=19;$i++)
{
  $print_all .= "<tr>\n";
  $print_all .= "<td>$i</td>\n";
  for($j=1;$j<=6;$j++)
  {
    if(!isset($hb[$i][$j]))
    {
      $print_all .= "<td></td>\n";
    }
    elseif($hb[$i][$j]=='x')
    {
      $print_all .= "";
    }
    elseif($hb[$i][$j]>1)
    {
      $print_all .= "<td rowspan='".$hb[$i][$j]."'>".$tb[$i][$j]."<br />".$txba[$i][$j]."<br />".$txbb[$i][$j]."</td>\n";
    } 
    else
      $print_all .= "<td> ".$tb[$i][$j]."<br />".$txba[$i][$j]."<br />".$txbb[$i][$j]."</td>\n";
  } 
  $print_all .= "</tr>\n";
}
$print_all .= "</table>";

 
  
   break;
}

$print_all .= '</body></html>';
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($print_all);
$mpdf->WriteHTML('<tocentry content="A4 landscape" />');
$mpdf->Output();


?>


