| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Label; |
| 6 |
use FluentBoards\App\Services\Constant; |
| 7 |
use FluentBoards\App\Services\Helper; |
| 8 |
use FluentBoards\Framework\Http\Request\Request; |
| 9 |
use FluentBoards\App\Services\LabelService; |
| 10 |
|
| 11 |
class LabelController extends Controller |
| 12 |
{ |
| 13 |
private LabelService $labelService; |
| 14 |
|
| 15 |
public function __construct(LabelService $labelService) |
| 16 |
{ |
| 17 |
parent::__construct(); |
| 18 |
$this->labelService = $labelService; |
| 19 |
} |
| 20 |
|
| 21 |
public function getLabelsByBoard($board_id) |
| 22 |
{ |
| 23 |
try { |
| 24 |
$labels = $this->labelService->getLabelsByBoard($board_id); |
| 25 |
|
| 26 |
return $this->sendSuccess([ |
| 27 |
'labels' => $labels, |
| 28 |
], 200); |
| 29 |
} catch(\Exception $e) { |
| 30 |
return $this->sendError($e->getMessage(), 404); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public function getLabelsByBoardUsedInTasks($board_id) |
| 35 |
{ |
| 36 |
try { |
| 37 |
$labels = $this->labelService->getLabelsByBoardUsedInTasks($board_id); |
| 38 |
|
| 39 |
return $this->sendSuccess([ |
| 40 |
'labels' => $labels, |
| 41 |
], 200); |
| 42 |
} catch(\Exception $e) { |
| 43 |
return $this->sendError($e->getMessage(), 404); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public function createLabel(Request $request, $board_id) |
| 48 |
{ |
| 49 |
$labelData = $this->labelSanitizeAndValidate($request->all(), [ |
| 50 |
'bg_color' => 'required|string', |
| 51 |
'color' => 'required|string', |
| 52 |
'label' => 'nullable|string', |
| 53 |
]); |
| 54 |
|
| 55 |
try { |
| 56 |
$label = $this->labelService->createLabel($labelData, $board_id); |
| 57 |
do_action('fluent_boards/board_label', $label, Constant::ACTIVITY_ACTION_CREATED); |
| 58 |
|
| 59 |
return $this->sendSuccess([ |
| 60 |
'message' => __('Label has been created', 'fluent-boards'), |
| 61 |
'label' => $label, |
| 62 |
], 201); |
| 63 |
} catch (\Exception $e) { |
| 64 |
return $this->sendError($e->getMessage(), 400); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public function createLabelForTask(Request $request, $board_id) |
| 69 |
{ |
| 70 |
$requestData = [ |
| 71 |
'task_id' => $request->taskId, |
| 72 |
'boardTerm_id' => (int) $request->labelId, |
| 73 |
]; |
| 74 |
|
| 75 |
$labelData = $this->labelSanitizeAndValidate($requestData, [ |
| 76 |
'task_id' => 'required|integer', |
| 77 |
'boardTerm_id' => 'required|integer', |
| 78 |
]); |
| 79 |
try { |
| 80 |
$label = $this->labelService->createLabelForTask($labelData); |
| 81 |
// do_action('fluent_boards/label_manage_for_task_activity', $label->task_id, 'added'); |
| 82 |
|
| 83 |
return $this->sendSuccess([ |
| 84 |
'message' => __('Label has been added', 'fluent-boards'), |
| 85 |
'label' => $label, |
| 86 |
], 201); |
| 87 |
} catch (\Exception $e) { |
| 88 |
return $this->sendError($e->getMessage(), 400); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public function getLabelsByTask($board_id, $task_id) |
| 93 |
{ |
| 94 |
try { |
| 95 |
$labels = $this->labelService->getLabelsByTask($task_id); |
| 96 |
|
| 97 |
return $this->sendSuccess([ |
| 98 |
'labels' => $labels, |
| 99 |
], 200); |
| 100 |
} catch(\Exception $e) { |
| 101 |
return $this->sendError($e->getMessage(), 404); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public function deleteLabelOfTask($board_id, $task_id, $label_id) |
| 106 |
{ |
| 107 |
try { |
| 108 |
$this->labelService->deleteLabelOfTask($task_id, $label_id); |
| 109 |
|
| 110 |
return $this->sendSuccess([ |
| 111 |
'message' => __('Label has been deleted', 'fluent-boards'), |
| 112 |
], 201); |
| 113 |
} catch (\Exception $e) { |
| 114 |
return $this->sendError($e->getMessage(), 400); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
public function deleteLabelOfBoard($board_id, $label_id) |
| 119 |
{ |
| 120 |
try { |
| 121 |
|
| 122 |
$label = Label::findOrFail($label_id); |
| 123 |
|
| 124 |
if (count($label->tasks) > 0) { |
| 125 |
return $this->sendError([ |
| 126 |
'message' => __("You can't delete. This label used in task.", 'fluent-boards'), |
| 127 |
'type' => 'warning', |
| 128 |
]); |
| 129 |
} |
| 130 |
|
| 131 |
$this->labelService->deleteLabelOfBoard($label_id); |
| 132 |
|
| 133 |
return $this->sendSuccess([ |
| 134 |
'message' => __('Label has been deleted', 'fluent-boards'), |
| 135 |
'type' => 'success', |
| 136 |
], 201); |
| 137 |
} catch (\Exception $e) { |
| 138 |
return $this->sendError($e->getMessage(), 400); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public function editLabelofBoard(Request $request, $board_id, $label_id) |
| 143 |
{ |
| 144 |
$labelData = $this->labelSanitizeAndValidate($request->all(), [ |
| 145 |
'bg_color' => 'required|string', |
| 146 |
'color' => 'required|string', |
| 147 |
'label' => 'nullable|string', |
| 148 |
]); |
| 149 |
try { |
| 150 |
$label = $this->labelService->editLabelofBoard($labelData, $label_id); |
| 151 |
do_action('fluent_boards/board_label', $label, Constant::ACTIVITY_ACTION_UPDATED); |
| 152 |
|
| 153 |
return $this->sendSuccess([ |
| 154 |
'message' => __('Label has been updated', 'fluent-boards'), |
| 155 |
'label' => $label, |
| 156 |
], 200); |
| 157 |
} catch (\Exception $e) { |
| 158 |
return $this->sendError($e->getMessage(), 400); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
private function labelSanitizeAndValidate($data, array $rules = []) |
| 163 |
{ |
| 164 |
$data = Helper::sanitizeLabel($data); |
| 165 |
|
| 166 |
return $this->validate($data, $rules); |
| 167 |
} |
| 168 |
} |
| 169 |
|