| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Board; |
| 6 |
use FluentBoards\App\Models\Stage; |
| 7 |
use FluentBoards\App\Models\Task; |
| 8 |
use FluentBoards\App\Models\User; |
| 9 |
use FluentBoards\App\Services\Constant; |
| 10 |
use FluentCrm\App\Models\Subscriber; |
| 11 |
use FluentBoards\App\Models\Comment; |
| 12 |
use FluentBoards\App\Services\Helper; |
| 13 |
|
| 14 |
class ActivityHandler |
| 15 |
{ |
| 16 |
public function createLogActivity($taskId, $action, $column, $oldValue = null, $newValue = null, $description = null, $settings = null ) |
| 17 |
{ |
| 18 |
$data = [ |
| 19 |
'object_type' => Constant::ACTIVITY_TASK, |
| 20 |
'object_id' => $taskId, |
| 21 |
'action' => $action, // action type: changed, updated, added, removed |
| 22 |
'column' => $column, |
| 23 |
'old_value' => $oldValue, |
| 24 |
'new_value' => $newValue, |
| 25 |
'description' => $description, |
| 26 |
'settings' => $settings |
| 27 |
]; |
| 28 |
$userId = get_current_user_id(); |
| 29 |
if($userId == 0){ |
| 30 |
$task = Task::find($taskId); |
| 31 |
$data['created_by'] = $task->created_by; |
| 32 |
} |
| 33 |
|
| 34 |
Helper::createActivity($data); |
| 35 |
} |
| 36 |
public function logMoveTaskToAnotherBoardActivity($task, $oldTask) |
| 37 |
{ |
| 38 |
$old = $this->getBoardTitleById($oldTask->board_id); |
| 39 |
$new = $this->getBoardTitleById($task->board_id); |
| 40 |
$this->createLogActivity( $task->id,'changed', 'board', $old, $new); |
| 41 |
} |
| 42 |
|
| 43 |
public function logAssigneeAddedActivity($task, $newAssigneeId) |
| 44 |
{ |
| 45 |
$user = User::findOrFail($newAssigneeId); |
| 46 |
$currentUserId = get_current_user_id(); |
| 47 |
if($user) { |
| 48 |
$this->createLogActivity($task->id, 'added', 'assignee', null, $user->display_name); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
public function logAssigneeRemovedActivity($task, $newAssigneeId) |
| 53 |
{ |
| 54 |
$user = User::findOrFail($newAssigneeId); |
| 55 |
if($user) { |
| 56 |
$this->createLogActivity($task->id, 'removed', 'assignee', null, $user->display_name); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public function logTaskCreationActivity($task) |
| 61 |
{ |
| 62 |
$this->createLogActivity( $task->id,'created', 'task', null, $task->title, null ); |
| 63 |
} |
| 64 |
|
| 65 |
public function logTaskContentUpdatedActivity($task, $col, $oldTask = null) |
| 66 |
{ |
| 67 |
$action = 'updated'; |
| 68 |
if($col == 'description'){ |
| 69 |
$this->createLogActivity( $task->id, $action, $col, $oldTask->description, $task->description ); |
| 70 |
}else{ |
| 71 |
if($task->parent_id){ |
| 72 |
$this->createLogActivity( $task->parent_id, $action, 'subtask', $oldTask->title, $task->title ); |
| 73 |
}else{ |
| 74 |
$this->createLogActivity( $task->id, $action, $col, $oldTask->title, $task->title ); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public function taskLabelActivity($task, $label, $action) |
| 80 |
{ |
| 81 |
$column = 'label'; |
| 82 |
$settings = [ |
| 83 |
'bg_color' => $label->bg_color, |
| 84 |
'color' => $label->color, |
| 85 |
'title' => $label->title, |
| 86 |
]; |
| 87 |
|
| 88 |
$this->createLogActivity($task->id, $action, $column, null, null, null, $settings); |
| 89 |
} |
| 90 |
|
| 91 |
public function logDueDateActivity($task, $oldDate) |
| 92 |
{ |
| 93 |
$column = 'Due Date'; |
| 94 |
$action = 'changed'; |
| 95 |
|
| 96 |
if($oldDate){ |
| 97 |
$new = gmdate("F j, Y, g:i a", strtotime($task->due_at)); |
| 98 |
$old = gmdate("F j, Y, g:i a", strtotime($oldDate)); |
| 99 |
if($new == $old) |
| 100 |
return; |
| 101 |
} |
| 102 |
$newDueDate = $task->due_at; |
| 103 |
$new = null; |
| 104 |
if ($newDueDate) { |
| 105 |
$new = gmdate("F j, Y, g:i a", strtotime($newDueDate)); |
| 106 |
if(str_contains($new, '12:00 am')){ |
| 107 |
$new = gmdate("F j, Y", strtotime($newDueDate)); |
| 108 |
} |
| 109 |
} |
| 110 |
$old = null; |
| 111 |
if(!$oldDate){ |
| 112 |
$action = 'added'; |
| 113 |
}else{ |
| 114 |
$old = gmdate("F j, Y, g:i a", strtotime($oldDate)); |
| 115 |
if(str_contains($old, '12:00 am')){ |
| 116 |
$old = gmdate("F j, Y", strtotime($oldDate)); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
$this->createLogActivity($task->id, $action, $column, $old, $new); |
| 121 |
} |
| 122 |
|
| 123 |
public function logStartDateActivity($task, $oldDate) |
| 124 |
{ |
| 125 |
$column = 'Start Date'; |
| 126 |
$action = 'changed'; |
| 127 |
|
| 128 |
if($oldDate){ |
| 129 |
$new = gmdate("F j, Y", strtotime($task->started_at)); |
| 130 |
$old = gmdate("F j, Y", strtotime($oldDate)); |
| 131 |
if($new == $old) |
| 132 |
return; |
| 133 |
} |
| 134 |
$newStartDate = $task->started_at; |
| 135 |
$new = null; |
| 136 |
if ($newStartDate) { |
| 137 |
$new = gmdate("F j, Y", strtotime($newStartDate)); |
| 138 |
} |
| 139 |
$old = null; |
| 140 |
if(!$oldDate){ |
| 141 |
$action = 'added'; |
| 142 |
}else{ |
| 143 |
$old = gmdate("F j, Y", strtotime($oldDate)); |
| 144 |
} |
| 145 |
|
| 146 |
$this->createLogActivity($task->id, $action, $column, $old, $new); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
public function logPriorityChangeActivity($task, $old) |
| 151 |
{ |
| 152 |
$new = $task->priority; |
| 153 |
$this->createLogActivity($task->id, 'changed', 'priority', $old, $new); |
| 154 |
} |
| 155 |
|
| 156 |
public function logCommentCreateActivity($comment) |
| 157 |
{ |
| 158 |
if($comment->parent_id) { |
| 159 |
// dd($comment); |
| 160 |
$parent = Comment::findOrFail($comment->parent_id); |
| 161 |
$parentDescription = $parent->description; |
| 162 |
$this->createLogActivity($comment->task_id, 'added', 'a reply'); |
| 163 |
}else{ |
| 164 |
$commentPlainText = strip_tags($comment->description); |
| 165 |
$this->createLogActivity($comment->task_id, 'added', 'comment', null, $commentPlainText, null); |
| 166 |
} |
| 167 |
} |
| 168 |
public function logCommentUpdateActivity($comment, $oldComment) |
| 169 |
{ |
| 170 |
$taskId = $comment->task_id; |
| 171 |
$newComment = $comment->settings['raw_description'] ?? $comment->description; |
| 172 |
|
| 173 |
$this->createLogActivity($taskId, 'updated', 'comment', $oldComment, $newComment); |
| 174 |
} |
| 175 |
public function logCommentDeleteActivity($comment) |
| 176 |
{ |
| 177 |
$commentDescription = strip_tags($comment->settings['raw_description'] ?? $comment->description ); |
| 178 |
$taskId = $comment->task_id; |
| 179 |
|
| 180 |
$this->createLogActivity($taskId, 'removed', 'comment', null, $commentDescription, null); |
| 181 |
} |
| 182 |
|
| 183 |
public function logSubtaskAddedActivity($parentTask, $subTask) |
| 184 |
{ |
| 185 |
$this->createLogActivity($parentTask->id, 'added', 'subtask', null, $subTask->title); |
| 186 |
} |
| 187 |
|
| 188 |
public function logSubtaskDeletedActivity($id, $subTaskTitle) |
| 189 |
{ |
| 190 |
$this->createLogActivity($id, 'removed', 'subtask', null, $subTaskTitle); |
| 191 |
} |
| 192 |
|
| 193 |
public function logTaskCompletedOrReopenActivity($task, $status) |
| 194 |
{ |
| 195 |
if($status == 'completed'){ |
| 196 |
if($task->parent_id){ |
| 197 |
$this->createLogActivity($task->parent_id, 'completed', 'subtask', $task->title); |
| 198 |
}else{ |
| 199 |
$this->createLogActivity($task->id, 'completed', 'task', $task->title); |
| 200 |
} |
| 201 |
}else{ |
| 202 |
if($task->parent_id){ |
| 203 |
$this->createLogActivity($task->parent_id, 'reopened', 'subtask', $task->title); |
| 204 |
}else{ |
| 205 |
$this->createLogActivity($task->id, 'reopened', 'task', $task->title); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
public function logTaskStageUpdatedActivity($task, $oldStageId) |
| 211 |
{ |
| 212 |
$oldStage = Stage::findOrFail($oldStageId); |
| 213 |
$newStage = Stage::findOrFail($task->stage_id); |
| 214 |
if($oldStage && $newStage){ |
| 215 |
$this->createLogActivity($task->id, 'changed', 'stage', $oldStage->title, $newStage->title); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
public function associateUserAddChangeRemoveActivity($oldAssociateId, $newAssociateId, $taskId) |
| 220 |
{ |
| 221 |
$subsciber = new Subscriber(); |
| 222 |
$oldAssociateUser = $subsciber->find($oldAssociateId); |
| 223 |
$newAssociateUser = $subsciber->find($newAssociateId); |
| 224 |
$action = 'added'; |
| 225 |
$column = 'the associate email'; |
| 226 |
$old = null; $new = null; |
| 227 |
|
| 228 |
if ($oldAssociateUser && $newAssociateUser) { |
| 229 |
$old = $oldAssociateUser->email; |
| 230 |
$new = $newAssociateUser->email; |
| 231 |
$action = 'changed'; |
| 232 |
} elseif ($oldAssociateUser && !$newAssociateUser) { |
| 233 |
$old = $oldAssociateUser->email; |
| 234 |
$action = 'removed'; |
| 235 |
} else { |
| 236 |
$new = $newAssociateUser->email; |
| 237 |
} |
| 238 |
$this->createLogActivity($taskId, $action, $column, $old, $new); |
| 239 |
} |
| 240 |
|
| 241 |
public function taskAssigneeJoin($id) |
| 242 |
{ |
| 243 |
$this->createLogActivity($id, 'joined', 'task', null, null); |
| 244 |
} |
| 245 |
|
| 246 |
public function taskAssigneeLeave($id) |
| 247 |
{ |
| 248 |
$this->createLogActivity($id, 'left', 'task', null, null); |
| 249 |
} |
| 250 |
|
| 251 |
public function labelManageForTaskActivity($id, $action) |
| 252 |
{ |
| 253 |
$this->createLogActivity($id, $action, 'label', null, null); |
| 254 |
} |
| 255 |
|
| 256 |
private function getBoardTitleById($boardId) |
| 257 |
{ |
| 258 |
return Board::findOrFail($boardId)->title; // it can be further optimized , we may limit multiple query here. |
| 259 |
} |
| 260 |
|
| 261 |
public function taskAddedFromFluentForms($task) |
| 262 |
{ |
| 263 |
$taskActivity = [ |
| 264 |
'object_type' => Constant::ACTIVITY_TASK, |
| 265 |
'object_id' => $task->id, |
| 266 |
'action' => 'created', // action type: changed, updated, added, removed |
| 267 |
'column' => 'task', |
| 268 |
'old_value' => $task->title, |
| 269 |
'new_value' => null, |
| 270 |
'description' => " from Fluent Forms", |
| 271 |
]; |
| 272 |
|
| 273 |
$board = Board::find($task->board_id); |
| 274 |
$settings = $board->settings ?? []; |
| 275 |
|
| 276 |
if (isset($settings['tasks_count'])) { |
| 277 |
if ($settings['tasks_count'] != 0) $settings['tasks_count'] += 1; |
| 278 |
} else { |
| 279 |
$settings['tasks_count'] = 1; |
| 280 |
} |
| 281 |
$board->settings = $settings; |
| 282 |
$board->save(); |
| 283 |
$taskStage = $task->stage; |
| 284 |
$settings = ['task_id' => $task->id]; |
| 285 |
|
| 286 |
$boardActivity = [ |
| 287 |
'object_type' => Constant::ACTIVITY_BOARD, |
| 288 |
'object_id' => $task->board_id, |
| 289 |
'action' => 'created', // action type: changed, updated, added, removed, created |
| 290 |
'column' => 'task', |
| 291 |
'old_value' => null, |
| 292 |
'new_value' => $task->title, |
| 293 |
'description' => 'on stage '. $taskStage->title . ' from Fluent Forms ', |
| 294 |
'settings' => $settings |
| 295 |
]; |
| 296 |
if (intval($task->created_by) > 0) { |
| 297 |
$taskActivity['created_by'] = intval($task->created_by); |
| 298 |
$boardActivity['created_by'] = intval($task->created_by); |
| 299 |
|
| 300 |
} else { |
| 301 |
$taskActivity['description'] .= ' by '. $task['settings']['author']['name'] . " (" . $task['settings']['author']['email'] . ")"; |
| 302 |
$boardActivity['description'] .= ' by '. $task['settings']['author']['name'] . " (" . $task['settings']['author']['email'] . ")"; |
| 303 |
} |
| 304 |
Helper::createActivity($taskActivity); |
| 305 |
Helper::createActivity($boardActivity); |
| 306 |
|
| 307 |
} |
| 308 |
public function taskAttachmentAdded($attachment) |
| 309 |
{ |
| 310 |
$this->createLogActivity($attachment->object_id , 'added', 'attachment', null, strlen($attachment->title) > 0 ? $attachment->title : $attachment->full_url); |
| 311 |
} |
| 312 |
public function taskAttachmentDeleted($attachment) |
| 313 |
{ |
| 314 |
$this->createLogActivity($attachment->object_id , 'deleted', 'attachment', null, strlen($attachment->title) > 0 ? $attachment->title : $attachment->full_url); |
| 315 |
} |
| 316 |
} |
| 317 |
|