FluentBoardsApi('boards') to get the class instance * * @package FluentBoards\App\Api\Classes * @namespace FluentBoards\App\Api\Classes * * @version 1.0.0 */ class Boards { private $instance = null; private $allowedInstanceMethods = [ 'all', 'get', 'find', 'first', 'paginate' ]; public function __construct(Board $instance) { $this->instance = $instance; } /** * Get Boards * * Use: * FluentBoardsApi('boards')->getBoards(); * * @param array $with * @return array|Board Model */ public function getBoards($with = [], $sortBy = 'title', $sortOrder = 'asc') { $userId = get_current_user_id(); $query = Board::byAccessUser($userId); if($sortBy) { $query->orderBy($sortBy, $sortOrder); } if ($with) { $query->with($with); } $boards = $query->get(); return $boards; } /** * Get stages by board * * Use: * FluentBoardsApi('boards')->getStagesByBoard($board_id); * * @param int|string $board_id * @return array Model */ public function getStagesByBoard($board_id) { if (empty($board_id)) { return []; } return Board::with('stages')->where('id', $board_id)->get(); } public function create($data) { if (empty($data['title'])) { return false; } $boardData = $this->boardSanitizeAndValidate($data); $boardService = new BoardService(); $labelService = new LabelService(); $stageService = new StageService(); $board = $boardService->createBoard($boardData); if (!$board) { return false; } $labelService->createDefaultLabel($board->id); $stageService->createDefaultStages($board); // if board is created from crm contact if (isset($boardData['crm_contact_id'])) { $boardService->updateAssociateMember($boardData['crm_contact_id'], $board->id); } do_action('fluent_boards/board_created', $board); return $board; } public function getStages($board_id) { if (empty($board_id)) { return []; } return Stage::where('board_id', $board_id)->where('archived_at', null)->get(); } private function boardSanitizeAndValidate($data) { return Helper::sanitizeBoard($data); } public function getInstance() { return $this->instance; } public function __call($method, $params) { if (in_array($method, $this->allowedInstanceMethods)) { return call_user_func_array([$this->instance, $method], $params); } throw new \Exception("Method {$method} does not exist."); } public function getLabels($boardId) { $board = Board::findOrFail($boardId); if (!$board) { return false; } if (!$this->userHasAccessToBoard($boardId)) { return false; } return Label::where('board_id', $board->id)->orderBy('created_at', 'ASC')->get(); } public function createLabel($boardId, $data) { if (empty($data['bg_color'])) { return false; } if (empty($data['color'])) { $data['color'] = '#1B2533'; } if (!$this->userHasAccessToBoard($boardId)) { return false; } $labelData = $this->labelSanitize($data); $labelService = new LabelService(); return $labelService->createLabel($labelData, $boardId); } private function labelSanitize($data) { return Helper::sanitizeLabel($data); } private function userHasAccessToBoard($boardId) { if (PermissionManager::userHasPermission($boardId)) { return true; } else { return false; } } }