PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.8.16
VikBooking Hotel Booking Engine & PMS v1.8.16
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / admin / helpers / src / history / tracker.php

tracker.php in VikBooking Hotel Booking Engine & PMS 1.8.16, at admin/helpers/src/history/tracker.php

140 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2025 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 * Generic history tracker.
16 *
17 * @since 1.18 (J) - 1.8 (WP)
18 */
19 class VBOHistoryTracker
20 {
21 /**
22 * The model that will be used to save the changes.
23 *
24 * @var VBOHistoryModel
25 */
26 protected $model;
27
28 /**
29 * The rules to run in search of changes.
30 *
31 * @var VBOHistoryDetector[]
32 */
33 protected $rules;
34
35 /**
36 * The instance of the user that made the changes.
37 *
38 * @var VBOHistoryCommitter
39 */
40 protected $committer = null;
41
42 /**
43 * Class constructor.
44 *
45 * @param VBOHistoryModel $model The storage model.
46 * @param VBOHistoryDetector[] $rules An array of rules.
47 */
48 public function __construct(VBOHistoryModel $model, array $rules)
49 {
50 $this->model = $model;
51 $this->rules = $rules;
52 }
53
54 /**
55 * Returns the details of the committer.
56 *
57 * @return VBOHistoryCommitter
58 */
59 public function getCommitter()
60 {
61 if ($this->committer === null) {
62 /**
63 * Check whether the user completed the authentication via APP.
64 *
65 * @since 1.18.16 (J) - 1.8.16 (WP)
66 */
67 if (class_exists('VCMAppFramework') && ($appUser = VCMAppFramework::getUser())) {
68 $user = (object) $appUser;
69 $role = $appUser['admin'] ? 'admin' : 'operator';
70 } else {
71 // fetch client section
72 $isSite = JFactory::getApplication()->isClient('site');
73
74 // check if the user is an operator
75 $operator = $isSite ? VikBooking::getOperatorInstance()->getOperatorAccount() : null;
76
77 if ($operator) {
78 // user logged in as operator
79 $user = (object) $operator;
80 $user->name = trim($user->first_name . ' ' . $user->last_name);
81 $role = 'operator';
82 } else {
83 // CMS user
84 $user = JFactory::getUser();
85 $role = $isSite ? 'guest' : 'admin';
86 }
87 }
88
89 // instantiate committer only once
90 $this->committer = new VBOHistoryCommitter($user->id, $user->name, $role);
91 }
92
93 return $this->committer;
94 }
95
96 /**
97 * Tracks any changes made to the provided item.
98 *
99 * @param object|array $prev The item details before the changes.
100 * @param object|array $curr The item details after the changes.
101 *
102 * @return bool True in case of changes, false otherwise.
103 */
104 public function track($prev, $curr)
105 {
106 $events = [];
107
108 // iterate all the supported rules
109 foreach ($this->rules as $rule) {
110 try {
111 if (!$rule instanceof VBOHistoryDetector) {
112 // detector class not valid
113 throw new UnexpectedValueException('Changes detector rule not valid: ' . get_class($rule), 400);
114 }
115
116 // check whether the current observer found some changes
117 if ($rule->hasChanged((object) $prev, (object) $curr)) {
118 $events[] = $rule;
119 }
120 } catch (Exception $error) {
121 // ignore error and go ahead
122 }
123 }
124
125 if (!$events) {
126 // nothing has changed
127 return false;
128 }
129
130 try {
131 // save the changes
132 $this->model->save($events, $this->getCommitter());
133 } catch (Exception $error) {
134 // ignore saving errors
135 }
136
137 return true;
138 }
139 }
140