source: trunk/Modules/OpeningHours/OpeningHours.php@ 874

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