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 / controllers / makerecurrence.php
vikappointments / admin / controllers Last commit date
analytics.php 2 days ago apiban.php 2 days ago apilog.php 2 days ago apiplugin.php 2 days ago apiuser.php 2 days ago backup.php 2 days ago calendar.php 2 days ago city.php 2 days ago closure.php 2 days ago configapp.php 2 days ago configcldays.php 2 days ago configcron.php 2 days ago configemp.php 2 days ago configsmsapi.php 2 days ago configuration.php 2 days ago conversion.php 2 days ago country.php 2 days ago coupon.php 2 days ago coupongroup.php 2 days ago cronjob.php 2 days ago cronjoblog.php 2 days ago customer.php 2 days ago customf.php 2 days ago dashboard.php 2 days ago emplocwdays.php 2 days ago employee.php 2 days ago emprates.php 2 days ago export.php 2 days ago exportres.php 2 days ago file.php 2 days ago findreservation.php 2 days ago group.php 2 days ago import.php 2 days ago index.html 2 days ago invoice.php 2 days ago langcustomf.php 2 days ago langemployee.php 2 days ago langgroup.php 2 days ago langmedia.php 2 days ago langoption.php 2 days ago langoptiongroup.php 2 days ago langpackage.php 2 days ago langpackgroup.php 2 days ago langpayment.php 2 days ago langservice.php 2 days ago langstatuscode.php 2 days ago langsubscr.php 2 days ago langtax.php 2 days ago location.php 2 days ago mailtext.php 2 days ago makerecurrence.php 2 days ago media.php 2 days ago multiorder.php 2 days ago option.php 2 days ago optiongroup.php 2 days ago package.php 2 days ago packgroup.php 2 days ago packorder.php 2 days ago payment.php 2 days ago rate.php 2 days ago reportsemp.php 2 days ago reportsser.php 2 days ago reservation.php 2 days ago restriction.php 2 days ago review.php 2 days ago service.php 2 days ago serworkday.php 2 days ago state.php 2 days ago statuscode.php 2 days ago subscription.php 2 days ago subscrorder.php 2 days ago tag.php 2 days ago tax.php 2 days ago usernote.php 2 days ago waitinglist.php 2 days ago webhook.php 2 days ago wizard.php 2 days ago
makerecurrence.php
220 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.controllers.admin');
15
16 /**
17 * VikAppointments make recurrence controller.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsControllerMakerecurrence extends VAPControllerAdmin
22 {
23 /**
24 * AJAX end-point used to get a recurrence preview.
25 * This task expects the following arguments set in request.
26 *
27 * @param integer id The appointment ID.
28 * @param integer by The recurrence by identifier.
29 * @param integer amount The recurrence amount.
30 * @param integer for The recurrence for identifier.
31 *
32 * @return void
33 */
34 public function preview()
35 {
36 $input = JFactory::getApplication()->input;
37
38 /**
39 * Added token validation.
40 *
41 * @since 1.7
42 */
43 if (!JSession::checkToken())
44 {
45 // missing CSRF-proof token
46 UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN'));
47 }
48
49 $id = $input->getUint('id', 0);
50
51 try
52 {
53 // get order details
54 VAPLoader::import('libraries.order.factory');
55 $order = VAPOrderFactory::getAppointments($id, JFactory::getLanguage()->getTag());
56 }
57 catch (Exception $e)
58 {
59 // raise AJAX error, order not found
60 UIErrorFactory::raiseError($e->getCode(), $e->getMessage());
61 }
62
63 // access first appointment
64 $appointment = $order->appointments[0];
65
66 // get recurrence instructions
67 $recurrence = array();
68 $recurrence['by'] = $input->getUint('by', 0);
69 $recurrence['amount'] = $input->getUint('amount', 0);
70 $recurrence['for'] = $input->getUint('for', 0);
71
72 // get recurrence model
73 $model = $this->getModel();
74
75 // create date by using the local timezone of the employee, because the DST
76 // might change over time
77 $tz = new DateTimeZone($this->getModel('employee')->getTimezone($appointment->employee->id));
78 $empDate = JFactory::getDate($appointment->checkin->utc);
79 $empDate->setTimezone($tz);
80
81 // compose dates recurrence
82 $arr = $model->getRecurrence($empDate, $recurrence);
83
84 if (!$arr)
85 {
86 // invalid recurrence
87 UIErrorFactory::raiseError(500, JText::translate('VAPMAKERECNOROWS'));
88 }
89
90 $config = VAPFactory::getConfig();
91
92 $results = array();
93
94 // iterate all dates found
95 foreach ($arr as $date)
96 {
97 $tmp = array();
98 $tmp['format'] = JHtml::fetch('date', $date, $config->get('dateformat') . ' ' . $config->get('timeformat'));
99 $tmp['date'] = $date;
100
101 // check the availability for the same appointment,
102 // but with the new date
103 $tmp['available'] = $model->checkAvailability($appointment, $date);
104
105 if ($tmp['available'])
106 {
107 // available
108 $tmp['message'] = JText::translate('VAPMAKERECDATEOK');
109 }
110 else
111 {
112 // not available
113 $tmp['message'] = JText::translate('VAPMAKERECDATEFAIL');
114
115 // get reason from model
116 $tmp['reason'] = $model->getError(null, true);
117
118 // check availability for other employees assigned to this service
119 $tmp['employees'] = $model->checkRandomAvailability($appointment, $date);
120
121 if (!$tmp['employees'])
122 {
123 // unset employees list if empty
124 $tmp['employees'] = null;
125 }
126
127 // check availability close to the current date
128 $tmp['times'] = $model->checkNearbyAvailability($appointment, $date);
129
130 if (!$tmp['times'])
131 {
132 // unset times list if empty
133 $tmp['times'] = null;
134 }
135 }
136
137 // register result
138 $results[] = $tmp;
139 }
140
141 // send response to caller
142 $this->sendJSON($results);
143 }
144
145 /**
146 * AJAX end-point used to create a recurrence for the appointment.
147 * This task expects the following arguments set in request.
148 *
149 * @param integer id The appointment ID.
150 * @param integer by The recurrence by identifier.
151 * @param integer amount The recurrence amount.
152 * @param integer for The recurrence for identifier.
153 * @param array hints An associative array containing the hints for
154 * those appointments without availability.
155 *
156 * @return void
157 */
158 public function create()
159 {
160 $input = JFactory::getApplication()->input;
161
162 /**
163 * Added token validation.
164 *
165 * @since 1.7
166 */
167 if (!JSession::checkToken())
168 {
169 // missing CSRF-proof token
170 UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN'));
171 }
172
173 $id = $input->getUint('id', 0);
174
175 try
176 {
177 // get order details
178 VAPLoader::import('libraries.order.factory');
179 $order = VAPOrderFactory::getAppointments($id, JFactory::getLanguage()->getTag());
180 }
181 catch (Exception $e)
182 {
183 // raise AJAX error, order not found
184 UIErrorFactory::raiseError($e->getCode(), $e->getMessage());
185 }
186
187 // access first appointment
188 $appointment = $order->appointments[0];
189
190 // get recurrence instructions
191 $recurrence = array();
192 $recurrence['by'] = $input->getUint('by', 0);
193 $recurrence['amount'] = $input->getUint('amount', 0);
194 $recurrence['for'] = $input->getUint('for', 0);
195
196 // get selected hints
197 $hints = $input->get('hints', array(), 'array');
198
199 // get recurrence model
200 $model = $this->getModel();
201
202 // create recurrence
203 $count = $model->createRecurrence($appointment, $recurrence, $hints);
204
205 if (!$count)
206 {
207 // raise AJAX error, no created appointments
208 UIErrorFactory::raiseError(500, JText::translate('VAPMAKERECSUCCESS0'));
209 }
210
211 // prepare response array
212 $result = array();
213 $result['message'] = JText::sprintf('VAPMAKERECSUCCESS1', $count);
214 $result['count'] = $count;
215
216 // send response to caller
217 $this->sendJSON($result);
218 }
219 }
220