| 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 manager implementation. |
| 16 |
* |
| 17 |
* @since 1.18.0 (J) - 1.8.0 (WP) |
| 18 |
*/ |
| 19 |
final class VBOTaskManager |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
private $drivers = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
private $errors = []; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
private $statusGroupTypes = []; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
private $statusTypes = []; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
private $taskClassPrefix = 'VBOTaskDriver'; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
private $taskStatusGroupClassPrefix = 'VBOTaskStatusGroupType'; |
| 50 |
|
| 51 |
/** |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
private $taskStatusClassPrefix = 'VBOTaskStatusType'; |
| 55 |
|
| 56 |
/** |
| 57 |
* Class constructor. |
| 58 |
*/ |
| 59 |
public function __construct() |
| 60 |
{ |
| 61 |
// pre-load the available task drivers |
| 62 |
$this->loadDrivers(); |
| 63 |
|
| 64 |
// pre-load the available task status group type implementations |
| 65 |
$this->loadStatusGroupTypes(); |
| 66 |
|
| 67 |
// pre-load the available task status type implementations |
| 68 |
$this->loadStatusTypes(); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Triggers the operations for scheduling tasks across all |
| 73 |
* projects/areas upon a booking confirmation event. |
| 74 |
* |
| 75 |
* @param array $booking The booking record. |
| 76 |
* @param array $booking_rooms The booking room records. |
| 77 |
* |
| 78 |
* @return bool |
| 79 |
*/ |
| 80 |
public function processBookingConfirmation(array $booking, array $booking_rooms = []) |
| 81 |
{ |
| 82 |
// reset the errors pool before starting |
| 83 |
$this->errors = []; |
| 84 |
|
| 85 |
// wrap the booking information into a registry |
| 86 |
$taskBooking = VBOTaskBooking::getInstance($booking, $booking_rooms); |
| 87 |
|
| 88 |
// iterate over all the active projects/areas, if any |
| 89 |
foreach ($this->getAreas() as $area) { |
| 90 |
// wrap the execution within a try-catch statement |
| 91 |
try { |
| 92 |
// bind the area record within a task-area registry |
| 93 |
$area = VBOTaskArea::getInstance((array) $area); |
| 94 |
|
| 95 |
// invoke the task area driver |
| 96 |
$taskDriver = $this->getDriverInstance($area->getType(), [$area]); |
| 97 |
|
| 98 |
// schedule tasks upon booking confirmation |
| 99 |
$taskDriver->scheduleBookingConfirmation($taskBooking); |
| 100 |
|
| 101 |
if ($newTasks = $taskDriver->getCollector()->getCreated()) { |
| 102 |
// store booking history record |
| 103 |
VikBooking::getBookingHistoryInstance($taskBooking->getID()) |
| 104 |
->setBookingData($booking, $booking_rooms) |
| 105 |
->setExtraData(array_column($newTasks, 'id')) |
| 106 |
->store('NT', implode(', ', array_map(function($id) { |
| 107 |
return sprintf('#%d', $id); |
| 108 |
}, array_column($newTasks, 'id')))); |
| 109 |
} |
| 110 |
} catch (Throwable $e) { |
| 111 |
// push the error caught |
| 112 |
$this->errors[] = $e; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return (bool) (!$this->errors); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Triggers the operations for re-scheduling tasks across all |
| 121 |
* projects/areas upon a booking modification event. |
| 122 |
* |
| 123 |
* @param array $booking The booking record. |
| 124 |
* @param array $booking_rooms The booking room records. |
| 125 |
* @param array $prev_booking The previous booking record. |
| 126 |
* |
| 127 |
* @return bool |
| 128 |
*/ |
| 129 |
public function processBookingModification(array $booking, array $booking_rooms = [], array $prev_booking = []) |
| 130 |
{ |
| 131 |
// reset the errors pool before starting |
| 132 |
$this->errors = []; |
| 133 |
|
| 134 |
// wrap the booking information into a registry |
| 135 |
$taskBooking = VBOTaskBooking::getInstance($booking, $booking_rooms, $prev_booking); |
| 136 |
|
| 137 |
// iterate over all the active projects/areas, if any |
| 138 |
foreach ($this->getAreas() as $area) { |
| 139 |
// wrap the execution within a try-catch statement |
| 140 |
try { |
| 141 |
// bind the area record within a task-area registry |
| 142 |
$area = VBOTaskArea::getInstance((array) $area); |
| 143 |
|
| 144 |
// invoke the task area driver |
| 145 |
$taskDriver = $this->getDriverInstance($area->getType(), [$area]); |
| 146 |
|
| 147 |
// re-schedule tasks upon booking alteration, if needed |
| 148 |
$taskDriver->scheduleBookingAlteration($taskBooking); |
| 149 |
|
| 150 |
if ($modifiedTasks = $taskDriver->getCollector()->getModified()) { |
| 151 |
// store booking history record |
| 152 |
VikBooking::getBookingHistoryInstance($taskBooking->getID()) |
| 153 |
->setBookingData($booking, $booking_rooms) |
| 154 |
->setExtraData(array_column($modifiedTasks, 'id')) |
| 155 |
->store('MT', implode(', ', array_map(function($id) { |
| 156 |
return sprintf('#%d', $id); |
| 157 |
}, array_column($modifiedTasks, 'id')))); |
| 158 |
} |
| 159 |
} catch (Throwable $e) { |
| 160 |
// push the error caught |
| 161 |
$this->errors[] = $e; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return (bool) (!$this->errors); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Triggers the operations for un-scheduling tasks across all |
| 170 |
* projects/areas upon a booking cancellation event. |
| 171 |
* |
| 172 |
* @param array $booking The booking record. |
| 173 |
* @param array $booking_rooms The booking room records. |
| 174 |
* |
| 175 |
* @return bool |
| 176 |
*/ |
| 177 |
public function processBookingCancellation(array $booking, array $booking_rooms = []) |
| 178 |
{ |
| 179 |
// reset the errors pool before starting |
| 180 |
$this->errors = []; |
| 181 |
|
| 182 |
// wrap the booking information into a registry |
| 183 |
$taskBooking = VBOTaskBooking::getInstance($booking, $booking_rooms); |
| 184 |
|
| 185 |
// iterate over all the active projects/areas, if any |
| 186 |
foreach ($this->getAreas() as $area) { |
| 187 |
// wrap the execution within a try-catch statement |
| 188 |
try { |
| 189 |
// bind the area record within a task-area registry |
| 190 |
$area = VBOTaskArea::getInstance((array) $area); |
| 191 |
|
| 192 |
// invoke the task area driver |
| 193 |
$taskDriver = $this->getDriverInstance($area->getType(), [$area]); |
| 194 |
|
| 195 |
// un-schedule tasks upon booking cancellation |
| 196 |
$taskDriver->scheduleBookingCancellation($taskBooking); |
| 197 |
|
| 198 |
if ($oldTasks = $taskDriver->getCollector()->getCancelled()) { |
| 199 |
// store booking history record |
| 200 |
VikBooking::getBookingHistoryInstance($taskBooking->getID()) |
| 201 |
->setBookingData($booking, $booking_rooms) |
| 202 |
->setExtraData(array_column($oldTasks, 'id')) |
| 203 |
->store('CT', implode(', ', array_map(function($id) { |
| 204 |
return sprintf('#%d', $id); |
| 205 |
}, array_column($oldTasks, 'id')))); |
| 206 |
} |
| 207 |
} catch (Throwable $e) { |
| 208 |
// push the error caught |
| 209 |
$this->errors[] = $e; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
return (bool) (!$this->errors); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Returns the current execution errors, if any. |
| 218 |
* |
| 219 |
* @return array |
| 220 |
*/ |
| 221 |
public function getErrors() |
| 222 |
{ |
| 223 |
return $this->errors; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Resets the current execution errors. |
| 228 |
* |
| 229 |
* @return VBOTaskManager |
| 230 |
*/ |
| 231 |
public function resetErrors() |
| 232 |
{ |
| 233 |
$this->errors = []; |
| 234 |
|
| 235 |
return $this; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Returns all the active task area objects. |
| 240 |
* |
| 241 |
* @return array |
| 242 |
*/ |
| 243 |
public function getAreas() |
| 244 |
{ |
| 245 |
return VBOTaskModelArea::getInstance()->getItems(); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Returns all areas with an active visibility by default. |
| 250 |
* Relies on the current session by default, then on the db. |
| 251 |
* |
| 252 |
* @param int $start Query limit start. |
| 253 |
* @param int $lim Query records limit. |
| 254 |
* |
| 255 |
* @return array List of visible area items, if any. |
| 256 |
*/ |
| 257 |
public function getVisibleAreas(int $start = 0, int $lim = 0) |
| 258 |
{ |
| 259 |
$active_area_ids = (array) JFactory::getSession()->get('tm.active_area_ids', [], 'vikbooking'); |
| 260 |
|
| 261 |
if ($active_area_ids) { |
| 262 |
// get all areas stored in the session |
| 263 |
return VBOTaskModelArea::getInstance()->getItems([ |
| 264 |
'id' => [ |
| 265 |
'value' => $active_area_ids, |
| 266 |
], |
| 267 |
], 0, 0); |
| 268 |
} |
| 269 |
|
| 270 |
// read the active areas from the db |
| 271 |
$active_areas = VBOTaskModelArea::getInstance()->getItems([ |
| 272 |
'display' => [ |
| 273 |
'value' => 1, |
| 274 |
], |
| 275 |
], $start, $lim); |
| 276 |
|
| 277 |
// set them as active |
| 278 |
$this->setVisibleArea(array_column($active_areas, 'id')); |
| 279 |
|
| 280 |
return $active_areas; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Sets visible areas in the PHP Session. |
| 285 |
* |
| 286 |
* @param int|array $id The area ID(s) to set as visible. |
| 287 |
* |
| 288 |
* @return void |
| 289 |
*/ |
| 290 |
public function setVisibleArea($id) |
| 291 |
{ |
| 292 |
$session = JFactory::getSession(); |
| 293 |
|
| 294 |
if (!is_array($id)) { |
| 295 |
$id = (array) $id; |
| 296 |
} |
| 297 |
|
| 298 |
$id = array_map('intval', $id); |
| 299 |
|
| 300 |
$active_area_ids = array_map('intval', (array) $session->get('tm.active_area_ids', [], 'vikbooking')); |
| 301 |
$active_area_ids = array_values(array_unique(array_merge($active_area_ids, $id))); |
| 302 |
|
| 303 |
$session->set('tm.active_area_ids', $active_area_ids, 'vikbooking'); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Unsets visible areas in the PHP Session. |
| 308 |
* |
| 309 |
* @param int|array $id The area ID(s) to unset as visible. |
| 310 |
* |
| 311 |
* @return void |
| 312 |
*/ |
| 313 |
public function unsetVisibleArea($id) |
| 314 |
{ |
| 315 |
$session = JFactory::getSession(); |
| 316 |
|
| 317 |
if (!is_array($id)) { |
| 318 |
$id = (array) $id; |
| 319 |
} |
| 320 |
|
| 321 |
$id = array_map('intval', $id); |
| 322 |
|
| 323 |
$active_area_ids = array_map('intval', (array) $session->get('tm.active_area_ids', [], 'vikbooking')); |
| 324 |
$active_area_ids = array_values(array_unique(array_diff($active_area_ids, $id))); |
| 325 |
|
| 326 |
$session->set('tm.active_area_ids', $active_area_ids, 'vikbooking'); |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Returns a list of areas that were configured as private, |
| 331 |
* hence not visible to operators within the front-end. |
| 332 |
* |
| 333 |
* @return array List of private area IDs, or empty array. |
| 334 |
*/ |
| 335 |
public function getPrivateAreas() |
| 336 |
{ |
| 337 |
$privateAreaIds = []; |
| 338 |
|
| 339 |
foreach ($this->getAreas() as $areaRecord) { |
| 340 |
$area = VBOTaskArea::getInstance((array) $areaRecord); |
| 341 |
if ($area->isPrivate()) { |
| 342 |
$privateAreaIds[] = $area->getID(); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
return array_values(array_filter($privateAreaIds)); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Returns a list of default tag colors. |
| 351 |
* |
| 352 |
* @param bool $keys True to return only the color identifiers. |
| 353 |
* |
| 354 |
* @return array |
| 355 |
*/ |
| 356 |
public function getTagColors(bool $keys = false) |
| 357 |
{ |
| 358 |
$def_tag_colors = [ |
| 359 |
'red' => '#fbdcd9', |
| 360 |
'green' => '#daebdc', |
| 361 |
'olive' => '#c7d8b4', |
| 362 |
'blue' => '#bed6fb', |
| 363 |
'ocean' => '#d2e5f2', |
| 364 |
'brown' => '#f0dfd7', |
| 365 |
'yellow' => '#f8e5b3', |
| 366 |
'orange' => '#ffe3ca', |
| 367 |
'purple' => '#e8ddee', |
| 368 |
'pink' => '#f6dfe9', |
| 369 |
'black' => '#d0d0d0', |
| 370 |
'gray' => '#e5e4e0', |
| 371 |
]; |
| 372 |
|
| 373 |
return $keys ? array_keys($def_tag_colors) : $def_tag_colors; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Returns a default list of color tags to be used when none is available. |
| 378 |
* |
| 379 |
* @return object[] List of dummy color tag objects. |
| 380 |
*/ |
| 381 |
public function buildDefaultColorTags() |
| 382 |
{ |
| 383 |
// build the default list of color tags |
| 384 |
$defaultTags = [ |
| 385 |
[ |
| 386 |
'name' => JText::translate('VBO_IMPORTANT'), |
| 387 |
'color' => 'red', |
| 388 |
], |
| 389 |
[ |
| 390 |
'name' => JText::translate('VBO_SUPERVISOR_REVIEW'), |
| 391 |
'color' => 'yellow', |
| 392 |
], |
| 393 |
[ |
| 394 |
'name' => JText::translate('VBO_TM_SCHED_CLEANING_TURNOVER'), |
| 395 |
'color' => 'blue', |
| 396 |
], |
| 397 |
[ |
| 398 |
'name' => JText::translate('VBO_TM_SCHED_CLEANING_DAILY'), |
| 399 |
'color' => 'green', |
| 400 |
], |
| 401 |
[ |
| 402 |
'name' => JText::translate('VBO_CHANGE_LINENS'), |
| 403 |
'color' => 'ocean', |
| 404 |
], |
| 405 |
[ |
| 406 |
'name' => JText::translate('VBO_TM_SCHED_CLEANING_WEEKLY'), |
| 407 |
'color' => 'olive', |
| 408 |
], |
| 409 |
[ |
| 410 |
'name' => JText::translate('VBO_DEEP_CLEANING'), |
| 411 |
'color' => 'purple', |
| 412 |
], |
| 413 |
[ |
| 414 |
'name' => JText::translate('VBO_INSPECTION_NEEDED'), |
| 415 |
'color' => 'orange', |
| 416 |
], |
| 417 |
[ |
| 418 |
'name' => JText::translate('VBO_GUEST_REQUEST'), |
| 419 |
'color' => 'pink', |
| 420 |
], |
| 421 |
[ |
| 422 |
'name' => JText::translate('VBO_MAINTENANCE_ALERT'), |
| 423 |
'color' => 'brown', |
| 424 |
], |
| 425 |
[ |
| 426 |
'name' => JText::translate('VBO_NO_SERVICE_REQUESTED'), |
| 427 |
'color' => 'gray', |
| 428 |
], |
| 429 |
[ |
| 430 |
'name' => JText::translate('VBO_HIGH_PRIORITY'), |
| 431 |
'color' => 'black', |
| 432 |
], |
| 433 |
]; |
| 434 |
|
| 435 |
// cast to objects |
| 436 |
foreach ($defaultTags as &$tag) { |
| 437 |
$tag = (object) $tag; |
| 438 |
} |
| 439 |
|
| 440 |
unset($tag); |
| 441 |
|
| 442 |
return $defaultTags; |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Returns all the available or requested color tags. |
| 447 |
* |
| 448 |
* @param array $ids Optional list of tag IDs to fetch. |
| 449 |
* |
| 450 |
* @return array |
| 451 |
*/ |
| 452 |
public function getColorTags(array $ids = []) |
| 453 |
{ |
| 454 |
// access the color tags model |
| 455 |
$ctagModel = VBOTaskModelColortag::getInstance(); |
| 456 |
|
| 457 |
if ($ids) { |
| 458 |
// return the requested tag IDs |
| 459 |
return $ctagModel->getItems([ |
| 460 |
'id' => [ |
| 461 |
'value' => $ids, |
| 462 |
], |
| 463 |
]); |
| 464 |
} |
| 465 |
|
| 466 |
// load all items |
| 467 |
$tags = $ctagModel->getItems(); |
| 468 |
|
| 469 |
if (!$tags) { |
| 470 |
// create at runtime the default tags for the first time |
| 471 |
$tags = $this->buildDefaultColorTags(); |
| 472 |
|
| 473 |
// store the default tags |
| 474 |
foreach ($tags as $tag) { |
| 475 |
$tag->id = $ctagModel->save($tag); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
return $tags; |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Attempts to instantiate the requested driver by passing the provided constructor arguments. |
| 484 |
* |
| 485 |
* @param string $driver The driver file key identifier. |
| 486 |
* @param array $args List of arguments for constructing the object. |
| 487 |
* |
| 488 |
* @return VBOTaskDriverinterface |
| 489 |
* |
| 490 |
* @throws InvalidArgumentException |
| 491 |
*/ |
| 492 |
public function getDriverInstance(string $driver, array $args = []) |
| 493 |
{ |
| 494 |
$className = $this->buildDriverClassName($driver); |
| 495 |
|
| 496 |
if (!class_exists($className)) { |
| 497 |
throw new InvalidArgumentException(sprintf('Could not load task driver [%s]', $driver), 500); |
| 498 |
} |
| 499 |
|
| 500 |
// construct the task driver object by passing the args through the splat operator |
| 501 |
return new $className(...$args); |
| 502 |
} |
| 503 |
|
| 504 |
/** |
| 505 |
* Returns the associative list of the available driver names. |
| 506 |
* |
| 507 |
* @param array $args Optional list of arguments for constructing the objects. |
| 508 |
* |
| 509 |
* @return array |
| 510 |
*/ |
| 511 |
public function getDriverNames(array $args = []) |
| 512 |
{ |
| 513 |
$list = []; |
| 514 |
|
| 515 |
foreach ($this->drivers as $key => $path) { |
| 516 |
try { |
| 517 |
$taskDriver = $this->getDriverInstance($key, $args); |
| 518 |
$driverId = $taskDriver->getID() ?: $key; |
| 519 |
$list[$driverId] = $taskDriver->getName(); |
| 520 |
} catch (Exception $e) { |
| 521 |
// silently catch the error |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
return $list; |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* Returns the list of the drivers loaded so far. |
| 530 |
* |
| 531 |
* @return array |
| 532 |
*/ |
| 533 |
public function getDrivers() |
| 534 |
{ |
| 535 |
return $this->drivers; |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Tells whether a driver exists, meaning that it was loaded. |
| 540 |
* |
| 541 |
* @param string $driver The driver file key identifier. |
| 542 |
* |
| 543 |
* @return bool |
| 544 |
*/ |
| 545 |
public function driverExists(string $driver) |
| 546 |
{ |
| 547 |
return isset($this->drivers[$driver]); |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* Builds the current task booking information for rendering the record as element. |
| 552 |
* |
| 553 |
* @param int $bid The booking record ID. |
| 554 |
* |
| 555 |
* @return array |
| 556 |
*/ |
| 557 |
public function buildBookingElement(int $bid) |
| 558 |
{ |
| 559 |
if (!$bid) { |
| 560 |
return []; |
| 561 |
} |
| 562 |
|
| 563 |
$booking = VikBooking::getBookingInfoFromID($bid); |
| 564 |
if (!$booking) { |
| 565 |
return []; |
| 566 |
} |
| 567 |
|
| 568 |
$customer = VikBooking::getCPinInstance()->getCustomerFromBooking($booking['id']); |
| 569 |
|
| 570 |
// build booking element |
| 571 |
$element = [ |
| 572 |
'id' => $booking['id'], |
| 573 |
'text' => $booking['id'], |
| 574 |
'img' => '', |
| 575 |
'icon_class' => VikBookingIcons::i('hotel'), |
| 576 |
]; |
| 577 |
|
| 578 |
if (!empty($customer['first_name'])) { |
| 579 |
// use customer nominative when available |
| 580 |
$element['text'] = trim($customer['first_name'] . ' ' . $customer['last_name']); |
| 581 |
} elseif (!empty($booking['custdata'])) { |
| 582 |
$element['text'] = VikBooking::getFirstCustDataField($booking['custdata']); |
| 583 |
} |
| 584 |
|
| 585 |
// build "img" property |
| 586 |
if (!empty($customer['pic'])) { |
| 587 |
// use guest profile picture |
| 588 |
$element['img'] = strpos($customer['pic'], 'http') === 0 ? $customer['pic'] : VBO_SITE_URI . 'resources/uploads/' . $customer['pic']; |
| 589 |
} elseif (!empty($booking['channel'])) { |
| 590 |
// use channel logo |
| 591 |
$ch_logo_obj = VikBooking::getVcmChannelsLogo($booking['channel'], true); |
| 592 |
$element['img'] = is_object($ch_logo_obj) ? $ch_logo_obj->getTinyLogoURL() : ''; |
| 593 |
} |
| 594 |
|
| 595 |
if (!empty($element['img'])) { |
| 596 |
// unset the default icon class |
| 597 |
unset($element['icon_class']); |
| 598 |
} |
| 599 |
|
| 600 |
return $element; |
| 601 |
} |
| 602 |
|
| 603 |
/** |
| 604 |
* Returns a list of task statuses sorted by group types to be rendered as elements. |
| 605 |
* |
| 606 |
* @param array $statuses Optional list of task status enumerations. |
| 607 |
* @param bool $flatten True to ignore the groups and return a linear list of statuses. |
| 608 |
* |
| 609 |
* @return array |
| 610 |
*/ |
| 611 |
public function getStatusGroupElements(array $statuses = [], bool $flatten = false) |
| 612 |
{ |
| 613 |
$groupElements = []; |
| 614 |
|
| 615 |
if (!$statuses) { |
| 616 |
$statuses = $this->getStatusTypes(true); |
| 617 |
} |
| 618 |
|
| 619 |
foreach ($statuses as $statusId) { |
| 620 |
// get status type object |
| 621 |
$statusType = $this->getStatusTypeInstance($statusId); |
| 622 |
|
| 623 |
// get status type values |
| 624 |
$statusEnum = $statusType->getEnum(); |
| 625 |
$statusName = $statusType->getName(); |
| 626 |
$statusColor = $statusType->getColor(); |
| 627 |
$statusGroup = $statusType->getGroupEnum(); |
| 628 |
$statusOrdering = $statusType->getOrdering(); |
| 629 |
|
| 630 |
// get status group details |
| 631 |
$groupName = $statusGroup; |
| 632 |
$groupOrdering = 1; |
| 633 |
if ($this->statusGroupTypeExists($statusGroup)) { |
| 634 |
// get status group type object |
| 635 |
$groupType = $this->getStatusGroupTypeInstance($statusGroup); |
| 636 |
|
| 637 |
// set status group details |
| 638 |
$groupName = $groupType->getName(); |
| 639 |
$groupOrdering = $groupType->getOrdering(); |
| 640 |
} |
| 641 |
|
| 642 |
if (!isset($groupElements[$statusGroup])) { |
| 643 |
// start group container |
| 644 |
$groupElements[$statusGroup] = [ |
| 645 |
'text' => $groupName, |
| 646 |
'ordering' => $groupOrdering, |
| 647 |
'elements' => [], |
| 648 |
]; |
| 649 |
} |
| 650 |
|
| 651 |
// push status |
| 652 |
$groupElements[$statusGroup]['elements'][] = [ |
| 653 |
'id' => $statusEnum, |
| 654 |
'text' => $statusName, |
| 655 |
'color' => $statusColor, |
| 656 |
'ordering' => $statusOrdering, |
| 657 |
]; |
| 658 |
} |
| 659 |
|
| 660 |
// sort groups by ordering value ascending |
| 661 |
uasort($groupElements, function($a, $b) { |
| 662 |
return $a['ordering'] <=> $b['ordering']; |
| 663 |
}); |
| 664 |
|
| 665 |
// iterate all status groups to sort the statuses by ordering |
| 666 |
foreach ($groupElements as &$statusGroup) { |
| 667 |
// sort statuses by ordering value ascending |
| 668 |
usort($statusGroup['elements'], function($a, $b) { |
| 669 |
return $a['ordering'] <=> $b['ordering']; |
| 670 |
}); |
| 671 |
} |
| 672 |
|
| 673 |
// unset last reference |
| 674 |
unset($statusGroup); |
| 675 |
|
| 676 |
if ($flatten) { |
| 677 |
$statuses = []; |
| 678 |
|
| 679 |
foreach ($groupElements as $group) { |
| 680 |
foreach ($group['elements'] as $status) { |
| 681 |
$statuses[] = $status; |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
$groupElements = $statuses; |
| 686 |
} |
| 687 |
|
| 688 |
// return the sorted list |
| 689 |
return $groupElements; |
| 690 |
} |
| 691 |
|
| 692 |
/** |
| 693 |
* Attempts to instantiate the requested status group type. |
| 694 |
* |
| 695 |
* @param string $group The group file key identifier. |
| 696 |
* |
| 697 |
* @return VBOTaskStatusGroupInterface |
| 698 |
* |
| 699 |
* @throws InvalidArgumentException |
| 700 |
*/ |
| 701 |
public function getStatusGroupTypeInstance(string $group) |
| 702 |
{ |
| 703 |
$className = $this->buildStatusGroupTypeClassName($group); |
| 704 |
|
| 705 |
if (!class_exists($className)) { |
| 706 |
throw new InvalidArgumentException(sprintf('Could not load task status group type [%s]', $group), 500); |
| 707 |
} |
| 708 |
|
| 709 |
return new $className; |
| 710 |
} |
| 711 |
|
| 712 |
/** |
| 713 |
* Returns the list of the task status group types loaded so far. |
| 714 |
* |
| 715 |
* @return array |
| 716 |
*/ |
| 717 |
public function getStatusGroupTypes() |
| 718 |
{ |
| 719 |
return $this->statusGroupTypes; |
| 720 |
} |
| 721 |
|
| 722 |
/** |
| 723 |
* Tells whether a status group type exists, meaning that it was loaded. |
| 724 |
* |
| 725 |
* @param string $group The group file key identifier. |
| 726 |
* |
| 727 |
* @return bool |
| 728 |
*/ |
| 729 |
public function statusGroupTypeExists(string $group) |
| 730 |
{ |
| 731 |
return isset($this->statusGroupTypes[$group]); |
| 732 |
} |
| 733 |
|
| 734 |
/** |
| 735 |
* Attempts to instantiate the requested status type. |
| 736 |
* |
| 737 |
* @param string $status The status file key identifier. |
| 738 |
* |
| 739 |
* @return VBOTaskStatusInterface |
| 740 |
* |
| 741 |
* @throws InvalidArgumentException |
| 742 |
*/ |
| 743 |
public function getStatusTypeInstance(string $status) |
| 744 |
{ |
| 745 |
$className = $this->buildStatusTypeClassName($status); |
| 746 |
|
| 747 |
if (!class_exists($className)) { |
| 748 |
throw new InvalidArgumentException(sprintf('Could not load task status type [%s]', $status), 500); |
| 749 |
} |
| 750 |
|
| 751 |
return new $className; |
| 752 |
} |
| 753 |
|
| 754 |
/** |
| 755 |
* Returns the list of the task status status types loaded so far. |
| 756 |
* |
| 757 |
* @param bool $enums True to get a list of status enumerations. |
| 758 |
* |
| 759 |
* @return array |
| 760 |
*/ |
| 761 |
public function getStatusTypes(bool $enums = false) |
| 762 |
{ |
| 763 |
return $enums ? array_keys($this->statusTypes) : $this->statusTypes; |
| 764 |
} |
| 765 |
|
| 766 |
/** |
| 767 |
* Tells whether a status type exists, meaning that it was loaded. |
| 768 |
* |
| 769 |
* @param string $status The status file key identifier. |
| 770 |
* |
| 771 |
* @return bool |
| 772 |
*/ |
| 773 |
public function statusTypeExists(string $status) |
| 774 |
{ |
| 775 |
return isset($this->statusTypes[$status]); |
| 776 |
} |
| 777 |
|
| 778 |
/** |
| 779 |
* Builds the task status status type class name. |
| 780 |
* |
| 781 |
* @param string $status The status file key identifier. |
| 782 |
* |
| 783 |
* @return string Status type class name or empty string. |
| 784 |
*/ |
| 785 |
private function buildStatusTypeClassName(string $status) |
| 786 |
{ |
| 787 |
if (!$this->statusTypeExists($status)) { |
| 788 |
return ''; |
| 789 |
} |
| 790 |
|
| 791 |
return $this->taskStatusClassPrefix . ucfirst(strtolower($status)); |
| 792 |
} |
| 793 |
|
| 794 |
/** |
| 795 |
* Builds the task status group type class name. |
| 796 |
* |
| 797 |
* @param string $group The group file key identifier. |
| 798 |
* |
| 799 |
* @return string Status group type class name or empty string. |
| 800 |
*/ |
| 801 |
private function buildStatusGroupTypeClassName(string $group) |
| 802 |
{ |
| 803 |
if (!$this->statusGroupTypeExists($group)) { |
| 804 |
return ''; |
| 805 |
} |
| 806 |
|
| 807 |
return $this->taskStatusGroupClassPrefix . ucfirst(strtolower($group)); |
| 808 |
} |
| 809 |
|
| 810 |
/** |
| 811 |
* Builds the task driver class name. |
| 812 |
* |
| 813 |
* @param string $driver The driver file key identifier. |
| 814 |
* |
| 815 |
* @return string Driver class name or empty string. |
| 816 |
*/ |
| 817 |
private function buildDriverClassName(string $driver) |
| 818 |
{ |
| 819 |
if (!$this->driverExists($driver)) { |
| 820 |
return ''; |
| 821 |
} |
| 822 |
|
| 823 |
return $this->taskClassPrefix . ucfirst(strtolower($driver)); |
| 824 |
} |
| 825 |
|
| 826 |
/** |
| 827 |
* Pre-loads all the available task driver implementations. |
| 828 |
* |
| 829 |
* @return void |
| 830 |
*/ |
| 831 |
private function loadDrivers() |
| 832 |
{ |
| 833 |
$drivers_base = implode(DIRECTORY_SEPARATOR, [VBO_ADMIN_PATH, 'helpers', 'src', 'task', 'driver', '']); |
| 834 |
$drivers_files = glob($drivers_base . '*.php'); |
| 835 |
|
| 836 |
/** |
| 837 |
* Trigger event to let other plugins register additional drivers. |
| 838 |
* |
| 839 |
* @return array A list of supported drivers. |
| 840 |
*/ |
| 841 |
$list = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadTaskManagerDrivers'); |
| 842 |
foreach ($list as $chunk) { |
| 843 |
// merge default driver files with the returned ones |
| 844 |
$drivers_files = array_merge($drivers_files, (array) $chunk); |
| 845 |
} |
| 846 |
|
| 847 |
foreach ($drivers_files as $df) { |
| 848 |
// push driver file key identifier and set related path |
| 849 |
$driver_base_name = basename($df, '.php'); |
| 850 |
$this->drivers[$driver_base_name] = $df; |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
/** |
| 855 |
* Pre-loads all the available task status group type implementations. |
| 856 |
* |
| 857 |
* @return void |
| 858 |
*/ |
| 859 |
private function loadStatusGroupTypes() |
| 860 |
{ |
| 861 |
$drivers_base = implode(DIRECTORY_SEPARATOR, [VBO_ADMIN_PATH, 'helpers', 'src', 'task', 'status', 'group', 'type', '']); |
| 862 |
$drivers_files = glob($drivers_base . '*.php'); |
| 863 |
|
| 864 |
/** |
| 865 |
* Trigger event to let other plugins register additional status group types. |
| 866 |
* |
| 867 |
* @return array A list of supported status group types. |
| 868 |
*/ |
| 869 |
$list = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadTaskManagerStatusGroupTypes'); |
| 870 |
foreach ($list as $chunk) { |
| 871 |
// merge default driver files with the returned ones |
| 872 |
$drivers_files = array_merge($drivers_files, (array) $chunk); |
| 873 |
} |
| 874 |
|
| 875 |
foreach ($drivers_files as $df) { |
| 876 |
// push driver file key identifier and set related path |
| 877 |
$driver_base_name = basename($df, '.php'); |
| 878 |
$this->statusGroupTypes[$driver_base_name] = $df; |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
/** |
| 883 |
* Pre-loads all the available task status type implementations. |
| 884 |
* |
| 885 |
* @return void |
| 886 |
*/ |
| 887 |
private function loadStatusTypes() |
| 888 |
{ |
| 889 |
$drivers_base = implode(DIRECTORY_SEPARATOR, [VBO_ADMIN_PATH, 'helpers', 'src', 'task', 'status', 'type', '']); |
| 890 |
$drivers_files = glob($drivers_base . '*.php'); |
| 891 |
|
| 892 |
/** |
| 893 |
* Trigger event to let other plugins register additional status types. |
| 894 |
* |
| 895 |
* @return array A list of supported status types. |
| 896 |
*/ |
| 897 |
$list = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadTaskManagerStatusTypes'); |
| 898 |
foreach ($list as $chunk) { |
| 899 |
// merge default driver files with the returned ones |
| 900 |
$drivers_files = array_merge($drivers_files, (array) $chunk); |
| 901 |
} |
| 902 |
|
| 903 |
foreach ($drivers_files as $df) { |
| 904 |
// push driver file key identifier and set related path |
| 905 |
$driver_base_name = basename($df, '.php'); |
| 906 |
$this->statusTypes[$driver_base_name] = $df; |
| 907 |
} |
| 908 |
} |
| 909 |
} |
| 910 |
|