PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 2.0.15
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v2.0.15
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / app / Hooks / Handlers / BoardHandler.php

BoardHandler.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 2.0.15, at app/Hooks/Handlers/BoardHandler.php

540 lines 17.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\App\Hooks\Handlers;
4
5 use FluentBoards\App\Models\Attachment;
6 use FluentBoards\App\Models\Comment;
7 use FluentBoards\App\Models\Meta;
8 use FluentBoards\App\Models\NotificationUser;
9 use FluentBoards\App\Models\Relation;
10 use FluentBoards\App\Models\Stage;
11 use FluentBoards\App\Models\Task;
12 use FluentBoards\App\Models\Board;
13 use FluentBoards\App\Models\User;
14 use FluentBoards\App\Services\Constant;
15 use FluentBoards\App\Services\BoardService;
16 use FluentBoards\App\Services\Helper;
17
18 class BoardHandler
19 {
20 public function createLogActivity($boardId, $action, $column = null, $oldValue = null, $newValue = null, $description = null, $settings = null, $userId = null )
21 {
22 $data = [
23 'object_type' => Constant::ACTIVITY_BOARD,
24 'object_id' => $boardId,
25 'action' => $action, // action type: changed, updated, added, removed, created
26 'column' => $column,
27 'old_value' => $oldValue,
28 'new_value' => $newValue,
29 'description' => $description,
30 'settings' => $settings
31 ];
32 if($userId) {
33 $data['created_by'] = $userId;
34 }
35
36 Helper::createActivity($data);
37 }
38 public function getAllBoards($sortBy = 'ASC')
39 {
40 $boards = Board::orderBy('created_at', $sortBy)->get();
41
42 return [
43 'boards' => $boards,
44 ];
45 }
46
47 public function attachFolderToBoard($board)
48 {
49 $board->folder = (new BoardService())->getBoardFolder($board->id);
50
51 return $board;
52 }
53
54 public function boardCreated($board)
55 {
56 $boardId = $board->id;
57 $this->createLogActivity($boardId, 'created', 'board', null, null, $board->title);
58 $this->updateOnboarding();
59 }
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
93 private function updateOnboarding()
94 {
95 $onboarding = Meta::where('key', Constant::FBS_ONBOARDING)->first();
96 if($onboarding && $onboarding->value == 'no'){
97 $onboarding->value = 'yes' ;
98 $onboarding->save();
99 }
100 }
101
102 public function taskCreatedOnBoard($task)
103 {
104 $boardId = $task->board_id;
105 $this->updateBoardTaskCount($boardId);
106 $taskStage = $task->stage;
107 $settings = ['task_id' => $task->id];
108 $this->createLogActivity($boardId, 'created', 'task', null, $task->title, 'on stage '.$taskStage->title, $settings, $task->created_by);
109 }
110
111 public function boardStagesReOrdered($boardId, $oldStageOrders)
112 {
113 $stages = [];
114 foreach ($oldStageOrders as $oldStageId){
115 $stage = Stage::findOrFail($oldStageId);
116 $stages[] = $stage->title;
117 }
118 $this->createLogActivity($boardId,'moved', 'stages', implode(',', $stages));
119 }
120
121 public function beforeBoardDeleted($board, $options = [])
122 {
123 // do something
124 }
125
126 public function boardUpdated($board, $oldBoard)
127 {
128 $boardId = $board->id;
129 $titleChanged = false;
130 $descriptionChanged = ($board->description != $oldBoard->description) ? true : false;
131 if ($board->title != $oldBoard->title) {
132 $titleChanged = true;
133 }
134 if ($descriptionChanged) {
135 $this->createLogActivity($boardId, 'changed', 'description',null , $oldBoard->description, $board->description);
136 }
137 if ($titleChanged) {
138 $this->createLogActivity($boardId, 'changed', 'title', $oldBoard->title, $board->title);
139 }
140 }
141
142 public function boardStageUpdated($boardId, $newStage, $oldStage)
143 {
144 $newStageLabel = $newStage['title'];
145 $oldStageLabel = $oldStage->title;
146 $this->createLogActivity($boardId, 'updated', 'title of stage', $oldStageLabel, $newStageLabel);
147 }
148
149 public function boardStageAdded($board, $stage)
150 {
151 $this->createLogActivity($board->id,'created', 'stage', null, $stage->title);
152 }
153 public function boardStageMoved($stage, $direction)
154 {
155 $this->createLogActivity($stage->board_id,'moved', $stage->title.' stage', $direction);
156 }
157
158 public function boardStageDeleted($board, $stageTobeDeleted)
159 {
160 $this->createLogActivity($board->id,'deleted', 'stage', $stageTobeDeleted);
161 }
162
163 public function boardStageArchived($boardId, $stage)
164 {
165 $stageTitle = $stage->title;
166 $this->createLogActivity($boardId,'archived', 'stage', $stageTitle);
167 }
168
169 public function boardArchivedStageRestore($boardId, $stageTitle)
170 {
171 $this->createLogActivity($boardId,'restored', 'stage', $stageTitle);
172 }
173
174 public function boardMemberAdded($boardId, $boardMember)
175 {
176 $this->createLogActivity($boardId,'added', 'member', $boardMember->dispaly_name, null, '');
177 }
178
179 public function boardViewerAdded($boardId, $boardMember)
180 {
181 $this->createLogActivity($boardId,'added', 'viewer', $boardMember->dispaly_name, null, '');
182
183 }
184
185 public function boardMemberRemoved($boardId, $memberName)
186 {
187 $this->createLogActivity($boardId,'removed', 'member', $memberName, 'from board');
188 }
189
190 public function boardAdminRemoved($boardId, $memberId)
191 {
192 $boardMember = $this->getMember($memberId);
193 $this->createLogActivity($boardId,'demoted', $boardMember->display_name, 'Manager', 'Member');
194 }
195
196 public function boardAdminAdded($boardId, $memberId)
197 {
198 $boardMember = $this->getMember($memberId);
199 $this->createLogActivity($boardId,'promoted', $boardMember->display_name, 'Member','Manager');
200 }
201
202 private function getMember($memberId)
203 {
204 return User::find($memberId);
205 }
206
207 public function beforeTaskDeleted($task, $options = [])
208 {
209 //do something
210 }
211
212 public function taskdeleted($task)
213 {
214 if (!$task->parent_id) {
215 $this->createLogActivity($task->board_id, 'deleted', 'task', $task->title);
216 }
217 }
218
219 public function taskArchivedOnBoard($task)
220 {
221 if(!$task->archived_at){
222 $this->createLogActivity($task->board_id, 'restored', 'task', $task->title);
223 $this->updateBoardTaskCount($task->board_id);
224 }else{
225 $this->createLogActivity($task->board_id, 'archived', 'task', $task->title);
226 $this->updateBoardTaskCount($task->board_id);
227 }
228 }
229
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)
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 {
258 $column = 'label';
259 $settings = [
260 'bg_color' => $label->bg_color,
261 'color' => $label->color,
262 'title' => $label->title ?? ''
263 ];
264 $this->createLogActivity(
265 $label->board_id,
266 'created',
267 $column,
268 null,
269 null,
270 null,
271 $settings
272 );
273 }
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
327 public static function getCurrencies()
328 {
329 return [
330 'AED' => 'United Arab Emirates Dirham',
331 'AFN' => 'Afghan Afghani',
332 'ALL' => 'Albanian Lek',
333 'AMD' => 'Armenian Dram',
334 'ANG' => 'Netherlands Antillean Gulden',
335 'AOA' => 'Angolan Kwanza',
336 'ARS' => 'Argentine Peso', // non amex
337 'AUD' => 'Australian Dollar',
338 'AWG' => 'Aruban Florin',
339 'AZN' => 'Azerbaijani Manat',
340 'BAM' => 'Bosnia & Herzegovina Convertible Mark',
341 'BBD' => 'Barbadian Dollar',
342 'BDT' => 'Bangladeshi Taka',
343 'BIF' => 'Burundian Franc',
344 'BGN' => 'Bulgarian Lev',
345 'BMD' => 'Bermudian Dollar',
346 'BND' => 'Brunei Dollar',
347 'BOB' => 'Bolivian Boliviano',
348 'BRL' => 'Brazilian Real',
349 'BSD' => 'Bahamian Dollar',
350 'BWP' => 'Botswana Pula',
351 'BZD' => 'Belize Dollar',
352 'CAD' => 'Canadian Dollar',
353 'CDF' => 'Congolese Franc',
354 'CHF' => 'Swiss Franc',
355 'CLP' => 'Chilean Peso',
356 'CNY' => 'Chinese Renminbi Yuan',
357 'COP' => 'Colombian Peso',
358 'CRC' => 'Costa Rican Colón',
359 'CVE' => 'Cape Verdean Escudo',
360 'CZK' => 'Czech Koruna',
361 'DJF' => 'Djiboutian Franc',
362 'DKK' => 'Danish Krone',
363 'DOP' => 'Dominican Peso',
364 'DZD' => 'Algerian Dinar',
365 'EGP' => 'Egyptian Pound',
366 'ETB' => 'Ethiopian Birr',
367 'EUR' => 'Euro',
368 'FJD' => 'Fijian Dollar',
369 'FKP' => 'Falkland Islands Pound',
370 'GBP' => 'British Pound',
371 'GEL' => 'Georgian Lari',
372 'GIP' => 'Gibraltar Pound',
373 'GMD' => 'Gambian Dalasi',
374 'GNF' => 'Guinean Franc',
375 'GTQ' => 'Guatemalan Quetzal',
376 'GYD' => 'Guyanese Dollar',
377 'HKD' => 'Hong Kong Dollar',
378 'HNL' => 'Honduran Lempira',
379 'HRK' => 'Croatian Kuna',
380 'HTG' => 'Haitian Gourde',
381 'HUF' => 'Hungarian Forint',
382 'IDR' => 'Indonesian Rupiah',
383 'ILS' => 'Israeli New Sheqel',
384 'INR' => 'Indian Rupee',
385 'ISK' => 'Icelandic Króna',
386 'JMD' => 'Jamaican Dollar',
387 'JPY' => 'Japanese Yen',
388 'KES' => 'Kenyan Shilling',
389 'KGS' => 'Kyrgyzstani Som',
390 'KHR' => 'Cambodian Riel',
391 'KMF' => 'Comorian Franc',
392 'KRW' => 'South Korean Won',
393 'KYD' => 'Cayman Islands Dollar',
394 'KZT' => 'Kazakhstani Tenge',
395 'LAK' => 'Lao Kip',
396 'LBP' => 'Lebanese Pound',
397 'LKR' => 'Sri Lankan Rupee',
398 'LRD' => 'Liberian Dollar',
399 'LSL' => 'Lesotho Loti',
400 'MAD' => 'Moroccan Dirham',
401 'MDL' => 'Moldovan Leu',
402 'MGA' => 'Malagasy Ariary',
403 'MKD' => 'Macedonian Denar',
404 'MNT' => 'Mongolian Tögrög',
405 'MOP' => 'Macanese Pataca',
406 'MRO' => 'Mauritanian Ouguiya',
407 'MUR' => 'Mauritian Rupee',
408 'MVR' => 'Maldivian Rufiyaa',
409 'MWK' => 'Malawian Kwacha',
410 'MXN' => 'Mexican Peso',
411 'MYR' => 'Malaysian Ringgit',
412 'MZN' => 'Mozambican Metical',
413 'NAD' => 'Namibian Dollar',
414 'NGN' => 'Nigerian Naira',
415 'NIO' => 'Nicaraguan Córdoba',
416 'NOK' => 'Norwegian Krone',
417 'NPR' => 'Nepalese Rupee',
418 'NZD' => 'New Zealand Dollar',
419 'PAB' => 'Panamanian Balboa',
420 'PEN' => 'Peruvian Nuevo Sol',
421 'PGK' => 'Papua New Guinean Kina',
422 'PHP' => 'Philippine Peso',
423 'PKR' => 'Pakistani Rupee',
424 'PLN' => 'Polish Złoty',
425 'PYG' => 'Paraguayan Guaraní',
426 'QAR' => 'Qatari Riyal',
427 'RON' => 'Romanian Leu',
428 'RSD' => 'Serbian Dinar',
429 'RUB' => 'Russian Ruble',
430 'RWF' => 'Rwandan Franc',
431 'SAR' => 'Saudi Riyal',
432 'SBD' => 'Solomon Islands Dollar',
433 'SCR' => 'Seychellois Rupee',
434 'SEK' => 'Swedish Krona',
435 'SGD' => 'Singapore Dollar',
436 'SHP' => 'Saint Helenian Pound',
437 'SLL' => 'Sierra Leonean Leone',
438 'SOS' => 'Somali Shilling',
439 'SRD' => 'Surinamese Dollar',
440 'STD' => 'São Tomé and Príncipe Dobra',
441 'SVC' => 'Salvadoran Colón',
442 'SZL' => 'Swazi Lilangeni',
443 'THB' => 'Thai Baht',
444 'TJS' => 'Tajikistani Somoni',
445 'TOP' => 'Tongan Paʻanga',
446 'TRY' => 'Turkish Lira',
447 'TTD' => 'Trinidad and Tobago Dollar',
448 'TWD' => 'New Taiwan Dollar',
449 'TZS' => 'Tanzanian Shilling',
450 'UAH' => 'Ukrainian Hryvnia',
451 'UGX' => 'Ugandan Shilling',
452 'USD' => 'United States Dollar',
453 'UYU' => 'Uruguayan Peso',
454 'UZS' => 'Uzbekistani Som',
455 'VND' => 'Vietnamese Đồng',
456 'VUV' => 'Vanuatu Vatu',
457 'WST' => 'Samoan Tala',
458 'XAF' => 'Central African Cfa Franc',
459 'XCD' => 'East Caribbean Dollar',
460 'XOF' => 'West African Cfa Franc',
461 'XPF' => 'Cfp Franc',
462 'YER' => 'Yemeni Rial',
463 'ZAR' => 'South African Rand',
464 'ZMW' => 'Zambian Kwacha',
465 ];
466 }
467 public function taskMovedFromBoard($task, $oldBoard, $newBoard)
468 {
469 // add tasks_count in the board
470 $this->updateBoardTaskCount($newBoard->id);
471 // subtract tasks_count in the board
472 $this->updateBoardTaskCount($oldBoard->id);
473
474 $this->createLogActivity($oldBoard->id, 'moved', 'task', $task->title . ' to ' . $newBoard->title);
475 $this->createLogActivity($newBoard->id, 'added', 'task', $task->title . ' from ' . $oldBoard->title);
476 }
477
478 public function deleteUserRelatedData($id, $reassign, $user)
479 {
480 try{
481 if(!$id) {
482 throw new \Exception(esc_html__('Member Not deleted', 'fluent-boards'));
483 }
484 Meta::where('object_type', Constant::OBJECT_TYPE_USER)
485 ->where('object_id', $id)
486 ->delete();
487 NotificationUser::where('user_id', $id)->delete();
488 Relation::where('foreign_id', $id)
489 ->whereIn('object_type', [
490 Constant::OBJECT_TYPE_BOARD_USER,
491 Constant::OBJECT_TYPE_USER_TASK_WATCH,
492 Constant::TASK_ASSIGNEE
493 ])->delete();
494 }catch (\Exception $e){}
495 }
496 public function updateBoardTaskCount($boardId)
497 {
498 $board = Board::find($boardId);
499 $settings = $board->settings ?? [];
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();
502 $board->settings = $settings;
503 $board->save();
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
539 }
540