| 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 |
$boardLabel = Label::where('board_id', $boardId)->where('type', 'label')->orderBy('created_at', 'ASC')->get(); |
| 21 |
$usedLabel = []; |
| 22 |
foreach ($boardLabel as $label) { |
| 23 |
$exist = Relation::where('foreign_id', $label->id)->where('object_type', 'task_label')->exists(); |
| 24 |
if ($exist) { |
| 25 |
$usedLabel[] = $label; |
| 26 |
} |
| 27 |
} |
| 28 |
return $usedLabel; |
| 29 |
} |
| 30 |
|
| 31 |
public function createLabel($labelData, $boardId) |
| 32 |
{ |
| 33 |
$label = new Label(); |
| 34 |
$label->board_id = $boardId; |
| 35 |
$label->title = $labelData['label']; |
| 36 |
$label->bg_color = $labelData['bg_color']; |
| 37 |
$label->color = $labelData['color']; |
| 38 |
$label->save(); |
| 39 |
|
| 40 |
return $label; |
| 41 |
} |
| 42 |
|
| 43 |
public function createDefaultLabel($boardId) |
| 44 |
{ |
| 45 |
$defaultColors = [ |
| 46 |
"green" => "#4bce97", |
| 47 |
"yellow" => "#f5cd47", |
| 48 |
"orange" => "#fea362", |
| 49 |
"red" => "#f87168", |
| 50 |
"purple" => "#9f8fef" |
| 51 |
]; |
| 52 |
|
| 53 |
$data = []; |
| 54 |
|
| 55 |
foreach ($defaultColors as $index => $bg_color) |
| 56 |
{ |
| 57 |
$data[] = [ |
| 58 |
'board_id' => $boardId, |
| 59 |
'slug' => $index, |
| 60 |
'type' => 'label', |
| 61 |
'bg_color' => $bg_color, |
| 62 |
'color' => Constant::TEXT_COLOR_MAP[$index], |
| 63 |
'created_at' => current_time('mysql'), |
| 64 |
'updated_at' => current_time('mysql') |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
Label::insert($data); |
| 69 |
} |
| 70 |
|
| 71 |
public function createLabelForTask($labelData, $boardId = null) |
| 72 |
{ |
| 73 |
$task = $boardId ? (new TaskService())->findTaskOnBoard($labelData['task_id'], $boardId) : Task::findOrFail($labelData['task_id']); |
| 74 |
$label = $this->findLabelOnBoard($labelData['board_term_id'], $boardId ?: $task->board_id); |
| 75 |
|
| 76 |
$task->labels()->syncWithoutDetaching([$labelData['board_term_id'] => ['object_type' => Constant::OBJECT_TYPE_TASK_LABEL]]); |
| 77 |
|
| 78 |
$label = $task->labels->find($label->id); |
| 79 |
|
| 80 |
do_action('fluent_boards/task_label',$task, $label, 'added'); |
| 81 |
|
| 82 |
return $label; |
| 83 |
} |
| 84 |
|
| 85 |
public function getLabelsByTask($taskId, $boardId = null) |
| 86 |
{ |
| 87 |
$task = $boardId ? (new TaskService())->findTaskOnBoard($taskId, $boardId) : Task::findOrFail($taskId); |
| 88 |
return $task->labels; |
| 89 |
} |
| 90 |
|
| 91 |
public function labelsByBoardId($boardId) |
| 92 |
{ |
| 93 |
return Label::where('board_id', $boardId)->whereNull('archived_at')->get(); |
| 94 |
} |
| 95 |
|
| 96 |
public function deleteLabelOfTask($taskId, $labelId, $boardId = null) |
| 97 |
{ |
| 98 |
$task = $boardId ? (new TaskService())->findTaskOnBoard($taskId, $boardId) : Task::findOrFail($taskId); |
| 99 |
$label = $this->findLabelOnBoard($labelId, $boardId ?: $task->board_id); |
| 100 |
$task->labels()->detach($labelId); |
| 101 |
do_action('fluent_boards/task_label',$task, $label, 'removed'); |
| 102 |
} |
| 103 |
|
| 104 |
public function deleteLabelOfBoard($labelId, $boardId = null) |
| 105 |
{ |
| 106 |
$label = $boardId ? $this->findLabelOnBoard($labelId, $boardId) : Label::findOrFail($labelId); |
| 107 |
$label->tasks()->detach(); |
| 108 |
$label->delete(); |
| 109 |
|
| 110 |
do_action('fluent_boards/board_label_deleted', $label); |
| 111 |
} |
| 112 |
|
| 113 |
public function editLabelofBoard($labelData, $id, $boardId = null) |
| 114 |
{ |
| 115 |
$label = $boardId ? $this->findLabelOnBoard($id, $boardId) : Label::findOrFail($id); |
| 116 |
$label->title = $labelData['label']; |
| 117 |
if ($label->bg_color != $labelData['bg_color']) { |
| 118 |
$label->bg_color = $labelData['bg_color']; |
| 119 |
$label->color = $labelData['color']; |
| 120 |
} |
| 121 |
$label->save(); |
| 122 |
return $label; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Resolve a label only when it belongs to the requested board. |
| 127 |
* |
| 128 |
* @param int $labelId |
| 129 |
* @param int $boardId |
| 130 |
* @return Label |
| 131 |
* @throws \Exception |
| 132 |
*/ |
| 133 |
public function findLabelOnBoard($labelId, $boardId) |
| 134 |
{ |
| 135 |
$label = Label::where('id', absint($labelId)) |
| 136 |
->where('board_id', absint($boardId)) |
| 137 |
->where('type', 'label') |
| 138 |
->first(); |
| 139 |
|
| 140 |
if (!$label) { |
| 141 |
throw new \Exception(esc_html__('Label not found', 'fluent-boards')); |
| 142 |
} |
| 143 |
|
| 144 |
return $label; |
| 145 |
} |
| 146 |
|
| 147 |
public function copyLabelsOfBoard($boardId, $board) |
| 148 |
{ |
| 149 |
$boardCopyFrom = Board::findOrFail($boardId); |
| 150 |
|
| 151 |
$labelMap = []; |
| 152 |
|
| 153 |
foreach($boardCopyFrom->labels as $label) |
| 154 |
{ |
| 155 |
$labelToSave = array(); |
| 156 |
$labelToSave['title'] = $label->title; |
| 157 |
$labelToSave['slug'] = $label->slug; |
| 158 |
$labelToSave['board_id'] = $board->id; |
| 159 |
$labelToSave['type'] = 'label'; |
| 160 |
$labelToSave['position'] = 0; |
| 161 |
$labelToSave['color'] = $label->color; |
| 162 |
$labelToSave['bg_color'] = $label->bg_color; |
| 163 |
$copiedLabel = Label::create($labelToSave); |
| 164 |
|
| 165 |
$labelMap[$label['id']] = $copiedLabel->id; |
| 166 |
} |
| 167 |
return $labelMap; |
| 168 |
} |
| 169 |
public function getLastOneMinuteUpdatedLabels($boardId, $lastUpdated = null, $includeArchived = true) |
| 170 |
{ |
| 171 |
if (!$lastUpdated) { |
| 172 |
$oneMinuteAgoTimestamp = current_time('timestamp') - 60; |
| 173 |
$lastUpdated = date_i18n('Y-m-d H:i:s', $oneMinuteAgoTimestamp); |
| 174 |
} |
| 175 |
|
| 176 |
$labelsQuery = Label::where('board_id', $boardId) |
| 177 |
->where('updated_at', '>=', $lastUpdated); |
| 178 |
|
| 179 |
if (!$includeArchived) { |
| 180 |
$labelsQuery->whereNull('archived_at'); |
| 181 |
} |
| 182 |
|
| 183 |
return $labelsQuery->get(); |
| 184 |
} |
| 185 |
} |
| 186 |
|