Title = 'Otvírací doby';
$this->Description = 'Otvírací doby místních subjektů';
$this->ParentClass = 'PagePortal';
}
function ToHumanTime(float $Time): string
{
$Hours = floor($Time / 60);
$Minutes = $Time - $Hours * 60;
if ($Minutes < 10) $Minutes = '0'.$Minutes;
$Hours = $Hours % 24;
return $Hours.':'.$Minutes;
}
function ToHumanTime2(float $Time): string
{
$Days = floor($Time / 24 / 60);
$Time = $Time - $Days * 24 * 60;
$Hours = floor($Time / 60);
$Time = $Time - $Hours * 60;
$Minutes = $Time;
$Output = '';
if ($Days > 0) $Output .= $Days.' dnů, ';
if ($Hours > 0) $Output .= $Hours.' hodin, ';
if ($Minutes > 0) $Output .= $Minutes.' minut';
return $Output;
}
function EditSubject(int $Id): string
{
if (ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('OpeningHours', 'Edit'))
{
$Output = '
';
$DbResult = $this->Database->select('Subject', 'Name', 'Id='.$Id);
$DbRow = $DbResult->fetch_assoc();
$Output .= '
';
} else $Output = 'Nemáte oprávnění';
return $Output;
}
function SaveSubject(int $Id): string
{
$Output = '';
if (ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('OpeningHours', 'Edit'))
{
$this->Database->delete('SubjectOpenTimeDay', 'Subject='.$Id);
foreach ($this->DaysOfWeek as $Index => $Name)
{
if ($_POST['day'.$Index.'_open1_h'] > 24) $_POST['day'.$Index.'_open1_h'] = 24;
if ($_POST['day'.$Index.'_close1_h'] > 24) $_POST['day'.$Index.'_close1_h'] = 24;
if ($_POST['day'.$Index.'_open2_h'] > 24) $_POST['day'.$Index.'_open2_h'] = 24;
if ($_POST['day'.$Index.'_close2_h'] > 24) $_POST['day'.$Index.'_close2_h'] = 24;
if ($_POST['day'.$Index.'_open1_m'] > 59) $_POST['day'.$Index.'_open1_m'] = 59;
if ($_POST['day'.$Index.'_close1_m'] > 59) $_POST['day'.$Index.'_close1_m'] = 59;
if ($_POST['day'.$Index.'_open2_m'] > 59) $_POST['day'.$Index.'_open2_m'] = 59;
if ($_POST['day'.$Index.'_close2_m'] > 59) $_POST['day'.$Index.'_close2_m'] = 59;
$Day = array('Subject' => $Id, 'Day' => $Index);
$Day['Open1'] = $_POST['day'.$Index.'_open1_m'] + $_POST['day'.$Index.'_open1_h'] * 60;
$Day['Close1'] = $_POST['day'.$Index.'_close1_m'] + $_POST['day'.$Index.'_close1_h'] * 60;
$Day['Open2'] = $_POST['day'.$Index.'_open2_m'] + $_POST['day'.$Index.'_open2_h'] * 60;
$Day['Close2'] = $_POST['day'.$Index.'_close2_m'] + $_POST['day'.$Index.'_close2_h'] * 60;
$this->Database->insert('SubjectOpenTimeDay', $Day);
}
$Output .= 'Uloženo';
$File = new File($this->System);
// Delete old file
$DbResult = $this->Database->select('SubjectOpenTime', 'Photo', 'Subject='.$Id);
$DbRow = $DbResult->fetch_assoc();
$File->Delete($DbRow['Photo']);
// Create new file
$FileId = $File->CreateFromUpload('photo');
$this->Database->update('SubjectOpenTime', 'Subject='.$Id, array('UpdateTime' => 'NOW()', 'Notice' => $_POST['notice'], 'Photo' => $FileId));
} else $Output = 'Nemáte oprávnění';
return $Output;
}
function ShowAll(): string
{
$Output = '';
$DbResult = $this->Database->query('SELECT SubjectOpenTime.*, DATE_FORMAT(SubjectOpenTime.UpdateTime, "%e.%c.%Y") as UpdateTime, '.
'Subject.Id, Subject.Name as Name FROM SubjectOpenTime '.
'JOIN Subject ON Subject.Id = SubjectOpenTime.Subject ORDER BY Name');
while ($Subject = $DbResult->fetch_assoc())
{
$Output .= '
'.$Subject['Name'].':';
// Load time event list
$Events = array();
$DbResult2 = $this->Database->query('SELECT * FROM `SubjectOpenTimeDay` WHERE Subject='.$Subject['Subject'].' ORDER BY Day ASC');
while ($DbRow = $DbResult2->fetch_assoc())
{
if (($DbRow['Open1'] != $DbRow['Close1']) and ($DbRow['Open1'] < $DbRow['Close1']))
{
$Events[] = array('Time' => $DbRow['Open1'] + $DbRow['Day'] * 24 * 60, 'Type' => 1);
$Events[] = array('Time' => $DbRow['Close1'] + $DbRow['Day'] * 24 * 60, 'Type' => 2);
}
if (($DbRow['Open2'] != $DbRow['Close2']) and ($DbRow['Open2'] < $DbRow['Close2']) and ($DbRow['Close1'] < $DbRow['Open2']))
{
$Events[] = array('Time' => $DbRow['Open2'] + $DbRow['Day'] * 24 * 60, 'Type' => 1);
$Events[] = array('Time' => $DbRow['Close2'] + $DbRow['Day'] * 24 * 60, 'Type' => 2);
}
}
// Calculate time to next event
if (count($Events) > 0)
{
$CurrentTime = ((date('w') + 6) % 7) * 24 * 60 + date('G') * 60 + date('i');
$I = 0;
while (($I < count($Events)) and ($Events[$I]['Time'] < $CurrentTime))
$I++;
if ($I < count($Events))
{
$NextTime = $Events[$I]['Time'];
$NextEventType = $Events[$I]['Type'];
} else
{
$NextTime = $Events[0]['Time'] + 7 * 24 * 60;
$NextEventType = $Events[0]['Type'];
}
$TimeDelta = $NextTime - $CurrentTime;
//$Output .= $CurrentTime.' '.$NextTime;
if ($NextEventType == 2) $Output .= 'Zavírá za '.$this->ToHumanTime2($TimeDelta);
else $Output .= 'Otevírá za '.$this->ToHumanTime2($TimeDelta);
}
// Show time inteval table
$Output .= '
Den | Čas |
';
foreach ($this->DaysOfWeek as $DayIndex => $DayOfWeek)
{
$DbResult2 = $this->Database->query('SELECT * FROM SubjectOpenTimeDay WHERE Subject = '.$Subject['Subject'].' AND Day='.$DayIndex);
$Output .= ''.$DayOfWeek.' | ';
if ($DbResult2->num_rows)
{
$DbRow = $DbResult2->fetch_assoc();
if (($DbRow['Open1'] != $DbRow['Close1']) and ($DbRow['Open1'] < $DbRow['Close1']))
{
$Output .= $this->ToHumanTime($DbRow['Open1']).' - '.$this->ToHumanTime($DbRow['Close1']).' ';
}
if (($DbRow['Open2'] != $DbRow['Close2']) and ($DbRow['Open2'] < $DbRow['Close2']) and ($DbRow['Close1'] < $DbRow['Open2']))
{
$Output .= $this->ToHumanTime($DbRow['Open2']).' - '.$this->ToHumanTime($DbRow['Close2']).'';
}
} else
$Output .= ' ';
$Output .= ' |
';
}
$Output .= '
Aktualizováno: '.$Subject['UpdateTime'].'
';
if ($Subject['Notice'] != '') $Output .= 'Poznámka: '.$Subject['Notice'].'
';
if ($Subject['Photo'] != 0) $Output .= '
Fotka ';
if (ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('SubjectOpenTime', 'Edit'))
$Output .= '
Editovat';
$Output .= '
';
}
$Output .= '
';
return $Output;
}
function Show(): string
{
if (count($this->System->PathItems) > 1)
{
if ($this->System->PathItems[1] == 'edit') $Output = $this->EditSubject($_GET['Subject']);
else if ($this->System->PathItems[1] == 'save')
{
$Output = $this->SaveSubject($_GET['Subject']);
$Output .= $this->ShowAll();
} else $Output = PAGE_NOT_FOUND;
} else $Output = $this->ShowAll();
return $Output;
}
}
class ModuleOpeningHours extends Module
{
function __construct(System $System)
{
parent::__construct($System);
$this->Name = 'OpeningHours';
$this->Version = '1.0';
$this->Creator = 'Chronos';
$this->License = 'GNU/GPLv3';
$this->Description = 'Show subject opening hours with time to open or time to close';
$this->Models = array(SubjectOpenTime::GetClassName(), SubjectOpenTimeDay::GetClassName());
}
function DoStart(): void
{
$this->System->RegisterPage(['otviraci-doby'], 'PageSubjectOpenTime');
}
function DoStop(): void
{
$this->System->UnregisterPage(['otviraci-doby']);
}
}
class SubjectOpenTime extends Model
{
static function GetModelDesc(): ModelDesc
{
$Desc = new ModelDesc(self::GetClassName());
$Desc->AddReference('Subject', Subject::GetClassName());
$Desc->AddDateTime('UpdateTime');
$Desc->AddString('Notice');
$Desc->AddString('Photo', File::GetClassName());
return $Desc;
}
}
class SubjectOpenTimeDay extends Model
{
static function GetModelDesc(): ModelDesc
{
$Desc = new ModelDesc(self::GetClassName());
$Desc->AddReference('Subject', Subject::GetClassName());
$Desc->AddInteger('Day');
$Desc->AddInteger('Open1');
$Desc->AddInteger('Close1');
$Desc->AddInteger('Open2');
$Desc->AddInteger('Close2');
return $Desc;
}
}