| 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 |
private $allowedInstanceMethods |
| 18 |
= [ |
| 19 |
'all', |
| 20 |
'get', |
| 21 |
'find', |
| 22 |
'first', |
| 23 |
'paginate', |
| 24 |
]; |
| 25 |
|
| 26 |
public function __construct(Stage $stage) |
| 27 |
{ |
| 28 |
$this->instance = $stage; |
| 29 |
} |
| 30 |
|
| 31 |
public function getStage($id, $with = []) |
| 32 |
{ |
| 33 |
if ( ! $id) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
$stage = Stage::where('id', $id)->with($with)->first(); |
| 38 |
|
| 39 |
if ( ! $stage) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
return $stage; |
| 44 |
} |
| 45 |
|
| 46 |
public function getStagesByBoard($boardId, $with = []) |
| 47 |
{ |
| 48 |
if ( ! $boardId) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
if ( ! PermissionManager::userHasPermission($boardId)) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
$query = Stage::where('board_id', $boardId) |
| 57 |
->whereNull('archived_at') |
| 58 |
->orderBy('position', 'asc'); |
| 59 |
|
| 60 |
if ( ! empty($with)) { |
| 61 |
$query->with($with); |
| 62 |
} |
| 63 |
|
| 64 |
$stages = $query->get(); |
| 65 |
|
| 66 |
return $stages; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Create a new stage |
| 71 |
* |
| 72 |
* @param array $data Stage data including title and board_id |
| 73 |
* @return Stage|false The created stage or false on failure |
| 74 |
*/ |
| 75 |
public function create($data) |
| 76 |
{ |
| 77 |
// Validate required data |
| 78 |
if (empty($data) || empty($data['title']) || empty($data['board_id'])) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
// Check if current user has access to board |
| 83 |
if (!PermissionManager::userHasPermission($data['board_id'])) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
$stageData = Helper::sanitizeStage($data); |
| 88 |
|
| 89 |
$stageService = new StageService(); |
| 90 |
$stage = $stageService->createStage($stageData, |
| 91 |
$stageData['board_id']); |
| 92 |
|
| 93 |
return $stage; |
| 94 |
} |
| 95 |
|
| 96 |
public function updateProperty($stageId, $property, $value) |
| 97 |
{ |
| 98 |
$stageService = new StageService(); |
| 99 |
|
| 100 |
$stage = Stage::findOrFail($stageId); |
| 101 |
|
| 102 |
if ( ! $stage) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
$allowedColumns = ['title', 'status', 'bg_color']; |
| 107 |
|
| 108 |
if ( ! in_array($property, $allowedColumns)) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
//checking if current user has access to board |
| 113 |
if ( ! PermissionManager::userHasPermission($stage->board_id)) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
$stage = $stageService->updateStageProperty($property, $value, |
| 118 |
$stageId); |
| 119 |
|
| 120 |
return $stage; |
| 121 |
} |
| 122 |
|
| 123 |
public function archiveStage($stageId) |
| 124 |
{ |
| 125 |
if ( ! $stageId) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
$stage = Stage::findOrFail($stageId); |
| 130 |
|
| 131 |
if ( ! $stage) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
//checking if current user has access to board |
| 136 |
if ( ! PermissionManager::userHasPermission($stage->board_id)) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
$boardService = new BoardService(); |
| 141 |
|
| 142 |
$stage = $boardService->archiveStage($stage->board_id, $stage); |
| 143 |
|
| 144 |
return $stage; |
| 145 |
} |
| 146 |
|
| 147 |
public function restoreStage($stageId) |
| 148 |
{ |
| 149 |
if ( ! $stageId) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
$stage = Stage::findOrFail($stageId); |
| 154 |
|
| 155 |
if ( ! $stage) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
//checking if current user has access to board |
| 160 |
if ( ! PermissionManager::userHasPermission($stage->board_id)) { |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
$boardService = new BoardService(); |
| 165 |
|
| 166 |
return $boardService->restoreStage($stage->board_id, $stage); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
public function getInstance() |
| 171 |
{ |
| 172 |
return $this->instance; |
| 173 |
} |
| 174 |
|
| 175 |
public function __call($method, $params) |
| 176 |
{ |
| 177 |
if (in_array($method, $this->allowedInstanceMethods)) { |
| 178 |
return call_user_func_array([$this->instance, $method], $params); |
| 179 |
} |
| 180 |
|
| 181 |
throw new \Exception(sprintf('Method %s does not exist.', esc_html($method))); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
} |