| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Task; |
| 6 |
use FluentBoards\App\Models\User; |
| 7 |
use FluentBoards\App\Models\Board; |
| 8 |
use FluentBoards\App\Models\Notification; |
| 9 |
use FluentBoards\App\Services\Constant; |
| 10 |
|
| 11 |
class NotificationHandler |
| 12 |
{ |
| 13 |
public function addCommentNotification($comment) |
| 14 |
{ |
| 15 |
if($comment->task_id){ |
| 16 |
$task = Task::findOrFail($comment->task_id); |
| 17 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 18 |
if(count($userIdsWhoGetNotification) > 0){ |
| 19 |
$plainDescription = wp_strip_all_tags($comment->description); |
| 20 |
$action = $comment->parent_id ? 'task_reply_added' : 'comment_created'; |
| 21 |
$message = $plainDescription; |
| 22 |
$settings = [ 'comment_id' => $comment->parent_id ?? $comment->id ]; |
| 23 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message, $settings); |
| 24 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public function mentionInCommentNotification($comment, $mentionIds) |
| 30 |
{ |
| 31 |
if($comment){ |
| 32 |
$task = Task::findOrFail($comment->task_id); |
| 33 |
$plainDescription = wp_strip_all_tags($comment->description); |
| 34 |
$action = 'task_comment_mentioned'; |
| 35 |
$message = $plainDescription; |
| 36 |
$settings = [ 'comment_id' => $comment->parent_id ?? $comment->id ]; |
| 37 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message, $settings); |
| 38 |
$notification->users()->attach($mentionIds); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
public function addSubtaskNotification($parentTask, $subTask) |
| 43 |
{ |
| 44 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($parentTask); |
| 45 |
if(count($userIdsWhoGetNotification) > 0){ |
| 46 |
$board_id = $subTask->board_id; |
| 47 |
$action = 'subtask_added'; |
| 48 |
$message = $subTask->title ; |
| 49 |
$notification = $this->createNotification($parentTask, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 50 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public function changeDueDateNotification($task, $oldDate) |
| 55 |
{ |
| 56 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 57 |
if(count($userIdsWhoGetNotification) > 0){ |
| 58 |
$action = 'task_due_date_changed'; |
| 59 |
$message = date_i18n('F j, Y, g:i a', strtotime($task->due_at)); |
| 60 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 61 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function changeStartDateNotification($task, $oldDate) |
| 66 |
{ |
| 67 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 68 |
if(count($userIdsWhoGetNotification) > 0){ |
| 69 |
$action = 'task_start_date_changed'; |
| 70 |
$message = date_i18n('F j, Y, g:i a', strtotime($task->started_at)); |
| 71 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 72 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public function changeStageNotification($task, $oldStageId) |
| 77 |
{ |
| 78 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 79 |
if(count($userIdsWhoGetNotification) > 0){ |
| 80 |
$action = 'task_moved_to_new_stage'; |
| 81 |
$message = $task->stage->title; |
| 82 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 83 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public function changePriorityNotification($task, $oldPriority) |
| 88 |
{ |
| 89 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 90 |
if(count($userIdsWhoGetNotification) > 0){ |
| 91 |
$action = 'task_priority_changed'; |
| 92 |
$message = $task->priority; |
| 93 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 94 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public function taskArchiveNotification($task) |
| 99 |
{ |
| 100 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 101 |
if(count($userIdsWhoGetNotification) > 0){ |
| 102 |
if ($task->archived_at) { |
| 103 |
$action = 'board_task_archived'; |
| 104 |
$message = $task->title; |
| 105 |
} else { |
| 106 |
$action = 'board_task_restored'; |
| 107 |
$message = $task->title; |
| 108 |
} |
| 109 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 110 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public function changeTitleOrDescriptionNotification($task, $col, $oldTask) |
| 115 |
{ |
| 116 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 117 |
if(count($userIdsWhoGetNotification) > 0){ |
| 118 |
if($col == 'title'){ |
| 119 |
$action = 'task_title_updated'; |
| 120 |
$message = $task->title; |
| 121 |
}else{ |
| 122 |
$action = 'task_description_updated'; |
| 123 |
$message = $task->description; |
| 124 |
} |
| 125 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 126 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public function changeBoardNotification($task, $oldBoardId) |
| 131 |
{ |
| 132 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 133 |
if(count($userIdsWhoGetNotification) > 0){ |
| 134 |
$new_board_id = Task::findOrFail($task->board_id)->board_id; |
| 135 |
$new_board_title = Board::findOrFail($new_board_id)->title; |
| 136 |
// translators: %1$s is the task title, %2$s is the board title |
| 137 |
$message = sprintf(__('moved %1$s task to %2$s board.','fluent-boards'), $task->title, $new_board_title); |
| 138 |
$notification = $this->createNotification($oldBoardId, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $message); |
| 139 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
public function assigneeAddedNotification($task, $newAssigneeId) |
| 144 |
{ |
| 145 |
if($newAssigneeId != get_current_user_id()){ |
| 146 |
$action = 'task_assignee_changed'; |
| 147 |
$message = __('has added you as an assignee.','fluent-boards'); |
| 148 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 149 |
$notification->users()->attach($newAssigneeId); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
public function assigneeRemovedNotification($task, $newAssigneeId) |
| 154 |
{ |
| 155 |
if($newAssigneeId != get_current_user_id()){ |
| 156 |
$action = 'task_assignee_changed'; |
| 157 |
$message = __('has removed you as an assignee.','fluent-boards'); |
| 158 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 159 |
$notification->users()->attach($newAssigneeId); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
public function createNotification($task, $objectType, $action, $description, $settings = null) |
| 164 |
{ |
| 165 |
$data = [ |
| 166 |
'object_id' => $task->board_id, |
| 167 |
'object_type' => $objectType, |
| 168 |
'task_id' => $task->id, |
| 169 |
'action' => $action, |
| 170 |
'description' => $description, |
| 171 |
'settings' => $settings |
| 172 |
]; |
| 173 |
$currentUser = get_current_user_id(); |
| 174 |
if($currentUser == 0) { |
| 175 |
$data['activity_by'] = $task->created_by; |
| 176 |
} |
| 177 |
return Notification::create($data); |
| 178 |
} |
| 179 |
|
| 180 |
public function findUsersWhoWillGetNotification($task) |
| 181 |
{ |
| 182 |
$currentUserId = get_current_user_id(); |
| 183 |
$watchersOfTask = $task->watchers; |
| 184 |
$watchersExceptCurrentUser = []; |
| 185 |
foreach($watchersOfTask as $watcher){ |
| 186 |
if($watcher->ID != $currentUserId){ |
| 187 |
$watchersExceptCurrentUser[] = $watcher->ID; |
| 188 |
} |
| 189 |
} |
| 190 |
return $watchersExceptCurrentUser; |
| 191 |
} |
| 192 |
} |
| 193 |
|