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

aitools.php in VikBooking Hotel Booking Engine & PMS trunk, at admin/helpers/widgets/aitools.php

147 lines 3.9 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 srl
6 * @copyright Copyright (C) 2024 E4J srl. 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 "AI Tools".
15 *
16 * @since 1.17 (J) - 1.7 (WP)
17 */
18 class VikBookingAdminWidgetAitools extends VikBookingAdminWidget
19 {
20 /**
21 * @inheritDOc
22 */
23 public function __construct()
24 {
25 // call parent constructor
26 parent::__construct();
27
28 $this->widgetName = JText::translate('VBO_W_AITOOLS_TITLE');
29 $this->widgetDescr = JText::translate('VBO_W_AITOOLS_DESCR');
30 $this->widgetId = basename(__FILE__, '.php');
31
32 $this->widgetIcon = '<i class="' . VikBookingIcons::i('magic') . '"></i>';
33 $this->widgetStyleName = 'violet';
34 }
35
36 /**
37 * @inheritDoc
38 */
39 public function getPriority()
40 {
41 // give this widget a higher priority
42 return 15;
43 }
44
45 /**
46 * @inheritDoc
47 */
48 public function preflight()
49 {
50 if (VBOPlatformDetection::isJoomla()) {
51 // can be used only if VCM is installed and the AI channel is supported (not necessarily active)
52 return class_exists('VikChannelManager') && defined('VikChannelManagerConfig::AI');
53 }
54
55 return parent::preflight();
56 }
57
58 /**
59 * @inheritDoc
60 */
61 public function preload()
62 {
63 $options = [
64 'version' => defined('VIKCHANNELMANAGER_SOFTWARE_VERSION') ? VIKCHANNELMANAGER_SOFTWARE_VERSION : VIKBOOKING_SOFTWARE_VERSION,
65 ];
66
67 if (class_exists('VikChannelManager')) {
68 // manually load required dependencies
69 JHtml::fetch('script', VCM_ADMIN_URI . 'layouts/ai/assistant/aitools.js', $options);
70 JHtml::fetch('stylesheet', VCM_ADMIN_URI . 'layouts/ai/assistant/aitools.css', $options);
71
72 JHtml::fetch('script', VCM_ADMIN_URI . 'assets/js/katex/katex.min.js', $options);
73 JHtml::fetch('script', VCM_ADMIN_URI . 'assets/js/katex/auto-render.min.js', $options);
74 JHtml::fetch('stylesheet', VCM_ADMIN_URI . 'assets/js/katex/katex.min.css', $options);
75
76 // language definitions
77 JText::script('VBO_AI_ASSISTANT_DISCLAIMER');
78 JText::script('VBO_AI_ASSISTANT_DISCOVER_HINT');
79 JText::script('VBO_AI_ASSISTANT_DISCOVER_TITLE');
80 }
81 }
82
83 /**
84 * @inheritDoc
85 */
86 public function render(?VBOMultitaskData $data = null)
87 {
88 $auto_focus = false;
89 $prompt = null;
90 $scope = null;
91
92 // if we are rendering the widget on a modal, register a script to prevent the latter
93 // from dismissing whenever the ESC key is pressed
94 if ($data && $data->isModalRendering()) {
95 $auto_focus = true;
96 $prompt = (array) $this->getOption('prompt', null);
97 $scope = $this->getOption('scope');
98 ?>
99 <script>
100 (function() {
101 const vbo_modal_ai_assistant_dismiss = (event) => {
102 if (event.key == 'Escape') {
103 event.preventDefault();
104 event.stopPropagation();
105 return false;
106 }
107 };
108
109 document.addEventListener('keyup', vbo_modal_ai_assistant_dismiss);
110
111 document.addEventListener(VBOCore.widget_modal_dismissed + '<?php echo $data->getModalJsIdentifier(); ?>', (e) => {
112 document.removeEventListener('keyup', vbo_modal_ai_assistant_dismiss);
113 VBOCore.emitEvent('vbo-widget-ai-tools-dismissed');
114 });
115 })();
116 </script>
117 <?php
118 }
119
120 // display by using a specific layout provided by VCM
121 echo JLayoutHelper::render(
122 'ai.assistant.chat',
123 [
124 // prevent the layout from loading the required assets
125 'load_assets' => false,
126 // auto-focus textarea
127 'auto_focus' => $auto_focus,
128 // default prompt
129 'prompt' => $prompt,
130 // default scope
131 'scope' => $scope,
132 // show widget title (VBO dashboard only)
133 'widget_title' => is_null($data),
134 // widget icon
135 'widget_icon' => $this->widgetIcon,
136 // widget name
137 'widget_name' => $this->widgetName,
138 ],
139 null,
140 [
141 'component' => 'com_vikchannelmanager',
142 'client' => 'admin',
143 ]
144 );
145 }
146 }
147