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
RevLine 
[473]1<?php
2
[501]3include_once(dirname(__FILE__).'/../../Common/Global.php');
[473]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');
[534]11 var $ParentClass = 'PagePortal';
[738]12
[473]13 function ToHumanTime($Time)
14 {
15 $Hours = floor($Time / 60);
16 $Minutes = $Time - $Hours * 60;
[873]17 if ($Minutes < 10) $Minutes = '0'.$Minutes;
[473]18 $Hours = $Hours % 24;
[874]19 return $Hours.':'.$Minutes;
[738]20 }
[473]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 = '';
[873]30 if ($Days > 0) $Output .= $Days.' dnů, ';
31 if ($Hours > 0) $Output .= $Hours.' hodin, ';
32 if ($Minutes > 0) $Output .= $Minutes.' minut';
[874]33 return $Output;
[473]34 }
[738]35
[473]36 function EditSubject($Id)
37 {
[873]38 if ($this->System->User->CheckPermission('OpeningHours', 'Edit'))
[473]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);
[873]48 while ($DbRow = $DbResult->fetch_assoc())
[473]49 $Day[$DbRow['Day']] = $DbRow;
[873]50 foreach ($this->DaysOfWeek as $Index => $Name)
[473]51 {
[873]52 if (!array_key_exists($Index, $Day)) $Day[$Index] = array('Open1' => 0, 'Close1' => 0, 'Open2' => 0, 'Close2' => 0);
[473]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í';
[874]72 return $Output;
[473]73 }
[738]74
[473]75 function SaveSubject($Id)
76 {
77 global $Config;
[738]78
[473]79 $Output = '';
[873]80 if ($this->System->User->CheckPermission('OpeningHours', 'Edit'))
[473]81 {
82 $this->Database->delete('SubjectOpenTimeDay', 'Subject='.$Id);
[873]83 foreach ($this->DaysOfWeek as $Index => $Name)
[473]84 {
[873]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;
[473]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);
[738]99 }
100 $Output .= 'Uloženo';
101
[473]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í';
[874]113 return $Output;
[738]114 }
115
[473]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');
[873]120 while ($Subject = $DbResult->fetch_assoc())
[473]121 {
122 $Output .= '<strong>'.$Subject['Name'].':</strong><br />';
[738]123
[473]124 // Load time event list
125 $Events = array();
126 $DbResult2 = $this->Database->query('SELECT * FROM `SubjectOpenTimeDay` WHERE Subject='.$Subject['Subject'].' ORDER BY Day ASC');
[873]127 while ($DbRow = $DbResult2->fetch_assoc())
[473]128 {
[873]129 if (($DbRow['Open1'] != $DbRow['Close1']) and ($DbRow['Open1'] < $DbRow['Close1']))
[473]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 }
[873]134 if (($DbRow['Open2'] != $DbRow['Close2']) and ($DbRow['Open2'] < $DbRow['Close2']) and ($DbRow['Close1'] < $DbRow['Open2']))
[473]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);
[738]141
[473]142 // Calculate time to next event
[873]143 if (count($Events) > 0)
[473]144 {
145 $CurrentTime = ((date('w') + 6) % 7) * 24 * 60 + date('G') * 60 + date('i');
[738]146
[473]147 $I = 0;
[873]148 while (($I < count($Events)) and ($Events[$I]['Time'] < $CurrentTime))
[473]149 $I++;
[873]150 if ($I < count($Events))
[473]151 {
152 $NextTime = $Events[$I]['Time'];
153 $NextEventType = $Events[$I]['Type'];
[738]154 } else
[473]155 {
156 $NextTime = $Events[0]['Time'] + 7 * 24 * 60;
157 $NextEventType = $Events[0]['Type'];
158 }
[738]159
[473]160 $TimeDelta = $NextTime - $CurrentTime;
161 //$Output .= $CurrentTime.' '.$NextTime;
[873]162 if ($NextEventType == 2) $Output .= 'Zavírá za '.$this->ToHumanTime2($TimeDelta);
[473]163 else $Output .= 'Otevírá za '.$this->ToHumanTime2($TimeDelta);
164 }
[738]165
166 // Show time inteval table
[473]167 $Output .= '<table class="WideTable"><tr><th>Den</th><th>Čas</th></tr>';
[873]168 foreach ($this->DaysOfWeek as $DayIndex => $DayOfWeek)
[473]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">';
[873]172 if ($DbResult2->num_rows)
[473]173 {
174 $DbRow = $DbResult2->fetch_assoc();
[873]175 if (($DbRow['Open1'] != $DbRow['Close1']) and ($DbRow['Open1'] < $DbRow['Close1']))
[473]176 {
177 $Output .= $this->ToHumanTime($DbRow['Open1']).' - '.$this->ToHumanTime($DbRow['Close1']).' &nbsp;&nbsp; ';
178 }
[873]179 if (($DbRow['Open2'] != $DbRow['Close2']) and ($DbRow['Open2'] < $DbRow['Close2']) and ($DbRow['Close1'] < $DbRow['Open2']))
[473]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 />';
[873]188 if ($Subject['Notice'] != '') $Output .= 'Poznámka: '.$Subject['Notice'].'<br />';
[738]189
[873]190 if ($Subject['Photo'] != 0) $Output .= '<a href="file?id='.$Subject['Photo'].'">Fotka</a> ';
[738]191
[873]192 if ($this->System->User->CheckPermission('SubjectOpenTime', 'Edit'))
[473]193 $Output .= '<a href="edit?Subject='.$Subject['Id'].'">Editovat</a><br />';
194 $Output .= '<br />';
195 }
196 $Output .= '</div>';
[874]197 return $Output;
[473]198 }
[738]199
[473]200 function Show()
201 {
[873]202 if (count($this->System->PathItems) > 1)
[738]203 {
[873]204 if ($this->System->PathItems[1] == 'edit') $Output = $this->EditSubject($_GET['Subject']);
205 else if ($this->System->PathItems[1] == 'save')
[473]206 {
207 $Output = $this->SaveSubject($_GET['Subject']);
208 $Output .= $this->ShowAll();
[738]209 } else $Output = PAGE_NOT_FOUND;
210 } else $Output = $this->ShowAll();
[874]211 return $Output;
[473]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 }
[738]227
[586]228 function DoStart()
[473]229 {
230 $this->System->Pages['otviraci-doby'] = 'PageSubjectOpenTime';
231 }
[738]232
[586]233 function DoInstall()
[473]234 {
235 }
236
[586]237 function DoUnInstall()
[473]238 {
239 }
240}
Note: See TracBrowser for help on using the repository browser.