PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.4
VikBooking Hotel Booking Engine & PMS v1.6.4
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 / multitask / parser.php

parser.php in VikBooking Hotel Booking Engine & PMS 1.6.4, at admin/helpers/src/multitask/parser.php

250 lines 5.0 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 Alessio Gaggii - E4J s.r.l.
6 * @copyright Copyright (C) 2022 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 * Multitask data extraction for admin widgets.
16 *
17 * @since 1.15.0 (J) - 1.5.0 (WP)
18 */
19 final class VBOMultitaskParser
20 {
21 /**
22 * The singleton instance of the class.
23 *
24 * @var VBOMultitaskFactory
25 */
26 private static $instance = null;
27
28 /**
29 * The view/task where multitask is being used.
30 *
31 * @var string
32 */
33 private $page_name = '';
34
35 /**
36 * The page URI where multitask is being used.
37 *
38 * @var string
39 */
40 private $page_uri = '';
41
42 /**
43 * The page query values.
44 *
45 * @var array
46 */
47 private $page_query = [];
48
49 /**
50 * Returns the global object to immediately support chaining.
51 * Either a new instance or the existing instance.
52 *
53 * @param string $page the name of the view/task.
54 * @param string $uri the page URI to parse.
55 *
56 * @return self
57 */
58 public static function getInstance($page = '', $uri = '')
59 {
60 if (is_null(static::$instance)) {
61 static::$instance = new static($page, $uri);
62 }
63
64 return static::$instance;
65 }
66
67 /**
68 * Class constructor.
69 *
70 * @param string $page the name of the view/task.
71 * @param string $uri the page URI to parse.
72 */
73 public function __construct($page = '', $uri = '')
74 {
75 // set name for view/task to consider
76 $this->setPageName($page);
77
78 // set URI to parse
79 $this->setPageURI($uri);
80 }
81
82 /**
83 * Builds a multitask data object with the page information
84 * ready to be passed to the admin widget being rendered.
85 *
86 * @return VBOMultitaskData
87 */
88 public function getData()
89 {
90 /**
91 * Rendering an admin widget does support injected request values.
92 *
93 * @since 1.16.0 (J) - 1.6.0 (WP)
94 */
95 $injected_data = JFactory::getApplication()->input->get('multitask_data', [], 'array');
96
97 // get a new multitask data object instance
98 $multitask_data = new VBOMultitaskData($injected_data);
99
100 // inject current page values
101 $multitask_data->setPage($this->getPageName());
102 $multitask_data->setURI($this->getPageURI());
103 $multitask_data->setQuery($this->getPageQueryValues());
104
105 // check if the page has a booking
106 $page_booking_id = $this->getPageBookingId();
107 if ($page_booking_id) {
108 $multitask_data->setBookingId($page_booking_id);
109 }
110
111 // return the data object for the admin widget
112 return $multitask_data;
113 }
114
115 /**
116 * Gets the current name of the view/task.
117 *
118 * @return string
119 */
120 public function getPageName()
121 {
122 return $this->page_name;
123 }
124
125 /**
126 * Sets the current name of the view/task.
127 *
128 * @param string $page the name of the view/task.
129 *
130 * @return self
131 */
132 public function setPageName($page = '')
133 {
134 $this->page_name = $page;
135
136 return $this;
137 }
138
139 /**
140 * Gets the current page URI.
141 *
142 * @return string
143 */
144 public function getPageURI()
145 {
146 return $this->page_uri;
147 }
148
149 /**
150 * Sets the current page URI.
151 *
152 * @param string $uri the page URI to parse
153 *
154 * @return self
155 */
156 public function setPageURI($uri = '')
157 {
158 $this->page_uri = $uri;
159
160 // parse current URI
161 $this->parseCurrentURI();
162
163 return $this;
164 }
165
166 /**
167 * Gets the current values for the page query.
168 *
169 * @return array
170 */
171 public function getPageQueryValues()
172 {
173 return $this->page_query;
174 }
175
176 /**
177 * Parses the current URI to detect values to extract.
178 *
179 * @return void
180 */
181 private function parseCurrentURI()
182 {
183 // reset query values
184 $this->page_query = [];
185
186 if (empty($this->page_uri)) {
187 // nothing to parse
188 return;
189 }
190
191 // parse URL data
192 $uri_data = parse_url($this->page_uri);
193
194 if (!isset($uri_data['query'])) {
195 // no query values to parse
196 return;
197 }
198
199 // parse query variables
200 $uri_data['query'] = str_replace('&amp;', '&', $uri_data['query']);
201
202 // inject parsed string values in page_query
203 parse_str($uri_data['query'], $this->page_query);
204
205 // adjust values, if needed
206 if (empty($this->page_name)) {
207 // try guessing the page from the query values
208 if (!empty($this->page_query['view'])) {
209 $this->page_name = $this->page_query['view'];
210 } elseif (!empty($this->page_query['task'])) {
211 $this->page_name = $this->page_query['task'];
212 }
213 }
214 }
215
216 /**
217 * Checks whether the page contains a booking.
218 *
219 * @return bool|int false on failure or reservation ID.
220 */
221 private function getPageBookingId()
222 {
223 if (!is_array($this->page_query) || !count($this->page_query)) {
224 return false;
225 }
226
227 // list of pages that can contain a reservation
228 $pages_with_booking = [
229 'editorder',
230 'editbusy',
231 'bookingcheckin',
232 ];
233
234 if (!in_array($this->page_name, $pages_with_booking)) {
235 return false;
236 }
237
238 if (!isset($this->page_query['cid']) || !is_array($this->page_query['cid'])) {
239 return false;
240 }
241
242 if (empty($this->page_query['cid'][0])) {
243 return false;
244 }
245
246 // booking ID found
247 return (int)$this->page_query['cid'][0];
248 }
249 }
250