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 / model / database.php

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

150 lines 4.6 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 * Database history model implementor.
16 *
17 * @since 1.18 (J) - 1.8 (WP)
18 */
19 class VBOHistoryModelDatabase implements VBOHistoryModel
20 {
21 /** @var VBOHistoryContext */
22 protected $context;
23
24 /** @var JDatabaseDriver */
25 protected $db;
26
27 /**
28 * Class constructor.
29 *
30 * @param VBOHistoryContext $context
31 */
32 public function __construct(VBOHistoryContext $context, $db = null)
33 {
34 if (!$db) {
35 $db = JFactory::getDbo();
36 }
37
38 $this->context = $context;
39 $this->db = $db;
40 }
41
42 /**
43 * @inheritDoc
44 */
45 public function getItems()
46 {
47 $query = $this->db->getQuery(true);
48
49 // history changes
50 $query->select($this->db->qn(['h.id', 'h.id_user', 'h.username', 'h.date']));
51 $query->from($this->db->qn('#__vikbooking_record_history', 'h'));
52
53 // history change events
54 $query->select($this->db->qn(['e.event', 'e.payload']));
55 $query->leftjoin($this->db->qn('#__vikbooking_record_history_event', 'e') . ' ON ' . $this->db->qn('e.id_change') . ' = ' . $this->db->qn('h.id'));
56
57 // filter changes by context
58 $query->where($this->db->qn('h.id_context') . ' = ' . (int) $this->context->getID());
59 $query->where($this->db->qn('h.context') . ' = ' . $this->db->q($this->context->getAlias()));
60
61 $this->db->setQuery($query);
62
63 $changes = [];
64
65 foreach ($this->db->loadObjectList() as $row) {
66 if (!isset($changes[$row->id])) {
67 // construct parent change
68 $change = new stdClass;
69 $change->id = $row->id;
70 $change->date = $row->date;
71 $change->icon = '';
72 $change->user = new stdClass;
73 $change->user->id = $row->id_user;
74 $change->user->name = $row->username;
75 $change->events = [];
76
77 // track parent change
78 $changes[$row->id] = $change;
79 }
80
81 if (!empty($row->event)) {
82 try {
83 // attempt to unserialize object
84 $event = @unserialize($row->payload);
85
86 if ($event instanceof VBOHistoryDetector) {
87 $changes[$row->id]->events[] = $event;
88 }
89 } catch (Exception $err) {
90 // ignore unserialization errors
91 }
92 }
93 }
94
95 // fetch a summary icon for each change
96 foreach ($changes as $change) {
97 // take each icon for all the events under this change
98 $icons = array_values(array_unique(array_map(function($event) {
99 return $event->getIcon();
100 }, $change->events)));
101
102 if (count($icons) == 1) {
103 // use the only icon found
104 $change->icon = $icons[0];
105 } else {
106 // multiple icons, use a generic one
107 $change->icon = VikBookingIcons::i('pencil-alt');
108 }
109 }
110
111 return array_values($changes);
112 }
113
114 /**
115 * @inheritDoc
116 */
117 public function save(array $events, VBOHistoryCommitter $committer)
118 {
119 $change = new stdClass;
120 $change->context = $this->context->getAlias();
121 $change->id_context = $this->context->getID();
122 $change->date = JFactory::getDate()->toSql();
123 $change->id_user = $committer->getID();
124 $change->username = $committer->getName();
125
126 // register parent change
127 $result = $this->db->insertObject('#__vikbooking_record_history', $change, 'id');
128
129 if (!$result || empty($change->id)) {
130 // query failed or record ID empty
131 throw new UnexpectedValueException('Unable to save the specified record changes.', 500);
132 }
133
134 // iterate all the detected change details
135 foreach ($events as $event) {
136 if (!$event instanceof VBOHistoryDetector) {
137 continue;
138 }
139
140 $data = new stdClass;
141 $data->id_change = $change->id;
142 $data->event = $event->getEvent();
143 $data->payload = serialize($event);
144
145 // register detected changes
146 $this->db->insertObject('#__vikbooking_record_history_event', $data, 'id');
147 }
148 }
149 }
150