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 / site / helpers / view.vbo.php

view.vbo.php in VikBooking Hotel Booking Engine & PMS 1.6.4, at site/helpers/view.vbo.php

330 lines 8.7 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 com_vikbooking
5 * @author Alessio Gaggii - e4j - Extensionsforjoomla.com
6 * @copyright Copyright (C) 2018 e4j - Extensionsforjoomla.com. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 * @link https://vikwp.com
9 */
10
11 defined('ABSPATH') or die('No script kiddies please!');
12
13 /**
14 * This class implements helpful methods for view instances.
15 * JViewBaseVikBooking is a placeholder used to support both JView and JViewLegacy.
16 *
17 * @since 1.2.0
18 */
19 class JViewVikBooking extends JViewBaseVikBooking
20 {
21 /**
22 * The current signature of the filters.
23 *
24 * @var array
25 */
26 protected $signatureId = '';
27
28 /**
29 * This method returns the correct limit start to use.
30 * In case the filters changes, the limit is always reset.
31 *
32 * @param array $args The filters associative array.
33 * @param mixed $id An optional value used to restrict
34 * the states only to a specific ID/page.
35 *
36 * @return integer The list start limit.
37 *
38 * @uses getPoolName()
39 * @uses registerSignature()
40 * @uses checkSignature()
41 * @uses resetLimit()
42 */
43 protected function getListLimitStart(array $args, $id = null)
44 {
45 $app = JFactory::getApplication();
46
47 // calculate pool name
48 $name = $this->getPoolName($id);
49
50 // get list limit
51 $start = $app->getUserStateFromRequest($name . '.limitstart', 'limitstart', 0, 'uint');
52
53 // register new filters signature
54 $this->registerSignature($args, $id);
55
56 if ($start > 0 && !$this->checkSignature($id))
57 {
58 // filters are changed, reset limit
59 $this->resetLimit($start, $id);
60 }
61
62 return $start;
63 }
64
65 /**
66 * Calculates the signature of the given filters and register it in the user state.
67 *
68 * @param array $args The filters associative array.
69 * @param mixed $id An optional value used to restrict
70 * the states only to a specific ID/page.
71 *
72 * @return string The old signature.
73 *
74 * @uses getPoolName()
75 */
76 protected function registerSignature(array $args, $id = null)
77 {
78 $app = JFactory::getApplication();
79
80 // calculate new signature
81 $sign = array();
82
83 foreach ($args as $k => $v)
84 {
85 if (strlen($v))
86 {
87 $sign[$k] = $v;
88 }
89 }
90
91 $sign = $sign ? serialize($sign) : '';
92
93 // calculate signature name
94 $name = $this->getPoolName($id);
95
96 // get old signature because `setUserState` owns a bug for returning the old state
97 $this->signatureId = $app->getUserState($name . '.signature', '');
98
99 // register new signature
100 $app->setUserState($name . '.signature', $sign);
101
102 // return old signature
103 return $this->signatureId;
104 }
105
106 /**
107 * Checks if the new signature matches the previous one.
108 *
109 * @param mixed $id An optional value used to restrict
110 * the states only to a specific ID/page.
111 * @param string $token The token to check against the new one.
112 * If not provided, the internal one will be used.
113 *
114 * @return boolean True if the tokens are equal.
115 *
116 * @uses getPoolName()
117 */
118 protected function checkSignature($id = null, $token = null)
119 {
120 if (!$token)
121 {
122 // use property in case the argument is empty
123 $token = $this->signatureId;
124 }
125
126 // calculate signature name
127 $name = $this->getPoolName($id);
128
129 // get current signature
130 $sign = JFactory::getApplication()->getUserState($name . '.signature', '');
131
132 // check if the 2 signatures are equal
133 return !strcasecmp($sign, $token);
134 }
135
136 /**
137 * Resets the list limit and save it in the user state.
138 *
139 * @param integer &$start The start list limit.
140 * @param mixed $id An optional value used to restrict
141 * the states only to a specific ID/page.
142 *
143 * @return void
144 *
145 * @uses getPoolName();
146 */
147 protected function resetLimit(&$start, $id = null)
148 {
149 // limit start passed by reference, reset it
150 $start = 0;
151
152 // calculate limit name
153 $name = $this->getPoolName($id);
154
155 // register the new limit within the user state
156 JFactory::getApplication()->setUserState($name . '.limitstart', $start);
157 }
158
159 /**
160 * Returns the pool base name in which is stored the user state.
161 *
162 * @param mixed $id An optional value used to restrict
163 * the states only to a specific ID/page.
164 *
165 * @return string The pool name.
166 */
167 protected function getPoolName($id = null)
168 {
169 // calculate pool name
170 $name = $this->getName();
171
172 if (!is_null($id))
173 {
174 // access the user state of a specific ID/page
175 $name .= "[$id]";
176 }
177
178 return $name;
179 }
180
181 /**
182 * Validates the list query to ensure that the specified limit
183 * doesn't exceed the total number of records. This might happen
184 * while erasing all the records from the last page.
185 *
186 * The query is always retrieved from the database object and
187 * must be invoked only once it has been set and executed.
188 *
189 * @param mixed &$offset The offset to use.
190 * @param mixed &$limit The limit to use.
191 * @param mixed $id An optional value used to restrict
192 * the states only to a specific ID/page.
193 *
194 * @return void
195 *
196 * @uses getPoolName()
197 */
198 protected function assertListQuery(&$offset, &$limit, $id = null)
199 {
200 $dbo = JFactory::getDbo();
201
202 // retrieve current query
203 $query = $dbo->getQuery();
204
205 if (!$offset || $dbo->getNumRows())
206 {
207 // we don't need to proceed as we are already fetching the first page
208 // or we found at least one record
209 return;
210 }
211
212 // No record found on the page we are (not the first one)!
213 // Try shifting by the offset found.
214 $limit = $limit ? $limit : 20;
215 $offset = max(array(0, $offset - (int) $limit));
216
217 // execute query again with updated limit
218 $dbo->setQuery($query, $offset, $limit);
219 $dbo->execute();
220
221 if (!$dbo->getNumRows())
222 {
223 $offset = 0;
224
225 // check if we are handling a limitable query object
226 if (interface_exists('JDatabaseQueryLimitable') && $query instanceof JDatabaseQueryLimitable)
227 {
228 // Update limit on query builder too because database might ignore it when offset
229 // is equals to 0. Note that offset and limit are specified in the opposite way.
230 $query->setLimit($limit, $offset);
231 }
232
233 // Still no rows found! Reset to the first page.
234 $dbo->setQuery($query, $offset, $limit);
235 $dbo->execute();
236 }
237
238 // calculate limit name
239 $name = $this->getPoolName($id);
240
241 // register the new limit within the user state
242 JFactory::getApplication()->setUserState($name . '.limitstart', $offset);
243 }
244
245 /**
246 * Creates an event that triggers when displaying a view.
247 * This is useful to include custom HTML in specific positions
248 * of the current view.
249 *
250 * Any specified arguments will be used when triggering the event.
251 *
252 * @param string $suffix An optional suffix to use for the event.
253 *
254 * @return array An array of forms.
255 *
256 * @since 1.5.10
257 */
258 protected function onDisplayView()
259 {
260 $events = array();
261
262 // get all specified arguments
263 $args = func_get_args();
264 // extract suffix from arguments
265 $suffix = array_shift($args);
266
267 // create event name based on the view name (e.g. onDisplayViewVikBookingManagereservation)
268 $events[] = 'onDisplayViewVikBooking' . ucfirst($this->getName()) . (string) $suffix;
269 // use also a different alias by trimming the initial "manage", "edit", "new" strings
270 // from the view name, such as "onDisplayViewVikBookingReservation"
271 $events[] = 'onDisplayViewVikBooking' . ucfirst(preg_replace("/^(manage|new|edit)/i", '', $this->getName())) . (string) $suffix;
272
273 $app = JFactory::getApplication();
274
275 JPluginHelper::importPlugin('e4j');
276
277 // merge default arguments with the given ones
278 $args = array_merge(
279 array($this),
280 $args
281 );
282
283 $forms = array();
284
285 // iterate events and make sure the same event name is not going to be used twice
286 foreach (array_unique($events) as $event)
287 {
288 /**
289 * Trigger event to allow the plugins to include custom HTML within the view.
290 * It is possible to return an associative array to group the HTML strings
291 * under different fieldsets. Plain/html string will be always pushed within
292 * the "custom" fieldset instead.
293 *
294 * @param mixed $view The current view instance.
295 *
296 * @return mixed The HTML to display.
297 *
298 * @since 1.5.10
299 */
300 $values = $app->triggerEvent($event, $args);
301
302 // iterate all the returned values
303 foreach ($values as $value)
304 {
305 if (!is_array($value))
306 {
307 // use "custom" group in case the returned value is a string
308 $value = array('VBO_CUSTOM_FIELDSET' => $value);
309 }
310
311 // iterate groups
312 foreach ($value as $key => $html)
313 {
314 // check if the fieldset already exists
315 if (!isset($forms[$key]))
316 {
317 $forms[$key] = '';
318 }
319
320 // push form within the specified fieldset
321 $forms[$key] .= $html;
322 }
323 }
324 }
325
326 // return array of forms
327 return $forms;
328 }
329 }
330