| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2025 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 |
* VikBooking AI controller. Designed to work with Vik Channel Manager. |
| 16 |
* Requires an active E4jConnect subscription to access the AI services. |
| 17 |
* |
| 18 |
* @since 1.17.3 (J) - 1.7.3 (WP) |
| 19 |
*/ |
| 20 |
class VikBookingControllerAi extends JControllerAdmin |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Performs an audit for the current capabilities before using the AI services. |
| 24 |
* |
| 25 |
* @return void |
| 26 |
* |
| 27 |
* @throws Exception |
| 28 |
*/ |
| 29 |
protected function auditServiceCapabilities() |
| 30 |
{ |
| 31 |
if (!class_exists('VikChannelManager')) { |
| 32 |
throw new Exception('The Channel Manager plugin must be installed and activated.', 400); |
| 33 |
} |
| 34 |
|
| 35 |
if (!defined('VikChannelManagerConfig::AI')) { |
| 36 |
throw new Exception('The Channel Manager plugin must be updated to the latest version.', 400); |
| 37 |
} |
| 38 |
|
| 39 |
if (!VikChannelManager::getApiKey()) { |
| 40 |
throw new Exception('The Channel Manager plugin requires an active E4jConnect subscription.', 400); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* AJAX endpoint to generate the content for a room. |
| 46 |
* |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
public function roomContent() |
| 50 |
{ |
| 51 |
$app = JFactory::getApplication(); |
| 52 |
|
| 53 |
if (!JSession::checkToken()) { |
| 54 |
VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN')); |
| 55 |
} |
| 56 |
|
| 57 |
try { |
| 58 |
// validate AI service capabilities |
| 59 |
$this->auditServiceCapabilities(); |
| 60 |
|
| 61 |
// let the AI model service generate the content |
| 62 |
$content = (new VCMAiModelService)->roomContent( |
| 63 |
$app->input->getAlnum('type', 'short'), |
| 64 |
$app->input->getString('information', 'I have a normal room to rent.'), |
| 65 |
$app->input->getString('language', '') |
| 66 |
); |
| 67 |
} catch (Exception $e) { |
| 68 |
VBOHttpDocument::getInstance($app)->close($e->getCode(), $e->getMessage()); |
| 69 |
} catch (Throwable $e) { |
| 70 |
VBOHttpDocument::getInstance($app)->close($e->getCode() ?: 500, $e->getMessage() ?: 'Unrecoverable error'); |
| 71 |
} |
| 72 |
|
| 73 |
// output the result content |
| 74 |
VBOHttpDocument::getInstance($app)->json(['content' => $content]); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* AJAX endpoint to generate the content for an email message or communication. |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function mailContent() |
| 83 |
{ |
| 84 |
$app = JFactory::getApplication(); |
| 85 |
|
| 86 |
if (!JSession::checkToken()) { |
| 87 |
VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN')); |
| 88 |
} |
| 89 |
|
| 90 |
try { |
| 91 |
// validate AI service capabilities |
| 92 |
$this->auditServiceCapabilities(); |
| 93 |
|
| 94 |
// let the AI model service generate the content |
| 95 |
$content = (new VCMAiModelService)->mailContent( |
| 96 |
$app->input->getString('information', ''), |
| 97 |
$app->input->getString('language', '') |
| 98 |
); |
| 99 |
} catch (Exception $e) { |
| 100 |
VBOHttpDocument::getInstance($app)->close($e->getCode(), $e->getMessage()); |
| 101 |
} catch (Throwable $e) { |
| 102 |
VBOHttpDocument::getInstance($app)->close($e->getCode() ?: 500, $e->getMessage() ?: 'Unrecoverable error'); |
| 103 |
} |
| 104 |
|
| 105 |
// output the result content |
| 106 |
VBOHttpDocument::getInstance($app)->json(['content' => $content]); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* AJAX endpoint to extract a list of appropriate tags from a task manager task. |
| 111 |
* |
| 112 |
* @return void |
| 113 |
* |
| 114 |
* @since 1.18.0 (J) - 1.8.0 (WP) |
| 115 |
*/ |
| 116 |
public function extractTaskTags() |
| 117 |
{ |
| 118 |
$app = JFactory::getApplication(); |
| 119 |
|
| 120 |
if (!JSession::checkToken()) { |
| 121 |
VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN')); |
| 122 |
} |
| 123 |
|
| 124 |
try { |
| 125 |
// validate AI service capabilities |
| 126 |
$this->auditServiceCapabilities(); |
| 127 |
|
| 128 |
// let the AI model service extract the tags |
| 129 |
$tags = (new VCMAiModelService)->extractTaskTags([ |
| 130 |
'area_id' => $app->input->getUInt('area_id', 0), |
| 131 |
'task_id' => $app->input->getUInt('task_id', 0), |
| 132 |
'task_title' => $app->input->getString('task_title', ''), |
| 133 |
'task_notes' => $app->input->getString('task_notes', '', 'raw'), |
| 134 |
'booking_id' => $app->input->getUInt('booking_id', 0), |
| 135 |
'listing_id' => $app->input->getUInt('listing_id', 0), |
| 136 |
]); |
| 137 |
} catch (Exception $e) { |
| 138 |
VBOHttpDocument::getInstance($app)->close($e->getCode(), $e->getMessage()); |
| 139 |
} catch (Throwable $e) { |
| 140 |
VBOHttpDocument::getInstance($app)->close($e->getCode() ?: 500, $e->getMessage() ?: 'Unrecoverable error'); |
| 141 |
} |
| 142 |
|
| 143 |
// output the result tags |
| 144 |
VBOHttpDocument::getInstance($app)->json(['tags' => $tags]); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Task used to summarize the provided conversation for the administrator. |
| 149 |
* |
| 150 |
* @return void |
| 151 |
* |
| 152 |
* @since 1.18.8 (J) - 1.8.8 (WP) |
| 153 |
*/ |
| 154 |
public function summarize() |
| 155 |
{ |
| 156 |
$app = JFactory::getApplication(); |
| 157 |
|
| 158 |
$messages = $app->input->get('messages', [], 'array'); |
| 159 |
|
| 160 |
try { |
| 161 |
if (!JSession::checkToken()) { |
| 162 |
// missing CSRF-proof token |
| 163 |
throw new Exception(JText::translate('JINVALID_TOKEN'), 403); |
| 164 |
} |
| 165 |
|
| 166 |
// validate AI service capabilities |
| 167 |
$this->auditServiceCapabilities(); |
| 168 |
|
| 169 |
$summary = (new VCMAiModelService)->summarizeThread($messages); |
| 170 |
} catch (Exception $error) { |
| 171 |
// something went wrong |
| 172 |
VCMHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage()); |
| 173 |
} |
| 174 |
|
| 175 |
VCMHttpDocument::getInstance($app)->json([ |
| 176 |
'result' => $summary, |
| 177 |
]); |
| 178 |
} |
| 179 |
} |
| 180 |
|