PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / models / empeditwdays.php
vikappointments / site / models Last commit date
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
empeditwdays.php
254 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 working days management model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelEmpeditwdays 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 // set user state for being recovered again
35 JFactory::getApplication()->setUserState('vap.emparea.worktime.data', $data);
36
37 // extend fields validation
38 $required = array();
39
40 if ($data['type'] == 1)
41 {
42 $required['day'] = JText::translate('VAPMANAGEWD3');
43 }
44 else
45 {
46 $required['date'] = JText::translate('VAPMANAGEWD3');
47 }
48
49 foreach ($required as $key => $fieldName)
50 {
51 if (!isset($data[$key]) || strlen($data[$key]) == 0)
52 {
53 // register error message
54 $this->setError(JText::sprintf('VAP_MISSING_REQ_FIELD', $fieldName));
55
56 return false;
57 }
58 }
59
60 // validation
61 if ($data['day'] < 0 || $data['day'] > 6)
62 {
63 $data['day'] = 0;
64 }
65
66 if ($data['fromts'] >= $data['endts'] || $data['fromts'] < 0 || $data['endts'] > 1440 || $data['fromts'] % 5 || $data['endts'] % 5)
67 {
68 // invalid times, use default ones
69 $data['fromts'] = 540;
70 $data['endts'] = 720;
71 }
72
73 // force employee ID
74 $data['id_employee'] = $auth->id;
75
76 if (!empty($data['id_service']))
77 {
78 // always unset parent, because we don't want to
79 // keep a relation with the original working day
80 $data['parent'] = -1;
81 }
82
83 $model = JModelVAP::getInstance('worktime');
84
85 // delegate save to worktime model
86 $result = $model->save($data);
87
88 if (!$result)
89 {
90 // obtain error from model
91 $error = $model->getError();
92
93 if ($error)
94 {
95 // propagate error
96 $this->setError($error);
97 }
98
99 return false;
100 }
101
102 $saved = $model->getData();
103
104 if (!VAPDateHelper::isNull($saved['tsdate']) && !VAPDateHelper::isNull($data['date_to']))
105 {
106 // create date seek
107 $seek = JFactory::getDate($saved['tsdate']);
108
109 // convert the ending date to a standard format
110 $ts = VikAppointments::createTimestamp($data['date_to'], 0, 0);
111 $end = JFactory::getDate(date('Y-m-d', $ts))->format('Y-m-d');
112
113 // iterate until with reach the ending date
114 while ($seek->format('Y-m-d') < $end)
115 {
116 // increase by one day
117 $seek->modify('+1 day');
118
119 // update bind data
120 $data['id'] = 0;
121 $data['date'] = $seek->format('Y-m-d');
122
123 // invoke model to save the working day
124 $model->save($data);
125 }
126 }
127
128 return $result;
129 }
130
131 /**
132 * Extend duplicate implementation to clone any related records
133 * stored within a separated table.
134 *
135 * @param mixed $ids Either the record ID or a list of records.
136 * @param mixed $src Specifies some values to be used while duplicating.
137 * @param array $ignore A list of columns to skip.
138 *
139 * @return mixed The ID of the records on success, false otherwise.
140 */
141 public function duplicate($ids, $src = array(), $ignore = array())
142 {
143 $auth = VAPEmployeeAuth::getInstance();
144
145 if (!$auth->isEmployee() || !$auth->manageWorkDays())
146 {
147 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
148 }
149
150 // only int values are accepted
151 $ids = array_map('intval', (array) $ids);
152
153 $new_ids = array();
154
155 // delegate duplicate to worktime model
156 $model = JModelVAP::getInstance('worktime');
157
158 foreach ($ids as $id_worktime)
159 {
160 // load details of working day
161 $item = $model->getItem(['id' => $id_worktime, 'id_employee' => $auth->id]);
162
163 if (!$item)
164 {
165 // record not found
166 continue;
167 }
168
169 $bindSrc = $src;
170
171 if ($item->ts == -1)
172 {
173 // go to next week day
174 $bindSrc['day'] = ($item->day + 1) % 7;
175 }
176 else
177 {
178 // go to next day of the year
179 $dt = JFactory::getDate($item->tsdate);
180 $dt->modify('+1 day');
181
182 $bindSrc['ts'] = $dt->getTimestamp();
183 }
184
185 // duplicate record
186 $new_id = $model->duplicate($ids, $bindSrc, $ignore);
187
188 if ($new_id)
189 {
190 $new_ids[] = $new_id;
191 }
192 }
193
194 return $new_ids;
195 }
196
197 /**
198 * Extend delete implementation to delete any related records
199 * stored within a separated table.
200 *
201 * @param mixed $ids Either the record ID or a list of records.
202 *
203 * @return boolean True on success, false otherwise.
204 */
205 public function delete($ids)
206 {
207 $auth = VAPEmployeeAuth::getInstance();
208
209 if (!$auth->isEmployee())
210 {
211 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
212 }
213
214 // only int values are accepted
215 $ids = array_map('intval', (array) $ids);
216
217 // get rid of those records that do not belong to this employee
218 $ids = array_values(array_filter($ids, function($id) use ($auth)
219 {
220 // make sure the employee can manage this record
221 return $auth->manageWorkDays($id);
222 }));
223
224 if (!$ids)
225 {
226 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
227 }
228
229 // delegate delete to worktime model
230 return JModelVAP::getInstance('worktime')->delete($ids);
231 }
232
233 /**
234 * Restores the working days for the given service and
235 * employee relation.
236 *
237 * @param integer $id_service The service ID.
238 *
239 * @return boolean True on success, false otherwise.
240 */
241 public function restore($id_service)
242 {
243 $auth = VAPEmployeeAuth::getInstance();
244
245 if (!$auth->manageWorkDays() || !$auth->manageServices($id_service, $readOnly = true))
246 {
247 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
248 }
249
250 // delegate restore to worktime model
251 return JModelVAP::getInstance('worktime')->restore($id_service, $auth->id);
252 }
253 }
254