| 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 |
private $allowedInstanceMethods = [ |
| 32 |
'all', |
| 33 |
'get', |
| 34 |
'find', |
| 35 |
'first', |
| 36 |
'paginate' |
| 37 |
]; |
| 38 |
|
| 39 |
public function __construct(Board $instance) |
| 40 |
{ |
| 41 |
$this->instance = $instance; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get Boards |
| 46 |
* |
| 47 |
* Use: |
| 48 |
* <code>FluentBoardsApi('boards')->getBoards();</code> |
| 49 |
* |
| 50 |
* @param array $with |
| 51 |
* @return array|Board Model |
| 52 |
*/ |
| 53 |
public function getBoards($with = [], $sortBy = 'title', $sortOrder = 'asc') |
| 54 |
{ |
| 55 |
$userId = get_current_user_id(); |
| 56 |
$query = Board::byAccessUser($userId); |
| 57 |
|
| 58 |
if($sortBy) { |
| 59 |
$query->orderBy($sortBy, $sortOrder); |
| 60 |
} |
| 61 |
|
| 62 |
if ($with) { |
| 63 |
$query->with($with); |
| 64 |
} |
| 65 |
|
| 66 |
$boards = $query->get(); |
| 67 |
return $boards; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get stages by board |
| 72 |
* |
| 73 |
* Use: |
| 74 |
* <code>FluentBoardsApi('boards')->getStagesByBoard($board_id);</code> |
| 75 |
* |
| 76 |
* @param int|string $board_id |
| 77 |
* @return array Model |
| 78 |
*/ |
| 79 |
public function getStagesByBoard($board_id) |
| 80 |
{ |
| 81 |
if (empty($board_id)) { |
| 82 |
return []; |
| 83 |
} |
| 84 |
|
| 85 |
return Board::with('stages')->where('id', $board_id)->get(); |
| 86 |
} |
| 87 |
|
| 88 |
public function create($data) |
| 89 |
{ |
| 90 |
if (empty($data['title'])) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
$boardData = $this->boardSanitizeAndValidate($data); |
| 95 |
|
| 96 |
$boardService = new BoardService(); |
| 97 |
$labelService = new LabelService(); |
| 98 |
$stageService = new StageService(); |
| 99 |
|
| 100 |
$board = $boardService->createBoard($boardData); |
| 101 |
|
| 102 |
if (!$board) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
$labelService->createDefaultLabel($board->id); |
| 107 |
$stageService->createDefaultStages($board); |
| 108 |
|
| 109 |
// if board is created from crm contact |
| 110 |
if (isset($boardData['crm_contact_id'])) { |
| 111 |
$boardService->updateAssociateMember($boardData['crm_contact_id'], $board->id); |
| 112 |
} |
| 113 |
do_action('fluent_boards/board_created', $board); |
| 114 |
|
| 115 |
return $board; |
| 116 |
} |
| 117 |
|
| 118 |
public function getStages($board_id) |
| 119 |
{ |
| 120 |
if (empty($board_id)) { |
| 121 |
return []; |
| 122 |
} |
| 123 |
|
| 124 |
return Stage::where('board_id', $board_id)->where('archived_at', null)->orderBy('position', 'asc')->get(); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
private function boardSanitizeAndValidate($data) |
| 130 |
{ |
| 131 |
return Helper::sanitizeBoard($data); |
| 132 |
} |
| 133 |
|
| 134 |
public function getInstance() |
| 135 |
{ |
| 136 |
return $this->instance; |
| 137 |
} |
| 138 |
|
| 139 |
public function __call($method, $params) |
| 140 |
{ |
| 141 |
if (in_array($method, $this->allowedInstanceMethods)) { |
| 142 |
return call_user_func_array([$this->instance, $method], $params); |
| 143 |
} |
| 144 |
|
| 145 |
throw new \Exception(sprintf('Method %s does not exist.', esc_html($method))); |
| 146 |
} |
| 147 |
|
| 148 |
public function getLabels($boardId) |
| 149 |
{ |
| 150 |
$board = Board::findOrFail($boardId); |
| 151 |
|
| 152 |
if (!$board) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
if (!$this->userHasAccessToBoard($boardId)) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
return Label::where('board_id', $board->id)->orderBy('created_at', 'ASC')->get(); |
| 161 |
} |
| 162 |
|
| 163 |
public function createLabel($boardId, $data) |
| 164 |
{ |
| 165 |
if (empty($data['bg_color'])) |
| 166 |
{ |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
if (empty($data['color'])) |
| 171 |
{ |
| 172 |
$data['color'] = '#1B2533'; |
| 173 |
} |
| 174 |
|
| 175 |
if (!$this->userHasAccessToBoard($boardId)) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
$labelData = $this->labelSanitize($data); |
| 180 |
|
| 181 |
$labelService = new LabelService(); |
| 182 |
return $labelService->createLabel($labelData, $boardId); |
| 183 |
} |
| 184 |
|
| 185 |
private function labelSanitize($data) |
| 186 |
{ |
| 187 |
return Helper::sanitizeLabel($data); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Check if the current user has access to a board |
| 192 |
* |
| 193 |
* @param int $boardId The board ID to check access for |
| 194 |
* @return bool True if user has access, false otherwise |
| 195 |
*/ |
| 196 |
private function userHasAccessToBoard($boardId) |
| 197 |
{ |
| 198 |
return PermissionManager::userHasPermission($boardId); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
} |
| 203 |
|