| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2026 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 |
* Abstract class handler for admin chat widgets. |
| 16 |
* |
| 17 |
* @since 1.18.8 (J) - 1.8.8 (WP) |
| 18 |
*/ |
| 19 |
abstract class VBOChatWidgetAdmin extends VikBookingAdminWidget |
| 20 |
{ |
| 21 |
/** |
| 22 |
* The instance counter of this widget. |
| 23 |
* |
| 24 |
* @var int |
| 25 |
*/ |
| 26 |
protected static $instance_counter = -1; |
| 27 |
|
| 28 |
/** |
| 29 |
* A list holding all the supported chat categories. |
| 30 |
* |
| 31 |
* @var string[] |
| 32 |
* @since 1.8.8 |
| 33 |
*/ |
| 34 |
protected $involvedCategories = []; |
| 35 |
|
| 36 |
/** |
| 37 |
* Beside loading the necessary assets, this widget preloads the |
| 38 |
* ID of the latest message for the administrators in order to |
| 39 |
* watch the new messages received and to be able to trigger notifications. |
| 40 |
* |
| 41 |
* @return ?object |
| 42 |
*/ |
| 43 |
public function preload() |
| 44 |
{ |
| 45 |
// get the chat mediator |
| 46 |
$chat = VBOFactory::getChatMediator(); |
| 47 |
|
| 48 |
// preload chat assets |
| 49 |
$chat->useAssets(); |
| 50 |
|
| 51 |
// get the latest message(s) for the administrators |
| 52 |
$messages = $chat->getMessages( |
| 53 |
(new VBOChatSearch) |
| 54 |
->forCategories($this->involvedCategories) |
| 55 |
->sender(0, false) |
| 56 |
->limit(1) |
| 57 |
); |
| 58 |
|
| 59 |
if ($messages) { |
| 60 |
$watch_data = [ |
| 61 |
'message_id' => $messages[0]->getID(), |
| 62 |
]; |
| 63 |
|
| 64 |
// return the data to watch for notifications |
| 65 |
return (object) $watch_data; |
| 66 |
} |
| 67 |
|
| 68 |
return null; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @inheritDoc |
| 73 |
*/ |
| 74 |
public function getWidgetDetails() |
| 75 |
{ |
| 76 |
// get common widget details from parent abstract class |
| 77 |
$details = parent::getWidgetDetails(); |
| 78 |
|
| 79 |
// append the modal rendering information |
| 80 |
$details['modal'] = [ |
| 81 |
'add_class' => 'vbo-modal-large', |
| 82 |
]; |
| 83 |
|
| 84 |
return $details; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @inheritDoc |
| 89 |
*/ |
| 90 |
public function render(?VBOMultitaskData $data = null) |
| 91 |
{ |
| 92 |
// increase widget's instance counter |
| 93 |
static::$instance_counter++; |
| 94 |
|
| 95 |
// check whether the widget is being rendered via AJAX when adding it through the customizer |
| 96 |
$is_ajax = $this->isAjaxRendering(); |
| 97 |
|
| 98 |
// generate a unique ID for the widget wrapper instance |
| 99 |
$wrapper_instance = !$is_ajax ? static::$instance_counter : rand(); |
| 100 |
$wrapper_id = 'vbo-widget-admin-chat-' . $wrapper_instance; |
| 101 |
|
| 102 |
// get the chat mediator |
| 103 |
$chat = VBOFactory::getChatMediator(); |
| 104 |
|
| 105 |
// multitask data event identifier for clearing intervals |
| 106 |
$js_intvals_id = ''; |
| 107 |
$chat_context = null; |
| 108 |
if ($data && $data->isModalRendering()) { |
| 109 |
// access Multitask data |
| 110 |
$js_intvals_id = $data->getModalJsIdentifier(); |
| 111 |
|
| 112 |
// access context alias and id, if any |
| 113 |
$context_alias = $data->get('context_alias', '') ?: $this->options()->get('context_alias', ''); |
| 114 |
$context_id = $data->get('context_id', 0) ?: $this->options()->get('context_id', 0); |
| 115 |
|
| 116 |
if ($context_alias && $context_id) { |
| 117 |
// build chat for the given context |
| 118 |
$chat_context = $chat->createContext($context_alias, $context_id); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* @see Keep the inline styling on the HTML elements for the JS functions to work properly. |
| 124 |
*/ |
| 125 |
?> |
| 126 |
<div id="<?php echo $wrapper_id; ?>" class="vbo-admin-widget-wrapper" style="height: 100%;"> |
| 127 |
<div class="vbo-admin-widget-head" style="border-bottom: 0;"> |
| 128 |
<div class="vbo-admin-widget-head-inline"> |
| 129 |
<h4 style="padding: 7px;"><?php echo $this->widgetIcon; ?> <span><?php echo $this->widgetName; ?></span></h4> |
| 130 |
</div> |
| 131 |
</div> |
| 132 |
<?php |
| 133 |
if ($chat_context) { |
| 134 |
// display the chat for the identified context |
| 135 |
echo $chat->render($chat_context, [ |
| 136 |
'assets' => false, |
| 137 |
]); |
| 138 |
} else { |
| 139 |
// display the chat for all threads |
| 140 |
|
| 141 |
// take all the threads where the administrator is involved |
| 142 |
$threads = $chat->getMessages( |
| 143 |
(new VBOChatSearch) |
| 144 |
->aggregate() |
| 145 |
->forCategories($this->involvedCategories) |
| 146 |
->reader($chat->getUser()->getID()) |
| 147 |
->limit(20) |
| 148 |
); |
| 149 |
|
| 150 |
if ($threads) { |
| 151 |
// render chat threads |
| 152 |
echo JLayoutHelper::render('chat.threads', [ |
| 153 |
'threads' => $threads, |
| 154 |
'categories' => $this->involvedCategories, |
| 155 |
'options' => [ |
| 156 |
'dateformat' => str_replace('/', $this->datesep, $this->df), |
| 157 |
'limit' => 20, |
| 158 |
'compact' => true, |
| 159 |
], |
| 160 |
]); |
| 161 |
} else { |
| 162 |
// render the blank layout |
| 163 |
echo JLayoutHelper::render('chat.blank', []); |
| 164 |
} |
| 165 |
} |
| 166 |
?> |
| 167 |
</div> |
| 168 |
|
| 169 |
<script> |
| 170 |
(function($) { |
| 171 |
'use strict'; |
| 172 |
|
| 173 |
<?php if ($js_intvals_id): ?> |
| 174 |
$(function() { |
| 175 |
/** |
| 176 |
* Register callback function for the widget "resize" event (modal only) |
| 177 |
*/ |
| 178 |
const resize_fn = (e) => { |
| 179 |
const modalContent = $('#<?php echo $wrapper_id; ?>'); |
| 180 |
|
| 181 |
if (modalContent.width() <= 940) { |
| 182 |
modalContent.find('.vbo-chat-interface').addClass('compact'); |
| 183 |
} else { |
| 184 |
modalContent.find('.vbo-chat-interface').removeClass('compact'); |
| 185 |
} |
| 186 |
}; |
| 187 |
|
| 188 |
// add listener for the modal dismissed event |
| 189 |
document.addEventListener(VBOCore.widget_modal_dismissed + '<?php echo $js_intvals_id; ?>', (e) => { |
| 190 |
// get rid of widget resizing events |
| 191 |
document.removeEventListener('vbo-resize-widget-modal<?php echo $js_intvals_id; ?>', resize_fn); |
| 192 |
document.removeEventListener('vbo-admin-dock-restore-<?php echo $this->getIdentifier(); ?>', resize_fn); |
| 193 |
window.removeEventListener('resize', resize_fn); |
| 194 |
|
| 195 |
// destroy the chat |
| 196 |
if (typeof VBOChat !== 'undefined') { |
| 197 |
VBOChat.getInstance().destroy(); |
| 198 |
} |
| 199 |
}, {once: true}); |
| 200 |
|
| 201 |
// register widget resizing events |
| 202 |
document.addEventListener('vbo-resize-widget-modal<?php echo $js_intvals_id; ?>', resize_fn); |
| 203 |
document.addEventListener('vbo-admin-dock-restore-<?php echo $this->getIdentifier(); ?>', resize_fn); |
| 204 |
window.addEventListener('resize', resize_fn); |
| 205 |
}); |
| 206 |
<?php endif; ?> |
| 207 |
})(jQuery); |
| 208 |
</script> |
| 209 |
<?php |
| 210 |
} |
| 211 |
} |
| 212 |
|