allorders.php
2 years ago
calendarweek.php
1 month ago
cart.php
1 month ago
confirmapp.php
1 month ago
empaccountstat.php
2 years ago
empattachser.php
2 years ago
empcoupons.php
2 years ago
empcustfields.php
2 years ago
empeditcoupon.php
3 years ago
empeditcustfield.php
3 years ago
empeditlocation.php
3 years ago
empeditpay.php
3 years ago
empeditprofile.php
4 months ago
empeditservice.php
3 years ago
empeditwdays.php
3 years ago
emplocations.php
2 years ago
emplocwdays.php
4 years ago
emplogin.php
1 month ago
employeesearch.php
1 month ago
employeeslist.php
1 month ago
empmanres.php
2 years ago
emppaylist.php
2 years ago
empserviceslist.php
2 years ago
empsettingsman.php
4 years ago
empsubscrcart.php
4 years ago
empsubscrhistory.php
2 years ago
empsubscrorder.php
4 months ago
empwdays.php
2 years ago
index.html
4 years ago
packages.php
2 years ago
packagescart.php
4 years ago
packagesconfirm.php
4 months ago
packorders.php
2 years ago
servicesearch.php
2 years ago
serviceslist.php
2 years ago
subscrcart.php
2 years ago
subscrhistory.php
2 years ago
subscrpayment.php
4 months ago
empattachser.php
213 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.model'); |
| 15 | |
| 16 | /** |
| 17 | * VikAppointments employee area attach services view model. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | class VikAppointmentsModelEmpattachser extends JModelVAP |
| 22 | { |
| 23 | /** |
| 24 | * Basic save implementation. |
| 25 | * |
| 26 | * @param mixed $data Either an array or an object of data to save. |
| 27 | * |
| 28 | * @return mixed The ID of the record on success, false otherwise. |
| 29 | */ |
| 30 | public function save($data) |
| 31 | { |
| 32 | $auth = VAPEmployeeAuth::getInstance(); |
| 33 | |
| 34 | if (!$auth->isEmployee()) |
| 35 | { |
| 36 | // raise error in case of no employee |
| 37 | throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403); |
| 38 | } |
| 39 | |
| 40 | $data = (array) $data; |
| 41 | |
| 42 | // extract selected services |
| 43 | $services = isset($data['services']) ? (array) $data['services'] : array(); |
| 44 | |
| 45 | // take only the services that can be attached to this employee |
| 46 | $services = array_intersect($services, $this->getAttachableItems()); |
| 47 | |
| 48 | // get helper models |
| 49 | $serviceModel = JModelVAP::getInstance('service'); |
| 50 | $assocModel = JModelVAP::getInstance('serempassoc'); |
| 51 | |
| 52 | $count = 0; |
| 53 | |
| 54 | foreach ($services as $id) |
| 55 | { |
| 56 | // load service details through model |
| 57 | $item = $serviceModel->getItem((int) $id); |
| 58 | |
| 59 | if (!$item) |
| 60 | { |
| 61 | // item not found, go ahead |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | // inject relation details |
| 66 | $item->id_employee = $auth->id; |
| 67 | $item->id_service = $item->id; |
| 68 | |
| 69 | // unset item PK |
| 70 | $item->id = 0; |
| 71 | |
| 72 | // clear description |
| 73 | $item->description = ''; |
| 74 | |
| 75 | // use global rates |
| 76 | $item->global = 1; |
| 77 | |
| 78 | // attempt to assign the service to the employee |
| 79 | if ($assocModel->save($item)) |
| 80 | { |
| 81 | // success, increase counter |
| 82 | $count++; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | // get registered error |
| 87 | $error = $assocModel->getError(); |
| 88 | |
| 89 | // propagate error, if any |
| 90 | if ($error) |
| 91 | { |
| 92 | $this->setError($error); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return $count; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Loads a list of services to be displayed within the |
| 102 | * employees area view. |
| 103 | * |
| 104 | * @return array A list of services. |
| 105 | */ |
| 106 | public function getItems() |
| 107 | { |
| 108 | $auth = VAPEmployeeAuth::getInstance(); |
| 109 | |
| 110 | if (!$auth->isEmployee()) |
| 111 | { |
| 112 | // raise error in case of no employee |
| 113 | throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403); |
| 114 | } |
| 115 | |
| 116 | $dbo = JFactory::getDbo(); |
| 117 | |
| 118 | // get all services assigned to the current employee |
| 119 | $assigned = JModelVAP::getInstance('employee')->getServices($auth->id, $strict = false); |
| 120 | |
| 121 | // map to take only the ID of the services |
| 122 | $assigned = array_map(function($elem) |
| 123 | { |
| 124 | return $elem->id; |
| 125 | }, $assigned); |
| 126 | |
| 127 | $no_group = JText::translate('VAPSERVICENOGROUP'); |
| 128 | |
| 129 | // load all the existing services |
| 130 | $services = array(); |
| 131 | |
| 132 | $q = $dbo->getQuery(true) |
| 133 | ->select(array( |
| 134 | $dbo->qn('s.id'), |
| 135 | $dbo->qn('s.name'), |
| 136 | $dbo->qn('s.id_group'), |
| 137 | $dbo->qn('g.name', 'group_name'), |
| 138 | )) |
| 139 | ->from($dbo->qn('#__vikappointments_service', 's')) |
| 140 | ->leftjoin($dbo->qn('#__vikappointments_group', 'g') . ' ON ' . $dbo->qn('s.id_group') . ' = ' . $dbo->qn('g.id')) |
| 141 | ->where(array( |
| 142 | $dbo->qn('s.published') . ' = 1', |
| 143 | $dbo->qn('s.createdby') . ' <= 0', |
| 144 | )) |
| 145 | ->order(array( |
| 146 | $dbo->qn('g.name') . ' ASC', |
| 147 | $dbo->qn('s.name') . ' ASC', |
| 148 | )); |
| 149 | |
| 150 | $dbo->setQuery($q); |
| 151 | |
| 152 | foreach ($dbo->loadObjectList() as $row) |
| 153 | { |
| 154 | $group = $row->id_group ? $row->group_name : $no_group; |
| 155 | |
| 156 | // group by category |
| 157 | if (!isset($services[$group])) |
| 158 | { |
| 159 | $services[$group] = array(); |
| 160 | } |
| 161 | |
| 162 | // create option |
| 163 | $opt = JHtml::fetch('select.option', $row->id, $row->name); |
| 164 | |
| 165 | if (in_array($row->id, $assigned)) |
| 166 | { |
| 167 | // disable option, since the service is already assigned to this employee |
| 168 | $opt->disable = true; |
| 169 | } |
| 170 | |
| 171 | // append option |
| 172 | $services[$group][] = $opt; |
| 173 | } |
| 174 | |
| 175 | if (isset($services[$no_group])) |
| 176 | { |
| 177 | // always move services without group at the end of the list |
| 178 | $tmp = $services[$no_group]; |
| 179 | unset($services[$no_group]); |
| 180 | $services[$no_group] = $tmp; |
| 181 | } |
| 182 | |
| 183 | return $services; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns an array of services that can be attached. |
| 188 | * |
| 189 | * @return array A list of services. |
| 190 | */ |
| 191 | public function getAttachableItems() |
| 192 | { |
| 193 | $list = array(); |
| 194 | |
| 195 | // iterate groups |
| 196 | foreach ($this->getItems() as $services) |
| 197 | { |
| 198 | // iterate group services |
| 199 | foreach ($services as $opt) |
| 200 | { |
| 201 | // make sure the service is not disabled |
| 202 | if (empty($opt->disable)) |
| 203 | { |
| 204 | // register service ID |
| 205 | $list[] = $opt->value; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return $list; |
| 211 | } |
| 212 | } |
| 213 |