source: trunk/Modules/OpeningHours/OpeningHours.php

Last change on this file was 912, checked in by chronos, 3 years ago
  • Modified: Updated Common package.
File size: 11.2 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/../../Common/Global.php');
4
5class PageSubjectOpenTime extends Page
6{
7 public array $DaysOfWeek = array('Pondělí', 'Úterý', 'Středa', 'Čtvrtek', 'Pátek', 'Sobota', 'Neděle');
8 public array $EventType = array('Žádný', 'Otevřeno', 'Zavřeno');
9
10 function __construct(System $System)
11 {
12 parent::__construct($System);
13 $this->Title = 'Otvírací doby';
14 $this->Description = 'Otvírací doby místních subjektů';
15 $this->ParentClass = 'PagePortal';
16 }
17
18 function ToHumanTime(float $Time): string
19 {
20 $Hours = floor($Time / 60);
21 $Minutes = $Time - $Hours * 60;
22 if ($Minutes < 10) $Minutes = '0'.$Minutes;
23 $Hours = $Hours % 24;
24 return $Hours.':'.$Minutes;
25 }
26
27 function ToHumanTime2(float $Time): string
28 {
29 $Days = floor($Time / 24 / 60);
30 $Time = $Time - $Days * 24 * 60;
31 $Hours = floor($Time / 60);
32 $Time = $Time - $Hours * 60;
33 $Minutes = $Time;
34 $Output = '';
35 if ($Days > 0) $Output .= $Days.' dnů, ';
36 if ($Hours > 0) $Output .= $Hours.' hodin, ';
37 if ($Minutes > 0) $Output .= $Minutes.' minut';
38 return $Output;
39 }
40
41 function EditSubject(int $Id): string
42 {
43 if (ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('OpeningHours', 'Edit'))
44 {
45 $Output = '<div class="Centered">';
46 $DbResult = $this->Database->select('Subject', 'Name', 'Id='.$Id);
47 $DbRow = $DbResult->fetch_assoc();
48 $Output .= '<form method="post" action="save?Subject='.$Id.'" enctype="multipart/form-data">'.
49 '<strong>'.$DbRow['Name'].'</strong>'.
50 '<table class="WideTable"><tr><th>Den</th><th>Od</th><th>Do</th><th>Od</th><th>Do</th></tr>';
51 $Day = array();
52 $DbResult = $this->Database->query('SELECT * FROM SubjectOpenTimeDay WHERE Subject = '.$Id);
53 while ($DbRow = $DbResult->fetch_assoc())
54 $Day[$DbRow['Day']] = $DbRow;
55 foreach ($this->DaysOfWeek as $Index => $Name)
56 {
57 if (!array_key_exists($Index, $Day)) $Day[$Index] = array('Open1' => 0, 'Close1' => 0, 'Open2' => 0, 'Close2' => 0);
58 $Output .= '<tr><td>'.$Name.'</td>'.
59 '<td><input type="text" name="day'.$Index.'_open1_h" value="'.floor($Day[$Index]['Open1'] / 60).'" size="2" />'.
60 ':<input type="text" name="day'.$Index.'_open1_m" value="'.($Day[$Index]['Open1'] - floor($Day[$Index]['Open1'] / 60) * 60).'" size="2" /></td>'.
61 '<td><input type="text" name="day'.$Index.'_close1_h" value="'.floor($Day[$Index]['Close1'] / 60).'" size="2" />'.
62 ':<input type="text" name="day'.$Index.'_close1_m" value="'.($Day[$Index]['Close1'] - floor($Day[$Index]['Close1'] / 60) * 60).'" size="2" /></td>'.
63 '<td><input type="text" name="day'.$Index.'_open2_h" value="'.floor($Day[$Index]['Open2'] / 60).'" size="2" />'.
64 ':<input type="text" name="day'.$Index.'_open2_m" value="'.($Day[$Index]['Open2'] - floor($Day[$Index]['Open2'] / 60) * 60).'" size="2" /></td>'.
65 '<td><input type="text" name="day'.$Index.'_close2_h" value="'.floor($Day[$Index]['Close2'] / 60).'" size="2" />'.
66 ':<input type="text" name="day'.$Index.'_close2_m" value="'.($Day[$Index]['Close2'] - floor($Day[$Index]['Close2'] / 60) * 60).'" size="2" /></td>'.
67 '</tr>';
68 }
69 $DbResult = $this->Database->select('SubjectOpenTime', 'Notice', 'Subject='.$Id);
70 $DbRow = $DbResult->fetch_assoc();
71 $Output .= '</table>'.
72 'Poznámka: <input type="text" name="notice" value="'.$DbRow['Notice'].'" size="50" /><br />'.
73 'Fotka: <input type="file" name="photo" size="38" /><br />'.
74 '<input type="submit" value="Uložit" />'.
75 '</form></div>';
76 } else $Output = 'Nemáte oprávnění';
77 return $Output;
78 }
79
80 function SaveSubject(int $Id): string
81 {
82 $Output = '';
83 if (ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('OpeningHours', 'Edit'))
84 {
85 $this->Database->delete('SubjectOpenTimeDay', 'Subject='.$Id);
86 foreach ($this->DaysOfWeek as $Index => $Name)
87 {
88 if ($_POST['day'.$Index.'_open1_h'] > 24) $_POST['day'.$Index.'_open1_h'] = 24;
89 if ($_POST['day'.$Index.'_close1_h'] > 24) $_POST['day'.$Index.'_close1_h'] = 24;
90 if ($_POST['day'.$Index.'_open2_h'] > 24) $_POST['day'.$Index.'_open2_h'] = 24;
91 if ($_POST['day'.$Index.'_close2_h'] > 24) $_POST['day'.$Index.'_close2_h'] = 24;
92 if ($_POST['day'.$Index.'_open1_m'] > 59) $_POST['day'.$Index.'_open1_m'] = 59;
93 if ($_POST['day'.$Index.'_close1_m'] > 59) $_POST['day'.$Index.'_close1_m'] = 59;
94 if ($_POST['day'.$Index.'_open2_m'] > 59) $_POST['day'.$Index.'_open2_m'] = 59;
95 if ($_POST['day'.$Index.'_close2_m'] > 59) $_POST['day'.$Index.'_close2_m'] = 59;
96 $Day = array('Subject' => $Id, 'Day' => $Index);
97 $Day['Open1'] = $_POST['day'.$Index.'_open1_m'] + $_POST['day'.$Index.'_open1_h'] * 60;
98 $Day['Close1'] = $_POST['day'.$Index.'_close1_m'] + $_POST['day'.$Index.'_close1_h'] * 60;
99 $Day['Open2'] = $_POST['day'.$Index.'_open2_m'] + $_POST['day'.$Index.'_open2_h'] * 60;
100 $Day['Close2'] = $_POST['day'.$Index.'_close2_m'] + $_POST['day'.$Index.'_close2_h'] * 60;
101 $this->Database->insert('SubjectOpenTimeDay', $Day);
102 }
103 $Output .= 'Uloženo';
104
105 $File = new File($this->System);
106
107 // Delete old file
108 $DbResult = $this->Database->select('SubjectOpenTime', 'Photo', 'Subject='.$Id);
109 $DbRow = $DbResult->fetch_assoc();
110 $File->Delete($DbRow['Photo']);
111
112 // Create new file
113 $FileId = $File->CreateFromUpload('photo');
114 $this->Database->update('SubjectOpenTime', 'Subject='.$Id, array('UpdateTime' => 'NOW()', 'Notice' => $_POST['notice'], 'Photo' => $FileId));
115 } else $Output = 'Nemáte oprávnění';
116 return $Output;
117 }
118
119 function ShowAll(): string
120 {
121 $Output = '<div class="Centered">';
122 $DbResult = $this->Database->query('SELECT SubjectOpenTime.*, DATE_FORMAT(SubjectOpenTime.UpdateTime, "%e.%c.%Y") as UpdateTime, '.
123 'Subject.Id, Subject.Name as Name FROM SubjectOpenTime '.
124 'JOIN Subject ON Subject.Id = SubjectOpenTime.Subject ORDER BY Name');
125 while ($Subject = $DbResult->fetch_assoc())
126 {
127 $Output .= '<strong>'.$Subject['Name'].':</strong><br />';
128
129 // Load time event list
130 $Events = array();
131 $DbResult2 = $this->Database->query('SELECT * FROM `SubjectOpenTimeDay` WHERE Subject='.$Subject['Subject'].' ORDER BY Day ASC');
132 while ($DbRow = $DbResult2->fetch_assoc())
133 {
134 if (($DbRow['Open1'] != $DbRow['Close1']) and ($DbRow['Open1'] < $DbRow['Close1']))
135 {
136 $Events[] = array('Time' => $DbRow['Open1'] + $DbRow['Day'] * 24 * 60, 'Type' => 1);
137 $Events[] = array('Time' => $DbRow['Close1'] + $DbRow['Day'] * 24 * 60, 'Type' => 2);
138 }
139 if (($DbRow['Open2'] != $DbRow['Close2']) and ($DbRow['Open2'] < $DbRow['Close2']) and ($DbRow['Close1'] < $DbRow['Open2']))
140 {
141 $Events[] = array('Time' => $DbRow['Open2'] + $DbRow['Day'] * 24 * 60, 'Type' => 1);
142 $Events[] = array('Time' => $DbRow['Close2'] + $DbRow['Day'] * 24 * 60, 'Type' => 2);
143 }
144 }
145
146 // Calculate time to next event
147 if (count($Events) > 0)
148 {
149 $CurrentTime = ((date('w') + 6) % 7) * 24 * 60 + date('G') * 60 + date('i');
150
151 $I = 0;
152 while (($I < count($Events)) and ($Events[$I]['Time'] < $CurrentTime))
153 $I++;
154 if ($I < count($Events))
155 {
156 $NextTime = $Events[$I]['Time'];
157 $NextEventType = $Events[$I]['Type'];
158 } else
159 {
160 $NextTime = $Events[0]['Time'] + 7 * 24 * 60;
161 $NextEventType = $Events[0]['Type'];
162 }
163
164 $TimeDelta = $NextTime - $CurrentTime;
165 //$Output .= $CurrentTime.' '.$NextTime;
166 if ($NextEventType == 2) $Output .= 'Zavírá za '.$this->ToHumanTime2($TimeDelta);
167 else $Output .= 'Otevírá za '.$this->ToHumanTime2($TimeDelta);
168 }
169
170 // Show time inteval table
171 $Output .= '<table class="WideTable"><tr><th>Den</th><th>Čas</th></tr>';
172 foreach ($this->DaysOfWeek as $DayIndex => $DayOfWeek)
173 {
174 $DbResult2 = $this->Database->query('SELECT * FROM SubjectOpenTimeDay WHERE Subject = '.$Subject['Subject'].' AND Day='.$DayIndex);
175 $Output .= '<tr><td>'.$DayOfWeek.'</td><td align="center">';
176 if ($DbResult2->num_rows)
177 {
178 $DbRow = $DbResult2->fetch_assoc();
179 if (($DbRow['Open1'] != $DbRow['Close1']) and ($DbRow['Open1'] < $DbRow['Close1']))
180 {
181 $Output .= $this->ToHumanTime($DbRow['Open1']).' - '.$this->ToHumanTime($DbRow['Close1']).' &nbsp;&nbsp; ';
182 }
183 if (($DbRow['Open2'] != $DbRow['Close2']) and ($DbRow['Open2'] < $DbRow['Close2']) and ($DbRow['Close1'] < $DbRow['Open2']))
184 {
185 $Output .= $this->ToHumanTime($DbRow['Open2']).' - '.$this->ToHumanTime($DbRow['Close2']).'';
186 }
187 } else
188 $Output .= '&nbsp;';
189 $Output .= '</td></tr>';
190 }
191 $Output .= '</table>Aktualizováno: '.$Subject['UpdateTime'].'<br />';
192 if ($Subject['Notice'] != '') $Output .= 'Poznámka: '.$Subject['Notice'].'<br />';
193
194 if ($Subject['Photo'] != 0) $Output .= '<a href="file?i='.$Subject['Photo'].'">Fotka</a> ';
195
196 if (ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('SubjectOpenTime', 'Edit'))
197 $Output .= '<a href="edit?Subject='.$Subject['Id'].'">Editovat</a><br />';
198 $Output .= '<br />';
199 }
200 $Output .= '</div>';
201 return $Output;
202 }
203
204 function Show(): string
205 {
206 if (count($this->System->PathItems) > 1)
207 {
208 if ($this->System->PathItems[1] == 'edit') $Output = $this->EditSubject($_GET['Subject']);
209 else if ($this->System->PathItems[1] == 'save')
210 {
211 $Output = $this->SaveSubject($_GET['Subject']);
212 $Output .= $this->ShowAll();
213 } else $Output = PAGE_NOT_FOUND;
214 } else $Output = $this->ShowAll();
215 return $Output;
216 }
217}
218
219class ModuleOpeningHours extends Module
220{
221 function __construct(System $System)
222 {
223 parent::__construct($System);
224 $this->Name = 'OpeningHours';
225 $this->Version = '1.0';
226 $this->Creator = 'Chronos';
227 $this->License = 'GNU/GPLv3';
228 $this->Description = 'Show subject opening hours with time to open or time to close';
229 $this->Models = array(SubjectOpenTime::GetClassName(), SubjectOpenTimeDay::GetClassName());
230 }
231
232 function DoStart(): void
233 {
234 $this->System->RegisterPage(['otviraci-doby'], 'PageSubjectOpenTime');
235 }
236
237 function DoStop(): void
238 {
239 $this->System->UnregisterPage(['otviraci-doby']);
240 }
241}
242
243class SubjectOpenTime extends Model
244{
245 static function GetModelDesc(): ModelDesc
246 {
247 $Desc = new ModelDesc(self::GetClassName());
248 $Desc->AddReference('Subject', Subject::GetClassName());
249 $Desc->AddDateTime('UpdateTime');
250 $Desc->AddString('Notice');
251 $Desc->AddString('Photo', File::GetClassName());
252 return $Desc;
253 }
254}
255
256class SubjectOpenTimeDay extends Model
257{
258 static function GetModelDesc(): ModelDesc
259 {
260 $Desc = new ModelDesc(self::GetClassName());
261 $Desc->AddReference('Subject', Subject::GetClassName());
262 $Desc->AddInteger('Day');
263 $Desc->AddInteger('Open1');
264 $Desc->AddInteger('Close1');
265 $Desc->AddInteger('Open2');
266 $Desc->AddInteger('Close2');
267 return $Desc;
268 }
269}
Note: See TracBrowser for help on using the repository browser.