allorders.php
1 day ago
calendarweek.php
1 day ago
cart.php
1 day ago
confirmapp.php
1 day ago
empaccountstat.php
1 day ago
empattachser.php
1 day ago
empcoupons.php
1 day ago
empcustfields.php
1 day ago
empeditcoupon.php
1 day ago
empeditcustfield.php
1 day ago
empeditlocation.php
1 day ago
empeditpay.php
1 day ago
empeditprofile.php
1 day ago
empeditservice.php
1 day ago
empeditwdays.php
1 day ago
emplocations.php
1 day ago
emplocwdays.php
1 day ago
emplogin.php
1 day ago
employeesearch.php
1 day ago
employeeslist.php
1 day ago
empmanres.php
1 day ago
emppaylist.php
1 day ago
empserviceslist.php
1 day ago
empsettingsman.php
1 day ago
empsubscrcart.php
1 day ago
empsubscrhistory.php
1 day ago
empsubscrorder.php
1 day ago
empwdays.php
1 day ago
index.html
1 day ago
packages.php
1 day ago
packagescart.php
1 day ago
packagesconfirm.php
1 day ago
packorders.php
1 day ago
servicesearch.php
1 day ago
serviceslist.php
1 day ago
subscrcart.php
1 day ago
subscrhistory.php
1 day ago
subscrpayment.php
1 day ago
empeditprofile.php
238 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 profile management model. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | class VikAppointmentsModelEmpeditprofile 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 | $dbo = JFactory::getDbo(); |
| 33 | |
| 34 | $auth = VAPEmployeeAuth::getInstance(); |
| 35 | |
| 36 | $data['id'] = $auth->id; |
| 37 | |
| 38 | JFactory::getApplication()->setUserState('vap.emparea.profile.data', $data); |
| 39 | |
| 40 | // extend fields validation |
| 41 | $required = array( |
| 42 | 'firstname' => JText::translate('VAPMANAGEEMPLOYEE2'), |
| 43 | 'lastname' => JText::translate('VAPMANAGEEMPLOYEE3'), |
| 44 | 'nickname' => JText::translate('VAPMANAGEEMPLOYEE4'), |
| 45 | 'email' => JText::translate('VAPMANAGEEMPLOYEE8'), |
| 46 | ); |
| 47 | |
| 48 | foreach ($required as $key => $fieldName) |
| 49 | { |
| 50 | if (empty($data[$key])) |
| 51 | { |
| 52 | // register error message |
| 53 | $this->setError(JText::sprintf('VAP_MISSING_REQ_FIELD', $fieldName)); |
| 54 | |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // import custom fields requestor and loader (as dependency) |
| 60 | VAPLoader::import('libraries.customfields.requestor'); |
| 61 | |
| 62 | // get relevant custom fields only |
| 63 | $_cf = VAPCustomFieldsLoader::getInstance() |
| 64 | ->employees() |
| 65 | ->noRequiredCheckbox() |
| 66 | ->fetch(); |
| 67 | |
| 68 | try |
| 69 | { |
| 70 | // load custom fields from request |
| 71 | $cust_req = VAPCustomFieldsRequestor::loadForm($_cf, $tmp, $strict = true); |
| 72 | } |
| 73 | catch (Exception $e) |
| 74 | { |
| 75 | // we probably have a missing field |
| 76 | $this->setError($e); |
| 77 | |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | if (!empty($tmp['uploads'])) |
| 82 | { |
| 83 | // inject uploads within the custom fields array |
| 84 | $cust_req = array_merge($cust_req, $tmp['uploads']); |
| 85 | } |
| 86 | |
| 87 | // inject custom fields within the employee table |
| 88 | foreach ($cust_req as $k => $v) |
| 89 | { |
| 90 | $data['field_' . $k] = $v; |
| 91 | } |
| 92 | |
| 93 | if (!empty($data['id_group'])) |
| 94 | { |
| 95 | // make sure the group exists |
| 96 | $q = $dbo->getQuery(true) |
| 97 | ->select(1) |
| 98 | ->from($dbo->qn('#__vikappointments_employee_group')) |
| 99 | ->where($dbo->qn('id') . ' = ' . (int) $data['id_group']); |
| 100 | |
| 101 | $dbo->setQuery($q, 0, 1); |
| 102 | $dbo->execute(); |
| 103 | |
| 104 | if (!$dbo->getNumRows()) |
| 105 | { |
| 106 | $data['id_group'] = 0; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // get media model |
| 111 | $media = JModelVAP::getInstance('media'); |
| 112 | |
| 113 | $image = $oldImage = null; |
| 114 | |
| 115 | // attempt to upload the new specified file |
| 116 | if ($media->save(['file' => 'image'])) |
| 117 | { |
| 118 | // register previous image |
| 119 | $oldImage = $data['image']; |
| 120 | |
| 121 | // get saved image data |
| 122 | $image = $media->getData(); |
| 123 | // replace image file |
| 124 | $data['image'] = $image['id']; |
| 125 | } |
| 126 | |
| 127 | $employeeModel = JModelVAP::getInstance('employee'); |
| 128 | |
| 129 | // delegate save to employee model |
| 130 | $result = $employeeModel->save($data); |
| 131 | |
| 132 | if (!$result) |
| 133 | { |
| 134 | // obtain error from model |
| 135 | $error = $employeeModel->getError(); |
| 136 | |
| 137 | if ($error) |
| 138 | { |
| 139 | // propagate error |
| 140 | $this->setError($error); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (!$result && $image) |
| 145 | { |
| 146 | // delete newly uploaded file in case of error |
| 147 | $media->delete($image['id']); |
| 148 | } |
| 149 | |
| 150 | if ($result && $oldImage) |
| 151 | { |
| 152 | // delete previous image on success |
| 153 | $media->delete($oldImage); |
| 154 | } |
| 155 | |
| 156 | return $result; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Custom fields save implementation. |
| 161 | * |
| 162 | * @param mixed $data Either an array or an object of data to save. |
| 163 | * |
| 164 | * @return mixed The ID of the record on success, false otherwise. |
| 165 | * |
| 166 | * @since 1.7.8 |
| 167 | */ |
| 168 | public function saveFields() |
| 169 | { |
| 170 | $dbo = JFactory::getDbo(); |
| 171 | |
| 172 | $auth = VAPEmployeeAuth::getInstance(); |
| 173 | |
| 174 | $data = []; |
| 175 | $data['id'] = $auth->id; |
| 176 | |
| 177 | JFactory::getApplication()->setUserState('vap.emparea.profile.data', $data); |
| 178 | |
| 179 | // import custom fields requestor and loader (as dependency) |
| 180 | VAPLoader::import('libraries.customfields.requestor'); |
| 181 | |
| 182 | // get relevant custom fields only |
| 183 | $_cf = VAPCustomFieldsLoader::getInstance() |
| 184 | ->employees() |
| 185 | ->noRequiredCheckbox() |
| 186 | ->fetch(); |
| 187 | |
| 188 | try |
| 189 | { |
| 190 | // load custom fields from request |
| 191 | $cust_req = VAPCustomFieldsRequestor::loadForm($_cf, $tmp, $strict = false); |
| 192 | } |
| 193 | catch (Exception $e) |
| 194 | { |
| 195 | // we probably have a missing field |
| 196 | $this->setError($e); |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | if (!empty($tmp['uploads'])) |
| 201 | { |
| 202 | // inject uploads within the custom fields array |
| 203 | $cust_req = array_merge($cust_req, $tmp['uploads']); |
| 204 | } |
| 205 | |
| 206 | // inject custom fields within the employee table |
| 207 | foreach ($cust_req as $k => $v) |
| 208 | { |
| 209 | if ($v === '' || $v === null) |
| 210 | { |
| 211 | // ignore in case the value is missing |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | $data['field_' . $k] = $v; |
| 216 | } |
| 217 | |
| 218 | $employeeModel = JModelVAP::getInstance('employee'); |
| 219 | |
| 220 | // delegate save to employee model |
| 221 | $result = $employeeModel->save($data); |
| 222 | |
| 223 | if (!$result) |
| 224 | { |
| 225 | // obtain error from model |
| 226 | $error = $employeeModel->getError(); |
| 227 | |
| 228 | if ($error) |
| 229 | { |
| 230 | // propagate error |
| 231 | $this->setError($error); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return $result; |
| 236 | } |
| 237 | } |
| 238 |