<?php
include_once('../global.php');

class EatingPlaceEdit extends Page
{
  var $Dependencies = array('Log');
  var $FullTitle = 'Jídleníček jídelny Na kopečku';
  var $ShortTitle = 'Editace jídelníčku';
  var $DayNames = array('Neděle', 'Pondělí', 'Úterý', 'Středa', 'Čtvrtek', 'Pátek', 'Sobota');
  var $Status = array('Nezveřejněno', 'Otevřeno', 'Zavřeno - svátek', 'Zavřeno - dovolená');
  var $DayCount = 20;    // počet dopředu zobrazených dnů

  function PrintTableRow($Row)
  {
    global $LastWeekOfYear;

    $Selected = array('', '', '', '');
    $Selected[$Row['Status']] = 'selected ';
    $Date = explode('-', $Row['Date']);
    $Week = date('w', mktime(0, 0, 0, $Date[1], $Date[2], $Date[0]));
    $WeekOfYear = date('W', mktime(0, 0, 0, $Date[1], $Date[2], $Date[0]));
    if($WeekOfYear != $LastWeekOfYear)
      $WeekRowSpan = '<td align="center" rowspan="'.(7 - (($Week + 7 - 1) % 7)).'">'.$WeekOfYear.'<br /><a href="tisk.php?date='.date('Y-m-d', mktime(0, 0, 0, $Date[1], $Date[2], $Date[0]) - ($Week - 1) * 86400).'">Tisk</a></td>';
      else $WeekRowSpan = '';
    if($Week == 0) $Color = ' style="color: #ff0000;" '; else $Color = ''; 
    $Output = '<tr><td'.$Color.'>'.$this->DayNames[$Week].'</td><td>'.HumanDate($Row['Date']).'</td>'.$WeekRowSpan.'
    <td><input name="soup_'.$Row['Date'].'" size="30" value="'.$Row['Soup'].'"></td>
    <td><input name="meal_'.$Row['Date'].'" size="30" value="'.$Row['Meal'].'"></td>
    <td><select name="status_'.$Row['Date'].'">';
    for($I = 0; $I < 4; $I++) $Output .= '    <option '.$Selected[$I].'value="'.$I.'">'.$this->Status[$I].'</option>';
    $Output .= '</select></td></tr>';
    $LastWeekOfYear = $WeekOfYear;
    return($Output);
  }

  function Show()
  {
    Header('Cache-Control: no-cache');

    $Output = '';
    if(array_key_exists('action', $_GET))
    {
      if($_GET['action'] == 'savemenu')
      {
        for($I = 0; $I < $this->DayCount; $I++)
        {
          $Time = time() + $I * 86400;
          $Date = date('Y-m-d', $Time);
          $this->Database->replace('Meals', array('Date' => $Date, 'Meal' => $_POST['meal_'.$Date], 'Soup' => $_POST['soup_'.$Date], 'Status' => $_POST['status_'.$Date]));
        }
        $Output .= '<div style="color: red; font-size: larger;">Menu uloženo!</div>';
        $this->System->Modules['Log']->NewRecord('EatingPlace', 'MenuSave');
      }
      if($_GET['action'] == 'saveinfo')
      {
        $this->Database->delete('MealsInfo', '1');
        $this->Database->insert('MealsInfo', array('Info' => $_POST['info'], 'Price' => $_POST['price']));
        $Output .= '<div style="color: red; font-size: larger;">Informační údaje uloženy!</div>';
        $this->System->Modules['Log']->NewRecord('EatingPlace', 'InfoSave');
      }
    }
    $Output = '<form action="menuedit.php?action=savemenu" method="post">
<fieldset><legend>Jídlo pro jednotlivé dny</legend>
<table align="center" class="WideTable"><tr><th>Den</th><th>Datum</th><th>Týden</th><th>Polévka</th><th>Hlavní jídlo</th><th>Stav</th></tr>';
    for($I = 0; $I < $this->DayCount; $I++)
    {
      $Time = time() + $I * 86400;
      $DbResult = $this->Database->select('Meals', '*', 'Date = "'.date('Y-m-d', $Time).'"');
      if($Row = $DbResult->fetch_array())
        $Output .= $this->PrintTableRow($Row);
      else
      {
        $Row = array('Status' => 0, 'Meal' => '', 'Soup' => '', 'Date' => date('Y-m-d', $Time));
        $Output .= $this->PrintTableRow($Row);
      }
    }
    $Output .= '</table><br />
<div align="center"><input type="submit" value="Uložit menu"></div>
</fieldset></form>';
    $Output .= '<form action="menuedit.php?action=saveinfo" method="post">
<fieldset><legend>Informační údaje</legend>';

    $DbResult = $this->Database->select('MealsInfo', '*');
    $Row = $DbResult->fetch_array();
    $Output .= '<textarea name="info" rows="20" cols="80" >'.$Row['Info'].'</textarea><br />'.
'Cena: <input type="text" name="price" size="5" value="'.$Row['Price'].'"> Kč<br />'.
'<div align="center"><input type="submit" value="Uložit údaje"></div>
</fieldset></form>';
    return($Output);
  }
}

$System->AddModule(new EatingPlaceEdit());
$System->Modules['EatingPlaceEdit']->GetOutput();

?>

