PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.23
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.23
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 1.45 All 41 releases
fluent-boards / app / Services / Helper.php

Helper.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.23, at app/Services/Helper.php

444 lines 12.5 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\Services;
4
5 use FluentBoards\App\Models\Activity;
6 use FluentBoards\App\Models\Stage;
7 use FluentBoards\App\Models\Webhook;
8 use FluentBoards\Framework\Support\Arr;
9 use FluentBoards\Framework\Support\Str;
10 use FluentBoards\App\Models\Board;
11 use FluentBoards\App\Services\Parsedown;
12 use FluentBoards\App\Services\PermissionManager;
13
14 class Helper
15 {
16 public static function snake_case($string)
17 {
18 return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $string));
19 }
20
21 public static function slugify($text, $id = '', $length = 20)
22 {
23 $text = substr($text, 0, $length); // limiting to max 20 chars
24 $text = Str::slug($text);
25 if ($id) {
26 $text = $id . '-' . $text;
27 }
28
29 return $text;
30 }
31
32 public static function loadView($template, $data)
33 {
34 extract($data, EXTR_OVERWRITE);
35
36 $template = sanitize_file_name($template);
37
38 $template = str_replace('.', DIRECTORY_SEPARATOR, $template);
39
40 ob_start();
41 include FLUENT_BOARDS_PLUGIN_PATH . 'app/Views/' . $template . '.php';
42
43 return ob_get_clean();
44 }
45
46 private static function sanitizeData($data, $fieldMaps)
47 {
48 foreach ($data as $key => $value) {
49 if ($value && isset($fieldMaps[$key]) && !is_array($value)) {
50 $data[$key] = call_user_func($fieldMaps[$key], $value);
51 }
52 }
53
54 return $data;
55 }
56
57 public static function sanitizeTask($data)
58 {
59 $fieldMaps = [
60 'title' => 'sanitize_text_field',
61 'board_id' => 'intval',
62 'parent_id' => 'intval',
63 'crm_contact_id' => 'intval',
64 'task_type' => 'sanitize_text_field',
65 'stage' => 'sanitize_text_field',
66 'reminder_type' => 'sanitize_text_field',
67 'priority' => 'sanitize_text_field',
68 'lead_value' => 'doubleval',
69 'remind_at' => 'sanitize_text_field',
70 'scope' => 'sanitize_text_field',
71 'source' => 'sanitize_text_field',
72 'description' => 'wp_kses_post',
73 'due_date' => 'sanitize_text_field',
74 'start_at' => 'sanitize_text_field',
75 'log_minutes' => 'sanitize_text_field',
76 'last_completed' => 'sanitize_text_field',
77 'assignees' => 'intval',
78 'is_archived' => 'intval',
79 'previous_stage' => 'sanitize_text_field',
80 'new_stage' => 'sanitize_text_field',
81 'new_index' => 'intval',
82 'old_index' => 'intval',
83 'new_board_id' => 'intval',
84 'position' => 'intval'
85
86 ];
87
88 return self::sanitizeData($data, $fieldMaps);
89 }
90
91 public static function sanitizeBoard($data)
92 {
93 $fieldMaps = [
94 'board_id' => 'intval',
95 'title' => 'sanitize_text_field',
96 'parent_id' => 'intval',
97 'type' => 'sanitize_text_field',
98 'description' => 'wp_kses_post',
99 'currency' => 'sanitize_text_field',
100 'image_url' => 'sanitize_url',
101 'is_auth_require' => 'intval',
102 'crm_contact_id' => 'intval',
103 'id' => 'sanitize_text_field',
104 'is_image' => 'rest_sanitize_boolean',
105 'color' => 'sanitize_text_field', // sanitize_hex_color doesn't work when color code is greater than 6 characters
106 ];
107
108 return self::sanitizeData($data, $fieldMaps);
109 }
110
111 public static function sanitizeStage($data)
112 {
113 $fieldMaps = [
114 'title' => 'sanitize_text_field',
115 'slug' => 'sanitize_text_field',
116 'type' => 'sanitize_text_field',
117 'status' => 'sanitize_text_field',
118 ];
119
120 return self::sanitizeData($data, $fieldMaps);
121 }
122
123 public static function sanitizeComment($data)
124 {
125 $fieldMaps = [
126 'description' => 'wp_kses_post',
127 'created_by' => 'intval',
128 'task_id' => 'intval',
129 'type' => 'sanitize_text_field',
130 ];
131
132 return self::sanitizeData($data, $fieldMaps);
133 }
134
135 public static function sanitizeLabel($data)
136 {
137 $fieldMaps = [
138 'bg_color' => 'sanitize_hex_color',
139 'color' => 'sanitize_hex_color',
140 'label' => 'sanitize_text_field',
141 'boardId' => 'intval',
142 'task_id' => 'intval',
143 'meta_value' => 'intval',
144 ];
145
146 return self::sanitizeData($data, $fieldMaps);
147 }
148
149 public static function sanitizeSubtask($data)
150 {
151 $fieldMaps = [
152 'title' => 'sanitize_text_field',
153 'stage' => 'sanitize_text_field',
154 'newPosition' => 'intval',
155 'priority' => 'sanitize_text_field',
156 'task_type' => 'sanitize_text_field',
157 'board_id' => 'intval',
158 'created_by' => 'intval',
159 'due_date' => 'sanitize_text_field',
160 ];
161
162 return self::sanitizeData($data, $fieldMaps);
163 }
164
165 public static function createActivity($data)
166 {
167 return Activity::create($data);
168 }
169
170 public static function sanitizeTaskMeta($data)
171 {
172 $fieldMaps = [
173 'title' => 'sanitize_text_field',
174 'url' => 'sanitize_url',
175
176 ];
177
178 return self::sanitizeData($data, $fieldMaps);
179 }
180
181 public static function getFormattedStagesByBoardId($boardId)
182 {
183 if (!$boardId) {
184 return [];
185 }
186
187 $board = Board::findOrFail($boardId);
188
189 $stages = $board->stages()->get();
190 // return static::formateStage($stages);
191 return $stages;
192 }
193
194 public static function getStagesByBoardId($boardId)
195 {
196 if (!$boardId) {
197 return [];
198 }
199
200 $board = Board::findOrFail($boardId);
201
202 $stages = $board->stages()->get();
203 return static::formateStage($stages);
204 // return $stages;
205 }
206
207 public static function formateStage($stages)
208 {
209 if (!$stages) return [];
210
211 $formattedStages = [];
212 foreach ($stages as $stage) {
213 $boardName = $stage->board->title;
214 $formattedStages[] = [
215 'id' => strval($stage->id),
216 'title' => $boardName . ' - ' . $stage->title
217 ];
218 }
219 return $formattedStages;
220 }
221
222 public static function getIdTitleArray($data)
223 {
224 $array = [];
225 foreach ($data as $item) {
226 $array[] = [
227 'id' => $item->id,
228 'title' => $item->title
229 ];
230 }
231 return $array;
232 }
233
234 // get Task Url by task id and board id
235 public static function getTaskUrl($taskId, $boardId): string
236 {
237 if (!$taskId || !$boardId) {
238 return '';
239 }
240 return fluent_boards_page_url() . 'boards/' . $boardId . '/tasks/' . $taskId;
241 }
242
243 public static function getTaskUrlByTask($task): string
244 {
245 if (!$task) {
246 return '';
247 }
248 return fluent_boards_page_url() . 'boards/' . $task->board_id . '/tasks/' . $task->id;
249 }
250
251 public static function getBoardUrl($boardId): string
252 {
253 if (!$boardId) {
254 return '';
255 }
256 return fluent_boards_page_url() . 'boards/' . $boardId;
257 }
258
259 public static function crm_contact($id)
260 {
261 if (!defined('FLUENTCRM')) {
262 return '';
263 }
264
265 $contact = \FluentCrm\App\Models\Subscriber::with(['tags', 'lists'])->find($id);
266
267 if (!$contact) {
268 return null;
269 }
270
271 return [
272 'id' => $contact->id,
273 'email' => $contact->email,
274 'first_name' => $contact->first_name,
275 'last_name' => $contact->last_name,
276 'full_name' => $contact->full_name,
277 'avatar' => $contact->avatar,
278 'photo' => $contact->photo,
279 'status' => $contact->status,
280 'contact_type' => $contact->contact_type,
281 'last_activity' => $contact->last_activity,
282 'life_time_value' => $contact->life_time_value,
283 'total_points' => $contact->total_points,
284 'user_id' => $contact->user_id,
285 'created_at' => $contact->created_at,
286 'tags' => Helper::getIdTitleArray($contact->tags),
287 'lists' => Helper::getIdTitleArray($contact->lists)
288
289 ];
290
291 }
292
293 public static function getStagesByBoardGroup()
294 {
295 $boards = self::getBoards();
296
297 $groups = [];
298 foreach ($boards as $board) {
299 $groups[] = [
300 'title' => $board->title,
301 'slug' => 'aaa' . '_' . $board->id,
302 'options' => self::getStagesByBoardId($board->id)
303 ];
304 }
305 return $groups;
306 }
307
308 public static function getBoards()
309 {
310 return Board::orderBy('created_at', 'ASC')->get();
311 }
312
313 public static function getStage($stageId)
314 {
315 return Stage::findOrFail($stageId);
316 }
317
318 public static function getBoardByStageId($stageId)
319 {
320 $stage = self::getStage($stageId);
321 return $stage->board;
322 }
323
324 public static function sanitizeUserCollections($users)
325 {
326 if (current_user_can('list_users')) {
327 return $users;
328 }
329
330 $users->makeHidden(['user_email', 'user_nicename', 'user_registered', 'user_url', 'user_status']);
331
332 return $users;
333 }
334
335 public static function sanitizeUsersArray($users, $boardId = null)
336 {
337 if (current_user_can('list_users')) {
338 return $users;
339 }
340
341 $sanitizedUsers = [];
342
343 if(!PermissionManager::isBoardManager($boardId)) //Todo: may create permission security issue, will be modified later
344 {
345 foreach ($users as $user) {
346 unset($user->user_email);
347 unset($user->email);
348 $sanitizedUsers[] = $user;
349 }
350 } else {
351 foreach ($users as $user) {
352 $sanitizedUsers[] = $user;
353 }
354 }
355
356 return $sanitizedUsers;
357 }
358
359 public static function getPriorityOptions()
360 {
361 return [
362 [
363 'id' => 'low',
364 'title' => 'Low'
365 ],
366 [
367 'id' => 'medium',
368 'title' => 'Medium'
369 ],
370 [
371 'id' => 'high',
372 'title' => 'High'
373 ],
374 ];
375 }
376
377 public static function dueDateConversion($due_time, $unit)
378 {
379 if($due_time <= 0) {
380 return null;
381 }
382 $currentTime = current_time('mysql');
383 $readyString = '+' . $due_time . ' ' . $unit;
384 return gmdate('Y-m-d H:i:s',strtotime($readyString,strtotime($currentTime)));
385 }
386
387 public static function searchWordPressUsers($searchQuery, $limit = 20)
388 {
389 $search = sanitize_text_field($searchQuery);
390
391 // Search by user login, email, and nicename
392 $args = array(
393 'role' => '',
394 'search' => '*' . $search . '*',
395 'number' => $limit,
396 );
397
398 // Get users by login, email, and nicename
399 $user_query = new \WP_User_Query($args);
400 $users_by_login = $user_query->get_results();
401
402 // Search by first name and last name
403 $meta_query_args = array(
404 'relation' => 'OR',
405 array(
406 'key' => 'first_name',
407 'value' => $search,
408 'compare' => 'LIKE',
409 ),
410 array(
411 'key' => 'last_name',
412 'value' => $search,
413 'compare' => 'LIKE',
414 ),
415 );
416
417 $meta_query = array(
418 'role' => '',
419 'meta_query' => $meta_query_args,
420 'number' => $limit,
421 );
422
423 // Get users by first name and last name
424 $users_by_meta = get_users($meta_query);
425
426 // Merge and remove duplicates
427 $users = array_merge($users_by_login, $users_by_meta);
428 $users = array_unique($users, SORT_REGULAR);
429
430 return $users;
431
432 }
433
434 public static function sanitizeTaskAttachment($data)
435 {
436 $fieldMaps = [
437 'title' => 'sanitize_text_field',
438 'url' => 'sanitize_url',
439 ];
440
441 return self::sanitizeData($data, $fieldMaps);
442 }
443 }
444