| 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) |
| 72 |
{ |
| 73 |
$task = Task::findOrFail($labelData['task_id']); |
| 74 |
|
| 75 |
$task->labels()->syncWithoutDetaching([$labelData['boardTerm_id'] => ['object_type' => Constant::OBJECT_TYPE_TASK_LABEL]]); |
| 76 |
|
| 77 |
$label = $task->labels->find($labelData['boardTerm_id']); |
| 78 |
|
| 79 |
do_action('fluent_boards/task_label',$task, $label, 'added'); |
| 80 |
|
| 81 |
return $label; |
| 82 |
} |
| 83 |
|
| 84 |
public function getLabelsByTask($taskId) |
| 85 |
{ |
| 86 |
$task = Task::findOrFail($taskId); |
| 87 |
return $task->labels; |
| 88 |
} |
| 89 |
|
| 90 |
public function labelsByBoardId($boardId) |
| 91 |
{ |
| 92 |
return Label::where('board_id', $boardId)->whereNull('archived_at')->get(); |
| 93 |
} |
| 94 |
|
| 95 |
public function deleteLabelOfTask($taskId, $labelId) |
| 96 |
{ |
| 97 |
$task = Task::findOrFail($taskId); |
| 98 |
$task->labels()->detach($labelId); |
| 99 |
$label = Label::findOrFail($labelId); |
| 100 |
do_action('fluent_boards/task_label',$task, $label, 'removed'); |
| 101 |
} |
| 102 |
|
| 103 |
public function deleteLabelOfBoard($labelId) |
| 104 |
{ |
| 105 |
$label = Label::findOrFail($labelId); |
| 106 |
$boardId = $label->board_id; |
| 107 |
$label->delete(); |
| 108 |
|
| 109 |
do_action('fluent_boards/board_label_deleted', $label); |
| 110 |
} |
| 111 |
|
| 112 |
public function editLabelofBoard($labelData, $id) |
| 113 |
{ |
| 114 |
$label = Label::findOrFail($id); |
| 115 |
$label->title = $labelData['label']; |
| 116 |
if ($label->bg_color != $labelData['bg_color']) { |
| 117 |
$label->bg_color = $labelData['bg_color']; |
| 118 |
$label->color = $labelData['color']; |
| 119 |
} |
| 120 |
$label->save(); |
| 121 |
return $label; |
| 122 |
} |
| 123 |
|
| 124 |
public function copyLabelsOfBoard($boardId, $board) |
| 125 |
{ |
| 126 |
$boardCopyFrom = Board::findOrFail($boardId); |
| 127 |
|
| 128 |
$labelMap = []; |
| 129 |
|
| 130 |
foreach($boardCopyFrom->labels as $label) |
| 131 |
{ |
| 132 |
$labelToSave = array(); |
| 133 |
$labelToSave['title'] = $label->title; |
| 134 |
$labelToSave['slug'] = $label->slug; |
| 135 |
$labelToSave['board_id'] = $board->id; |
| 136 |
$labelToSave['type'] = 'label'; |
| 137 |
$labelToSave['position'] = 0; |
| 138 |
$labelToSave['color'] = $label->color; |
| 139 |
$labelToSave['bg_color'] = $label->bg_color; |
| 140 |
$copiedLabel = Label::create($labelToSave); |
| 141 |
|
| 142 |
$labelMap[$label['id']] = $copiedLabel->id; |
| 143 |
} |
| 144 |
return $labelMap; |
| 145 |
} |
| 146 |
public function getLastOneMinuteUpdatedLabels($boardId) |
| 147 |
{ |
| 148 |
$oneMinuteAgoTimestamp = current_time('timestamp') - 60; |
| 149 |
return Label::where('board_id', $boardId) |
| 150 |
->where('updated_at', '>=', date_i18n('Y-m-d H:i:s', $oneMinuteAgoTimestamp)) |
| 151 |
->get(); |
| 152 |
} |
| 153 |
} |