PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / trunk
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration vtrunk
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
fluent-boards / app / Services / LabelService.php

LabelService.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration trunk, at app/Services/LabelService.php

213 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\App\Services;
4
5 use FluentBoards\App\Models\Board;
6 use FluentBoards\App\Models\Label;
7 use FluentBoards\App\Models\Relation;
8 use FluentBoards\App\Models\Task;
9
10
11 class LabelService
12 {
13 public function getLabelsByBoard($boardId)
14 {
15 return Label::where('board_id', $boardId)->orderBy('created_at', 'ASC')->get();
16 }
17
18 public function getLabelsByBoardUsedInTasks($boardId)
19 {
20 $boardLabels = Label::where('board_id', $boardId)
21 ->where('type', 'label')
22 ->orderBy('created_at', 'ASC')
23 ->get();
24
25 if ($boardLabels->isEmpty()) {
26 return [];
27 }
28
29 // One relation query for the whole board rather than an exists() per label.
30 $usedIds = Relation::where('object_type', Constant::OBJECT_TYPE_TASK_LABEL)
31 ->whereIn('foreign_id', $boardLabels->pluck('id')->all())
32 ->distinct()
33 ->pluck('foreign_id')
34 ->all();
35
36 $usedIds = array_map('intval', $usedIds);
37
38 $usedLabel = [];
39 foreach ($boardLabels as $label) {
40 if (in_array((int) $label->id, $usedIds, true)) {
41 $usedLabel[] = $label;
42 }
43 }
44
45 return $usedLabel;
46 }
47
48 public function createLabel($labelData, $boardId)
49 {
50 $label = new Label();
51 $label->board_id = $boardId;
52 $label->title = $labelData['label'];
53 $label->bg_color = $labelData['bg_color'];
54 $label->color = $labelData['color'];
55 $label->save();
56
57 return $label;
58 }
59
60 public function createDefaultLabel($boardId)
61 {
62 $defaultColors = [
63 "green" => "#4bce97",
64 "yellow" => "#f5cd47",
65 "orange" => "#fea362",
66 "red" => "#f87168",
67 "purple" => "#9f8fef"
68 ];
69
70 $data = [];
71
72 foreach ($defaultColors as $index => $bg_color)
73 {
74 $data[] = [
75 'board_id' => $boardId,
76 // Titles match the create-board modal defaults, otherwise boards created
77 // outside that modal end up with colour chips carrying no text.
78 'title' => ucfirst($index),
79 'slug' => $index,
80 'type' => 'label',
81 'bg_color' => $bg_color,
82 'color' => Constant::TEXT_COLOR_MAP[$index],
83 'created_at' => current_time('mysql'),
84 'updated_at' => current_time('mysql')
85 ];
86 }
87
88 Label::insert($data);
89 }
90
91 public function createLabelForTask($labelData, $boardId = null)
92 {
93 $task = $boardId ? (new TaskService())->findTaskOnBoard($labelData['task_id'], $boardId) : Task::findOrFail($labelData['task_id']);
94 $label = $this->findLabelOnBoard($labelData['board_term_id'], $boardId ?: $task->board_id);
95
96 $task->labels()->syncWithoutDetaching([$labelData['board_term_id'] => ['object_type' => Constant::OBJECT_TYPE_TASK_LABEL]]);
97
98 $label = $task->labels->find($label->id);
99
100 do_action('fluent_boards/task_label',$task, $label, 'added');
101
102 return $label;
103 }
104
105 public function getLabelsByTask($taskId, $boardId = null)
106 {
107 $task = $boardId ? (new TaskService())->findTaskOnBoard($taskId, $boardId) : Task::findOrFail($taskId);
108 return $task->labels;
109 }
110
111 public function labelsByBoardId($boardId)
112 {
113 return Label::where('board_id', $boardId)->whereNull('archived_at')->get();
114 }
115
116 public function deleteLabelOfTask($taskId, $labelId, $boardId = null)
117 {
118 $task = $boardId ? (new TaskService())->findTaskOnBoard($taskId, $boardId) : Task::findOrFail($taskId);
119 $label = $this->findLabelOnBoard($labelId, $boardId ?: $task->board_id);
120 $task->labels()->detach($labelId);
121 do_action('fluent_boards/task_label',$task, $label, 'removed');
122 }
123
124 public function deleteLabelOfBoard($labelId, $boardId = null)
125 {
126 $label = $boardId ? $this->findLabelOnBoard($labelId, $boardId) : Label::findOrFail($labelId);
127 $label->tasks()->detach();
128 $label->delete();
129
130 do_action('fluent_boards/board_label_deleted', $label);
131 }
132
133 public function editLabelofBoard($labelData, $id, $boardId = null)
134 {
135 $label = $boardId ? $this->findLabelOnBoard($id, $boardId) : Label::findOrFail($id);
136 $label->title = $labelData['label'];
137
138 // Background and text colour move independently: coupling them dropped a
139 // text-colour-only change on the floor while still reporting success.
140 if (isset($labelData['bg_color']) && $labelData['bg_color'] !== '') {
141 $label->bg_color = $labelData['bg_color'];
142 }
143
144 if (isset($labelData['color']) && $labelData['color'] !== '') {
145 $label->color = $labelData['color'];
146 }
147
148 $label->save();
149 return $label;
150 }
151
152 /**
153 * Resolve a label only when it belongs to the requested board.
154 *
155 * @param int $labelId
156 * @param int $boardId
157 * @return Label
158 * @throws \Exception
159 */
160 public function findLabelOnBoard($labelId, $boardId)
161 {
162 $label = Label::where('id', absint($labelId))
163 ->where('board_id', absint($boardId))
164 ->where('type', 'label')
165 ->first();
166
167 if (!$label) {
168 throw new \Exception(esc_html__('Label not found', 'fluent-boards'));
169 }
170
171 return $label;
172 }
173
174 public function copyLabelsOfBoard($boardId, $board)
175 {
176 $boardCopyFrom = Board::findOrFail($boardId);
177
178 $labelMap = [];
179
180 foreach($boardCopyFrom->labels as $label)
181 {
182 $labelToSave = array();
183 $labelToSave['title'] = $label->title;
184 $labelToSave['slug'] = $label->slug;
185 $labelToSave['board_id'] = $board->id;
186 $labelToSave['type'] = 'label';
187 $labelToSave['position'] = 0;
188 $labelToSave['color'] = $label->color;
189 $labelToSave['bg_color'] = $label->bg_color;
190 $copiedLabel = Label::create($labelToSave);
191
192 $labelMap[$label['id']] = $copiedLabel->id;
193 }
194 return $labelMap;
195 }
196 public function getLastOneMinuteUpdatedLabels($boardId, $lastUpdated = null, $includeArchived = true)
197 {
198 if (!$lastUpdated) {
199 $oneMinuteAgoTimestamp = current_time('timestamp') - 60;
200 $lastUpdated = date_i18n('Y-m-d H:i:s', $oneMinuteAgoTimestamp);
201 }
202
203 $labelsQuery = Label::where('board_id', $boardId)
204 ->where('updated_at', '>=', $lastUpdated);
205
206 if (!$includeArchived) {
207 $labelsQuery->whereNull('archived_at');
208 }
209
210 return $labelsQuery->get();
211 }
212 }
213