PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
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 1.7.3 All 36 releases
vikbooking / admin / helpers / src / history / tracker.php

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

130 lines 3.4 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 // fetch client section
63 $isSite = JFactory::getApplication()->isClient('site');
64
65 // check if the user is an operator
66 $operator = $isSite ? VikBooking::getOperatorInstance()->getOperatorAccount() : null;
67
68 if ($operator) {
69 // user logged in as operator
70 $user = (object) $operator;
71 $user->name = trim($user->first_name . ' ' . $user->last_name);
72 $role = 'operator';
73 } else {
74 // CMS user
75 $user = JFactory::getUser();
76 $role = $isSite ? 'guest' : 'admin';
77 }
78
79 // instantiate committer only once
80 $this->committer = new VBOHistoryCommitter($user->id, $user->name, $role);
81 }
82
83 return $this->committer;
84 }
85
86 /**
87 * Tracks any changes made to the provided item.
88 *
89 * @param object|array $prev The item details before the changes.
90 * @param object|array $curr The item details after the changes.
91 *
92 * @return bool True in case of changes, false otherwise.
93 */
94 public function track($prev, $curr)
95 {
96 $events = [];
97
98 // iterate all the supported rules
99 foreach ($this->rules as $rule) {
100 try {
101 if (!$rule instanceof VBOHistoryDetector) {
102 // detector class not valid
103 throw new UnexpectedValueException('Changes detector rule not valid: ' . get_class($rule), 400);
104 }
105
106 // check whether the current observer found some changes
107 if ($rule->hasChanged((object) $prev, (object) $curr)) {
108 $events[] = $rule;
109 }
110 } catch (Exception $error) {
111 // ignore error and go ahead
112 }
113 }
114
115 if (!$events) {
116 // nothing has changed
117 return false;
118 }
119
120 try {
121 // save the changes
122 $this->model->save($events, $this->getCommitter());
123 } catch (Exception $error) {
124 // ignore saving errors
125 }
126
127 return true;
128 }
129 }
130