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 / helpers / libraries / models / orderstatus.php
vikappointments / site / helpers / libraries / models Last commit date
conversion.php 3 days ago customer.php 3 days ago customfields.php 3 days ago index.html 3 days ago locations.php 3 days ago orderstatus.php 3 days ago restrictions.php 3 days ago specialrates.php 3 days ago statistics.php 3 days ago subscriptions.php 3 days ago
orderstatus.php
431 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 order status class handler.
16 *
17 * @since 1.6
18 */
19 class VAPOrderStatus
20 {
21 /**
22 * A list of instances.
23 *
24 * @var array
25 */
26 protected static $instances = array();
27
28 /**
29 * The database table.
30 *
31 * @var string
32 */
33 protected $table;
34
35 /**
36 * The table primary key.
37 *
38 * @var string
39 */
40 protected $pk;
41
42 /**
43 * The table status column.
44 *
45 * @var string
46 */
47 protected $statusColumn;
48
49 /**
50 * Returns a new instance of this object, only creating it
51 * if it doesn't already exist.
52 *
53 * @param mixed $options The database table or an array of options.
54 *
55 * @return self A new instance of this object.
56 *
57 * @see __construct() for further details about the $options array.
58 */
59 public static function getInstance($options = null)
60 {
61 $sign = serialize($options);
62
63 if (!isset(static::$instances[$sign]))
64 {
65 static::$instances[$sign] = new static($options);
66 }
67
68 return static::$instances[$sign];
69 }
70
71 /**
72 * Class constructor.
73 *
74 * @param mixed $options The database table or an array of options.
75 * The options array can contain the values below
76 * - table the database table name ("reservations" by default).
77 * Since we are using a class of VikAppointments,
78 * the prefix "#__vikappointments_" must be omitted;
79 * - pk the primary key of the table ("id" by default);
80 * - statuscol the status column name ("status" by default).
81 */
82 public function __construct($options = null)
83 {
84 if (!is_array($options))
85 {
86 // string given, create an array of options
87 $options = array('table' => $options);
88 }
89
90 if (empty($options['table']))
91 {
92 // the table attribute is empty, use the default table
93 $options['table'] = '#__vikappointments_reservation';
94 }
95 else
96 {
97 // prepend the table prefix to the existing value
98 $options['table'] = '#__vikappointments_' . $options['table'];
99 }
100
101 if (empty($options['pk']))
102 {
103 // the primary key is empty, use the default one (id)
104 $options['pk'] = 'id';
105 }
106
107 if (empty($options['statuscol']))
108 {
109 // the status column is empty, use the default one (status)
110 $options['statuscol'] = 'status';
111 }
112
113 $this->table = $options['table'];
114 $this->pk = $options['pk'];
115 $this->statusColumn = $options['statuscol'];
116 }
117
118 /**
119 * Method used to change the status of an order.
120 *
121 * @param string $status The new status of the order.
122 * @param integer $id The order ID.
123 * @param mixed $track Used to track the status change or not.
124 * - false the order status won't be tracked;
125 * - true the order status will be tracked;
126 * - string the order status will be tracked by
127 * registering the given string as comment.
128 *
129 * @return boolean True on success, otherwise false.
130 *
131 * @uses keepTrack()
132 */
133 public function change($status, $id, $track = false)
134 {
135 // fetch status group from internal type
136 switch ($this->getType())
137 {
138 case 'reservation':
139 $group = 'appointments';
140 break;
141
142 case 'package_order':
143 $group = 'packages';
144 break;
145
146 case 'subscr_order':
147 $group = 'subscriptions';
148 break;
149 }
150
151 // load matching status code
152 $status = JHtml::fetch('vaphtml.status.' . strtolower($status), $group, 'code');
153
154 $dbo = JFactory::getDbo();
155
156 $q = $dbo->getQuery(true)
157 ->update($dbo->qn($this->table))
158 ->set($dbo->qn($this->statusColumn) . ' = ' . $dbo->q(strtoupper($status)))
159 ->where($dbo->qn($this->pk) . ' = ' . $dbo->q($id));
160
161 $dbo->setQuery($q);
162 $dbo->execute();
163
164 $res = (bool) $dbo->getAffectedRows();
165
166 // If there is no affected rows, we don't need to track anything
167 // because the query hasn't altered any record.
168 if ($track && $res)
169 {
170 // get the comment if specified
171 $comment = is_string($track) ? $track : '';
172
173 // track the status change
174 $res = $this->keepTrack($status, $id, $comment);
175 }
176
177 return $res;
178 }
179
180 /**
181 * Method used to change the status of an order to CONFIRMED.
182 *
183 * @param integer $id The order ID.
184 * @param mixed $track Used to track the status change or not.
185 *
186 * @return boolean True on success, otherwise false.
187 *
188 * @uses change()
189 */
190 public function confirm($id, $track = false)
191 {
192 return $this->change('CONFIRMED', $id, $track);
193 }
194
195 /**
196 * Method used to change the status of an order to PENDING.
197 *
198 * @param integer $id The order ID.
199 * @param mixed $track Used to track the status change or not.
200 *
201 * @return boolean True on success, otherwise false.
202 *
203 * @uses change()
204 */
205 public function pendent($id, $track = false)
206 {
207 return $this->change('PENDING', $id, $track);
208 }
209
210 /**
211 * Method used to change the status of an order to REMOVED.
212 *
213 * @param integer $id The order ID.
214 * @param mixed $track Used to track the status change or not.
215 *
216 * @return boolean True on success, otherwise false.
217 *
218 * @uses change()
219 */
220 public function remove($id, $track = false)
221 {
222 return $this->change('REMOVED', $id, $track);
223 }
224
225 /**
226 * Method used to change the status of an order to CANCELED.
227 *
228 * @param integer $id The order ID.
229 * @param mixed $track Used to track the status change or not.
230 *
231 * @return boolean True on success, otherwise false.
232 *
233 * @uses change()
234 */
235 public function cancel($id, $track = false)
236 {
237 return $this->change('CANCELLED', $id, $track);
238 }
239
240 /**
241 * Method used to change the status of an order to PAID.
242 *
243 * @param integer $id The order ID.
244 * @param mixed $track Used to track the status change or not.
245 *
246 * @return boolean True on success, otherwise false.
247 *
248 * @since 1.7
249 *
250 * @uses change()
251 */
252 public function paid($id, $track = false)
253 {
254 return $this->change('PAID', $id, $track);
255 }
256
257 /**
258 * Method used to change the status of an order to REFUNDED.
259 *
260 * @param integer $id The order ID.
261 * @param mixed $track Used to track the status change or not.
262 *
263 * @return boolean True on success, otherwise false.
264 *
265 * @since 1.7
266 *
267 * @uses change()
268 */
269 public function refunded($id, $track = false)
270 {
271 return $this->change('REFUNDED', $id, $track);
272 }
273
274 /**
275 * Method used to keep track of the status changes.
276 * Every time the status of an order changes, this method
277 * should be invoked to keep an history log of the order.
278 *
279 * @param string $status The new status of the order.
280 * @param mixed $id The order ID or an array of IDs.
281 * @param string $comment An optional comment to detect the event
282 * the triggered the status change.
283 *
284 * @return boolean True on success, false otherwise.
285 *
286 * @uses getType()
287 */
288 public function keepTrack($status, $id, $comment = '')
289 {
290 $ids = (array) $id;
291
292 // prepare save data
293 $data = array(
294 'status' => strtoupper($status),
295 'comment' => (string) $comment,
296 'type' => $this->getType(),
297 );
298
299 $saved = false;
300
301 // get order status model
302 $model = JModelVAP::getInstance('orderstatus');
303
304 // iterate all specified IDs
305 foreach ($ids as $id)
306 {
307 // save status for current order
308 $data['id_order'] = (int) $id;
309
310 // save order status
311 $saved = $model->save($data) || $saved;
312 }
313
314 return $saved;
315 }
316
317 /**
318 * Returns the list of all the status changes that
319 * have been tracked for the specified order ID.
320 *
321 * @param mixed $id The order ID or an array of IDs.
322 * @param boolean $locale True to translate the records, false otherwise.
323 *
324 * @return array The track list.
325 *
326 * @uses getType()
327 */
328 public function getOrderTrack($id, $locale = false)
329 {
330 $dbo = JFactory::getDbo();
331
332 $q = $dbo->getQuery(true)
333 ->select('`o`.*')
334 ->select($dbo->qn(array('u.name', 'u.username')))
335 ->from($dbo->qn('#__vikappointments_order_status', 'o'))
336 ->leftjoin($dbo->qn('#__users', 'u') . ' ON ' . $dbo->qn('u.id') . ' = ' . $dbo->qn('o.createdby'))
337 ->where($dbo->qn('o.type') . ' = ' . $dbo->q($this->getType()))
338 ->order($dbo->qn('o.id') . ' ASC');
339
340 if (is_scalar($id) || !$id)
341 {
342 $q->where($dbo->qn('o.id_order') . ' = ' . (int) $id);
343 }
344 else
345 {
346 // mainly used for reservation with multi-order (see 'id_parent' column)
347 $id = array_map('intval', $id);
348 $q->where($dbo->qn('o.id_order') . ' IN (' . implode(', ', $id) . ')');
349 }
350
351 $dbo->setQuery($q);
352 $track = $dbo->loadObjectList();
353
354 if (!$track)
355 {
356 return array();
357 }
358
359 if ($locale)
360 {
361 // translate the records
362 foreach ($track as $i => $change)
363 {
364 // keep also the default status code
365 $track[$i]->statusCode = $change->status;
366
367 // get status code name
368 $statusName = JHtml::fetch('vaphtml.status.find', 'name', array('code' => $change->status), $limit = true);
369
370 // register status name
371 $track[$i]->status = $statusName ? $statusName : $change->status;
372
373 // A comment can be translated only if it doesn't contain any spaces,
374 // as the language keys don't support them.
375 if (!preg_match("/\s/", $change->comment))
376 {
377 // try to translate the comment
378 $track[$i]->comment = JText::translate($change->comment);
379 }
380 }
381 }
382
383 return $track;
384 }
385
386 /**
387 * Returns the current status of the specified order.
388 *
389 * @param integer $id The order ID.
390 *
391 * @return mixed The order status if exists, false otherwise.
392 *
393 * @since 1.6.3
394 */
395 public function getStatus($id)
396 {
397 $dbo = JFactory::getDbo();
398
399 $q = $dbo->getQuery(true)
400 ->select($dbo->qn($this->statusColumn))
401 ->from($dbo->qn($this->table))
402 ->where($dbo->qn($this->pk) . ' = ' . $dbo->q($id));
403
404 $dbo->setQuery($q, 0, 1);
405
406 if ($status = $dbo->loadResult())
407 {
408 return strtoupper($status);
409 }
410
411 return false;
412 }
413
414 /**
415 * Returns the type for which a status is changed.
416 * It depends on the database table assigned to this object
417 * and it is taken by excluding the extension DB prefix.
418 *
419 * @return string The type.
420 */
421 protected function getType()
422 {
423 if (preg_match("/#__vikappointments_(.+)/", $this->table, $matches))
424 {
425 return $matches[1];
426 }
427
428 return false;
429 }
430 }
431