PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 2.0.12
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v2.0.12
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 / TaskHandler.php

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

243 lines 7.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 DateTimeImmutable;
6 use Exception;
7 use FluentBoards\App\Models\Activity;
8 use FluentBoards\App\Models\Meta;
9 use FluentBoards\App\Models\TaskMeta;
10 use FluentBoards\App\Models\Task;
11 use FluentBoards\App\Models\User;
12 use FluentBoards\App\Services\Constant;
13 use FluentBoards\App\Services\NotificationService;
14 use FluentCrm\App\Models\Subscriber;
15 use FluentBoards\App\Models\Relation;
16 use FluentCrm\App\Services\ContactsQuery;
17 use FluentCrm\App\Services\PermissionManager as CRMPermissionManager;
18
19 class TaskHandler
20 {
21 private $fileHandler;
22
23 public function __construct(FileHandler $fileHandler)
24 {
25 $this->fileHandler = $fileHandler;
26 }
27 public function taskDeleted($task)
28 {
29 // Delete all activities and comments related to this task
30 Activity::where('object_id', $task->id)->where('object_type', Constant::ACTIVITY_TASK)->delete();
31 Meta::where('object_id', $task->id)->where('object_type', Constant::REPEAT_TASK_META)->delete();
32 }
33
34 public function searchAssignees($options, $search, $includedIds)
35 {
36 $users = get_users([
37 'number' => 20,
38 'search' => $search,
39 ]);
40
41 $formattedUsers = [];
42
43 $pushedIds = [];
44
45 foreach ($users as $user) {
46 $pushedIds[] = $user->ID;
47
48 $formattedUsers[] = [
49 'id' => $user->ID,
50 'title' => $user->display_name . ' (' . $user->user_email . ')',
51 'photo' => get_avatar_url($user->user_email),
52 'left_side_value' => $user->display_name,
53 'right_side_value' => $user->user_email,
54 ];
55 }
56
57 if (!$includedIds) {
58 return $formattedUsers;
59 }
60
61 if (!is_array($includedIds)) {
62 $includedIds = [$includedIds];
63 }
64
65 $includedIds = array_diff(array_map('intval', $includedIds), $pushedIds);
66
67 if ($includedIds) {
68 $users = get_users([
69 'include' => $includedIds,
70 ]);
71
72 foreach ($users as $user) {
73 $formattedUsers[] = [
74 'id' => $user->ID,
75 'title' => $user->display_name . ' (' . $user->user_email . ')',
76 'photo' => get_avatar_url($user->user_email),
77 'left_side_value' => $user->display_name,
78 'right_side_value' => $user->user_email,
79 ];
80 }
81 }
82
83 return $formattedUsers;
84 }
85
86 public function searchNonBoardWordpressUsers()
87 {
88 $superAdmin = Relation::select('user_id')->distinct()->pluck('user_id');
89 $users = User::whereDoesntHave('boards')->whereNotIn('ID', $superAdmin)->get();
90
91 $formattedUsers = [];
92
93 foreach ($users as $user) {
94 $formattedUsers[] = [
95 'id' => $user->ID,
96 'photo' => $user->photo,
97 'title' => $user->display_name . ' (' . $user->user_email . ')',
98 'left_side_value' => $user->display_name,
99 'right_side_value' => $user->user_email,
100 ];
101 }
102
103 return $formattedUsers;
104 }
105
106 public function searchContact($options, $search, $includedIds)
107 {
108 if (!defined('FLUENTCRM')) {
109 return [];
110 }
111
112 // check if the use has permission to access contacts
113 $contactPermission = CRMPermissionManager::currentUserCan('fcrm_read_contacts');
114 if(!$contactPermission) {
115 return [];
116 }
117
118 $contactsQuery = new ContactsQuery([
119 'search' => $search,
120 'with' => [],
121 'limit' => 20,
122 'offset' => 0,
123 ]);
124 $subscribers = $contactsQuery->get();
125
126 $formattedSubscribers = [];
127
128 $pushedIs = [];
129 foreach ($subscribers as $subscriber) {
130 $pushedIs[] = $subscriber->id;
131 $formattedSubscribers[] = [
132 'id' => $subscriber->id,
133 'title' => $subscriber->full_name . ' (' . $subscriber->email . ')',
134 'photo' => $subscriber->photo,
135 'name' => $subscriber->full_name,
136 'email' => $subscriber->email,
137 ];
138 }
139
140 if (!is_array($includedIds)) {
141 $includedIds = [$includedIds];
142 }
143
144 if (!$includedIds) {
145 return $formattedSubscribers;
146 }
147
148 $remainingIds = array_diff($includedIds, $pushedIs);
149
150 if ($remainingIds) {
151 $remainingContacts = Subscriber::whereIn('id', $remainingIds)->get();
152 foreach ($remainingContacts as $subscriber) {
153 $formattedSubscribers[] = [
154 'id' => $subscriber->id,
155 'title' => $subscriber->full_name . ' (' . $subscriber->email . ')',
156 'photo' => $subscriber->photo,
157 'name' => $subscriber->full_name,
158 'email' => $subscriber->email,
159 ];
160 }
161 }
162
163 return $formattedSubscribers;
164 }
165
166
167
168 /**
169 * Summary of taskAttachmentDeleted
170 * @param mixed $task
171 * @param mixed $deleteUrl
172 * @return void
173 */
174 public function taskAttachmentDeleted($deletedAttachment)
175 {
176 try {
177 $deleteUrl = $deletedAttachment->full_url;
178 $this->fileHandler->deleteFileByUrl($deleteUrl);
179 } catch (Exception $e) {
180 wp_send_json_error($e->getMessage());
181 }
182 }
183
184 /**
185 * Auto-watch newly created tasks when the board preference allows it.
186 */
187 public function onTaskCreated($task)
188 {
189 $userId = get_current_user_id();
190
191 if (!$userId) {
192 return;
193 }
194
195 if ($this->shouldAutoWatchForBoard($task->board_id, Constant::GLOBAL_EMAIL_NOTIFICATION_CREATING_TASK)) {
196 $task->watchers()->syncWithoutDetaching([$userId => ['object_type' => Constant::OBJECT_TYPE_USER_TASK_WATCH]]);
197 }
198 }
199
200 /**
201 * Auto-watch commented tasks when the board preference allows it.
202 */
203 public function onCommentCreated($comment)
204 {
205 $task = $comment->task;
206 $userId = get_current_user_id();
207
208 if (!$userId) {
209 return;
210 }
211
212 if ($this->shouldAutoWatchForBoard($task->board_id, Constant::GLOBAL_EMAIL_NOTIFICATION_COMMENTING)) {
213 $task->watchers()->syncWithoutDetaching([$userId => ['object_type' => Constant::OBJECT_TYPE_USER_TASK_WATCH]]);
214 }
215 }
216
217 /**
218 * Auto-watch assigned tasks when the board preference allows it.
219 */
220 public function onAssignAnotherUser($task, $assigneeId)
221 {
222 if ($this->shouldAutoWatchForBoard($task->board_id, Constant::GLOBAL_EMAIL_NOTIFICATION_ASSIGNING)) {
223 $task->watchers()->syncWithoutDetaching([$assigneeId => ['object_type' => Constant::OBJECT_TYPE_USER_TASK_WATCH]]);
224 }
225 }
226
227 /**
228 * Determine auto-watch behavior from the current user's board preference.
229 */
230 private function shouldAutoWatchForBoard($boardId, $preferenceKey)
231 {
232 return (new NotificationService())->isBoardAutoWatchEnabled($boardId, get_current_user_id(), $preferenceKey);
233 }
234
235 public function taskCloned($originalTask, $clonedTask)
236 {
237 Activity::where('object_id', $clonedTask->id)
238 ->where('object_type', Constant::ACTIVITY_TASK)
239 ->delete();
240 do_action('fluent_boards/task_cloned_activity', $originalTask, $clonedTask);
241 }
242 }
243