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 / widgets / latest_events.php

latest_events.php in VikBooking Hotel Booking Engine & PMS 1.6.4, at admin/helpers/widgets/latest_events.php

738 lines 25.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 * Class handler for admin widget "latest events".
15 *
16 * @since 1.15.0 (J) - 1.5.0 (WP)
17 */
18 class VikBookingAdminWidgetLatestEvents extends VikBookingAdminWidget
19 {
20 /**
21 * The instance counter of this widget. Since we do not load individual parameters
22 * for each widget's instance, we use a static counter to determine its settings.
23 *
24 * @var int
25 */
26 protected static $instance_counter = -1;
27
28 /**
29 * Default number of events per page.
30 *
31 * @var int
32 */
33 protected $events_per_page = 6;
34
35 /**
36 * Default number of loading skeletons (should be less than events per page).
37 *
38 * @var int
39 */
40 protected $tot_skeletons = 4;
41
42 /**
43 * Class constructor will define the widget name and identifier.
44 */
45 public function __construct()
46 {
47 // call parent constructor
48 parent::__construct();
49
50 $this->widgetName = JText::translate('VBO_W_LATESTEVS_TITLE');
51 $this->widgetDescr = JText::translate('VBO_W_LATESTEVS_DESCR');
52 $this->widgetId = basename(__FILE__, '.php');
53
54 // define widget and icon and style name
55 $this->widgetIcon = '<i class="' . VikBookingIcons::i('history') . '"></i>';
56 $this->widgetStyleName = 'blue';
57
58 // load widget's settings
59 $this->widgetSettings = $this->loadSettings();
60 $this->widgetSettings = !is_array($this->widgetSettings) ? [] : $this->widgetSettings;
61 }
62
63 /**
64 * This widget returns the latest history record ID to schedule
65 * periodic watch data in order to be able to trigger notifications.
66 * No CSS/JS assets are needed during preloading.
67 *
68 * @return void|object
69 */
70 public function preload()
71 {
72 // lang vars for JS
73 JText::script('VBOBOOKHISTORYLBLTYPE');
74 JText::script('VBANYTHING');
75 JText::script('VBO_HISTORY_GROUPS');
76
77 // use the history class to get the latest event id
78 $history_obj = VikBooking::getBookingHistoryInstance();
79 $events = $history_obj->getLatestBookingEvents(0, 1);
80
81 if (count($events)) {
82 $watch_data = new stdClass;
83 $watch_data->history_id = $events[0]->id;
84
85 return $watch_data;
86 }
87
88 return null;
89 }
90
91 /**
92 * Checks for new notifications by using the previous preloaded watch-data.
93 *
94 * @param VBONotificationWatchdata $watch_data the preloaded watch-data object.
95 *
96 * @return array data object to watch next and notifications array.
97 *
98 * @see preload()
99 *
100 * @since 1.15.0 (J) - 1.5.0 (WP)
101 */
102 public function getNotifications(VBONotificationWatchdata $watch_data = null)
103 {
104 // default empty values
105 $watch_next = null;
106 $notifications = null;
107
108 if (!$watch_data) {
109 return [$watch_next, $notifications];
110 }
111
112 $latest_history_id = (int)$watch_data->get('history_id', 0);
113 if (empty($latest_history_id)) {
114 return [$watch_next, $notifications];
115 }
116
117 // load the latest worthy-of-notification history events
118 $history_obj = VikBooking::getBookingHistoryInstance();
119 $events = $history_obj->getWorthyEvents($latest_history_id, 10);
120 if (!count($events)) {
121 return [$watch_next, $notifications];
122 }
123
124 // first off, build the next watch data for this widget
125 $watch_next = new stdClass;
126 $watch_next->history_id = $events[(count($events) - 1)]->id;
127
128 // compose the notification(s) to dispatch
129 $notifications = VBONotificationScheduler::getInstance()->buildHistoryDataObjects($events);
130
131 return [$watch_next, $notifications];
132 }
133
134 /**
135 * Custom method for this widget only to load the history events.
136 * The method is called by the admin controller through an AJAX request.
137 * The visibility should be public, it should not exit the process, and
138 * any content sent to output will be returned to the AJAX response.
139 * In this case we return an array because this method requires "return":1.
140 *
141 * It's the actual rendering of the widget which also allows navigation.
142 *
143 * @return mixed boolean false in case of error, or associative array.
144 */
145 public function loadHistoryEvents()
146 {
147 $instance = VikRequest::getInt('instance', 0, 'request');
148 $offset = VikRequest::getInt('offset', 0, 'request');
149 $evtype = VikRequest::getString('evtype', '', 'request');
150 $length = VikRequest::getInt('length', $this->events_per_page, 'request');
151 $wrapper = VikRequest::getString('wrapper', '', 'request');
152
153 // update widget's settings
154 if ($instance >= 0) {
155 if (!isset($this->widgetSettings[$instance])) {
156 // push a new instance
157 $this->widgetSettings[] = $this->getDefaultEventSettings($evtype);
158 $instance = count($this->widgetSettings) - 1;
159 }
160 $this->widgetSettings[$instance]->evtype = $evtype;
161 $this->updateSettings(json_encode($this->widgetSettings));
162 }
163
164 // use the history class to get the events
165 $history_obj = VikBooking::getBookingHistoryInstance();
166 $events = $history_obj->getLatestBookingEvents($offset, $length, $min_id = 0, [$evtype]);
167
168 // check if a next page can be available
169 $has_next_page = (count($events) >= $length);
170
171 // start output buffering
172 ob_start();
173
174 // loop through all history events
175 $now = time();
176 $today = date('Y-m-d');
177 foreach ($events as $history) {
178 if ($history->status == 'confirmed') {
179 $ord_status = '<span class="label label-success vbo-status-label">'.JText::translate('VBCONFIRMED').'</span>';
180 } elseif ($history->status == 'standby') {
181 $ord_status = '<span class="label label-warning vbo-status-label">'.JText::translate('VBSTANDBY').'</span>';
182 } else {
183 $ord_status = '<span class="label label-error vbo-status-label" style="background-color: #d9534f;">'.JText::translate('VBCANCELLED').'</span>';
184 }
185 $nominative = strlen($history->nominative) > 1 ? $history->nominative : VikBooking::getFirstCustDataField($history->custdata);
186 $ch_logo_obj = VikBooking::getVcmChannelsLogo($history->channel, true);
187 $channel_logo = is_object($ch_logo_obj) ? $ch_logo_obj->getSmallLogoURL() : '';
188 $nights_lbl = $history->days > 1 ? JText::translate('VBDAYS') : JText::translate('VBDAY');
189
190 $dt_obj = new JDate($history->dt);
191 $dt_obj->setTimezone(new DateTimeZone(date_default_timezone_get()));
192
193 // calculate check-in date information
194 $checkin_info = getdate($history->checkin);
195 if (date('Y-m-d', $history->checkin) == $today) {
196 // check-in today
197 $checkind_lbl = JText::translate('VBTODAY');
198 $checkind_icn = '<i class="' . VikBookingIcons::i('plane-arrival') . '"></i>';
199 } elseif ($history->checkin > $now) {
200 // check-in in the future
201 $checkind_lbl = VikBooking::sayWeekDay($checkin_info['wday'], true) . ', ' . date(str_replace("/", $this->datesep, $this->df), $history->checkin);
202 $checkind_icn = '<i class="' . VikBookingIcons::i('sort-up') . '"></i> ';
203 } else {
204 // check-in in the past
205 $checkind_lbl = VikBooking::sayWeekDay($checkin_info['wday'], true) . ', ' . date(str_replace("/", $this->datesep, $this->df), $history->checkin);
206 $checkind_icn = '<i class="' . VikBookingIcons::i('sort-down') . '"></i> ';
207 }
208 ?>
209 <div class="vbo-widget-history-record" data-recordid="<?php echo $history->id; ?>" onclick="vboWidgetLatestEventsOpenBooking('<?php echo $history->idorder; ?>');">
210 <div class="vbo-widget-history-avatar">
211 <?php
212 if (!empty($channel_logo)) {
213 // channel logo has got the highest priority
214 ?>
215 <img class="vbo-widget-history-avatar-profile" src="<?php echo $channel_logo; ?>" />
216 <?php
217 } elseif (!empty($history->pic)) {
218 // customer profile picture
219 ?>
220 <img class="vbo-widget-history-avatar-profile" src="<?php echo strpos($history->pic, 'http') === 0 ? $history->pic : VBO_SITE_URI . 'resources/uploads/' . $history->pic; ?>" />
221 <?php
222 } else {
223 // we use an icon as fallback
224 VikBookingIcons::e('hotel', 'vbo-widget-history-avatar-icon');
225 }
226 ?>
227 </div>
228 <div class="vbo-widget-history-content">
229 <div class="vbo-widget-history-content-head">
230 <div class="vbo-widget-history-content-info-details">
231 <?php echo $ord_status; ?>
232 <h4><?php echo $nominative; ?></h4>
233 </div>
234 <div class="vbo-widget-history-content-info-booking">
235 <div class="vbo-widget-history-content-info-dates">
236 <span class="vbo-widget-history-booking-id">
237 <span class="label label-info"><?php echo $history->idorder; ?></span>
238 </span>
239 <span class="vbo-widget-history-date"><?php echo $dt_obj->format('D, d M Y H:i', true); ?></span>
240 </div>
241 <div class="vbo-widget-history-booking-checkin">
242 <span><?php echo $checkind_icn . $checkind_lbl; ?></span>
243 <span><?php echo ' - ' . $history->days . ' ' . $nights_lbl; ?></span>
244 </div>
245 </div>
246 </div>
247 <div class="vbo-widget-history-content-info-msg">
248 <span class="vbo-widget-history-content-info-msg-descr"><?php echo $history_obj->validType($history->type, true); ?></span>
249 </div>
250 </div>
251 </div>
252 <?php
253 }
254
255 // append navigation
256 ?>
257 <div class="vbo-widget-commands vbo-widget-commands-right">
258 <div class="vbo-widget-commands-main">
259 <?php
260 if ($offset > 0) {
261 // show backward navigation button
262 ?>
263 <div class="vbo-widget-command-chevron vbo-widget-command-prev">
264 <span class="vbo-widget-command-chevron-prev" onclick="vboWidgetLatestEventsNavigate('<?php echo $wrapper; ?>', -1);"><?php VikBookingIcons::e('chevron-left'); ?></span>
265 </div>
266 <?php
267 }
268 if ($has_next_page) {
269 // show forward navigation button
270 ?>
271 <div class="vbo-widget-command-chevron vbo-widget-command-next">
272 <span class="vbo-widget-command-chevron-next" onclick="vboWidgetLatestEventsNavigate('<?php echo $wrapper; ?>', 1);"><?php VikBookingIcons::e('chevron-right'); ?></span>
273 </div>
274 <?php
275 }
276 ?>
277 </div>
278 </div>
279 <?php
280
281 // get the HTML buffer
282 $html_content = ob_get_contents();
283 ob_end_clean();
284
285 // return an associative array of values
286 return array(
287 'html' => $html_content,
288 'tot_records' => count($events),
289 'next_page' => (int)$has_next_page,
290 );
291 }
292
293 public function render(VBOMultitaskData $data = null)
294 {
295 // increase widget's instance counter
296 static::$instance_counter++;
297
298 // check whether the widget is being rendered via AJAX when adding it through the customizer
299 $is_ajax = $this->isAjaxRendering();
300
301 // check whether we are in the multitask panel
302 $is_multitask = $this->isMultitaskRendering();
303
304 // check whether the widget requires settings
305 $needs_settings = !$is_ajax;
306 $data_instance = !$is_ajax ? static::$instance_counter : '-1';
307 if ($is_multitask && $is_ajax) {
308 $guess_inst_counter = $this->guessMultitaskHistoryInstance($data);
309 if ($guess_inst_counter !== false) {
310 // force the loading of the guessed widget instance's settings
311 static::$instance_counter = $guess_inst_counter;
312 $data_instance = $guess_inst_counter;
313 $needs_settings = true;
314 }
315 }
316
317 // generate a unique ID for the sticky notes wrapper instance
318 $wrapper_instance = !$is_ajax ? static::$instance_counter : rand();
319 $wrapper_id = 'vbo-widget-latestevents-' . $wrapper_instance;
320
321 // get permissions
322 $vbo_auth_bookings = JFactory::getUser()->authorise('core.vbo.bookings', 'com_vikbooking');
323
324 // load the settings for this specific instance of the widget
325 $instance_settings = $this->getDefaultEventSettings();
326 if ($needs_settings && is_array($this->widgetSettings) && isset($this->widgetSettings[static::$instance_counter])) {
327 if (is_object($this->widgetSettings[static::$instance_counter]) && count(get_object_vars($this->widgetSettings[static::$instance_counter]))) {
328 $instance_settings = $this->widgetSettings[static::$instance_counter];
329 }
330 }
331
332 // history object
333 $history_obj = VikBooking::getBookingHistoryInstance();
334
335 // get all history types and groups
336 $history_types = $history_obj->getTypesMap();
337 $history_groups = $history_obj->getTypeGroups();
338
339 // get the current event-type filter
340 $current_evtype = '';
341 $current_evname = '';
342 if (!empty($instance_settings->evtype)) {
343 if (isset($history_types[$instance_settings->evtype])) {
344 $current_evtype = $instance_settings->evtype;
345 $current_evname = $history_types[$current_evtype];
346 } elseif (isset($history_groups[$instance_settings->evtype])) {
347 $current_evtype = $instance_settings->evtype;
348 $current_evname = JText::translate('VBO_HISTORY_GROUPS') . ' - ' . $history_groups[$current_evtype]['name'];
349 }
350 }
351
352 ?>
353 <div id="<?php echo $wrapper_id; ?>" class="vbo-admin-widget-wrapper" data-instance="<?php echo $data_instance; ?>" data-offset="0" data-evtype="<?php echo $current_evtype; ?>" data-length="<?php echo $this->events_per_page; ?>">
354 <div class="vbo-admin-widget-head">
355 <div class="vbo-admin-widget-head-inline">
356 <h4><?php echo $this->widgetIcon; ?> <span><?php echo $this->widgetName; ?></span></h4>
357 <div class="vbo-admin-widget-head-commands">
358
359 <div class="vbo-reportwidget-commands">
360 <div class="vbo-reportwidget-commands-main">
361 <div class="vbo-reportwidget-command-dates">
362 <div class="vbo-reportwidget-period-name"><?php echo JText::translate('VBOBOOKHISTORYLBLTYPE'); ?></div>
363 <div class="vbo-widget-latestevents-filttype-name"><?php echo !empty($current_evname) ? $current_evname : JText::translate('VBANYTHING'); ?></div>
364 </div>
365 </div>
366 <div class="vbo-reportwidget-command-dots">
367 <span class="vbo-widget-command-togglefilters vbo-widget-latestevents-togglefilters" onclick="vboWidgetLatestEventsToggleFilters('<?php echo $wrapper_id; ?>');"><?php VikBookingIcons::e('ellipsis-v'); ?></span>
368 </div>
369 </div>
370 <div class="vbo-reportwidget-filters">
371 <div class="vbo-reportwidget-filter">
372 <select class="vbo-latestevents-filttype-event">
373 <option value=""></option>
374 <optgroup label="<?php echo htmlspecialchars(JText::translate('VBO_HISTORY_GROUPS')); ?>">
375 <?php
376 foreach ($history_groups as $ev_key => $ev_data) {
377 ?>
378 <option value="<?php echo $ev_key; ?>"<?php echo !empty($instance_settings->evtype) && $instance_settings->evtype == $ev_key ? ' selected="selected"' : ''; ?>><?php echo $ev_data['name']; ?></option>
379 <?php
380 }
381 ?>
382 </optgroup>
383 <optgroup label="<?php echo htmlspecialchars(JText::translate('VBOBOOKHISTORYTAB')); ?>">
384 <?php
385 foreach ($history_types as $ev_key => $ev_name) {
386 ?>
387 <option value="<?php echo $ev_key; ?>"<?php echo !empty($instance_settings->evtype) && $instance_settings->evtype == $ev_key ? ' selected="selected"' : ''; ?>><?php echo $ev_name; ?></option>
388 <?php
389 }
390 ?>
391 </optgroup>
392 </select>
393 </div>
394 <div class="vbo-reportwidget-filter vbo-reportwidget-filter-confirm">
395 <button type="button" class="btn vbo-config-btn" onclick="vboWidgetLatestEventsFilterType('<?php echo $wrapper_id; ?>');"><?php echo JText::translate('VBADMINNOTESUPD'); ?></button>
396 </div>
397 </div>
398
399 </div>
400 </div>
401 </div>
402 <div class="vbo-widget-latestevents-wrap">
403 <div class="vbo-widget-latestevents-inner">
404 <div class="vbo-widget-latestevents-list">
405 <?php
406 for ($i = 0; $i < $this->tot_skeletons; $i++) {
407 ?>
408 <div class="vbo-dashboard-guest-activity vbo-dashboard-guest-activity-skeleton">
409 <div class="vbo-dashboard-guest-activity-avatar">
410 <div class="vbo-skeleton-loading vbo-skeleton-loading-avatar"></div>
411 </div>
412 <div class="vbo-dashboard-guest-activity-content">
413 <div class="vbo-dashboard-guest-activity-content-head">
414 <div class="vbo-skeleton-loading vbo-skeleton-loading-title"></div>
415 </div>
416 <div class="vbo-dashboard-guest-activity-content-info-msg">
417 <div class="vbo-skeleton-loading vbo-skeleton-loading-content"></div>
418 </div>
419 </div>
420 </div>
421 <?php
422 }
423 ?>
424 </div>
425 </div>
426 </div>
427 </div>
428 <?php
429
430 if (static::$instance_counter === 0 || $is_ajax) {
431 /**
432 * Print the JS code only once for all instances of this widget.
433 * The real rendering is made through AJAX, not when the page loads.
434 */
435 ?>
436 <a class="vbo-widget-latest-events-basenavuri" href="index.php?option=com_vikbooking&task=editorder&cid[]=%d#bookhistory" style="display: none;"></a>
437
438 <script type="text/javascript">
439
440 /**
441 * Open the booking details page for the clicked event
442 */
443 function vboWidgetLatestEventsOpenBooking(id) {
444 var has_perms = <?php echo (int)$vbo_auth_bookings; ?>;
445 if (!has_perms) {
446 return false;
447 }
448 var open_url = jQuery('.vbo-widget-latest-events-basenavuri').first().attr('href');
449 open_url = open_url.replace('%d', id);
450 // navigate in a new tab
451 window.open(open_url, '_blank');
452 }
453
454 /**
455 * Display the loading skeletons.
456 */
457 function vboWidgetLatestEventsSkeletons(wrapper) {
458 var widget_instance = jQuery('#' + wrapper);
459 if (!widget_instance.length) {
460 return false;
461 }
462 widget_instance.find('.vbo-widget-latestevents-list').html('');
463 for (var i = 0; i < <?php echo $this->tot_skeletons; ?>; i++) {
464 var skeleton = '';
465 skeleton += '<div class="vbo-dashboard-guest-activity vbo-dashboard-guest-activity-skeleton">';
466 skeleton += ' <div class="vbo-dashboard-guest-activity-avatar">';
467 skeleton += ' <div class="vbo-skeleton-loading vbo-skeleton-loading-avatar"></div>';
468 skeleton += ' </div>';
469 skeleton += ' <div class="vbo-dashboard-guest-activity-content">';
470 skeleton += ' <div class="vbo-dashboard-guest-activity-content-head">';
471 skeleton += ' <div class="vbo-skeleton-loading vbo-skeleton-loading-title"></div>';
472 skeleton += ' </div>';
473 skeleton += ' <div class="vbo-dashboard-guest-activity-content-info-msg">';
474 skeleton += ' <div class="vbo-skeleton-loading vbo-skeleton-loading-content"></div>';
475 skeleton += ' </div>';
476 skeleton += ' </div>';
477 skeleton += '</div>';
478 // append skeleton
479 jQuery(skeleton).appendTo(widget_instance.find('.vbo-widget-latestevents-list'));
480 }
481 }
482
483 /**
484 * Toggle widget filters.
485 */
486 function vboWidgetLatestEventsToggleFilters(wrapper) {
487 var widget_instance = jQuery('#' + wrapper);
488 if (!widget_instance.length) {
489 return false;
490 }
491
492 widget_instance.find('.vbo-reportwidget-filters').toggle();
493 }
494
495 /**
496 * Perform the request to load the history events.
497 */
498 function vboWidgetLatestEventsLoad(wrapper) {
499 var widget_instance = jQuery('#' + wrapper);
500 if (!widget_instance.length) {
501 return false;
502 }
503
504 // get vars for making the request
505 var current_instance = widget_instance.attr('data-instance');
506 var current_offset = parseInt(widget_instance.attr('data-offset'));
507 var current_evtype = widget_instance.attr('data-evtype');
508 var length_per_page = parseInt(widget_instance.attr('data-length'));
509
510 // the widget method to call
511 var call_method = 'loadHistoryEvents';
512
513 // make a request to load the history events
514 VBOCore.doAjax(
515 "<?php echo $this->getExecWidgetAjaxUri(); ?>",
516 {
517 widget_id: "<?php echo $this->getIdentifier(); ?>",
518 call: call_method,
519 return: 1,
520 instance: current_instance,
521 offset: current_offset,
522 evtype: current_evtype,
523 length: length_per_page,
524 wrapper: wrapper,
525 tmpl: "component"
526 },
527 function(response) {
528 try {
529 var obj_res = typeof response === 'string' ? JSON.parse(response) : response;
530 if (!obj_res.hasOwnProperty(call_method) || !obj_res[call_method]) {
531 console.error('Unexpected JSON response', obj_res);
532 return false;
533 }
534
535 // replace HTML with new history events
536 widget_instance.find('.vbo-widget-latestevents-list').html(obj_res[call_method]['html']);
537
538 // check results
539 var tot_records = obj_res[call_method]['tot_records'] || 0;
540 if (!isNaN(tot_records) && parseInt(tot_records) < 1) {
541 // no results can indicate the offset is invalid or too high
542 if (!isNaN(current_offset) && parseInt(current_offset) > 0) {
543 // reset offset to 0
544 widget_instance.attr('data-offset', 0);
545 // show loading skeletons
546 vboWidgetLatestEventsSkeletons(wrapper);
547 // reload the first page
548 vboWidgetLatestEventsLoad(wrapper);
549 }
550 }
551 } catch(err) {
552 console.error('could not parse JSON response', err, response);
553 }
554 },
555 function(error) {
556 // remove the skeleton loading
557 widget_instance.find('.vbo-widget-latestevents-list').find('.vbo-dashboard-guest-activity-skeleton').remove();
558 console.error(error);
559 }
560 );
561 }
562
563 /**
564 * Navigate between the various pages of the history events.
565 */
566 function vboWidgetLatestEventsNavigate(wrapper, direction) {
567 var widget_instance = jQuery('#' + wrapper);
568 if (!widget_instance.length) {
569 return false;
570 }
571
572 // show loading skeletons
573 vboWidgetLatestEventsSkeletons(wrapper);
574
575 // current offset
576 var current_offset = parseInt(widget_instance.attr('data-offset'));
577
578 // events per page per type
579 var steps = <?php echo $this->events_per_page; ?>;
580
581 // check direction and update offsets for nav
582 if (direction > 0) {
583 // navigate forward
584 widget_instance.attr('data-offset', (current_offset + steps));
585 } else {
586 // navigate backward
587 var new_offset = current_offset - steps;
588 new_offset = new_offset >= 0 ? new_offset : 0;
589 widget_instance.attr('data-offset', new_offset);
590 }
591
592 // launch navigation
593 vboWidgetLatestEventsLoad(wrapper);
594 }
595
596 /**
597 * Update the events (history) type filter
598 */
599 function vboWidgetLatestEventsFilterType(wrapper) {
600 var widget_instance = jQuery('#' + wrapper);
601 if (!widget_instance.length) {
602 return false;
603 }
604
605 // get selected event-type filter
606 var cur_evname = '';
607 var cur_evtype = widget_instance.find('select.vbo-latestevents-filttype-event').val();
608 if (!cur_evtype || !cur_evtype.length) {
609 cur_evtype = '';
610 cur_evname = Joomla.JText._('VBANYTHING');
611 } else {
612 cur_evname = widget_instance.find('select.vbo-latestevents-filttype-event').find('option:selected').text();
613 if (cur_evtype.length === 3) {
614 cur_evname = Joomla.JText._('VBO_HISTORY_GROUPS') + ' - ' + cur_evname;
615 }
616 }
617
618 // hide filters when making a new request
619 widget_instance.find('.vbo-reportwidget-filters').hide();
620
621 // update title text with the new event name
622 widget_instance.find('.vbo-widget-latestevents-filttype-name').text(cur_evname);
623
624 // update current event-type filter
625 widget_instance.attr('data-evtype', cur_evtype);
626
627 // always reset the offset to 0
628 widget_instance.attr('data-offset', 0);
629
630 // show loading skeletons
631 vboWidgetLatestEventsSkeletons(wrapper);
632
633 // reload data
634 vboWidgetLatestEventsLoad(wrapper);
635 }
636
637 </script>
638 <?php
639 }
640 ?>
641
642 <script type="text/javascript">
643
644 jQuery(function() {
645
646 // when document is ready, load history events for this widget's instance
647 vboWidgetLatestEventsLoad('<?php echo $wrapper_id; ?>');
648
649 // render select2
650 jQuery('#<?php echo $wrapper_id; ?>').find('select.vbo-latestevents-filttype-event').select2({
651 placeholder: Joomla.JText._('VBOBOOKHISTORYLBLTYPE'),
652 width: '100%',
653 allowClear: true,
654 });
655
656 });
657
658 </script>
659
660 <?php
661 }
662
663 /**
664 * Protected method to guess if the widget being rendered via AJAX in the
665 * multitask panel should load specific settings. Should be called when
666 * settings have been loaded, and if AJAX + multitask rendering is detected.
667 * It's assumed that the AJAX rendering of a multitask widget takes place one
668 * second before the AJAX event that updates the multitask map to push the widget.
669 *
670 * @param VBOMultitaskData $data the data object injected to the widget.
671 *
672 * @return bool|int false on failure, guessed settings index otherwise.
673 *
674 * @since 1.16.0 (J) - 1.6.0 (WP)
675 */
676 protected function guessMultitaskHistoryInstance(VBOMultitaskData $data = null)
677 {
678 if (!is_array($this->widgetSettings) || !count($this->widgetSettings)) {
679 // nothing to guess if this widget has got no saved settings
680 return false;
681 }
682
683 if (!is_object($data)) {
684 // multitask data object must be set
685 return false;
686 }
687
688 // the page must be set in the multitask object
689 $vbo_page = $data->getPage();
690 if (empty($vbo_page)) {
691 // nothing to guess if no current page set
692 return false;
693 }
694
695 // get the map for the current page
696 $page_map = VikBooking::getAdminWidgetsInstance()->getMultitaskingMap($vbo_page, $whole = false);
697 if (!is_array($page_map) || !count($page_map)) {
698 // the multitask panel of this page has got no widgets saved, return the first index for settings
699 return 0;
700 }
701
702 // count how many widgets of this type are already on this page
703 $guessed_index = 0;
704 foreach ($page_map as $widget_type) {
705 if ($widget_type == $this->getIdentifier()) {
706 $guessed_index++;
707 }
708 }
709
710 // return the guessed index, which will load the next hypothetical instance
711 return $guessed_index;
712 }
713
714 /**
715 * Returns the default settings object for reading the latest history events.
716 *
717 * @param string $evtype optional event type requested.
718 *
719 * @return stdClass default object with default setting properties.
720 *
721 * @since 1.16.0 (J) - 1.6.0 (WP)
722 */
723 protected function getDefaultEventSettings($evtype = null)
724 {
725 $opts = new stdClass;
726 $opts->evtype = '';
727
728 if (!empty($evtype)) {
729 $opts->evtype = $evtype;
730 } elseif (!$this->isAjaxRendering() && !$this->isMultitaskRendering() && static::$instance_counter > 0) {
731 // by default, the 2nd instance of the widget will fetch a group of events
732 $opts->evtype = $this->hasChannelManager() ? 'GCM' : 'GBK';
733 }
734
735 return $opts;
736 }
737 }
738