PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.20
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.20
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 / Api / Classes / Tasks.php

Tasks.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.20, at app/Api/Classes/Tasks.php

247 lines 7.1 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\Api\Classes;
4
5 defined('ABSPATH') || exit;
6
7 use FluentBoards\App\Models\Label;
8 use FluentBoards\App\Models\Task;
9 use FluentBoards\App\Services\BoardService;
10 use FluentBoards\App\Services\Helper;
11 use FluentBoards\App\Services\PermissionManager;
12 use FluentCrm\App\Models\Subscriber;
13
14
15 /**
16 * Contacts Class - PHP APi Wrapper
17 *
18 * Contacts API Wrapper Class that can be used as <code>FluentBoardsApi('tasks')</code> to get the class instance
19 *
20 * @package FluentBoards\App\Api\Classes
21 * @namespace FluentBoards\App\Api\Classes
22 *
23 * @version 1.0.0
24 */
25 class Tasks
26 {
27 private $instance = null;
28
29 private $allowedInstanceMethods = [
30 'all',
31 'get',
32 'find',
33 'first',
34 'paginate'
35 ];
36
37 public function __construct(Task $instance)
38 {
39 $this->instance = $instance;
40 }
41
42 /**
43 * Get Tasks by board
44 *
45 * Use:
46 * <code>FluentBoardsApi('tasks')->getTasksByBoard();</code>
47 *
48 * @param string|int $board_id
49 * @param array $with
50 * @return array|Task Model
51 */
52 public function getTasksByBoard($board_id, $with = [])
53 {
54 if (!$board_id) {
55 return [];
56 }
57
58 $query = Task::query()
59 ->where('board_id', $board_id)
60 ->whereNull('parent_id')
61 ->whereNull('archived_at')
62 ->orderBy('due_at', 'ASC');
63
64 if (!empty($with)) {
65 $query->with($with);
66 }
67
68 $tasks = $query->get();
69
70 $tasks->transform(function ($task) {
71 $task->isOverdue = $task->isOverdue();
72 $task->isUpcoming = $task->upcoming();
73 $task->contact = Helper::crm_contact($task->crm_contact_id);
74 $task->is_watching = $task->isWatching();
75 return $task;
76 });
77
78 return $tasks;
79 }
80
81
82 /**
83 * Get Task by id
84 *
85 * Use:
86 * <code>FluentBoardsApi('tasks')->getTask($id);</code>
87 *
88 * @param int|string $id Task id
89 * @return false|Task Model
90 */
91 public function getTask($id)
92 {
93 if (!empty($id)) {
94 $task = Task::where('id', $id)->first();
95
96 //checking if current user has access to board
97 if (!PermissionManager::userHasPermission($task->board_id)) {
98 return false;
99 }
100 return $task;
101 }
102 return false;
103 }
104
105
106 /**
107 * Get Task Created by
108 *
109 * Use:
110 * <code>FluentBoardsApi('tasks')->getTasksCreatedBy($userId);</code>
111 *
112 * @param int|string $userId Task id
113 * @param array $with
114 * @return false|Task Model
115 */
116 public function getTasksCreatedBy($userId, $with = [])
117 {
118 if (!empty($userId)) {
119 $query = Task::where('created_by', $userId);
120
121 if (!empty($with)) {
122 $query->with($with);
123 }
124
125 $tasks = $query->get();
126
127 //checking if current user has access to board
128 if (!PermissionManager::userHasPermission($tasks->board_id)) {
129 return false;
130 }
131
132 return $tasks;
133 }
134 return false;
135 }
136
137
138 public function create($data)
139 {
140 if (empty($data)) {
141 return false;
142 }
143
144 if (empty($data['title']) || empty($data['board_id']) || empty($data['stage_id'])) {
145 return false;
146 }
147
148 $taskData = Helper::sanitizeTask($data);
149
150 if(!empty($taskData['priority']) && !in_array($taskData['priority'], ['low', 'medium', 'high'])) {
151 $taskData['priority'] = 'low';
152 }
153 if(!empty($taskData['status']) && !in_array($taskData['status'], ['open', 'closed'])) {
154 $taskData['status'] = 'open';
155 }
156 if(!empty($data['crm_contact_id'])) {
157 $taskData['crm_contact_id'] = $data['crm_contact_id'];
158 }
159
160 if(!empty($data['contact_email']) && empty($data['crm_contact_id'])) {
161 // Find first if the contact exists
162 $contact = FluentCrmApi('contacts')->creteOrUpdate([
163 'email' => $data['contact_email'],
164 'first_name' => $data['contact_first_name'],
165 'last_name' => $data['contact_last_name'],
166 'status' => 'subscribed'
167 ]);
168 $taskData['crm_contact_id'] = $contact->id;
169 }
170
171 if(!empty($data['assignees']) && is_array($data['assignees'])) {
172 // check if the assignees are valid, they have to be wp user ids
173 $users = get_users(['include' => $data['assignees']]);
174 $taskData['assignees'] = array_map(function($user) {
175 return $user->ID;
176 }, $users);
177 // after successful task creation we have to add them as board members
178 }
179
180 if(!empty($data['labels']) && is_array($data['labels'])) {
181 $labelIds = [];
182 // check if the labels are valid, they have to be label ids
183 foreach ($data['labels'] as $label) {
184 if(is_numeric($label)) {
185 $labelModel = Label::where('id', $label)->where('board_id', $data['board_id'])->first();
186 if($labelModel) {
187 $labelId = $labelModel->id;
188 $labelIds[] = $labelId;
189 }
190 continue;
191 }
192 if(is_string($label)) {
193 $labelModel = Label::where('board_id', $data['board_id'])
194 ->where(function($query) use ($label) {
195 $query->where('title', $label)
196 ->orWhere('slug', $label);
197 })->first();
198 if($labelModel) {
199 $labelId = $labelModel->id;
200 $labelIds[] = $labelId;
201 }
202 }
203 }
204 $taskData['labels'] = $labelIds;
205 // we have to map the labels after task creation
206 }
207
208 $task = $this->instance->createTask($taskData);
209 // push assignees to board Members
210 $boardService = new BoardService();
211 if(!empty($taskData['assignees'])) {
212 foreach ($taskData['assignees'] as $assigneeId) {
213 $boardService->addMembersInBoard($data['board_id'], $assigneeId);
214 }
215 }
216
217 return $task;
218 }
219
220
221 public function createTask($data = [])
222 {
223 $defaultData = [
224 'title',
225 'board_id',
226 'stage_id',
227 'parent_id',
228 'status', // open | closed
229 'priority', // low | medium | high
230 'source',
231 'source_id',
232 'description',
233 'due_at',
234 'crm_contact_id',
235 // <or>
236 'contact_email',
237 'contact_first_name', // this is the full name
238 'contact_last_name', // this is the full name
239 'contact_status', // default subscribed
240 // </or>
241 'assignees', // WP User IDs / WP User Emails as an array
242 'labels', // array of label ids or titles [1, 'New']
243 ];
244
245 }
246 }
247