PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / views / findreservation / view.html.php
vikappointments / admin / views / findreservation Last commit date
tmpl 3 days ago index.html 3 days ago view.html.php 3 days ago
view.html.php
268 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * VikAppointments reservation availability view.
16 *
17 * @since 1.0
18 */
19 class VikAppointmentsViewfindreservation extends JViewVAP
20 {
21 /**
22 * VikAppointments view display method.
23 *
24 * @return void
25 */
26 function display($tpl = null)
27 {
28 $app = JFactory::getApplication();
29 $input = $app->input;
30 $dbo = JFactory::getDbo();
31 $config = VAPFactory::getConfig();
32
33 // get view model
34 $model = JModelVAP::getInstance('findreservation');
35
36 // set the toolbar
37 $this->addToolBar();
38
39 $filters = array();
40
41 $filters['id_emp'] = $app->getUserStateFromRequest($this->getPoolName() . '.id_emp', 'id_emp', 0, 'uint');
42 $filters['id_ser'] = $app->getUserStateFromRequest($this->getPoolName() . '.id_ser', 'id_ser', 0, 'uint');
43
44 $filters['search_mode'] = $app->getUserStateFromRequest($this->getPoolName() . '.searchmode', 'searchmode', null, 'uint');
45
46 $filters['id_res'] = $input->getUint('id_res', 0);
47 $filters['day'] = $input->getString('day', '');
48 $filters['people'] = $input->getUint('people', 1);
49
50 if ($filters['day'])
51 {
52 // rebuild date into military format and strip the time
53 $filters['day'] = JFactory::getDate($filters['day'])->format('Y-m-d');
54 }
55
56 // get search mode stored within the configuration
57 $stored_search_mode = $config->getUint('findresmode');
58
59 if (empty($filters['search_mode']))
60 {
61 // use default one stored in config
62 $filters['search_mode'] = $stored_search_mode;
63 }
64 else if ($stored_search_mode != $filters['search_mode'])
65 {
66 // search mode as changed, update configuration
67 $config->set('findresmode', $filters['search_mode']);
68 }
69
70 $employees = array();
71 $services = array();
72
73 // search by employee > service
74 if ($filters['search_mode'] == 1)
75 {
76 // get employees (sorted by the total number of assigned reservations)
77 $q = $dbo->getQuery(true)
78 ->select($dbo->qn(array('e.id', 'e.nickname')))
79 ->from($dbo->qn('#__vikappointments_employee', 'e'))
80 ->leftjoin($dbo->qn('#__vikappointments_reservation', 'r') . ' ON ' . $dbo->qn('e.id') . ' = ' . $dbo->qn('r.id_employee'))
81 ->group($dbo->qn('e.id'))
82 ->order('COUNT(' . $dbo->qn('r.id') . ') DESC')
83 ->order($dbo->qn('e.id') . ' ASC');
84
85 $dbo->setQuery($q);
86 $employees = $dbo->loadObjectList();
87
88 if (!$filters['id_emp'])
89 {
90 // use first available employee
91 $filters['id_emp'] = $employees[0]->id;
92 }
93
94 // get employee model
95 $empModel = JModelVAP::getInstance('employee');
96
97 // get assigned services
98 $services = $empModel->getServices($filters['id_emp']);
99
100 if ($services)
101 {
102 /**
103 * After switching search mode, we can face this scenario:
104 * - id_service = X
105 * - id_employee = ALL EMPLOYEES
106 *
107 * is switched to:
108 * - id_employee = Y (the first employee available)
109 * - id_service = X
110 *
111 * Since the service ID is not changed, we need to make
112 * sure that it is supported by the selected employee.
113 * If it isn't, we need to use the first available service.
114 *
115 * @since 1.6
116 */
117 if (!$filters['id_ser'] || !$this->isSupported($filters['id_ser'], $services))
118 {
119 // use first available service
120 $filters['id_ser'] = $services[0]->id;
121 }
122 }
123 }
124 // search by service > employee
125 else
126 {
127 // get services
128 $q = $dbo->getQuery(true)
129 ->select($dbo->qn(array('s.id', 's.name')))
130 ->from($dbo->qn('#__vikappointments_service', 's'))
131 ->order(array(
132 $dbo->qn('s.published') . ' DESC',
133 $dbo->qn('s.ordering') . ' ASC',
134 ));
135
136 $dbo->setQuery($q);
137 $services = $dbo->loadObjectList();
138
139 if (!$filters['id_ser'])
140 {
141 // use first available service
142 $filters['id_ser'] = $services[0]->id;
143 }
144
145 // get service model
146 $serModel = JModelVAP::getInstance('service');
147
148 // get assigned employees
149 $employees = $serModel->getEmployees($filters['id_ser']);
150
151 if ($employees)
152 {
153 /**
154 * After switching service, we can face this scenario
155 * - id_service = X
156 * - id_employee = Y
157 *
158 * then:
159 * - id_employee = Y
160 * - id_service = Z
161 *
162 * Since the employee ID is not changed, we need to make
163 * sure that it is supported by the selected service.
164 * If it isn't, we need to unset the selected employee.
165 *
166 * @since 1.6
167 */
168 if ($filters['id_emp'] && !$this->isSupported($filters['id_emp'], $employees))
169 {
170 $filters['id_emp'] = 0;
171 }
172
173 if (!$filters['id_emp'] && count($employees) == 1)
174 {
175 // if the list contains only one employee, use it in
176 // place of the empty option to ignore the employee
177 $filters['id_emp'] = $employees[0]->id;
178 }
179 }
180 }
181
182 /**
183 * In case both the employee and the service have
184 * been specified, look for the correct overrides.
185 *
186 * @since 1.7
187 */
188 if ($filters['id_emp'] && $filters['id_ser'])
189 {
190 // get service-employee association model
191 $assocModel = JModelVAP::getInstance('serempassoc');
192 // get service-employee overrides
193 $this->override = $assocModel->getOverrides($filters['id_ser'], $filters['id_emp']);
194 }
195 else if ($filters['id_ser'])
196 {
197 // get service model
198 $serModel = JModelVAP::getInstance('service');
199 // get service details
200 $this->override = $serModel->getItem($filters['id_ser']);
201 }
202
203 $this->services = $services;
204 $this->employees = $employees;
205
206 // fetch calendar by using the view model
207 $this->calendar = $model->getCalendar($filters);
208
209 $this->filters = $filters;
210
211 if ($filters['id_res'] > 0)
212 {
213 VAPLoader::import('libraries.order.factory');
214
215 try
216 {
217 // load details of the appointment that we are editing
218 $this->appointment = VAPOrderFactory::getAppointments($filters['id_res'], JFactory::getLanguage()->getTag());
219 // take only the details of the first appointment
220 $this->appointment = array_shift($this->appointment->appointments);
221 }
222 catch (Exception $e)
223 {
224 // appointment not found, suppress error
225 }
226 }
227
228 // display the template
229 parent::display($tpl);
230 }
231
232 /**
233 * Setting the toolbar.
234 *
235 * @return void
236 */
237 protected function addToolBar()
238 {
239 // add menu title and some buttons to the page
240 JToolBarHelper::title(JText::translate('VAPMAINTITLEFINDRESERVATION'), 'vikappointments');
241
242 JToolBarHelper::cancel('reservation.cancel');
243 }
244
245 /**
246 * Checks if the specified service/employee is contained within the list.
247 *
248 * @param integer $needle The service/employee to search.
249 * @param array $haystack The haystack.
250 *
251 * @return boolean True if supported, otherwise false.
252 *
253 * @since 1.6
254 */
255 protected function isSupported($needle, array $haystack)
256 {
257 foreach ($haystack as $tmp)
258 {
259 if ($tmp->id == $needle)
260 {
261 return true;
262 }
263 }
264
265 return false;
266 }
267 }
268