| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Api\Classes; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Stage; |
| 6 |
use FluentBoards\App\Services\BoardService; |
| 7 |
use FluentBoards\App\Services\Helper; |
| 8 |
use FluentBoards\App\Services\PermissionManager; |
| 9 |
use FluentBoards\App\Services\StageService; |
| 10 |
|
| 11 |
defined('ABSPATH') || exit; |
| 12 |
|
| 13 |
class Stages |
| 14 |
{ |
| 15 |
private $instance = null; |
| 16 |
|
| 17 |
public function __construct(Stage $stage) |
| 18 |
{ |
| 19 |
$this->instance = $stage; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Get a stage only when the current user can read its board. |
| 24 |
* |
| 25 |
* @param int|string $id |
| 26 |
* @param array $with |
| 27 |
* @return Stage|false |
| 28 |
*/ |
| 29 |
public function getStage($id, $with = []) |
| 30 |
{ |
| 31 |
if ( ! $id) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
$stage = Stage::where('id', $id)->with($with)->first(); |
| 36 |
|
| 37 |
if ( ! $stage) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
if (!$this->canReadBoard($stage->board_id)) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
return $stage; |
| 46 |
} |
| 47 |
|
| 48 |
public function getStagesByBoard($boardId, $with = []) |
| 49 |
{ |
| 50 |
if ( ! $boardId) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
if (!$this->canReadBoard($boardId)) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
$query = Stage::where('board_id', $boardId) |
| 59 |
->whereNull('archived_at') |
| 60 |
->orderBy('position', 'asc'); |
| 61 |
|
| 62 |
if ( ! empty($with)) { |
| 63 |
$query->with($with); |
| 64 |
} |
| 65 |
|
| 66 |
$stages = $query->get(); |
| 67 |
|
| 68 |
return $stages; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Create a new stage |
| 73 |
* |
| 74 |
* @param array $data Stage data including title and board_id |
| 75 |
* @return Stage|false The created stage or false on failure |
| 76 |
*/ |
| 77 |
public function create($data) |
| 78 |
{ |
| 79 |
// Validate required data |
| 80 |
if (empty($data) || empty($data['title']) || empty($data['board_id'])) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
// Check if current user has access to board |
| 85 |
if (!$this->canWriteBoard($data['board_id'])) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
$stageData = Helper::sanitizeStage($data); |
| 90 |
|
| 91 |
$stageService = new StageService(); |
| 92 |
$stage = $stageService->createStage($stageData, |
| 93 |
$stageData['board_id']); |
| 94 |
|
| 95 |
return $stage; |
| 96 |
} |
| 97 |
|
| 98 |
public function updateProperty($stageId, $property, $value) |
| 99 |
{ |
| 100 |
$stageService = new StageService(); |
| 101 |
|
| 102 |
$stage = Stage::findOrFail($stageId); |
| 103 |
|
| 104 |
if ( ! $stage) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
$allowedColumns = ['title', 'status', 'bg_color']; |
| 109 |
|
| 110 |
if ( ! in_array($property, $allowedColumns)) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
//checking if current user has access to board |
| 115 |
if (!$this->canWriteBoard($stage->board_id)) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
$stage = $stageService->updateStageProperty($property, $value, |
| 120 |
$stageId); |
| 121 |
|
| 122 |
return $stage; |
| 123 |
} |
| 124 |
|
| 125 |
public function archiveStage($stageId) |
| 126 |
{ |
| 127 |
if ( ! $stageId) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
$stage = Stage::findOrFail($stageId); |
| 132 |
|
| 133 |
if ( ! $stage) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
//checking if current user has access to board |
| 138 |
if (!$this->canWriteBoard($stage->board_id)) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
$boardService = new BoardService(); |
| 143 |
|
| 144 |
$stage = $boardService->archiveStage($stage->board_id, $stage); |
| 145 |
|
| 146 |
return $stage; |
| 147 |
} |
| 148 |
|
| 149 |
public function restoreStage($stageId) |
| 150 |
{ |
| 151 |
if ( ! $stageId) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
$stage = Stage::findOrFail($stageId); |
| 156 |
|
| 157 |
if ( ! $stage) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
//checking if current user has access to board |
| 162 |
if (!$this->canWriteBoard($stage->board_id)) { |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
$boardService = new BoardService(); |
| 167 |
|
| 168 |
return $boardService->restoreStage($stage->board_id, $stage); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
/** |
| 173 |
* Block raw model proxy calls so board access cannot be bypassed. |
| 174 |
* |
| 175 |
* @param string $method |
| 176 |
* @param array $params |
| 177 |
* @throws \Exception |
| 178 |
*/ |
| 179 |
public function __call($method, $params) |
| 180 |
{ |
| 181 |
throw new \Exception(sprintf('Method %s does not exist.', esc_html($method))); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Check if the current user can read a board. |
| 186 |
* |
| 187 |
* @param int $boardId |
| 188 |
* @return bool |
| 189 |
*/ |
| 190 |
private function canReadBoard($boardId) |
| 191 |
{ |
| 192 |
return PermissionManager::userHasBoardPermission($boardId, 'GET'); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Check if the current user can write to a board. |
| 197 |
* |
| 198 |
* @param int $boardId |
| 199 |
* @return bool |
| 200 |
*/ |
| 201 |
private function canWriteBoard($boardId) |
| 202 |
{ |
| 203 |
return PermissionManager::userHasBoardPermission($boardId, 'POST'); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
} |
| 208 |
|