| 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 model area implementation. |
| 16 |
* |
| 17 |
* @since 1.18.0 (J) - 1.8.0 (WP) |
| 18 |
*/ |
| 19 |
final class VBOTaskModelArea |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Proxy for immediately accessing the object. |
| 23 |
* |
| 24 |
* @return VBOTaskModelArea |
| 25 |
*/ |
| 26 |
public static function getInstance() |
| 27 |
{ |
| 28 |
return new static; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Class constructor. |
| 33 |
*/ |
| 34 |
public function __construct() |
| 35 |
{} |
| 36 |
|
| 37 |
/** |
| 38 |
* Item loading implementation. |
| 39 |
* |
| 40 |
* @param mixed $pk An optional primary key value to load the row by, |
| 41 |
* or an associative array of fields to match. |
| 42 |
* |
| 43 |
* @return object|null The record object on success, null otherwise. |
| 44 |
*/ |
| 45 |
public function getItem($pk) |
| 46 |
{ |
| 47 |
$dbo = JFactory::getDbo(); |
| 48 |
|
| 49 |
$q = $dbo->getQuery(true) |
| 50 |
->select('*') |
| 51 |
->from($dbo->qn('#__vikbooking_tm_areas')); |
| 52 |
|
| 53 |
if (is_array($pk)) { |
| 54 |
foreach ($pk as $column => $value) { |
| 55 |
$q->where($dbo->qn($column) . ' = ' . $dbo->q($value)); |
| 56 |
} |
| 57 |
} else { |
| 58 |
$q->where($dbo->qn('id') . ' = ' . (int) $pk); |
| 59 |
} |
| 60 |
|
| 61 |
$dbo->setQuery($q, 0, 1); |
| 62 |
|
| 63 |
return $dbo->loadObject(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Items loading implementation. |
| 68 |
* |
| 69 |
* @param array $clauses List of associative columns to filter |
| 70 |
* (column => [operator, value]) |
| 71 |
* @param int $start Query limit start. |
| 72 |
* @param int $lim Query limit value. |
| 73 |
* @param array $cols Optional list of columns to fetch. |
| 74 |
* |
| 75 |
* @return array List of record objects. |
| 76 |
*/ |
| 77 |
public function getItems(array $clauses = [], $start = 0, $lim = 0, array $cols = []) |
| 78 |
{ |
| 79 |
$dbo = JFactory::getDbo(); |
| 80 |
|
| 81 |
$q = $dbo->getQuery(true); |
| 82 |
|
| 83 |
if (!$cols) { |
| 84 |
$q->select('*'); |
| 85 |
} else { |
| 86 |
$q->select(array_map([$dbo, 'qn'], $cols)); |
| 87 |
} |
| 88 |
|
| 89 |
$q->from($dbo->qn('#__vikbooking_tm_areas')); |
| 90 |
|
| 91 |
foreach ($clauses as $column => $data) { |
| 92 |
if (!is_array($data) || !isset($data['value'])) { |
| 93 |
continue; |
| 94 |
} |
| 95 |
|
| 96 |
if (is_array($data['value'])) { |
| 97 |
// default to "IN" for a list of integers |
| 98 |
$q->where($dbo->qn($column) . ' IN (' . implode(', ', array_map('intval', $data['value'])) . ')'); |
| 99 |
} else { |
| 100 |
// singular fetching value |
| 101 |
$q->where($dbo->qn($column) . ' ' . ($data['operator'] ?? '=') . ' ' . $dbo->q($data['value'])); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
$q->order($dbo->qn('name') . ' ASC'); |
| 106 |
$q->order($dbo->qn('id') . ' ASC'); |
| 107 |
|
| 108 |
$dbo->setQuery($q, $start, $lim); |
| 109 |
|
| 110 |
return $dbo->loadObjectList(); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Returns a list of area/project IDs to which the given assignee ID is assigned. |
| 115 |
* |
| 116 |
* @param int $assigneeId The operator ID. |
| 117 |
* |
| 118 |
* @return array List of supported area IDs or empty array. |
| 119 |
*/ |
| 120 |
public function getAssigneeItems(int $assigneeId) |
| 121 |
{ |
| 122 |
$areaIds = []; |
| 123 |
|
| 124 |
foreach ($this->getItems() as $areaRecord) { |
| 125 |
$area = VBOTaskArea::getInstance((array) $areaRecord); |
| 126 |
$areaAssignees = $area->getOperatorIds(); |
| 127 |
if (!$areaAssignees || in_array($assigneeId, $areaAssignees)) { |
| 128 |
// push supported area/project |
| 129 |
$areaIds[] = $area->getID(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return $areaIds; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Stores a new task area record. |
| 138 |
* |
| 139 |
* @param array|object $record The record to store. |
| 140 |
* |
| 141 |
* @return int|null The new record ID or null. |
| 142 |
*/ |
| 143 |
public function save($record) |
| 144 |
{ |
| 145 |
$dbo = JFactory::getDbo(); |
| 146 |
|
| 147 |
$record = (object) $record; |
| 148 |
|
| 149 |
if (is_array(($record->settings ?? null)) || is_object(($record->settings ?? null))) { |
| 150 |
$record->settings = json_encode($record->settings); |
| 151 |
} |
| 152 |
|
| 153 |
if (is_array(($record->tags ?? null))) { |
| 154 |
// parse all tags, even custom ones, into a list of IDs |
| 155 |
$record->tags = json_encode(VBOTaskModelColortag::getInstance()->parseIds($record->tags)); |
| 156 |
} |
| 157 |
|
| 158 |
if (is_array(($record->status_enums ?? null))) { |
| 159 |
$record->status_enums = json_encode($record->status_enums); |
| 160 |
} |
| 161 |
|
| 162 |
$dbo->insertObject('#__vikbooking_tm_areas', $record, 'id'); |
| 163 |
|
| 164 |
return $record->id ?? null; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Updates an existing task area record. |
| 169 |
* |
| 170 |
* @param array|object $record The record details to update. |
| 171 |
* |
| 172 |
* @return bool |
| 173 |
*/ |
| 174 |
public function update($record) |
| 175 |
{ |
| 176 |
$dbo = JFactory::getDbo(); |
| 177 |
|
| 178 |
$record = (object) $record; |
| 179 |
|
| 180 |
if (empty($record->id)) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
if (is_array(($record->settings ?? null)) || is_object(($record->settings ?? null))) { |
| 185 |
$record->settings = json_encode($record->settings); |
| 186 |
} |
| 187 |
|
| 188 |
if (is_array(($record->tags ?? null))) { |
| 189 |
// parse all tags, even custom ones, into a list of IDs |
| 190 |
$record->tags = json_encode(VBOTaskModelColortag::getInstance()->parseIds($record->tags)); |
| 191 |
} |
| 192 |
|
| 193 |
if (is_array(($record->status_enums ?? null))) { |
| 194 |
$record->status_enums = json_encode(array_values(array_filter($record->status_enums))); |
| 195 |
} |
| 196 |
|
| 197 |
return (bool) $dbo->updateObject('#__vikbooking_tm_areas', $record, 'id'); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Deletes a task area record. |
| 202 |
* |
| 203 |
* @param array|int $id The record(s) to delete. |
| 204 |
* |
| 205 |
* @return bool |
| 206 |
*/ |
| 207 |
public function delete($id) |
| 208 |
{ |
| 209 |
$dbo = JFactory::getDbo(); |
| 210 |
|
| 211 |
if (!is_array($id)) { |
| 212 |
$id = (array) $id; |
| 213 |
} |
| 214 |
|
| 215 |
$id = array_map('intval', $id); |
| 216 |
|
| 217 |
if (!$id) { |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
$dbo->setQuery( |
| 222 |
$dbo->getQuery(true) |
| 223 |
->delete($dbo->qn('#__vikbooking_tm_areas')) |
| 224 |
->where($dbo->qn('id') . ' IN (' . implode(', ', $id) . ')') |
| 225 |
); |
| 226 |
|
| 227 |
$dbo->execute(); |
| 228 |
$result = (bool) $dbo->getAffectedRows(); |
| 229 |
|
| 230 |
if ($result) { |
| 231 |
// fetch all the tasks assigned to the deleted areas |
| 232 |
$dbo->setQuery( |
| 233 |
$dbo->getQuery(true) |
| 234 |
->select($dbo->qn('id')) |
| 235 |
->from($dbo->qn('#__vikbooking_tm_tasks')) |
| 236 |
->where($dbo->qn('id_area') . ' IN (' . implode(', ', $id) . ')') |
| 237 |
); |
| 238 |
|
| 239 |
// delete all the tasks found on cascade |
| 240 |
VBOTaskModelTask::getInstance()->delete($dbo->loadColumn()); |
| 241 |
} |
| 242 |
|
| 243 |
return $result; |
| 244 |
} |
| 245 |
} |
| 246 |
|