calendarweek.php
1 month ago
cart.php
1 month ago
confirmapp.php
1 month ago
empattachser.php
1 month ago
empeditcoupon.php
1 month ago
empeditcustfield.php
1 month ago
empeditlocation.php
1 month ago
empeditpay.php
1 month ago
empeditprofile.php
1 month ago
empeditservice.php
1 month ago
empeditwdays.php
1 month ago
emplocwdays.php
1 month ago
emplogin.php
1 month ago
employeesearch.php
1 month ago
employeeslist.php
1 month ago
empmakerecur.php
1 month ago
empmanres.php
1 month ago
empsettings.php
1 month ago
empsubscr.php
1 month ago
empsubscrorder.php
1 month ago
index.html
1 month ago
modules.php
1 month ago
order.php
1 month ago
packages.php
1 month ago
packagesconfirm.php
1 month ago
packagesorder.php
1 month ago
servicesearch.php
1 month ago
subscriptions.php
1 month ago
subscrpayment.php
1 month ago
userprofile.php
1 month ago
waitinglist.php
1 month ago
empmakerecur.php
244 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.employee.area.controller'); |
| 15 | |
| 16 | /** |
| 17 | * Employee area make recurrence controller. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | class VikAppointmentsControllerEmpmakerecur extends VAPEmployeeAreaController |
| 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 | $auth = VAPEmployeeAuth::getInstance(); |
| 38 | |
| 39 | /** |
| 40 | * Added token validation. |
| 41 | * |
| 42 | * @since 1.7 |
| 43 | */ |
| 44 | if (!JSession::checkToken()) |
| 45 | { |
| 46 | // missing CSRF-proof token |
| 47 | UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN')); |
| 48 | } |
| 49 | |
| 50 | $id = $input->getUint('id'); |
| 51 | |
| 52 | // check user permissions |
| 53 | if (!$id || !$auth->manageReservation($id, $readOnly = true)) |
| 54 | { |
| 55 | // not allowed to access the specified order |
| 56 | UIErrorFactory::raiseError(403, JText::translate('JERROR_ALERTNOAUTHOR')); |
| 57 | } |
| 58 | |
| 59 | try |
| 60 | { |
| 61 | // get order details |
| 62 | VAPLoader::import('libraries.order.factory'); |
| 63 | $order = VAPOrderFactory::getAppointments($id, JFactory::getLanguage()->getTag()); |
| 64 | } |
| 65 | catch (Exception $e) |
| 66 | { |
| 67 | // raise AJAX error, order not found |
| 68 | UIErrorFactory::raiseError($e->getCode(), $e->getMessage()); |
| 69 | } |
| 70 | |
| 71 | // access first appointment |
| 72 | $appointment = $order->appointments[0]; |
| 73 | |
| 74 | // get recurrence instructions |
| 75 | $recurrence = array(); |
| 76 | $recurrence['by'] = $input->getUint('by', 0); |
| 77 | $recurrence['amount'] = $input->getUint('amount', 0); |
| 78 | $recurrence['for'] = $input->getUint('for', 0); |
| 79 | |
| 80 | // get recurrence model |
| 81 | $model = $this->getModel('makerecurrence'); |
| 82 | |
| 83 | // create date by using the local timezone of the employee, because the DST |
| 84 | // might change over time |
| 85 | $tz = new DateTimeZone($auth->timezone); |
| 86 | $empDate = JFactory::getDate($appointment->checkin->utc); |
| 87 | $empDate->setTimezone($tz); |
| 88 | |
| 89 | // compose dates recurrence |
| 90 | $arr = $model->getRecurrence($empDate, $recurrence); |
| 91 | |
| 92 | if (!$arr) |
| 93 | { |
| 94 | // invalid recurrence |
| 95 | UIErrorFactory::raiseError(500, JText::translate('VAPMAKERECNOROWS')); |
| 96 | } |
| 97 | |
| 98 | $config = VAPFactory::getConfig(); |
| 99 | |
| 100 | $results = array(); |
| 101 | |
| 102 | // iterate all dates found |
| 103 | foreach ($arr as $date) |
| 104 | { |
| 105 | $tmp = array(); |
| 106 | $tmp['format'] = JHtml::fetch('date', $date, $config->get('dateformat') . ' ' . $config->get('timeformat')); |
| 107 | $tmp['date'] = $date; |
| 108 | |
| 109 | // check the availability for the same appointment, |
| 110 | // but with the new date |
| 111 | $tmp['available'] = $model->checkAvailability($appointment, $date); |
| 112 | |
| 113 | if ($tmp['available']) |
| 114 | { |
| 115 | // available |
| 116 | $tmp['message'] = JText::translate('VAPMAKERECDATEOK'); |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | // not available |
| 121 | $tmp['message'] = JText::translate('VAPMAKERECDATEFAIL'); |
| 122 | |
| 123 | // get reason from model |
| 124 | $tmp['reason'] = $model->getError(null, true); |
| 125 | |
| 126 | // DO NOT suggest here new employee |
| 127 | $tmp['employees'] = null; |
| 128 | |
| 129 | // check availability close to the current date |
| 130 | $tmp['times'] = $model->checkNearbyAvailability($appointment, $date); |
| 131 | |
| 132 | if (!$tmp['times']) |
| 133 | { |
| 134 | // unset times list if empty |
| 135 | $tmp['times'] = null; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // register result |
| 140 | $results[] = $tmp; |
| 141 | } |
| 142 | |
| 143 | // send response to caller |
| 144 | $this->sendJSON($results); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * AJAX end-point used to create a recurrence for the appointment. |
| 149 | * This task expects the following arguments set in request. |
| 150 | * |
| 151 | * @param integer id The appointment ID. |
| 152 | * @param integer by The recurrence by identifier. |
| 153 | * @param integer amount The recurrence amount. |
| 154 | * @param integer for The recurrence for identifier. |
| 155 | * @param array hints An associative array containing the hints for |
| 156 | * those appointments without availability. |
| 157 | * |
| 158 | * @return void |
| 159 | */ |
| 160 | public function create() |
| 161 | { |
| 162 | $input = JFactory::getApplication()->input; |
| 163 | $auth = VAPEmployeeAuth::getInstance(); |
| 164 | |
| 165 | /** |
| 166 | * Added token validation. |
| 167 | * |
| 168 | * @since 1.7 |
| 169 | */ |
| 170 | if (!JSession::checkToken()) |
| 171 | { |
| 172 | // missing CSRF-proof token |
| 173 | UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN')); |
| 174 | } |
| 175 | |
| 176 | $id = $input->getUint('id', 0); |
| 177 | |
| 178 | // check user permissions |
| 179 | if (!$id || !$auth->manageReservation($id, $readOnly = true) || !$auth->createReservation()) |
| 180 | { |
| 181 | // not allowed to access the specified order |
| 182 | UIErrorFactory::raiseError(403, JText::translate('JERROR_ALERTNOAUTHOR')); |
| 183 | } |
| 184 | |
| 185 | try |
| 186 | { |
| 187 | // get order details |
| 188 | VAPLoader::import('libraries.order.factory'); |
| 189 | $order = VAPOrderFactory::getAppointments($id, JFactory::getLanguage()->getTag()); |
| 190 | } |
| 191 | catch (Exception $e) |
| 192 | { |
| 193 | // raise AJAX error, order not found |
| 194 | UIErrorFactory::raiseError($e->getCode(), $e->getMessage()); |
| 195 | } |
| 196 | |
| 197 | // access first appointment |
| 198 | $appointment = $order->appointments[0]; |
| 199 | |
| 200 | // get recurrence instructions |
| 201 | $recurrence = array(); |
| 202 | $recurrence['by'] = $input->getUint('by', 0); |
| 203 | $recurrence['amount'] = $input->getUint('amount', 0); |
| 204 | $recurrence['for'] = $input->getUint('for', 0); |
| 205 | |
| 206 | // get selected hints |
| 207 | $hints = $input->get('hints', array(), 'array'); |
| 208 | |
| 209 | // get recurrence model |
| 210 | $model = $this->getModel('makerecurrence'); |
| 211 | |
| 212 | // create recurrence |
| 213 | $count = $model->createRecurrence($appointment, $recurrence, $hints); |
| 214 | |
| 215 | if (!$count) |
| 216 | { |
| 217 | // raise AJAX error, no created appointments |
| 218 | UIErrorFactory::raiseError(500, JText::translate('VAPMAKERECSUCCESS0')); |
| 219 | } |
| 220 | |
| 221 | // prepare response array |
| 222 | $result = array(); |
| 223 | $result['message'] = JText::sprintf('VAPMAKERECSUCCESS1', $count); |
| 224 | $result['count'] = $count; |
| 225 | |
| 226 | // send response to caller |
| 227 | $this->sendJSON($result); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Redirects the users to the main records list. |
| 232 | * |
| 233 | * @return void |
| 234 | * |
| 235 | * @since 1.7 |
| 236 | */ |
| 237 | public function cancel() |
| 238 | { |
| 239 | $cid = JFactory::getApplication()->input->getUint('cid', array(0)); |
| 240 | |
| 241 | $this->setRedirect('index.php?option=com_vikappointments&task=empmanres.edit&cid[]=' . $cid[0]); |
| 242 | } |
| 243 | } |
| 244 |