PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
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 1 month ago apiban.php 1 month ago apilog.php 1 month ago apiplugin.php 1 month ago apiuser.php 1 month ago backup.php 1 month ago calendar.php 1 month ago city.php 1 month ago closure.php 1 month ago configapp.php 1 month ago configcldays.php 1 month ago configcron.php 1 month ago configemp.php 1 month ago configsmsapi.php 1 month ago configuration.php 1 month ago conversion.php 1 month ago country.php 1 month ago coupon.php 1 month ago coupongroup.php 1 month ago cronjob.php 1 month ago cronjoblog.php 1 month ago customer.php 1 month ago customf.php 1 month ago dashboard.php 1 month ago emplocwdays.php 1 month ago employee.php 1 month ago emprates.php 1 month ago export.php 1 month ago exportres.php 1 month ago file.php 1 month ago findreservation.php 1 month ago group.php 1 month ago import.php 1 month ago index.html 1 month ago invoice.php 1 month ago langcustomf.php 1 month ago langemployee.php 1 month ago langgroup.php 1 month ago langmedia.php 1 month ago langoption.php 1 month ago langoptiongroup.php 1 month ago langpackage.php 1 month ago langpackgroup.php 1 month ago langpayment.php 1 month ago langservice.php 1 month ago langstatuscode.php 1 month ago langsubscr.php 1 month ago langtax.php 1 month ago location.php 1 month ago mailtext.php 1 month ago makerecurrence.php 1 month ago media.php 1 month ago multiorder.php 1 month ago option.php 1 month ago optiongroup.php 1 month ago package.php 1 month ago packgroup.php 1 month ago packorder.php 1 month ago payment.php 1 month ago rate.php 1 month ago reportsemp.php 1 month ago reportsser.php 1 month ago reservation.php 1 month ago restriction.php 1 month ago review.php 1 month ago service.php 1 month ago serworkday.php 1 month ago state.php 1 month ago statuscode.php 1 month ago subscription.php 1 month ago subscrorder.php 1 month ago tag.php 1 month ago tax.php 1 month ago usernote.php 1 month ago waitinglist.php 1 month ago webhook.php 1 month ago wizard.php 1 month 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