| 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 |
* Task area implementation. |
| 16 |
* |
| 17 |
* @since 1.18.0 (J) - 1.8.0 (WP) |
| 18 |
*/ |
| 19 |
final class VBOTaskArea |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
protected $record = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* Proxy to construct the object from a task area record ID. |
| 28 |
* |
| 29 |
* @param int $recordId The task area record ID. |
| 30 |
* |
| 31 |
* @return VBOTaskArea |
| 32 |
*/ |
| 33 |
public static function getRecordInstance(int $recordId) |
| 34 |
{ |
| 35 |
$area = VBOTaskModelArea::getInstance()->getItem($recordId); |
| 36 |
|
| 37 |
return new static((array) $area); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Proxy to construct the object. |
| 42 |
* |
| 43 |
* @param array $record The task area record. |
| 44 |
* |
| 45 |
* @return VBOTaskArea |
| 46 |
*/ |
| 47 |
public static function getInstance(array $record) |
| 48 |
{ |
| 49 |
return new static($record); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Class constructor. |
| 54 |
* |
| 55 |
* @param array $record The task area record. |
| 56 |
* |
| 57 |
* @throws Exception |
| 58 |
*/ |
| 59 |
public function __construct(array $record) |
| 60 |
{ |
| 61 |
if (empty($record['id'])) { |
| 62 |
throw new Exception('Missing task area record ID.', 500); |
| 63 |
} |
| 64 |
|
| 65 |
if (!empty($record['settings']) && is_string($record['settings'])) { |
| 66 |
// decode task area params into settings |
| 67 |
$record['settings'] = json_decode($record['settings'], true); |
| 68 |
} |
| 69 |
|
| 70 |
if (!empty($record['tags']) && is_string($record['tags'])) { |
| 71 |
// decode task area tags |
| 72 |
$record['tags'] = array_filter(array_map('intval', (array) json_decode($record['tags'], true))); |
| 73 |
} |
| 74 |
|
| 75 |
if (!empty($record['status_enums']) && is_string($record['status_enums'])) { |
| 76 |
// decode task status enumerations |
| 77 |
$record['status_enums'] = array_filter((array) json_decode($record['status_enums'], true)); |
| 78 |
} |
| 79 |
|
| 80 |
// always cast settings to an array |
| 81 |
$record['settings'] = (array) $record['settings']; |
| 82 |
|
| 83 |
// set internal record property |
| 84 |
$this->record = $record; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Loads the current task area settings. |
| 89 |
* |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
public function loadSettings() |
| 93 |
{ |
| 94 |
return $this->record['settings']; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Saves the current task area settings. |
| 99 |
* |
| 100 |
* @param array $settings List of settings to save. |
| 101 |
* @param bool $merge True for merging the settings. |
| 102 |
* |
| 103 |
* @return bool |
| 104 |
*/ |
| 105 |
public function saveSettings(array $settings, $merge = true) |
| 106 |
{ |
| 107 |
$dbo = JFactory::getDbo(); |
| 108 |
|
| 109 |
if ($merge && !empty($this->record['settings'])) { |
| 110 |
$settings = array_merge($this->record['settings'], $settings); |
| 111 |
} |
| 112 |
|
| 113 |
$dbo->setQuery( |
| 114 |
$dbo->getQuery(true) |
| 115 |
->update($dbo->qn('#__vikbooking_tm_areas')) |
| 116 |
->set($dbo->qn('settings') . ' = ' . $dbo->q(json_encode($settings))) |
| 117 |
->where($dbo->qn('id') . ' = ' . $this->record['id']) |
| 118 |
); |
| 119 |
|
| 120 |
return (bool) $dbo->execute(); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Returns the current task area record. |
| 125 |
* |
| 126 |
* @return array |
| 127 |
*/ |
| 128 |
public function getRecord() |
| 129 |
{ |
| 130 |
return $this->record; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Attempts to fetch the requested record property. |
| 135 |
* |
| 136 |
* @param string $prop The record property to get. |
| 137 |
* @param mixed $default The default value to return. |
| 138 |
* |
| 139 |
* @return mixed |
| 140 |
*/ |
| 141 |
public function get(string $prop, $default = null) |
| 142 |
{ |
| 143 |
return $this->record[$prop] ?? $default; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Returns the task area ID. |
| 148 |
* |
| 149 |
* @return int |
| 150 |
*/ |
| 151 |
public function getID() |
| 152 |
{ |
| 153 |
return (int) $this->get('id'); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Returns the task area name. |
| 158 |
* |
| 159 |
* @return string |
| 160 |
*/ |
| 161 |
public function getName() |
| 162 |
{ |
| 163 |
return $this->get('name', ''); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Returns the list of tag IDs for the current task area. |
| 168 |
* |
| 169 |
* @return array |
| 170 |
*/ |
| 171 |
public function getTags() |
| 172 |
{ |
| 173 |
return (array) $this->get('tags', []); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Returns the list of tag records for the current task area. |
| 178 |
* |
| 179 |
* @return array |
| 180 |
*/ |
| 181 |
public function getTagRecords() |
| 182 |
{ |
| 183 |
return VBOFactory::getTaskManager()->getColorTags($this->getTags()); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Returns the list of status enumerations for the current task area. |
| 188 |
* |
| 189 |
* @return array |
| 190 |
*/ |
| 191 |
public function getStatuses() |
| 192 |
{ |
| 193 |
return (array) $this->get('status_enums', []); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Returns a list of statuses to be rendered as elements for the current task area. |
| 198 |
* |
| 199 |
* @param string $activeStatus Optional task active status to ensure it exists. |
| 200 |
* |
| 201 |
* @return array |
| 202 |
*/ |
| 203 |
public function getStatusElements(string $activeStatus = '') |
| 204 |
{ |
| 205 |
$statuses = $this->getStatuses(); |
| 206 |
|
| 207 |
if ($activeStatus && !in_array($activeStatus, $statuses)) { |
| 208 |
// append the active status that would not be available otherwise |
| 209 |
$statuses[] = $activeStatus; |
| 210 |
} |
| 211 |
|
| 212 |
return VBOFactory::getTaskManager()->getStatusGroupElements($statuses); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Returns the task area default status for new tasks. |
| 217 |
* |
| 218 |
* @return string The default status enumeration. |
| 219 |
*/ |
| 220 |
public function getDefaultStatus() |
| 221 |
{ |
| 222 |
// get all area status types sorted by group |
| 223 |
$statuses = $this->getStatusElements(); |
| 224 |
|
| 225 |
// iterate over the status groups |
| 226 |
foreach ($statuses as $statusGroup) { |
| 227 |
// iterate over the first group of elements |
| 228 |
foreach ($statusGroup['elements'] as $status) { |
| 229 |
// return the first status enumeration |
| 230 |
return $status['id']; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return ''; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Returns the task area icon (class). |
| 239 |
* |
| 240 |
* @return string |
| 241 |
*/ |
| 242 |
public function getIcon() |
| 243 |
{ |
| 244 |
$area_icon = $this->get('icon', ''); |
| 245 |
|
| 246 |
if (!empty($area_icon)) { |
| 247 |
// custom area icon was set |
| 248 |
return $area_icon; |
| 249 |
} |
| 250 |
|
| 251 |
$taskManager = VBOFactory::getTaskManager(); |
| 252 |
if ($taskManager->driverExists($this->getType())) { |
| 253 |
// get the icon implemented by the task driver |
| 254 |
$task_icon = $taskManager->getDriverInstance($this->getType(), [$this])->getIcon(); |
| 255 |
|
| 256 |
if (!empty($task_icon)) { |
| 257 |
return $task_icon; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
return 'tasks'; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Returns the task area type (task driver instance id). |
| 266 |
* |
| 267 |
* @return string |
| 268 |
*/ |
| 269 |
public function getType() |
| 270 |
{ |
| 271 |
return $this->get('instanceof', ''); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Returns the default area duration in minutes. |
| 276 |
* |
| 277 |
* @return int |
| 278 |
*/ |
| 279 |
public function getDefaultDuration() |
| 280 |
{ |
| 281 |
// the driver may declare a parameter within the area/project for the task default duration in minutes |
| 282 |
return intval(($this->record['settings']['taskduration'] ?? 0)) ?: 60; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Returns the eligible operator IDs for the area. |
| 287 |
* |
| 288 |
* @return array List of eligible operator IDs or empty array. |
| 289 |
*/ |
| 290 |
public function getOperatorIds() |
| 291 |
{ |
| 292 |
// the driver may declare a parameter within the area/project to filter the eligible operators |
| 293 |
return array_values(array_filter((array) ($this->record['settings']['operators'] ?? []))); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Returns the eligible listing IDs for the area. |
| 298 |
* |
| 299 |
* @return array List of eligible listing IDs or empty array. |
| 300 |
*/ |
| 301 |
public function getListingIds() |
| 302 |
{ |
| 303 |
// the driver may declare a parameter within the area/project to filter the eligible listings |
| 304 |
return array_values(array_filter((array) ($this->record['settings']['listings'] ?? []))); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Tells whether the area is private, hence not visible to operators. |
| 309 |
* |
| 310 |
* @return bool True if private, false otherwise. |
| 311 |
*/ |
| 312 |
public function isPrivate() |
| 313 |
{ |
| 314 |
// the driver may declare a parameter within the area/project to define the private visibility |
| 315 |
return (bool) ($this->record['settings']['private'] ?? 0); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Tells whether the area allows AI to create tasks from guest requests. |
| 320 |
* |
| 321 |
* @return bool |
| 322 |
* |
| 323 |
* @since 1.18.4 (J) - 1.8.4 (WP) |
| 324 |
*/ |
| 325 |
public function isAiCapable() |
| 326 |
{ |
| 327 |
if (isset($this->record['settings']['ai'])) { |
| 328 |
return (bool) $this->record['settings']['ai']; |
| 329 |
} |
| 330 |
|
| 331 |
// check if the driver implements an AI support parameter |
| 332 |
$taskManager = VBOFactory::getTaskManager(); |
| 333 |
if ($taskManager->driverExists($this->getType())) { |
| 334 |
// access the task driver parameters |
| 335 |
$params = $taskManager->getDriverInstance($this->getType(), [$this])->getParams(); |
| 336 |
|
| 337 |
return (bool) ($params['ai']['default'] ?? false); |
| 338 |
} |
| 339 |
|
| 340 |
return false; |
| 341 |
} |
| 342 |
} |
| 343 |
|