| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Api\Classes; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
use FluentBoards\App\Models\Board; |
| 8 |
use FluentBoards\App\Models\Label; |
| 9 |
use FluentBoards\App\Models\Stage; |
| 10 |
use FluentBoards\App\Services\BoardService; |
| 11 |
use FluentBoards\App\Services\Helper; |
| 12 |
use FluentBoards\App\Services\PermissionManager; |
| 13 |
use FluentBoards\App\Services\StageService; |
| 14 |
use FluentBoards\App\Services\LabelService; |
| 15 |
|
| 16 |
|
| 17 |
/** |
| 18 |
* Boards Class - PHP API Wrapper |
| 19 |
* |
| 20 |
* Boards API Wrapper Class that can be used as <code>FluentBoardsApi('boards')</code> to get the class instance |
| 21 |
* |
| 22 |
* @package FluentBoards\App\Api\Classes |
| 23 |
* @namespace FluentBoards\App\Api\Classes |
| 24 |
* |
| 25 |
* @version 1.0.0 |
| 26 |
*/ |
| 27 |
class Boards |
| 28 |
{ |
| 29 |
private $instance = null; |
| 30 |
|
| 31 |
public function __construct(Board $instance) |
| 32 |
{ |
| 33 |
$this->instance = $instance; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get Boards |
| 38 |
* |
| 39 |
* Use: |
| 40 |
* <code>FluentBoardsApi('boards')->getBoards();</code> |
| 41 |
* |
| 42 |
* @param array $with |
| 43 |
* @return array|Board Model |
| 44 |
*/ |
| 45 |
public function getBoards($with = [], $sortBy = 'title', $sortOrder = 'asc') |
| 46 |
{ |
| 47 |
$userId = get_current_user_id(); |
| 48 |
$query = Board::byAccessUser($userId); |
| 49 |
|
| 50 |
if($sortBy) { |
| 51 |
$query->orderBy($sortBy, $sortOrder); |
| 52 |
} |
| 53 |
|
| 54 |
if ($with) { |
| 55 |
$query->with($with); |
| 56 |
} |
| 57 |
|
| 58 |
$boards = $query->get(); |
| 59 |
return $boards; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get stages by board |
| 64 |
* |
| 65 |
* Use: |
| 66 |
* <code>FluentBoardsApi('boards')->getStagesByBoard($board_id);</code> |
| 67 |
* |
| 68 |
* @param int|string $board_id |
| 69 |
* @return array Model |
| 70 |
*/ |
| 71 |
public function getStagesByBoard($board_id) |
| 72 |
{ |
| 73 |
if (empty($board_id)) { |
| 74 |
return []; |
| 75 |
} |
| 76 |
|
| 77 |
if (!$this->canReadBoard($board_id)) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
return Board::with('stages')->where('id', $board_id)->get(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Create a board only when the current user can create boards. |
| 86 |
* |
| 87 |
* @param array $data |
| 88 |
* @return Board|false |
| 89 |
*/ |
| 90 |
public function create($data) |
| 91 |
{ |
| 92 |
if (empty($data['title'])) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
if (!PermissionManager::userHasBoardCreationPermission()) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
$boardData = $this->boardSanitizeAndValidate($data); |
| 101 |
|
| 102 |
$boardService = new BoardService(); |
| 103 |
$labelService = new LabelService(); |
| 104 |
$stageService = new StageService(); |
| 105 |
|
| 106 |
$board = $boardService->createBoard($boardData); |
| 107 |
|
| 108 |
if (!$board) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
$labelService->createDefaultLabel($board->id); |
| 113 |
$stageService->createDefaultStages($board); |
| 114 |
|
| 115 |
// if board is created from crm contact |
| 116 |
if (isset($boardData['crm_contact_id'])) { |
| 117 |
$boardService->updateAssociateMember($boardData['crm_contact_id'], $board->id); |
| 118 |
} |
| 119 |
do_action('fluent_boards/board_created', $board); |
| 120 |
|
| 121 |
return $board; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Get non-archived stages for a board the current user can read. |
| 126 |
* |
| 127 |
* @param int|string $board_id |
| 128 |
* @return array|false |
| 129 |
*/ |
| 130 |
public function getStages($board_id) |
| 131 |
{ |
| 132 |
if (empty($board_id)) { |
| 133 |
return []; |
| 134 |
} |
| 135 |
|
| 136 |
if (!$this->canReadBoard($board_id)) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
return Stage::where('board_id', $board_id)->where('archived_at', null)->orderBy('position', 'asc')->get(); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private function boardSanitizeAndValidate($data) |
| 146 |
{ |
| 147 |
return Helper::sanitizeBoard($data); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Block raw model proxy calls so board access cannot be bypassed. |
| 152 |
* |
| 153 |
* @param string $method |
| 154 |
* @param array $params |
| 155 |
* @throws \Exception |
| 156 |
*/ |
| 157 |
public function __call($method, $params) |
| 158 |
{ |
| 159 |
throw new \Exception(sprintf('Method %s does not exist.', esc_html($method))); |
| 160 |
} |
| 161 |
|
| 162 |
public function getLabels($boardId) |
| 163 |
{ |
| 164 |
$board = Board::findOrFail($boardId); |
| 165 |
|
| 166 |
if (!$board) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
if (!$this->canReadBoard($boardId)) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
return Label::where('board_id', $board->id)->orderBy('created_at', 'ASC')->get(); |
| 175 |
} |
| 176 |
|
| 177 |
public function createLabel($boardId, $data) |
| 178 |
{ |
| 179 |
if (empty($data['bg_color'])) |
| 180 |
{ |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
if (empty($data['color'])) |
| 185 |
{ |
| 186 |
$data['color'] = '#1B2533'; |
| 187 |
} |
| 188 |
|
| 189 |
if (!$this->canWriteBoard($boardId)) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
$labelData = $this->labelSanitize($data); |
| 194 |
|
| 195 |
$labelService = new LabelService(); |
| 196 |
return $labelService->createLabel($labelData, $boardId); |
| 197 |
} |
| 198 |
|
| 199 |
private function labelSanitize($data) |
| 200 |
{ |
| 201 |
return Helper::sanitizeLabel($data); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Check if the current user can read a board. |
| 206 |
* |
| 207 |
* @param int $boardId The board ID to check access for |
| 208 |
* @return bool True if user has access, false otherwise |
| 209 |
*/ |
| 210 |
private function canReadBoard($boardId) |
| 211 |
{ |
| 212 |
return PermissionManager::userHasBoardPermission($boardId, 'GET'); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Check if the current user can write to a board. |
| 217 |
* |
| 218 |
* @param int $boardId The board ID to check access for |
| 219 |
* @return bool True if user can write, false otherwise |
| 220 |
*/ |
| 221 |
private function canWriteBoard($boardId) |
| 222 |
{ |
| 223 |
return PermissionManager::userHasBoardPermission($boardId, 'POST'); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
} |
| 228 |
|