PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / models / serempassoc.php
vikappointments / admin / models Last commit date
apiban.php 4 years ago apilog.php 2 years ago apiplugin.php 4 years ago apiuser.php 2 years ago apiuseroptions.php 2 years ago backup.php 5 months ago caldays.php 1 month ago calendar.php 1 month ago city.php 4 years ago closure.php 1 month ago configapp.php 4 years ago configcldays.php 4 years ago configcron.php 4 years ago configemp.php 4 years ago configsmsapi.php 4 years ago configuration.php 5 months ago conversion.php 4 years ago country.php 2 years ago coupon.php 2 years ago couponemployee.php 4 years ago coupongroup.php 2 years ago couponservice.php 4 years ago cronjob.php 2 years ago cronjoblog.php 4 years ago customer.php 5 months ago customf.php 2 years ago customfservice.php 4 years ago customizer.php 4 years ago empgroup.php 2 years ago employee.php 2 years ago empsettings.php 4 years ago file.php 4 years ago findreservation.php 1 month ago group.php 2 years ago import.php 4 years ago index.html 4 years ago invoice.php 1 month ago langcustomf.php 4 years ago langempgroup.php 4 years ago langemployee.php 4 years ago langgroup.php 4 years ago langmedia.php 4 years ago langoption.php 2 years ago langoptiongroup.php 4 years ago langoptionvar.php 4 years ago langpackage.php 4 years ago langpackgroup.php 4 years ago langpayment.php 4 years ago langservice.php 4 years ago langstatuscode.php 4 years ago langsubscr.php 4 years ago langtax.php 2 years ago langtaxrule.php 4 years ago location.php 2 years ago mailtext.php 2 years ago makerecurrence.php 1 month ago media.php 2 years ago multiorder.php 1 month ago option.php 1 year ago optiongroup.php 2 years ago optionvar.php 1 year ago orderstatus.php 2 years ago package.php 2 years ago packageservice.php 4 years ago packgroup.php 2 years ago packorder.php 2 years ago packorderitem.php 1 month ago payment.php 2 years ago rate.php 2 years ago reportsemp.php 5 months ago reportsser.php 5 months ago reservation.php 1 month ago resoptassoc.php 2 years ago restriction.php 2 years ago review.php 3 years ago serempassoc.php 1 year ago seroptassoc.php 4 years ago serrateassoc.php 4 years ago serrestrassoc.php 4 years ago service.php 2 years ago state.php 2 years ago statswidget.php 2 years ago statuscode.php 2 years ago subscription.php 1 month ago subscrorder.php 1 month ago tag.php 4 years ago tax.php 2 years ago taxrule.php 2 years ago updateprogram.php 4 years ago usernote.php 2 years ago waitinglist.php 1 month ago webhook.php 1 year ago worktime.php 1 month ago
serempassoc.php
263 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 service-employee relation model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelSerempassoc extends JModelVAP
22 {
23 /**
24 * Cache of service-employee overrides.
25 *
26 * @var array
27 */
28 public static $overrides = array();
29
30 /**
31 * Basic save implementation.
32 *
33 * @param mixed $data Either an array or an object of data to save.
34 *
35 * @return mixed The ID of the record on success, false otherwise.
36 */
37 public function save($data)
38 {
39 $data = (array) $data;
40
41 // check whether we are creating or updating
42 $is_new = empty($data['id']);
43
44 // check whether a relation between the specified service and employee already exists
45 if ($is_new && !empty($data['id_employee']) && !empty($data['id_service']))
46 {
47 $exists = $this->getItem(array(
48 'id_service' => $data['id_service'],
49 'id_employee' => $data['id_employee']
50 ));
51
52 if ($exists)
53 {
54 // update ID with the existing one
55 $data['id'] = $exists->id;
56 $is_new = false;
57 }
58 }
59
60 // attempt to save the relation
61 $id = parent::save($data);
62
63 if (!$id)
64 {
65 // an error occurred, do not go ahead
66 return false;
67 }
68
69 if ($is_new && isset($data['id_employee']) && isset($data['id_service']))
70 {
71 $dbo = JFactory::getDbo();
72
73 // load default working days of the employee
74 $q = $dbo->getQuery(true)
75 ->select('*')
76 ->from($dbo->qn('#__vikappointments_emp_worktime'))
77 ->where($dbo->qn('id_service') . ' <= 0')
78 ->where($dbo->qn('id_employee') . ' = ' . (int) $data['id_employee']);
79
80 $dbo->setQuery($q);
81 $worktimes = $dbo->loadObjectList();
82
83 // get working times model
84 $wdModel = JModelVAP::getInstance('worktime');
85
86 foreach ($worktimes as $wd)
87 {
88 // make relation with parent working time
89 $wd->parent = $wd->id;
90 // make relation with service
91 $wd->id_service = $data['id_service'];
92 // unset ID to create new record
93 $wd->id = 0;
94
95 $wdModel->save($wd);
96 }
97 }
98
99 return $id;
100 }
101
102 /**
103 * Extend delete implementation to delete any related records
104 * stored within a separated table.
105 *
106 * @param mixed $ids Either the record ID or a list of records.
107 *
108 * @return boolean True on success, false otherwise.
109 */
110 public function delete($ids)
111 {
112 // only int values are accepted
113 $ids = array_map('intval', (array) $ids);
114
115 if (!$ids)
116 {
117 return false;
118 }
119
120 $dbo = JFactory::getDbo();
121
122 // load any assigned employees first
123 $q = $dbo->getQuery(true)
124 ->select($dbo->qn(array('id_service', 'id_employee')))
125 ->from($dbo->qn('#__vikappointments_ser_emp_assoc'))
126 ->where($dbo->qn('id') . ' IN (' . implode(',', $ids) . ')' )
127 ->where($dbo->qn('id_service') . ' > 0')
128 ->where($dbo->qn('id_employee') . ' > 0');
129
130 $dbo->setQuery($q);
131 $ser_emp_assoc = $dbo->loadObjectList();
132
133 // invoke parent first
134 if (!parent::delete($ids))
135 {
136 // nothing to delete
137 return false;
138 }
139
140 // get working times model
141 $wdModel = JModelVAP::getInstance('worktime');
142
143 foreach ($ser_emp_assoc as $assoc)
144 {
145 // load any assigned working times
146 $q = $dbo->getQuery(true)
147 ->select($dbo->qn('id'))
148 ->from($dbo->qn('#__vikappointments_emp_worktime'))
149 ->where($dbo->qn('id_employee') . ' = ' . $assoc->id_employee)
150 ->where($dbo->qn('id_service') . ' = ' . $assoc->id_service);
151
152 $dbo->setQuery($q);
153
154 if ($worktime_ids = $dbo->loadColumn())
155 {
156 // delete assigned working times
157 $wdModel->delete($worktime_ids);
158 }
159 }
160
161 return true;
162 }
163
164 /**
165 * Basic item loading implementation.
166 *
167 * @param integer $id_ser The service ID.
168 * @param integer $id_emp The employee ID.
169 *
170 * @return mixed The record details on success, null otherwise.
171 */
172 public function getOverrides($id_ser, $id_emp = 0)
173 {
174 // prepare primary keys for search
175 $pk = array(
176 'id_service' => (int) $id_ser,
177 'id_employee' => (int) $id_emp,
178 );
179
180 $sign = serialize($pk);
181
182 // search for a cached override first
183 if (array_key_exists($sign, static::$overrides))
184 {
185 // return cached version
186 return static::$overrides[$sign];
187 }
188
189 // get service model
190 $serModel = JModelVAP::getInstance('service');
191
192 // get service details
193 $service = $serModel->getItem($id_ser);
194
195 if (!$service)
196 {
197 // service not found
198 return null;
199 }
200
201 if ($id_emp <= 0)
202 {
203 // employee not specified, return service details.
204 return $service;
205 }
206
207 // load matching overrides
208 $assoc = parent::getItem($pk);
209
210 if (!$assoc)
211 {
212 // assoc not found
213 return null;
214 }
215
216 // check whether the relation should rely on the
217 // global parameters given by the service
218 if (!$assoc->global)
219 {
220 // copy service details by iterating the properties set within
221 // the association table, so that we can copy third-party columns
222 // too (as long as they share the same name)
223 foreach ($assoc as $k => $v)
224 {
225 if (preg_match("/^id(_$)?/", $k) || $k == 'ordering')
226 {
227 // ignore primary key, foreign keys, and ordering,
228 // which are always appended to the default one
229 continue;
230 }
231
232 // rate is the only property that do not use the same name and
233 // needs to be treated in a different way
234 if ($k == 'rate')
235 {
236 $service->price = $assoc->rate;
237 }
238 // the description doesn't overwrite the default one
239 else if ($k == 'description')
240 {
241 // append description to default one
242 $service->description .= "\n" . $v;
243 // and register the override description also in a
244 // different property, because the default one may
245 // be replaced by the translation
246 $service->overrideDescription = $v;
247 }
248 // make sure the property exists
249 else if (isset($service->{$k}))
250 {
251 // copy within assoc
252 $service->{$k} = $v;
253 }
254 }
255 }
256
257 // cache result for later usage
258 static::$overrides[$sign] = $service;
259
260 return $service;
261 }
262 }
263