| 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 |
* Assigner helper trait to use for status change behaviors. |
| 16 |
* |
| 17 |
* @since 1.18 (J) - 1.8 (WP) |
| 18 |
*/ |
| 19 |
trait VBOTaskStatusHelperAssigner |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Attaches the currently logged-in operator to the assignees list |
| 23 |
* of the provided task. |
| 24 |
* |
| 25 |
* @param VBOTaskTaskregistry $task |
| 26 |
* |
| 27 |
* @return bool True whether the user has been assigned, false otherwise. |
| 28 |
*/ |
| 29 |
public function assignUser(VBOTaskTaskregistry $task) |
| 30 |
{ |
| 31 |
// do not go ahead if we are in the back-end |
| 32 |
if (!JFactory::getApplication()->isClient('site')) { |
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
// get logged in operator |
| 37 |
$operator = VikBooking::getOperatorInstance()->getOperatorAccount(); |
| 38 |
|
| 39 |
if (!$operator) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
$assignees = $task->getAssigneeIds(); |
| 44 |
|
| 45 |
// abort in case the operator is already assigned to this task |
| 46 |
if (in_array($operator['id'], $assignees)) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
$assignees[] = $operator['id']; |
| 51 |
|
| 52 |
try { |
| 53 |
// attach this user to the list of task assignees |
| 54 |
VBOTaskModelTask::getInstance()->update([ |
| 55 |
'id' => $task->getID(), |
| 56 |
'assignees' => $assignees, |
| 57 |
]); |
| 58 |
} catch (Exception $error) { |
| 59 |
// ignore and go ahead |
| 60 |
} |
| 61 |
|
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Detaches the currently logged-in operator from the assignees list |
| 67 |
* of the provided task. |
| 68 |
* |
| 69 |
* @param VBOTaskTaskregistry $task |
| 70 |
* |
| 71 |
* @return bool True whether the user has been assigned, false otherwise. |
| 72 |
*/ |
| 73 |
public function unassignUser(VBOTaskTaskregistry $task) |
| 74 |
{ |
| 75 |
// do not go ahead if we are in the back-end |
| 76 |
if (!JFactory::getApplication()->isClient('site')) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
// get logged in operator |
| 81 |
$operator = VikBooking::getOperatorInstance()->getOperatorAccount(); |
| 82 |
|
| 83 |
if (!$operator) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
$assignees = $task->getAssigneeIds(); |
| 88 |
|
| 89 |
$index = array_search($operator['id'], $assignees); |
| 90 |
|
| 91 |
// abort in case the operator is not assigned to this task |
| 92 |
if ($index === false) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
// remove the assignee from the list |
| 97 |
array_splice($assignees, $index, 1); |
| 98 |
|
| 99 |
try { |
| 100 |
// detach this user from the list of task assignees |
| 101 |
VBOTaskModelTask::getInstance()->update([ |
| 102 |
'id' => $task->getID(), |
| 103 |
'assignees' => $assignees, |
| 104 |
]); |
| 105 |
} catch (Exception $error) { |
| 106 |
// ignore and go ahead |
| 107 |
} |
| 108 |
|
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Displays some instructions in case the user is interested in having |
| 114 |
* the provided task assigned. |
| 115 |
* |
| 116 |
* @param VBOTaskTaskregistry $task |
| 117 |
* |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
public function displayAssignable(VBOTaskTaskregistry $task) |
| 121 |
{ |
| 122 |
// do not go ahead if we are in the back-end |
| 123 |
if (!JFactory::getApplication()->isClient('site')) { |
| 124 |
return ''; |
| 125 |
} |
| 126 |
|
| 127 |
// get logged in operator |
| 128 |
$operator = VikBooking::getOperatorInstance()->getOperatorAccount(); |
| 129 |
|
| 130 |
if (!$operator) { |
| 131 |
return ''; |
| 132 |
} |
| 133 |
|
| 134 |
$assignees = $task->getAssigneeIds(); |
| 135 |
|
| 136 |
// abort in case the operator is already assigned to this task |
| 137 |
if (in_array($operator['id'], $assignees)) { |
| 138 |
return ''; |
| 139 |
} |
| 140 |
|
| 141 |
/** @var VBOTaskStatusInterface|null */ |
| 142 |
$status = $this->findFirstSupportedStatus('accepted', $task); |
| 143 |
|
| 144 |
if (!$status) { |
| 145 |
// do not display instructions because the "accepted" status is not supported |
| 146 |
return ''; |
| 147 |
} |
| 148 |
|
| 149 |
return JText::sprintf('VBO_TASK_STATUS_DISPLAY_ASSIGNABLE', $status->getName()); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Displays some instructions in case the user is interested in having |
| 154 |
* the provided task no longer assigned. |
| 155 |
* |
| 156 |
* @param VBOTaskTaskregistry $task |
| 157 |
* |
| 158 |
* @return string |
| 159 |
*/ |
| 160 |
public function displayUnassignable(VBOTaskTaskregistry $task) |
| 161 |
{ |
| 162 |
// do not go ahead if we are in the back-end |
| 163 |
if (!JFactory::getApplication()->isClient('site')) { |
| 164 |
return ''; |
| 165 |
} |
| 166 |
|
| 167 |
// get logged in operator |
| 168 |
$operator = VikBooking::getOperatorInstance()->getOperatorAccount(); |
| 169 |
|
| 170 |
if (!$operator) { |
| 171 |
return ''; |
| 172 |
} |
| 173 |
|
| 174 |
$assignees = $task->getAssigneeIds(); |
| 175 |
|
| 176 |
// abort in case the operator is not assigned to this task |
| 177 |
if (!in_array($operator['id'], $assignees)) { |
| 178 |
return ''; |
| 179 |
} |
| 180 |
|
| 181 |
/** @var VBOTaskStatusInterface|null */ |
| 182 |
$status = $this->findFirstSupportedStatus(['notstarted', 'pending'], $task); |
| 183 |
|
| 184 |
if (!$status) { |
| 185 |
// do not display instructions because the "notstarted" and "pending" statuses are not supported |
| 186 |
return ''; |
| 187 |
} |
| 188 |
|
| 189 |
return JText::sprintf('VBO_TASK_STATUS_DISPLAY_UNASSIGNABLE', $status->getName()); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Returns an instance of the very first status that is actually supported by the |
| 194 |
* area of the provided task. |
| 195 |
* |
| 196 |
* @param array|string $statuses |
| 197 |
* @param VBOTaskTaskregistry $task |
| 198 |
* |
| 199 |
* @return VBOTaskStatusInterface|null |
| 200 |
*/ |
| 201 |
protected function findFirstSupportedStatus($statuses, VBOTaskTaskregistry $task) |
| 202 |
{ |
| 203 |
$area = $task->getArea(); |
| 204 |
|
| 205 |
if (!$area) { |
| 206 |
return null; |
| 207 |
} |
| 208 |
|
| 209 |
$statuses = (array) $statuses; |
| 210 |
|
| 211 |
// in case of empty statuses, use the provided ones as the area supports all them |
| 212 |
$areaStatuses = $area->getStatuses() ?: $statuses; |
| 213 |
|
| 214 |
// obtain all the provided statuses that are actually supported by the area of the task |
| 215 |
$matching = array_intersect($statuses, $areaStatuses); |
| 216 |
|
| 217 |
if (!$matching) { |
| 218 |
return null; |
| 219 |
} |
| 220 |
|
| 221 |
// instantiate the first matching status |
| 222 |
return VBOFactory::getTaskManager()->getStatusTypeInstance(reset($matching)); |
| 223 |
} |
| 224 |
} |
| 225 |
|