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 / html / status.php
vikappointments / site / helpers / libraries / html Last commit date
assets.php 3 days ago color.php 3 days ago countries.php 3 days ago index.html 3 days ago media.php 3 days ago site.php 3 days ago sitescripts.php 3 days ago status.php 3 days ago vikappointments.php 3 days ago
status.php
515 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 * Utility class working with VikAppointments status codes.
16 *
17 * @since 1.7
18 */
19 abstract class VAPHtmlStatus
20 {
21 /**
22 * A list of status codes to be cached.
23 *
24 * @var array
25 */
26 protected static $statusCodes = null;
27
28 /**
29 * Get a list of the available status codes.
30 *
31 * @return array
32 */
33 public static function codes()
34 {
35 if (static::$statusCodes === null)
36 {
37 $dbo = JFactory::getDbo();
38
39 $q = $dbo->getQuery(true)
40 ->select('*')
41 ->from($dbo->qn('#__vikappointments_status_code'))
42 ->order($dbo->qn('ordering') . ' ASC');
43
44 $dbo->setQuery($q);
45 static::$statusCodes = $dbo->loadObjectList();
46 }
47
48 return static::$statusCodes;
49 }
50
51 /**
52 * Checks whether the specified status code owns the APPROVED rule.
53 *
54 * @param string $group The group to look for (appointments, packages or subscriptions).
55 * @param mixed $code The code to look for.
56 *
57 * @return boolean True if matching, false otherwise.
58 */
59 public static function isapproved($group, $code)
60 {
61 $where = array('approved' => 1, 'code' => $code, $group => 1);
62
63 return (bool) self::findStrict('code', $where, $strict = false);
64 }
65
66 /**
67 * Returns the CONFIRMED status record or a specific column.
68 *
69 * @param string $group The group to look for (appointments, packages or subscriptions).
70 * @param mixed $column The column to return or a list of columns.
71 * Use * to return the whole record.
72 * @param boolean $strict True to throw an exception in case of missing status.
73 *
74 * @return mixed The object status or the column value.
75 * False if the status doesn't exist.
76 */
77 public static function confirmed($group, $columns = '*', $strict = true)
78 {
79 return self::findStrict($columns, array('approved' => 1, 'reserved' => 1, 'paid' => 0, $group => 1), $strict, 'confirmed');
80 }
81
82 /**
83 * Checks whether the specified status code owns the CONFIRMED rules.
84 *
85 * @param string $group The group to look for (appointments, packages or subscriptions).
86 * @param mixed $code The code to look for.
87 *
88 * @return boolean True if matching, false otherwise.
89 */
90 public static function isconfirmed($group, $code)
91 {
92 $where = array('approved' => 1, 'reserved' => 1, 'paid' => 0, 'code' => $code, $group => 1);
93
94 return (bool) self::findStrict('code', $where, $strict = false);
95 }
96
97 /**
98 * Returns the PENDING status record or a specific column.
99 *
100 * @param string $group The group to look for (appointments, packages or subscriptions).
101 * @param mixed $column The column to return or a list of columns.
102 * Use * to return the whole record.
103 * @param boolean $strict True to throw an exception in case of missing status.
104 *
105 * @return mixed The object status or the column value.
106 * False if the status doesn't exist.
107 */
108 public static function pending($group, $columns = '*', $strict = true)
109 {
110 return self::findStrict($columns, array('approved' => 0, 'reserved' => 1, $group => 1), $strict, 'pending');
111 }
112
113 /**
114 * Checks whether the specified status code owns the PENDING rules.
115 *
116 * @param string $group The group to look for (appointments, packages or subscriptions).
117 * @param mixed $code The code to look for.
118 *
119 * @return boolean True if matching, false otherwise.
120 */
121 public static function ispending($group, $code)
122 {
123 $where = array('approved' => 0, 'reserved' => 1, 'code' => $code, $group => 1);
124
125 return (bool) self::findStrict('code', $where, $strict = false);
126 }
127
128 /**
129 * Returns the REMOVED status record or a specific column.
130 *
131 * @param string $group The group to look for (appointments, packages or subscriptions).
132 * @param mixed $column The column to return or a list of columns.
133 * Use * to return the whole record.
134 * @param boolean $strict True to throw an exception in case of missing status.
135 *
136 * @return mixed The object status or the column value.
137 * False if the status doesn't exist.
138 */
139 public static function removed($group, $columns = '*', $strict = true)
140 {
141 return self::findStrict($columns, array('expired' => 1, 'reserved' => 0, $group => 1), $strict, 'removed');
142 }
143
144 /**
145 * Checks whether the specified status code owns the REMOVED rules.
146 *
147 * @param string $group The group to look for (appointments, packages or subscriptions).
148 * @param mixed $code The code to look for.
149 *
150 * @return boolean True if matching, false otherwise.
151 */
152 public static function isremoved($group, $code)
153 {
154 $where = array('expired' => 1, 'reserved' => 0, 'code' => $code, $group => 1);
155
156 return (bool) self::findStrict('code', $where, $strict = false);
157 }
158
159 /**
160 * Returns the CANCELLED status record or a specific column.
161 *
162 * @param string $group The group to look for (appointments, packages or subscriptions).
163 * @param mixed $column The column to return or a list of columns.
164 * Use * to return the whole record.
165 * @param boolean $strict True to throw an exception in case of missing status.
166 *
167 * @return mixed The object status or the column value.
168 * False if the status doesn't exist.
169 */
170 public static function cancelled($group, $columns = '*', $strict = true)
171 {
172 return self::findStrict($columns, array('cancelled' => 1, 'paid' => 0, $group => 1), $strict, 'cancelled');
173 }
174
175 /**
176 * Checks whether the specified status code owns the CANCELLED rules.
177 *
178 * @param string $group The group to look for (appointments, packages or subscriptions).
179 * @param mixed $code The code to look for.
180 *
181 * @return boolean True if matching, false otherwise.
182 */
183 public static function iscancelled($group, $code)
184 {
185 $where = array('cancelled' => 1, 'code' => $code, $group => 1);
186
187 return (bool) self::findStrict('code', $where, $strict = false);
188 }
189
190 /**
191 * Returns the REFUNDED status record or a specific column.
192 *
193 * @param string $group The group to look for (appointments, packages or subscriptions).
194 * @param mixed $column The column to return or a list of columns.
195 * Use * to return the whole record.
196 * @param boolean $strict True to throw an exception in case of missing status.
197 *
198 * @return mixed The object status or the column value.
199 * False if the status doesn't exist.
200 */
201 public static function refunded($group, $columns = '*', $strict = true)
202 {
203 return self::findStrict($columns, array('cancelled' => 1, 'paid' => 1, $group => 1), $strict, 'refunded');
204 }
205
206 /**
207 * Checks whether the specified status code owns the REFUNDED rules.
208 *
209 * @param string $group The group to look for (appointments, packages or subscriptions).
210 * @param mixed $code The code to look for.
211 *
212 * @return boolean True if matching, false otherwise.
213 */
214 public static function isrefunded($group, $code)
215 {
216 $where = array('cancelled' => 1, 'paid' => 1, 'code' => $code, $group => 1);
217
218 return (bool) self::findStrict('code', $where, $strict = false);
219 }
220
221 /**
222 * Returns the PAID status record or a specific column.
223 *
224 * @param string $group The group to look for (appointments, packages or subscriptions).
225 * @param mixed $column The column to return or a list of columns.
226 * Use * to return the whole record.
227 * @param boolean $strict True to throw an exception in case of missing status.
228 *
229 * @return mixed The object status or the column value.
230 * False if the status doesn't exist.
231 */
232 public static function paid($group, $columns = '*', $strict = true)
233 {
234 return self::findStrict($columns, array('approved' => 1, 'reserved' => 1, 'paid' => 1, $group => 1), $strict, 'paid');
235 }
236
237 /**
238 * Checks whether the specified status code owns the PAID rules.
239 *
240 * @param string $group The group to look for (appointments, packages or subscriptions).
241 * @param mixed $code The code to look for.
242 *
243 * @return boolean True if matching, false otherwise.
244 */
245 public static function ispaid($group, $code)
246 {
247 $where = array('approved' => 1, 'reserved' => 1, 'paid' => 1, 'code' => $code, $group => 1);
248
249 return (bool) self::findStrict('code', $where, $strict = false);
250 }
251
252 /**
253 * Displays the specified status by using the given layout.
254 *
255 * @param mixed $status Either an array, an object or the status code.
256 * @param string $layout The type of layout to use.
257 *
258 * @return string The HTML code to display.
259 */
260 public static function display($status, $layout = null)
261 {
262 if (is_scalar($status))
263 {
264 // find status by code
265 $status = self::find('*', array('code' => $status), $limit = true);
266 }
267
268 if (!$status)
269 {
270 // status not found, do not go ahead
271 return '';
272 }
273
274 if (is_object($status))
275 {
276 // clone object to lose the reference from the original element
277 $status = clone $status;
278 }
279 else
280 {
281 // otherwise treat as object
282 $status = (object) $status;
283 }
284
285 /**
286 * Ignore translation in case the multilingual feature is disabled.
287 *
288 * @since 1.7.4
289 */
290 if (VAPFactory::getConfig()->getBool('ismultilang'))
291 {
292 // get translator object
293 $translator = VAPFactory::getTranslator();
294 // translate status code
295 $tx = $translator->translate('statuscode', $status->id);
296
297 if ($tx)
298 {
299 // apply translation
300 $status->name = $tx->name;
301 $status->description = $tx->description;
302 }
303 }
304
305 if (!$layout)
306 {
307 // use default layout when omitted
308 $layout = 'default';
309 }
310 else if ($layout == 'plain')
311 {
312 // return only the status name without style
313 return $status->name;
314 }
315
316 // render layout, hoping that the specified one exists
317 return JLayoutHelper::render('status.' . $layout, array('status' => $status));
318 }
319
320 /**
321 * Displays the popup used to change status code.
322 *
323 * @param integer $group The group to which the codes belong (appointments,
324 * packages of subscriptions).
325 * @param string $selector The popup trigger selector.
326 *
327 * @return void
328 */
329 public static function contextmenu($group, $selector = null)
330 {
331 $document = JFactory::getDocument();
332
333 static $loaded = array();
334
335 if (!isset($loaded[$group]))
336 {
337 // get status codes
338 $codes = static::find('*', array($group => 1));
339
340 // encode codes for JS usage
341 $json = json_encode($codes);
342
343 // register supported status codes
344 $document->addScriptDeclaration(
345 <<<JS
346 onInstanceReady(() => {
347 return typeof VIKAPPOINTMENTS_STATUS_CODES_MAP !== 'undefined';
348 }).then(() => {
349 VIKAPPOINTMENTS_STATUS_CODES_MAP['$group'] = $json;
350 });
351 JS
352 );
353 }
354
355 if ($selector)
356 {
357 switch ($group)
358 {
359 case 'appointments':
360 $controller = 'reservation';
361 break;
362
363 case 'packages':
364 $controller = 'packorder';
365 break;
366
367 case 'subscriptions':
368 $controller = 'subscrorder';
369 break;
370
371 default:
372 $controller = '';
373 }
374
375 // fetch AJAX URL
376 $url = "index.php?option=com_vikappointments&task={$controller}.changestatusajax";
377 $url = VAPApplication::getInstance()->ajaxUrl($url, false);
378
379 $document->addScriptDeclaration(
380 <<<JS
381 jQuery(function($) {
382 $('$selector').statusCodesPopup({
383 group: '{$group}',
384 url: '{$url}',
385 });
386 });
387 JS
388 );
389 }
390
391 static $_loaded = 0;
392
393 if (!$_loaded)
394 {
395 $_loaded = 1;
396
397 JText::script('VAPCONNECTIONLOSTERROR');
398
399 // load script
400 JHtml::fetch('vaphtml.assets.statuscodes');
401
402 $document->addScriptDeclaration(
403 <<<JS
404 jQuery(window).on('statuscode.error', function(event, error) {
405 alert(error.responseText || Joomla.JText._('VAPCONNECTIONLOSTERROR'));
406 });
407 JS
408 );
409 }
410 }
411
412 /**
413 * Helper method used to find the requested status(es).
414 *
415 * @param mixed $column The column to return or a list of columns.
416 * Use * to return the whole record.
417 * @param array $where An associative array containing the keys and
418 * the values that should assume.
419 *
420 *
421 * @return mixed The object status or the column value.
422 * In case of omitted limit, the resulting values
423 * will be wrapped within an array.
424 * False (or an empty list) if the status doesn't exist.
425 */
426 public static function find($columns, array $where, $limit = false)
427 {
428 // get codes
429 $codes = self::codes();
430
431 $result = array();
432
433 // iterate the codes
434 foreach ($codes as $status)
435 {
436 $found = true;
437
438 // check if where is matching
439 foreach ($where as $k => $v)
440 {
441 $found = $found && ($status->{$k} == $v);
442 }
443
444 // check if the status has been found
445 if ($found)
446 {
447 if ($columns == '*')
448 {
449 // return the whole record
450 $ret = $status;
451 }
452 else if (is_scalar($columns))
453 {
454 // return requested column
455 $ret = isset($status->{$columns}) ? $status->{$columns} : null;
456 }
457 else
458 {
459 // return an object containing the specified columns
460 $ret = new stdClass;
461
462 // copy the columns within the temporary object
463 foreach ($columns as $col)
464 {
465 $ret->{$col} = isset($status->{$col}) ? $status->{$col} : null;
466 }
467 }
468
469 // include return value within the list
470 $result[] = $ret;
471 }
472 }
473
474 if (!$limit)
475 {
476 // return list of codes
477 return $result;
478 }
479
480 // in case of matching result, return the first one available, otherwise false
481 return $result ? $result[0] : false;
482 }
483
484 /**
485 * Helper method used to find the requested status.
486 * In case of missing status code, an exception will be thrown.
487 *
488 * @param mixed $column The column to return or a list of columns.
489 * Use * to return the whole record.
490 * @param array $where An associative array containing the keys and
491 * the values that should assume.
492 * @param boolean $strict True to raise the exception in case of error.
493 * @param string $id The status code identifier, only use to describe
494 * the error message.
495 *
496 *
497 * @return mixed The object status or the column value.
498 *
499 * @throws Exception
500 */
501 protected static function findStrict($columns, $where, $strict, $id = '')
502 {
503 // load requested status code
504 $result = self::find($columns, $where, $limit = true);
505
506 if ($result === false && $strict)
507 {
508 // status code not found, throw the exception
509 throw new Exception(sprintf('Status code [%s] not found!', $id), 404);
510 }
511
512 return $result;
513 }
514 }
515