| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Stage; |
| 6 |
use FluentBoards\App\Services\Helper; |
| 7 |
use FluentBoards\App\Services\StageService; |
| 8 |
use FluentBoards\Framework\Http\Request\Request; |
| 9 |
|
| 10 |
class StageController extends Controller |
| 11 |
{ |
| 12 |
private $stageService; |
| 13 |
public function __construct(StageService $stageService) |
| 14 |
{ |
| 15 |
parent::__construct(); |
| 16 |
$this->stageService = $stageService; |
| 17 |
} |
| 18 |
public function updateStageProperty(Request $request, $board_id, $stage_id) |
| 19 |
{ |
| 20 |
$board_id = absint($board_id); |
| 21 |
$stage_id = absint($stage_id); |
| 22 |
$col = $request->getSafe('property', 'sanitize_text_field'); |
| 23 |
$value = $request->getSafe('value', 'sanitize_text_field'); |
| 24 |
|
| 25 |
try { |
| 26 |
$validatedData = $this->updateStagePropValidationAndSanitation($col, $value); |
| 27 |
$this->findStageOnBoard($stage_id, $board_id); |
| 28 |
|
| 29 |
$stage = $this->stageService->updateStageProperty($col, $validatedData[$col], $stage_id); |
| 30 |
|
| 31 |
return $this->sendSuccess([ |
| 32 |
'message' => __('Stage has been updated', 'fluent-boards'), |
| 33 |
'stage' => $stage |
| 34 |
], 200); |
| 35 |
} catch (\Exception $e) { |
| 36 |
return $this->sendError($e->getMessage(), 404); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
private function updateStagePropValidationAndSanitation($col, $value) |
| 41 |
{ |
| 42 |
$rules = [ |
| 43 |
'title' => 'required|string', |
| 44 |
'color' => 'nullable|string', |
| 45 |
'bg_color' => 'nullable|string', |
| 46 |
'archived_at' => 'nullable|string', |
| 47 |
'status' => 'required|string', |
| 48 |
'settings' => 'nullable|array', |
| 49 |
]; |
| 50 |
if (array_key_exists($col, $rules)) { |
| 51 |
$rule = $rules[$col]; |
| 52 |
$data = Helper::sanitizeStage([$col => $value]); |
| 53 |
|
| 54 |
return $this->validate($data, [ |
| 55 |
$col => $rule, |
| 56 |
]); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public function sortStageTasks(Request $request, $board_id, $stage_id) |
| 61 |
{ |
| 62 |
$board_id = absint($board_id); |
| 63 |
$stage_id = absint($stage_id); |
| 64 |
$order = $request->getSafe('order', 'sanitize_text_field'); |
| 65 |
$orderBy = $request->getSafe('orderBy', 'sanitize_text_field'); |
| 66 |
$this->findStageOnBoard($stage_id, $board_id); |
| 67 |
|
| 68 |
$updatedTasks = $this->stageService->sortStageTasks($order, $orderBy, $stage_id); |
| 69 |
return [ |
| 70 |
'message' => __('Tasks has been sorted', 'fluent-boards'), |
| 71 |
'updatedTasks' => $updatedTasks, |
| 72 |
]; |
| 73 |
} |
| 74 |
|
| 75 |
public function dragStage(Request $request, $board_id) |
| 76 |
{ |
| 77 |
$board_id = absint($board_id); |
| 78 |
$stage_id = $request->getSafe('stageId', 'intval'); |
| 79 |
$position = $request->getSafe('newPosition', 'intval'); |
| 80 |
try{ |
| 81 |
$stage = $this->findStageOnBoard($stage_id, $board_id); |
| 82 |
$stage->moveToNewPosition($position); |
| 83 |
do_action('fluent_boards/board_stages_reordered', $board_id, [$stage_id]); |
| 84 |
return $this->sendSuccess([ |
| 85 |
'message' => __('Board stage has been updated', 'fluent-boards'), |
| 86 |
'updatedStages' => $this->stageService->getLastOneMinuteUpdatedStages($board_id) |
| 87 |
], 200); |
| 88 |
} catch (\Exception $e) { |
| 89 |
return $this->sendError($e->getMessage(), 400); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public function updateStage(Request $request, $board_id, $stage_id) |
| 94 |
{ |
| 95 |
$board_id = absint($board_id); |
| 96 |
$stage_id = absint($stage_id); |
| 97 |
$stageData = $request->getSafe('stage'); |
| 98 |
if (!is_array($stageData)) { |
| 99 |
$stageData = []; |
| 100 |
} |
| 101 |
$updatedStage = $this->stageSanitizeAndValidate($stageData, [ |
| 102 |
'title' => 'required|string', |
| 103 |
'cover_bg' => 'nullable' |
| 104 |
]); |
| 105 |
try { |
| 106 |
$oldStage = $this->findStageOnBoard($stage_id, $board_id); |
| 107 |
$board = $oldStage->board; |
| 108 |
$stages = $board->stages()->where('id', '!=', $stage_id)->get(); |
| 109 |
|
| 110 |
$updatedStage = $this->stageService->updateStage($updatedStage, $board->id, $oldStage); |
| 111 |
|
| 112 |
return $this->sendSuccess([ |
| 113 |
'success' => true, |
| 114 |
'stages' => $board->stages()->get(), |
| 115 |
'updatedStage' => $updatedStage, |
| 116 |
'message' => __('Stage has been updated', 'fluent-boards'), |
| 117 |
], 200); |
| 118 |
} catch (\Exception $e) { |
| 119 |
return $this->sendError($e->getMessage(), 400); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
private function stageSanitizeAndValidate($data, array $rules = []) |
| 124 |
{ |
| 125 |
$data = Helper::sanitizeStage($data); |
| 126 |
|
| 127 |
return $this->validate($data, $rules); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Resolve a stage only when it belongs to the requested board. |
| 132 |
* |
| 133 |
* @param int $stageId |
| 134 |
* @param int $boardId |
| 135 |
* @return Stage |
| 136 |
* @throws \Exception |
| 137 |
*/ |
| 138 |
private function findStageOnBoard($stageId, $boardId) |
| 139 |
{ |
| 140 |
$stage = Stage::where('id', absint($stageId)) |
| 141 |
->where('board_id', absint($boardId)) |
| 142 |
->first(); |
| 143 |
|
| 144 |
if (!$stage) { |
| 145 |
throw new \Exception(esc_html__('Stage not found', 'fluent-boards')); |
| 146 |
} |
| 147 |
|
| 148 |
return $stage; |
| 149 |
} |
| 150 |
} |
| 151 |
|