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 service details (calendar) view. |
| 16 | * |
| 17 | * @since 1.0 |
| 18 | */ |
| 19 | class VikAppointmentsViewservicesearch 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 | |
| 31 | $itemid = $input->getInt('Itemid', 0); |
| 32 | |
| 33 | // request args |
| 34 | $id_employee = $input->getUint('id_employee', 0); |
| 35 | $id_service = $input->getUint('id_service', 0); |
| 36 | $date = $input->getString('date', null); |
| 37 | $month = $input->getString('month', null); |
| 38 | $hour = $input->getUint('hour', null); |
| 39 | $min = $input->getUint('min', null); |
| 40 | |
| 41 | if (!$id_service) |
| 42 | { |
| 43 | // fallback to XML notation |
| 44 | $id_service = $input->getUint('id_ser', 0); |
| 45 | } |
| 46 | |
| 47 | // get view model |
| 48 | $model = JModelVAP::getInstance('servicesearch'); |
| 49 | |
| 50 | // load details of requested employee |
| 51 | $service = $model->getService($id_service, array('id_employee' => $id_employee)); |
| 52 | |
| 53 | if (!$service) |
| 54 | { |
| 55 | // throw exception to safely break the process |
| 56 | throw new Exception($model->getError($index = null, $string = true)); |
| 57 | } |
| 58 | |
| 59 | if (VAPFactory::getConfig()->getUint('loginreq') == 3 && !VikAppointments::isUserLogged()) |
| 60 | { |
| 61 | // login is required |
| 62 | $tpl = 'login'; |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | // get reviews |
| 67 | $base_uri = "index.php?option=com_vikappointments&view=servicesearch&id_service={$id_service}"; |
| 68 | |
| 69 | if ($id_employee) |
| 70 | { |
| 71 | $base_uri .= '&id_employee=' . $id_employee; |
| 72 | } |
| 73 | |
| 74 | if ($itemid) |
| 75 | { |
| 76 | $base_uri .= '&Itemid=' . $itemid; |
| 77 | } |
| 78 | |
| 79 | $rev_ord_by = $input->getString('revordby', ''); |
| 80 | $rev_ord_mode = $input->getString('revordmode', ''); |
| 81 | |
| 82 | $this->userCanLeaveReview = VikAppointments::userCanLeaveServiceReview($id_service); |
| 83 | $this->reviewsOrderingLinks = VikAppointments::getReviewsOrderingLinks($base_uri, $rev_ord_by, $rev_ord_mode); |
| 84 | |
| 85 | /** |
| 86 | * Use the first available employee if not specified. |
| 87 | * |
| 88 | * Make sure the random selection is disabled, otherwise we need to avoid |
| 89 | * picking the employees, because the availability must be calculated for |
| 90 | * all the supported employees (@since 1.7). |
| 91 | */ |
| 92 | if ($id_employee <= 0 && count($service->employees) && $service->choose_emp && !$service->random_emp) |
| 93 | { |
| 94 | $id_employee = $service->employees[0]->id; |
| 95 | } |
| 96 | |
| 97 | if ($id_employee > 0) |
| 98 | { |
| 99 | /** |
| 100 | * Get the selected employee to obtain here the details to use. |
| 101 | * |
| 102 | * @since 1.6 |
| 103 | */ |
| 104 | $employee = $this->getSelectedEmployee($id_employee, $service->employees); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | $employee = null; |
| 109 | } |
| 110 | |
| 111 | // get locations from request |
| 112 | $locations = $input->getUint('locations', array()); |
| 113 | |
| 114 | // get all IDs of the supported locations |
| 115 | $loc_ids = array_map(function($l) |
| 116 | { |
| 117 | return $l->id; |
| 118 | }, $service->locations); |
| 119 | |
| 120 | /** |
| 121 | * It is needed to unset all the locations that don't belong |
| 122 | * to the current employee, as they may have been submitted |
| 123 | * after selecting a different employee. |
| 124 | * |
| 125 | * @since 1.6 |
| 126 | */ |
| 127 | $locations = array_values(array_intersect($locations, $loc_ids)); |
| 128 | |
| 129 | // use all the locations if not provided |
| 130 | if (count($locations) == 0 && count($service->locations) > 1) |
| 131 | { |
| 132 | $locations = $loc_ids; |
| 133 | } |
| 134 | |
| 135 | // remove all the expired reservations (switch status from PENDING to REMOVED) |
| 136 | VikAppointments::removeAllServicesReservationsOutOfTime($service->id); |
| 137 | |
| 138 | // prepare calendar availability search options |
| 139 | $options = array( |
| 140 | 'id_ser' => $id_service, |
| 141 | 'id_emp' => $id_employee, |
| 142 | 'locations' => $locations, |
| 143 | ); |
| 144 | |
| 145 | if ($month) |
| 146 | { |
| 147 | // use the specified month date |
| 148 | $options['start'] = $month; |
| 149 | } |
| 150 | |
| 151 | if (!VAPDateHelper::isNull($date) && !$month) |
| 152 | { |
| 153 | if ($date) |
| 154 | { |
| 155 | // re-format date |
| 156 | $date = VAPDateHelper::getDate($date)->format('Y-m-d'); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @todo Add helper method to check whether the date is contained within |
| 161 | * the calendar range. If it isn't, we should use the first available |
| 162 | * date, or the current date in case the selected one is in the past. |
| 163 | */ |
| 164 | |
| 165 | $options['date'] = $date; |
| 166 | } |
| 167 | |
| 168 | // fetch calendar data |
| 169 | $this->calendar = $model->getCalendar($options); |
| 170 | |
| 171 | // load cart instance |
| 172 | $cart = JModelVAP::getInstance('cart')->getCart(); |
| 173 | |
| 174 | $this->isCartEmpty = $cart->isEmpty(); |
| 175 | |
| 176 | if ($service && $service->checkout_selection) |
| 177 | { |
| 178 | /** |
| 179 | * In case the checkout selection is allowed, we need to include |
| 180 | * the script used to handle the checkin/checkout events. |
| 181 | * |
| 182 | * @since 1.6 |
| 183 | */ |
| 184 | $js = JLayoutHelper::render('javascript.timeline.dropdown'); |
| 185 | $this->document->addScriptDeclaration($js); |
| 186 | } |
| 187 | |
| 188 | // fetch employee details |
| 189 | $this->employee = $employee; |
| 190 | |
| 191 | // define search options |
| 192 | $this->options = $options; |
| 193 | } |
| 194 | |
| 195 | // register service instance |
| 196 | $this->service = $service; |
| 197 | |
| 198 | $this->idService = $id_service; |
| 199 | $this->idEmployee = $id_employee; |
| 200 | $this->date = $date; |
| 201 | $this->month = $month; |
| 202 | $this->hour = $hour; |
| 203 | $this->min = $min; |
| 204 | $this->itemid = $itemid; |
| 205 | |
| 206 | // prepare view contents and microdata |
| 207 | VikAppointments::prepareContent($this); |
| 208 | |
| 209 | // extend pathway for breadcrumbs module |
| 210 | $this->extendPathway($app); |
| 211 | |
| 212 | // display the template |
| 213 | parent::display($tpl); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Finds the specified employee located in the given array. |
| 218 | * |
| 219 | * @param integer $id_employee The employee to search. |
| 220 | * @param array $employees The haystack. |
| 221 | * |
| 222 | * @return mixed The employee array on success, otherwise false. |
| 223 | * |
| 224 | * @since 1.6 |
| 225 | */ |
| 226 | protected function getSelectedEmployee($id_employee, array $employees) |
| 227 | { |
| 228 | foreach ($employees as $e) |
| 229 | { |
| 230 | if ($e->id == $id_employee) |
| 231 | { |
| 232 | return $e; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Extends the pathway for breadcrumbs module. |
| 241 | * |
| 242 | * @param mixed $app The application instance. |
| 243 | * |
| 244 | * @return void |
| 245 | * |
| 246 | * @since 1.6 |
| 247 | */ |
| 248 | protected function extendPathway($app) |
| 249 | { |
| 250 | $pathway = $app->getPathway(); |
| 251 | $items = $pathway->getPathway(); |
| 252 | $last = end($items); |
| 253 | |
| 254 | $name = $this->service->name; |
| 255 | $id = $this->service->id; |
| 256 | |
| 257 | // Make sure this service is not a menu item, otherwise |
| 258 | // the pathway will display something like: |
| 259 | // Home > Menu > Item > Item |
| 260 | if ($last && strpos($last->link, '&id_service=' . $id) === false) |
| 261 | { |
| 262 | // register link into the Breadcrumb |
| 263 | $link = 'index.php?option=com_vikappointments&view=servicesearch&id_service=' . $id; |
| 264 | $pathway->addItem($name, $link); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 |