| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentBoards\App\Hooks\Handlers; |
| 4 | 4 | |
| 5 | +use FluentBoards\App\Models\Attachment; | |
| 5 | 6 | use FluentBoards\App\Models\Comment; |
| 6 | 7 | use FluentBoards\App\Models\Meta; |
| 7 | 8 | use FluentBoards\App\Models\NotificationUser; |
| 8 | 9 | use FluentBoards\App\Models\Relation; |
| @@ -10,13 +11,14 @@ | ||
| 10 | 11 | use FluentBoards\App\Models\Task; |
| 11 | 12 | use FluentBoards\App\Models\Board; |
| 12 | 13 | use FluentBoards\App\Models\User; |
| 13 | 14 | use FluentBoards\App\Services\Constant; |
| 15 | +use FluentBoards\App\Services\BoardService; | |
| 14 | 16 | use FluentBoards\App\Services\Helper; |
| 15 | 17 | |
| 16 | 18 | class BoardHandler |
| 17 | 19 | { |
| 18 | - public function createLogActivity($boardId, $action, $column = null, $oldValue = null, $newValue = null, $description = null, $settings = null ) | |
| 20 | + public function createLogActivity($boardId, $action, $column = null, $oldValue = null, $newValue = null, $description = null, $settings = null, $userId = null ) | |
| 19 | 21 | { |
| 20 | 22 | $data = [ |
| 21 | 23 | 'object_type' => Constant::ACTIVITY_BOARD, |
| 22 | 24 | 'object_id' => $boardId, |
| @@ -26,8 +28,11 @@ | ||
| 26 | 28 | 'new_value' => $newValue, |
| 27 | 29 | 'description' => $description, |
| 28 | 30 | 'settings' => $settings |
| 29 | 31 | ]; |
| 32 | + if($userId) { | |
| 33 | + $data['created_by'] = $userId; | |
| 34 | + } | |
| 30 | 35 | |
| 31 | 36 | Helper::createActivity($data); |
| 32 | 37 | } |
| 33 | 38 | public function getAllBoards($sortBy = 'ASC') |
| @@ -38,8 +43,15 @@ | ||
| 38 | 43 | 'boards' => $boards, |
| 39 | 44 | ]; |
| 40 | 45 | } |
| 41 | 46 | |
| 47 | + public function attachFolderToBoard($board) | |
| 48 | + { | |
| 49 | + $board->folder = (new BoardService())->getBoardFolder($board->id); | |
| 50 | + | |
| 51 | + return $board; | |
| 52 | + } | |
| 53 | + | |
| 42 | 54 | public function boardCreated($board) |
| 43 | 55 | { |
| 44 | 56 | $boardId = $board->id; |
| 45 | 57 | $this->createLogActivity($boardId, 'created', 'board', null, null, $board->title); |
| @@ -45,8 +57,40 @@ | ||
| 45 | 57 | $this->createLogActivity($boardId, 'created', 'board', null, null, $board->title); |
| 46 | 58 | $this->updateOnboarding(); |
| 47 | 59 | } |
| 48 | 60 | |
| 61 | + /** | |
| 62 | + * Log a system activity without allowing a non-critical log failure to fail the automation. | |
| 63 | + * | |
| 64 | + * @param Board $board | |
| 65 | + * @return void | |
| 66 | + */ | |
| 67 | + public function boardCreatedFromAutomation($board) | |
| 68 | + { | |
| 69 | + $previousUserId = get_current_user_id(); | |
| 70 | + | |
| 71 | + try { | |
| 72 | + wp_set_current_user(0); | |
| 73 | + | |
| 74 | + $this->createLogActivity( | |
| 75 | + $board->id, | |
| 76 | + 'created', | |
| 77 | + 'board', | |
| 78 | + null, | |
| 79 | + null, | |
| 80 | + null, | |
| 81 | + ['source' => 'fluentcrm_automation'] | |
| 82 | + ); | |
| 83 | + } catch (\Throwable $exception) { | |
| 84 | + error_log(sprintf( | |
| 85 | + 'FluentBoards: Failed to log FluentCRM automation board activity: %s', | |
| 86 | + sanitize_text_field($exception->getMessage()) | |
| 87 | + )); | |
| 88 | + } finally { | |
| 89 | + wp_set_current_user($previousUserId); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + | |
| 49 | 93 | private function updateOnboarding() |
| 50 | 94 | { |
| 51 | 95 | $onboarding = Meta::where('key', Constant::FBS_ONBOARDING)->first(); |
| 52 | 96 | if($onboarding && $onboarding->value == 'no'){ |
| @@ -57,12 +101,12 @@ | ||
| 57 | 101 | |
| 58 | 102 | public function taskCreatedOnBoard($task) |
| 59 | 103 | { |
| 60 | 104 | $boardId = $task->board_id; |
| 61 | - $this->updateBoardTaskCount($boardId, 1); | |
| 105 | + $this->updateBoardTaskCount($boardId); | |
| 62 | 106 | $taskStage = $task->stage; |
| 63 | 107 | $settings = ['task_id' => $task->id]; |
| 64 | - $this->createLogActivity($boardId, 'created', 'task', null, $task->title, 'on stage '.$taskStage->title, $settings); | |
| 108 | + $this->createLogActivity($boardId, 'created', 'task', null, $task->title, 'on stage '.$taskStage->title, $settings, $task->created_by); | |
| 65 | 109 | } |
| 66 | 110 | |
| 67 | 111 | public function boardStagesReOrdered($boardId, $oldStageOrders) |
| 68 | 112 | { |
| @@ -78,22 +122,8 @@ | ||
| 78 | 122 | { |
| 79 | 123 | // do something |
| 80 | 124 | } |
| 81 | 125 | |
| 82 | - public function boardDeleted($board) | |
| 83 | - { | |
| 84 | - // do something // commented for better code | |
| 85 | - $taskIdsByBoard = (array) Task::where('board_id', '=', $board->id)->pluck('id')->toArray(); | |
| 86 | - foreach ($taskIdsByBoard as $taskId) { | |
| 87 | - $task = Task::find($taskId); | |
| 88 | - $task->delete(); | |
| 89 | - do_action('fluent_boards/task_deleted', $task); | |
| 90 | - } | |
| 91 | -// TaskActivity::whereIn('task_id', $taskIdsByBoard)->delete(); | |
| 92 | -// TaskAssignee::whereIn('task_id', $taskIdsByBoard)->delete(); | |
| 93 | -// Task::destroy($taskIdsByBoard); | |
| 94 | - } | |
| 95 | - | |
| 96 | 126 | public function boardUpdated($board, $oldBoard) |
| 97 | 127 | { |
| 98 | 128 | $boardId = $board->id; |
| 99 | 129 | $titleChanged = false; |
| @@ -108,19 +138,15 @@ | ||
| 108 | 138 | $this->createLogActivity($boardId, 'changed', 'title', $oldBoard->title, $board->title); |
| 109 | 139 | } |
| 110 | 140 | } |
| 111 | 141 | |
| 112 | - public function boardStageUpdated($boardId, $newStageLabel, $oldStageLabel) | |
| 142 | + public function boardStageUpdated($boardId, $newStage, $oldStage) | |
| 113 | 143 | { |
| 144 | + $newStageLabel = $newStage['title']; | |
| 145 | + $oldStageLabel = $oldStage->title; | |
| 114 | 146 | $this->createLogActivity($boardId, 'updated', 'title of stage', $oldStageLabel, $newStageLabel); |
| 115 | 147 | } |
| 116 | 148 | |
| 117 | - public function boardStageDragged($stage) | |
| 118 | - { | |
| 119 | - $message = sprintf(__('had changed the position of <em><strong>%1s</strong></em> stage.', 'fluent-boards'), $stage->label); | |
| 120 | - $this->createLogActivity($stage->board_id,'moved', 'stage', $stage->label); | |
| 121 | - } | |
| 122 | - | |
| 123 | 149 | public function boardStageAdded($board, $stage) |
| 124 | 150 | { |
| 125 | 151 | $this->createLogActivity($board->id,'created', 'stage', null, $stage->title); |
| 126 | 152 | } |
| @@ -133,10 +159,11 @@ | ||
| 133 | 159 | { |
| 134 | 160 | $this->createLogActivity($board->id,'deleted', 'stage', $stageTobeDeleted); |
| 135 | 161 | } |
| 136 | 162 | |
| 137 | - public function boardStageArchived($boardId, $stageTitle) | |
| 163 | + public function boardStageArchived($boardId, $stage) | |
| 138 | 164 | { |
| 165 | + $stageTitle = $stage->title; | |
| 139 | 166 | $this->createLogActivity($boardId,'archived', 'stage', $stageTitle); |
| 140 | 167 | } |
| 141 | 168 | |
| 142 | 169 | public function boardArchivedStageRestore($boardId, $stageTitle) |
| @@ -143,13 +170,19 @@ | ||
| 143 | 170 | { |
| 144 | 171 | $this->createLogActivity($boardId,'restored', 'stage', $stageTitle); |
| 145 | 172 | } |
| 146 | 173 | |
| 147 | - public function boardMemberAdded($boardId, $memberName) | |
| 174 | + public function boardMemberAdded($boardId, $boardMember) | |
| 148 | 175 | { |
| 149 | - $this->createLogActivity($boardId,'added', 'member', $memberName, null, ''); | |
| 176 | + $this->createLogActivity($boardId,'added', 'member', $boardMember->dispaly_name, null, ''); | |
| 150 | 177 | } |
| 151 | 178 | |
| 179 | + public function boardViewerAdded($boardId, $boardMember) | |
| 180 | + { | |
| 181 | + $this->createLogActivity($boardId,'added', 'viewer', $boardMember->dispaly_name, null, ''); | |
| 182 | + | |
| 183 | + } | |
| 184 | + | |
| 152 | 185 | public function boardMemberRemoved($boardId, $memberName) |
| 153 | 186 | { |
| 154 | 187 | $this->createLogActivity($boardId,'removed', 'member', $memberName, 'from board'); |
| 155 | 188 | } |
| @@ -182,21 +215,47 @@ | ||
| 182 | 215 | $this->createLogActivity($task->board_id, 'deleted', 'task', $task->title); |
| 183 | 216 | } |
| 184 | 217 | } |
| 185 | 218 | |
| 186 | - public function taskarchivedOnBoard($task) | |
| 219 | + public function taskArchivedOnBoard($task) | |
| 187 | 220 | { |
| 188 | 221 | if(!$task->archived_at){ |
| 189 | 222 | $this->createLogActivity($task->board_id, 'restored', 'task', $task->title); |
| 190 | - $this->updateBoardTaskCount($task->board_id, 1); | |
| 223 | + $this->updateBoardTaskCount($task->board_id); | |
| 191 | 224 | }else{ |
| 192 | 225 | $this->createLogActivity($task->board_id, 'archived', 'task', $task->title); |
| 193 | - $this->updateBoardTaskCount($task->board_id, -1); | |
| 226 | + $this->updateBoardTaskCount($task->board_id); | |
| 194 | 227 | } |
| 195 | 228 | } |
| 196 | 229 | |
| 197 | - public function boardLabelActivity($label, $action) | |
| 230 | + /** | |
| 231 | + * Log one board activity for a stage-level bulk task move. | |
| 232 | + * | |
| 233 | + * @param int $boardId | |
| 234 | + * @param Stage $sourceStage | |
| 235 | + * @param Stage $targetStage | |
| 236 | + * @param int $taskCount | |
| 237 | + * @return void | |
| 238 | + */ | |
| 239 | + public function tasksMovedBetweenStages($boardId, $sourceStage, $targetStage, $taskCount) | |
| 198 | 240 | { |
| 241 | + $description = sprintf( | |
| 242 | + _n('%d task moved', '%d tasks moved', $taskCount, 'fluent-boards'), | |
| 243 | + $taskCount | |
| 244 | + ); | |
| 245 | + | |
| 246 | + $this->createLogActivity( | |
| 247 | + $boardId, | |
| 248 | + 'moved', | |
| 249 | + 'tasks', | |
| 250 | + $sourceStage->title, | |
| 251 | + $targetStage->title, | |
| 252 | + $description | |
| 253 | + ); | |
| 254 | + } | |
| 255 | + | |
| 256 | + public function boardLabelCreatedActivity($label) | |
| 257 | + { | |
| 199 | 258 | $column = 'label'; |
| 200 | 259 | $settings = [ |
| 201 | 260 | 'bg_color' => $label->bg_color, |
| 202 | 261 | 'color' => $label->color, |
| @@ -203,9 +262,9 @@ | ||
| 203 | 262 | 'title' => $label->title ?? '' |
| 204 | 263 | ]; |
| 205 | 264 | $this->createLogActivity( |
| 206 | 265 | $label->board_id, |
| 207 | - $action, | |
| 266 | + 'created', | |
| 208 | 267 | $column, |
| 209 | 268 | null, |
| 210 | 269 | null, |
| 211 | 270 | null, |
| @@ -212,8 +271,60 @@ | ||
| 212 | 271 | $settings |
| 213 | 272 | ); |
| 214 | 273 | } |
| 215 | 274 | |
| 275 | + public function boardLabelUpdatedActivity($label) | |
| 276 | + { | |
| 277 | + $column = 'label'; | |
| 278 | + $settings = [ | |
| 279 | + 'bg_color' => $label->bg_color, | |
| 280 | + 'color' => $label->color, | |
| 281 | + 'title' => $label->title ?? '' | |
| 282 | + ]; | |
| 283 | + $this->createLogActivity( | |
| 284 | + $label->board_id, | |
| 285 | + 'updated', | |
| 286 | + $column, | |
| 287 | + null, | |
| 288 | + null, | |
| 289 | + null, | |
| 290 | + $settings | |
| 291 | + ); | |
| 292 | + } | |
| 293 | + | |
| 294 | + public function boardLabelDeletedActivity($label) | |
| 295 | + { | |
| 296 | + $column = 'label'; | |
| 297 | + $settings = [ | |
| 298 | + 'bg_color' => $label->bg_color, | |
| 299 | + 'color' => $label->color, | |
| 300 | + 'title' => $label->title ?? '' | |
| 301 | + ]; | |
| 302 | + $this->createLogActivity( | |
| 303 | + $label->board_id, | |
| 304 | + 'deleted', | |
| 305 | + $column, | |
| 306 | + null, | |
| 307 | + null, | |
| 308 | + null, | |
| 309 | + $settings | |
| 310 | + ); | |
| 311 | + } | |
| 312 | + | |
| 313 | + public function defaultAssigneesUpdated($stage, $assignees) | |
| 314 | + { | |
| 315 | + $column = 'default assignees of stage'; | |
| 316 | + | |
| 317 | + $this->createLogActivity( | |
| 318 | + $stage->board_id, | |
| 319 | + 'updated', | |
| 320 | + $column, | |
| 321 | + null, | |
| 322 | + null, | |
| 323 | + $stage->title | |
| 324 | + ); | |
| 325 | + } | |
| 326 | + | |
| 216 | 327 | public static function getCurrencies() |
| 217 | 328 | { |
| 218 | 329 | return [ |
| 219 | 330 | 'AED' => 'United Arab Emirates Dirham', |
| @@ -355,11 +466,11 @@ | ||
| 355 | 466 | } |
| 356 | 467 | public function taskMovedFromBoard($task, $oldBoard, $newBoard) |
| 357 | 468 | { |
| 358 | 469 | // add tasks_count in the board |
| 359 | - $this->updateBoardTaskCount($newBoard->id, 1); | |
| 470 | + $this->updateBoardTaskCount($newBoard->id); | |
| 360 | 471 | // subtract tasks_count in the board |
| 361 | - $this->updateBoardTaskCount($oldBoard->id, -1); | |
| 472 | + $this->updateBoardTaskCount($oldBoard->id); | |
| 362 | 473 | |
| 363 | 474 | $this->createLogActivity($oldBoard->id, 'moved', 'task', $task->title . ' to ' . $newBoard->title); |
| 364 | 475 | $this->createLogActivity($newBoard->id, 'added', 'task', $task->title . ' from ' . $oldBoard->title); |
| 365 | 476 | } |
| @@ -367,9 +478,9 @@ | ||
| 367 | 478 | public function deleteUserRelatedData($id, $reassign, $user) |
| 368 | 479 | { |
| 369 | 480 | try{ |
| 370 | 481 | if(!$id) { |
| 371 | - throw new \Exception('Member Not deleted'); | |
| 482 | + throw new \Exception(esc_html__('Member Not deleted', 'fluent-boards')); | |
| 372 | 483 | } |
| 373 | 484 | Meta::where('object_type', Constant::OBJECT_TYPE_USER) |
| 374 | 485 | ->where('object_id', $id) |
| 375 | 486 | ->delete(); |
| @@ -380,16 +491,49 @@ | ||
| 380 | 491 | Constant::OBJECT_TYPE_USER_TASK_WATCH, |
| 381 | 492 | Constant::TASK_ASSIGNEE |
| 382 | 493 | ])->delete(); |
| 383 | 494 | }catch (\Exception $e){} |
| 384 | - | |
| 385 | 495 | } |
| 386 | - private function updateBoardTaskCount($boardId, $count) | |
| 496 | + public function updateBoardTaskCount($boardId) | |
| 387 | 497 | { |
| 388 | 498 | $board = Board::find($boardId); |
| 389 | 499 | $settings = $board->settings ?? []; |
| 390 | - | |
| 391 | - $settings['tasks_count'] = $board->tasks->where('parent_id', null)->count(); | |
| 500 | + $stageIds = Stage::where('board_id', $boardId)->whereNull('archived_at')->pluck('id')->toArray(); | |
| 501 | + $settings['tasks_count'] = $board->tasks->where('parent_id', null)->whereNull('archived_at')->whereIn('stage_id', $stageIds)->count(); | |
| 392 | 502 | $board->settings = $settings; |
| 393 | 503 | $board->save(); |
| 394 | 504 | } |
| 505 | + | |
| 506 | + /** | |
| 507 | + * Delete a board-owned previous background attachment and log the change. | |
| 508 | + * | |
| 509 | + * @param int $boardId | |
| 510 | + * @param array|string $oldBackground | |
| 511 | + * @return void | |
| 512 | + */ | |
| 513 | + public function backgroundUpdated($boardId, $oldBackground) | |
| 514 | + { | |
| 515 | + if ( | |
| 516 | + is_array($oldBackground) && | |
| 517 | + !empty($oldBackground['is_image']) && | |
| 518 | + !empty($oldBackground['image_url']) | |
| 519 | + ) { | |
| 520 | + // Only the target board's background attachment may be removed by this hook. | |
| 521 | + if (!empty($oldBackground['id'])) { | |
| 522 | + $attachment = Attachment::where('id', absint($oldBackground['id'])) | |
| 523 | + ->where('object_id', absint($boardId)) | |
| 524 | + ->where('object_type', Constant::BOARD_BACKGROUND_IMAGE) | |
| 525 | + ->first(); | |
| 526 | + | |
| 527 | + if ($attachment) { | |
| 528 | + $deletedAttachment = clone $attachment; | |
| 529 | + $attachment->delete(); | |
| 530 | + (new FileHandler())->deleteAttachmentFile($deletedAttachment, absint($boardId)); | |
| 531 | + } | |
| 532 | + } | |
| 533 | + } | |
| 534 | + | |
| 535 | + $this->createLogActivity($boardId, 'changed', 'board background', null, null); | |
| 536 | + } | |
| 537 | + | |
| 538 | + | |
| 395 | 539 | } |