| 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 |
$col = $request->getSafe('property'); |
| 21 |
$value = $request->getSafe('value'); |
| 22 |
|
| 23 |
try { |
| 24 |
$validatedData = $this->updateStagePropValidationAndSanitation($col, $value); |
| 25 |
|
| 26 |
$stage = $this->stageService->updateStageProperty($col, $validatedData[$col], $stage_id); |
| 27 |
|
| 28 |
return $this->sendSuccess([ |
| 29 |
'message' => __('Stage has been updated', 'fluent-boards'), |
| 30 |
'stage' => $stage |
| 31 |
], 200); |
| 32 |
} catch (\Exception $e) { |
| 33 |
return $this->sendError($e->getMessage(), 404); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
private function updateStagePropValidationAndSanitation($col, $value) |
| 38 |
{ |
| 39 |
$rules = [ |
| 40 |
'title' => 'required|string', |
| 41 |
'color' => 'nullable|string', |
| 42 |
'bg_color' => 'nullable|string', |
| 43 |
'archived_at' => 'nullable|string', |
| 44 |
'status' => 'required|string', |
| 45 |
'settings' => 'nullable|array', |
| 46 |
]; |
| 47 |
if (array_key_exists($col, $rules)) { |
| 48 |
$rule = $rules[$col]; |
| 49 |
$data = Helper::sanitizeStage([$col => $value]); |
| 50 |
|
| 51 |
return $this->validate($data, [ |
| 52 |
$col => $rule, |
| 53 |
]); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public function sortStageTasks(Request $request, $board_id, $stage_id) |
| 58 |
{ |
| 59 |
|
| 60 |
$order = $request->getSafe('order', 'sanitize_text_field'); |
| 61 |
$orderBy = $request->getSafe('orderBy', 'sanitize_text_field'); |
| 62 |
|
| 63 |
$updatedTasks = $this->stageService->sortStageTasks($order, $orderBy, $stage_id); |
| 64 |
return [ |
| 65 |
'message' => __('Tasks has been sorted', 'fluent-boards'), |
| 66 |
'updatedTasks' => $updatedTasks, |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
public function dragStage(Request $request, $board_id) |
| 71 |
{ |
| 72 |
$stage_id = $request->getSafe('stageId'); |
| 73 |
$position = $request->getSafe('newPosition'); |
| 74 |
try{ |
| 75 |
$stage = Stage::findOrFail($stage_id); |
| 76 |
$stage->moveToNewPosition($position); |
| 77 |
do_action('fluent_boards/board_stages_reordered', $board_id, [$stage_id]); |
| 78 |
return $this->sendSuccess([ |
| 79 |
'message' => __('Board stage has been updated', 'fluent-boards'), |
| 80 |
'updatedStages' => $this->stageService->getLastOneMinuteUpdatedStages($board_id) |
| 81 |
], 200); |
| 82 |
} catch (\Exception $e) { |
| 83 |
return $this->sendError($e->getMessage(), 400); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public function updateStage(Request $request, $board_id, $stage_id) |
| 88 |
{ |
| 89 |
$updatedStage = $this->stageSanitizeAndValidate($request->stage, [ |
| 90 |
'title' => 'required|string' |
| 91 |
]); |
| 92 |
/* |
| 93 |
* TODO: Check if the stage is empty or not. Let's allow duplicate stage |
| 94 |
*/ |
| 95 |
try { |
| 96 |
$oldStage = Stage::findOrFail($stage_id); |
| 97 |
$board = $oldStage->board; |
| 98 |
$stages = $board->stages()->where('id', '!=', $stage_id)->get(); |
| 99 |
$stageEditable = $this->stageService->checkStageEditable($stages, $updatedStage); |
| 100 |
|
| 101 |
if ($stageEditable) { |
| 102 |
$updatedStage = $this->stageService->updateStage($updatedStage, $board->id, $oldStage); |
| 103 |
|
| 104 |
return $this->sendSuccess([ |
| 105 |
'success' => true, |
| 106 |
'stages' => $board->stages()->get(), |
| 107 |
'updatedStage' => $updatedStage, |
| 108 |
'message' => __('Stage has been updated', 'fluent-boards'), |
| 109 |
], 200); |
| 110 |
} else { |
| 111 |
return $this->sendError([ |
| 112 |
'success' => false, |
| 113 |
'message' => __('Duplicate or empty stage', 'fluent-boards'), |
| 114 |
], 422); |
| 115 |
} |
| 116 |
} catch (\Exception $e) { |
| 117 |
return $this->sendError($e->getMessage(), 400); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
private function stageSanitizeAndValidate($data, array $rules = []) |
| 122 |
{ |
| 123 |
$data = Helper::sanitizeStage($data); |
| 124 |
|
| 125 |
return $this->validate($data, $rules); |
| 126 |
} |
| 127 |
} |