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 / site / models / emplogin.php
vikappointments / site / models Last commit date
allorders.php 1 day ago calendarweek.php 1 day ago cart.php 1 day ago confirmapp.php 1 day ago empaccountstat.php 1 day ago empattachser.php 1 day ago empcoupons.php 1 day ago empcustfields.php 1 day ago empeditcoupon.php 1 day ago empeditcustfield.php 1 day ago empeditlocation.php 1 day ago empeditpay.php 1 day ago empeditprofile.php 1 day ago empeditservice.php 1 day ago empeditwdays.php 1 day ago emplocations.php 1 day ago emplocwdays.php 1 day ago emplogin.php 1 day ago employeesearch.php 1 day ago employeeslist.php 1 day ago empmanres.php 1 day ago emppaylist.php 1 day ago empserviceslist.php 1 day ago empsettingsman.php 1 day ago empsubscrcart.php 1 day ago empsubscrhistory.php 1 day ago empsubscrorder.php 1 day ago empwdays.php 1 day ago index.html 1 day ago packages.php 1 day ago packagescart.php 1 day ago packagesconfirm.php 1 day ago packorders.php 1 day ago servicesearch.php 1 day ago serviceslist.php 1 day ago subscrcart.php 1 day ago subscrhistory.php 1 day ago subscrpayment.php 1 day ago
emplogin.php
420 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 VAPLoader::import('libraries.mvc.model');
15
16 /**
17 * VikAppointments employee area view model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelEmplogin extends JModelVAP
22 {
23 /**
24 * The list view pagination object.
25 *
26 * @var JPagination
27 */
28 protected $pagination = null;
29
30 /**
31 * The total number of fetched rows.
32 *
33 * @var integer
34 */
35 protected $total = 0;
36
37 /**
38 * Returns an array of services assigned to the current logged employee.
39 *
40 * @return array An array of services.
41 */
42 public function getServices()
43 {
44 $auth = VAPEmployeeAuth::getInstance();
45
46 if (!$auth->isEmployee())
47 {
48 // raise error in case of no employee
49 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
50 }
51
52 $dbo = JFactory::getDbo();
53
54 // get employee services
55 $q = $dbo->getQuery(true)
56 ->select('s.*')
57 ->select(array(
58 $dbo->qn('a.rate', 'price'),
59 $dbo->qn('a.duration'),
60 $dbo->qn('g.name', 'group_name'),
61 ))
62 ->from($dbo->qn('#__vikappointments_service', 's'))
63 ->leftjoin($dbo->qn('#__vikappointments_group', 'g') . ' ON ' . $dbo->qn('g.id') . ' = ' . $dbo->qn('s.id_group'))
64 ->leftjoin($dbo->qn('#__vikappointments_ser_emp_assoc', 'a') . ' ON ' . $dbo->qn('s.id') . ' = ' . $dbo->qn('a.id_service'))
65 ->where($dbo->qn('a.id_employee') . ' = ' . $auth->id)
66 ->order(array(
67 $dbo->qn('g.ordering') . ' ASC',
68 $dbo->qn('s.ordering') . ' ASC',
69 ));
70
71 $dbo->setQuery($q);
72 return $dbo->loadObjectList();
73 }
74
75 /**
76 * Returns an array of incoming appointments.
77 *
78 * @param array $options An array of options.
79 *
80 * @return array An array of appointments.
81 */
82 public function getAppointments(array $options = array())
83 {
84 // always reset pagination and total count
85 $this->pagination = null;
86 $this->total = 0;
87
88 $auth = VAPEmployeeAuth::getInstance();
89
90 if (!$auth->isEmployee())
91 {
92 // raise error in case of no employee
93 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
94 }
95
96 $dispatcher = VAPFactory::getEventDispatcher();
97
98 $dbo = JFactory::getDbo();
99
100 $options['start'] = !isset($options['start']) ? 0 : $options['start'];
101 $options['limit'] = !isset($options['limit']) ? $auth->getSettings()->listlimit : $options['limit'];
102
103 // get any reserved codes
104 $reserved = JHtml::fetch('vaphtml.status.find', 'code', array('appointments' => 1, 'reserved' => 1));
105
106 $q = $dbo->getQuery(true)
107 ->select('SQL_CALC_FOUND_ROWS r.*')
108 ->select($dbo->qn('s.name', 'service_name'))
109 ->from($dbo->qn('#__vikappointments_reservation', 'r'))
110 ->leftjoin($dbo->qn('#__vikappointments_service', 's') . ' ON ' . $dbo->qn('r.id_service') . ' = ' . $dbo->qn('s.id'))
111 ->where(array(
112 $dbo->qn('r.id_employee') . ' = ' . $auth->id,
113 $dbo->qn('r.id_parent') . ' <> -1',
114 $dbo->qn('r.closure') . ' = 0',
115 ))
116 ->order($dbo->qn('r.checkin_ts') . ' ' . $auth->getSettings()->listordering);
117
118 // take only the upcoming appointments
119 $q->where(sprintf(
120 'DATE_ADD(%s, INTERVAL (%s + %s) MINUTE) > %s',
121 $dbo->qn('r.checkin_ts'),
122 $dbo->qn('r.duration'),
123 $dbo->qn('r.sleep'),
124 $dbo->q(JFactory::getDate()->toSql())
125 ));
126
127 if ($reserved)
128 {
129 // filter by reserved status
130 $q->where($dbo->qn('r.status') . ' IN (' . implode(',', array_map(array($dbo, 'q'), $reserved)) . ')');
131 }
132
133 /**
134 * Trigger hook to manipulate the query at runtime. Third party plugins
135 * can extend the query by applying further conditions or selecting
136 * additional data.
137 *
138 * @param mixed &$query Either a query builder or a query string.
139 * @param array &$options An array of options.
140 * @param VAPEmployeeAuth $auth The authenticated employee instance.
141 *
142 * @return void
143 *
144 * @since 1.7.9
145 */
146 $dispatcher->trigger('onBuildEmploginAppointmentsQuery', [&$q, &$options, $auth]);
147
148 $dbo->setQuery($q, $options['start'], $options['limit']);
149 $rows = $dbo->loadAssocList();
150
151 if ($rows)
152 {
153 // fetch pagination
154 $this->getPagination($options);
155 }
156
157 /**
158 * Trigger hook to manipulate the query response at runtime. Third party
159 * plugins can alter the resulting list of orders.
160 *
161 * @param array &$rows An array of fetched orders.
162 * @param VAPEmployeeAuth $auth The authenticated employee instance.
163 * @param JModel $model The current model.
164 *
165 * @return void
166 *
167 * @since 1.7.9
168 */
169 $dispatcher->trigger('onBuildEmploginAppointmentsData', [&$rows, $auth, $this]);
170
171 return $rows;
172 }
173
174 /**
175 * Returns the list pagination.
176 *
177 * @param array $options An array of options.
178 *
179 * @return JPagination
180 */
181 public function getPagination(array $options = array())
182 {
183 if (!$this->pagination)
184 {
185 jimport('joomla.html.pagination');
186 $dbo = JFactory::getDbo();
187 $dbo->setQuery('SELECT FOUND_ROWS();');
188 $this->total = (int) $dbo->loadResult();
189 $this->pagination = new JPagination($this->total, $options['start'], $options['limit']);
190 }
191
192 return $this->pagination;
193 }
194
195 /**
196 * Returns an object holding the details of the calendar.
197 *
198 * @param array &$options An array of options.
199 *
200 * @return object
201 */
202 public function getCalendar(&$options)
203 {
204 $auth = VAPEmployeeAuth::getInstance();
205
206 if (!$auth->isEmployee())
207 {
208 // raise error in case of no employee
209 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
210 }
211
212 // load employee preferences
213 $settings = $auth->getSettings();
214
215 // set number of visible months
216 $options['numcal'] = $settings->numcals;
217
218 // get current date at midnight
219 $date = JFactory::getDate('today 00:00:00', VikAppointments::getUserTimezone());
220 // Back to the first day of the month.
221 // Do not use "first day of" modifier because PHP 7.3
222 // seems to experience some strange behaviors.
223 $date->modify($date->format('Y-m-01', true));
224
225 // check whether an initial date was set
226 if (empty($options['date']))
227 {
228 // get initial month and year from configuration
229 $month = $settings->firstmonth;
230 $year = $date->format('Y', true);
231
232 // make sure the selected month is not in the past
233 if ($month >= 1 && JFactory::getDate("{$year}-{$month}-01") < $date->format('Y-m-d', $local = true))
234 {
235 // use current month and year
236 $month = (int) $date->format('n', $local = true);
237 $year = (int) $date->format('Y', $local = true);
238 }
239
240 if ($month < 1)
241 {
242 // in case of invalid month, use the current one
243 $month = $date->format('m', true);
244 }
245
246 // set initial date
247 $options['date'] = JFactory::getDate("{$year}-{$month}-01")->format('Y-m-d');
248 }
249
250 // set initial date
251 $options['start'] = $options['date'];
252
253 // grant administrator rights
254 $options['admin'] = true;
255
256 // obtain calendar data through back-end model
257 $calendar = JModelVAP::getInstance('calendar');
258 $data = $calendar->getCalendar($options);
259
260 // include months select options
261 $data->select = array();
262
263 $dt = JFactory::getDate($options['date']);
264
265 for ($i = 1; $i <= 12; $i++)
266 {
267 // update month
268 $dt->modify($dt->format('Y') . '-' . $i . '-01');
269
270 // get date string
271 $k = $dt->format('Y-m-01');
272 // register select option
273 $data->select[$k] = $dt->monthToString($i);
274 }
275
276 return $data;
277 }
278
279 /**
280 * Helper method used to create a new employee record
281 * after a successful registration.
282 *
283 * @param array $args The user details.
284 *
285 * @return boolean
286 */
287 public function register(array $args)
288 {
289 $dbo = JFactory::getDbo();
290
291 // check whether the employee should be immediately listable
292 $listable = VAPEmployeeAreaManager::getSignUpStatus() == 2 ? 1 : 0;
293
294 // lifetime license by default
295 $active_to = -1;
296
297 if (!$listable)
298 {
299 // not listable, set the subscription to pending
300 $active_to = 0;
301 }
302
303 // Even if the user is not active, it is still assigned to a specific ID.
304 // We should recover the user ID that matches the username specified in the args.
305 if (!isset($args['id']) || (int) $args['id'] <= 0)
306 {
307 $q = $dbo->getQuery(true)
308 ->select($dbo->qn('id'))
309 ->from($dbo->qn('#__users'))
310 ->where($dbo->qn('username') . ' = ' . $dbo->q($args['username']));
311
312 $dbo->setQuery($q, 0, 1);
313 $dbo->execute();
314
315 if (!$dbo->getNumRows())
316 {
317 /**
318 * Do not proceed with the employee creation in case
319 * the user registration failed (e.g. due to a duplicated e-mail).
320 *
321 * @since 1.6.2
322 */
323 return false;
324 }
325
326 $args['id'] = (int) $dbo->loadResult();
327 }
328
329 $data = array();
330 $data['firstname'] = $args['firstname'];
331 $data['lastname'] = $args['lastname'];
332 $data['nickname'] = $args['lastname'] . ' ' . $args['firstname'];
333 $data['email'] = $args['email'];
334 $data['jid'] = $args['id'];
335 $data['listable'] = $listable;
336 $data['active_to'] = $active_to;
337
338 $employeeModel = JModelVAP::getInstance('employee');
339
340 // create a new employee
341 $data['id'] = $employeeModel->save($data);
342
343 if (!$data['id'])
344 {
345 // something went wrong...
346 return false;
347 }
348
349 // auto assign services
350 $auto_services = VAPEmployeeAreaManager::getServicesToAssign();
351
352 $serviceModel = JModelVAP::getInstance('service');
353 $assocModel = JModelVAP::getInstance('serempassoc');
354
355 foreach ($auto_services as $id_service)
356 {
357 // load service details through model
358 $item = $serviceModel->getItem((int) $id_service);
359
360 if (!$item)
361 {
362 // item not found, go ahead
363 continue;
364 }
365
366 // inject relation details
367 $item->id_employee = $data['id'];
368 $item->id_service = $item->id;
369
370 // unset item PK
371 $item->id = 0;
372
373 // clear description
374 $item->description = '';
375
376 // use global rates
377 $item->global = 1;
378
379 // attempt to assign the service to the employee
380 $assocModel->save($item);
381 }
382
383 // MAIL
384
385 $admin_mail_list = VikAppointments::getAdminMailList();
386 $sender_mail = VikAppointments::getSenderMail();
387 $company_name = VAPFactory::getConfig()->get('agencyname');
388
389 $mail_subject = JText::sprintf('VAPEMPREGADMINSUBJECT', $data['nickname']);
390 $mail_content = JText::sprintf('VAPEMPREGADMINCONTENT', $data['nickname']);
391
392 $dispatcher = VAPFactory::getEventDispatcher();
393
394 /**
395 * Trigger hook to allow external plugins to manipulate the e-mail subject and text
396 * sent to the administrator(s) after a successful employee registration.
397 *
398 * @param string &$subject The e-mail subject.
399 * @param string &$content The e-mail (HTML) content.
400 * @param array $employee An array containing the details filled by the employee.
401 *
402 * @return boolean False to prevent the e-mail sending.
403 *
404 * @since 1.7
405 */
406 if (!$dispatcher->false('onBeforeSendMailEmployeeRegistration', array(&$mail_subject, &$mail_content, $data)))
407 {
408 $vik = VAPApplication::getInstance();
409
410 // send e-mail notification
411 foreach ($admin_mail_list as $_m)
412 {
413 $vik->sendMail($sender_mail, $company_name, $_m, $sender_mail, $mail_subject, $mail_content, $attachments = null, $is_html = true);
414 }
415 }
416
417 return true;
418 }
419 }
420