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