<?php

include("include/config.php");
mysql_query("SET CHARACTER SET cp1250");
mysql_query("SET NAMES cp1250");


if ($_POST['t']=="zarizeni") 
    {
    if ($_POST['id']=='1') {$id="devices.id,"; $ida="ID;";}
    if ($_POST['name']=='1') {$name="devices.name,"; $namea="Název spotřebiče;";}
    if ($_POST['type']=='1') {$type="devices.type,"; $typea="Popis;";}
    if ($_POST['sensor']=='1') {$sensor="sensors.mac,"; $sensora="Aktuální MAC snímače;";}
    if ($_POST['icon']=='1') {$icon="icons.name,"; $icona="Typ spotřebiče;";}
    if ($_POST['person']=='1') {$person="devices.person,"; $persona="Zodpovědná osoba;";}
    if ($_POST['building']=='1') {$building="buildings.name,"; $buildinga="Budova;";}
    if ($_POST['floor']=='1') {$floor="devices.floor,"; $floora="Patro;";}
    if ($_POST['room']=='1') {$room="devices.room,"; $rooma="Místnost;";}
    if ($_POST['loc']=='1') {$loc="devices.loc_x,devices.loc_y,"; $loca="Souřadnice;";}
    
    if ($_POST['jake']=='asociovane') {$filtr="AND devices.sensor!=0";$asoc="asociovaných ";}
    
    $komplet=$id.$name.$type.$sensor.$icon.$person.$building.$floor.$room.$loc;
    $kompleta=$ida.$namea.$typea.$sensora.$icona.$persona.$buildinga.$floora.$rooma.$loca;
    
    $dotaz=mysql_query("SELECT ".substr($komplet,0,-1)." FROM devices JOIN icons,sensors,buildings WHERE devices.icon = icons.id AND devices.sensor=sensors.id AND buildings.id=devices.building ".$filtr);
     
    header('Expires: 0');
    header("Content-Type: application/csv");
    header('Content-Encoding: windows-1250');
    header('Content-disposition: attachment; filename="spotrebice.csv"');
    
echo 'Seznam '.$asoc.'spotřebičů';
echo "\n";
echo 'Exportováno: '.Date("j.n.Y  H:i:s");
echo "\n";echo "\n";
echo substr($kompleta,0,-1);
echo "\n";echo "\n";
    for($i=0;$i<mysql_num_rows($dotaz);$i++)
     {
      $data=mysql_fetch_row($dotaz);
      for($y=0;$y<mysql_num_fields($dotaz);$y++)
      {echo $data[$y].';';}
      echo $data[mysql_num_fields($dotaz)]."\n";
     }
    
     
  }  
    
    
    
    
    
    
elseif ($_POST['t']=="snimace") 
    {  $poradicasu=0;
    if ($_POST['id']=='1') {$id="id,"; $ida="ID;";$poradicasu++;}
    if ($_POST['mac']=='1') {$mac="CONCAT('0x',mac),"; $maca="MAC;";$poradicasu++;}
    if ($_POST['lastseen']=='1') {$lastseen="lastseen,"; $lastseena="Naposledy odeslal data;";}
    
    if ($_POST['jake']=='aktivni') {$filtr="WHERE active=1";$aktiv="aktivních ";}
    
    $komplet=$id.$mac.$lastseen;
    $kompleta=$ida.$maca.$lastseena;
    
    $dotaz=mysql_query("SELECT ".substr($komplet,0,-1)." FROM sensors ".$filtr);
     
    header('Expires: 0');
    header("Content-Type: application/csv");
    header('Content-Encoding: windows-1250');
    header('Content-disposition: attachment; filename="snimace.csv"');
    
echo 'Seznam '.$aktiv.'snímačů';
echo "\n";
echo 'Exportováno: '.Date("j.n.Y  H:i:s");
echo "\n";echo "\n";
echo substr($kompleta,0,-1);
echo "\n";echo "\n";
    for($i=0;$i<mysql_num_rows($dotaz);$i++)
     {
      $data=mysql_fetch_row($dotaz);
      for($y=0;$y<mysql_num_fields($dotaz);$y++)
      {
       if ($y==$poradicasu) {echo Date("j.n.Y  H:i:s",$data[$y]).';';} else {echo $data[$y].';';}
      }
      echo $data[mysql_num_fields($dotaz)]."\n";
     }
  }
    
    
    
    
    
    
    
elseif ($_POST['t']=="data") 
    {         $poradicasu=1;
    if ($_POST['id']=='1') {$id="data.id,"; $ida="ID;"; $poradicasu++;}
    $data="data.data,"; $dataa="Data;";
    if ($_POST['time']=='1') {$time="data.time,"; $timea="Čas měření;";}
    
    if ($_POST['razeni']=='hodnota') {$razeni="ORDER BY data.data";} else {$razeni="ORDER BY data.time";}
    if ($_POST['postup']=='sestupne') {$postup="DESC";} else {$postup="ASC";}
    
    if (strlen($_POST['od'])>0) {$od=" data.time>".strtotime($_POST['od'])." AND ";}
    if (strlen($_POST['do'])>0) {$do=" data.time<".strtotime($_POST['do'])." AND ";}
    if (strlen($_POST['min'])>0) {$min=" data.data>".$_POST['min']." AND ";}
    if (strlen($_POST['max'])>0) {$max=" data.data<".$_POST['max']." AND ";}     
       
    $komplet=$id.$data.$time;
    $kompleta=$ida.$dataa.$timea;
    
    $dotaz=mysql_query("SELECT ".substr($komplet,0,-1)." FROM data WHERE ".$od.$do.$min.$max." data.device=".$_POST['device']." ".$razeni." ".$postup);
    $dotaz1=mysql_query("SELECT devices.id,devices.name,devices.type,icons.name,devices.person,devices.room FROM devices,icons WHERE devices.id=".$_POST['device']." AND icons.id=devices.icon;");
    $dev=mysql_fetch_row($dotaz1);
    
    header('Expires: 0');
    header("Content-Type: application/csv");
    header('Content-Encoding: windows-1250');
    header('Content-disposition: attachment; filename="'.$dev[1].'.csv"');
      
echo 'Výpis naměřených dat';
echo "\n";
echo 'Exportováno: '.Date("j.n.Y  H:i:s");
echo "\n";echo "\n";
echo 'Zařízení: '.$dev[1].' ('.$dev[2].')';echo "\n";
echo 'Místnost '.$dev[5];echo "\n";
echo 'Zodpovědná osoba: '.$dev[4];echo "\n";
echo "\n";
    
if (strlen($_POST['od'])>0) {echo 'Od: '.$_POST['od'];echo "\n";}
if (strlen($_POST['do'])>0) {echo 'Do: '.$_POST['do'];echo "\n";}
if (strlen($_POST['min'])>0) {echo 'Min. hodnota: '.$_POST['min'].' kWh';echo "\n";}
if (strlen($_POST['max'])>0) {echo 'Min. hodnota: '.$_POST['max'].' kWh';echo "\n";}     

echo substr($kompleta,0,-1);
echo "\n";echo "\n";
    for($i=0;$i<mysql_num_rows($dotaz);$i++)
     {
      $data=mysql_fetch_row($dotaz);
      for($y=0;$y<mysql_num_fields($dotaz);$y++)
      if ($y==$poradicasu) {echo Date("j.n.Y  H:i:s",$data[$y]).';';} else {echo $data[$y].';';}
      echo $data[mysql_num_fields($dotaz)]."\n";
     }
    
     
  }  

?>