| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Services\Intergrations\FluentCRM; |
| 4 |
|
| 5 |
|
| 6 |
use FluentBoards\App\Models\Board; |
| 7 |
use FluentBoards\App\Models\Task; |
| 8 |
use FluentBoards\App\Models\TaskMeta; |
| 9 |
use FluentBoards\App\Models\User; |
| 10 |
use FluentBoards\App\Services\PermissionManager; |
| 11 |
|
| 12 |
class DeepIntegration |
| 13 |
{ |
| 14 |
public function init() |
| 15 |
{ |
| 16 |
add_filter('fluentcrm_ajax_options_boards', [$this, 'getBoards'], 10, 3); |
| 17 |
add_filter('fluentcrm_ajax_options_task_templates', [$this, 'getTaskTemplates'], 10, 3); |
| 18 |
add_filter('fluentcrm_ajax_options_board_default_templates', [$this, 'getDefaultBoardTemplates'], 10, 3); |
| 19 |
add_filter('fluentcrm_ajax_options_board_user_templates', [$this, 'getUserBoardTemplates'], 10, 3); |
| 20 |
add_filter('fluentcrm_ajax_options_board_members', [$this, 'getBoardMembers'], 10, 3); |
| 21 |
} |
| 22 |
|
| 23 |
public function getBoards($records, $search, $includeIds) |
| 24 |
{ |
| 25 |
$query = Board::select(['id', 'title']); |
| 26 |
|
| 27 |
if (!empty($search)) { |
| 28 |
$query->where('title', 'like', "%$search%"); |
| 29 |
} |
| 30 |
|
| 31 |
$boards = $query->orderBy('title', 'ASC')->get(); |
| 32 |
|
| 33 |
return $this->getFormattedBoards($boards); |
| 34 |
} |
| 35 |
|
| 36 |
public function getFormattedBoards($boards) |
| 37 |
{ |
| 38 |
$formattedBoards = []; |
| 39 |
foreach ($boards as $board) { |
| 40 |
$formattedBoards[] = [ |
| 41 |
'id' => strval($board->id), |
| 42 |
'title' => $board->title |
| 43 |
]; |
| 44 |
} |
| 45 |
return $formattedBoards; |
| 46 |
} |
| 47 |
|
| 48 |
public function getTaskTemplates($records, $search, $includeIds) |
| 49 |
{ |
| 50 |
if (!defined('FLUENT_BOARDS_PRO')) { |
| 51 |
return []; |
| 52 |
} |
| 53 |
|
| 54 |
$userId = get_current_user_id(); |
| 55 |
$currentUser = User::find($userId); |
| 56 |
|
| 57 |
$relatedBoardsQuery = Board::query(); |
| 58 |
|
| 59 |
if (!PermissionManager::isAdmin($userId)) { |
| 60 |
$relatedBoardsQuery->whereIn('id', $currentUser->whichBoards->pluck('id')); |
| 61 |
} |
| 62 |
|
| 63 |
$templateTaskIds = TaskMeta::where('key', 'is_template')->where('value', 'yes')->pluck('task_id'); |
| 64 |
$query = Task::whereIn('id', $templateTaskIds) |
| 65 |
->where('archived_at', null) |
| 66 |
->whereIn('board_id', $relatedBoardsQuery->pluck('id')) |
| 67 |
->with('assignees', 'labels'); |
| 68 |
|
| 69 |
|
| 70 |
if (!empty($search)) { |
| 71 |
$query->where('title', 'like', "%$search%"); |
| 72 |
} |
| 73 |
|
| 74 |
$templateTasks = $query->orderBy('title', 'ASC')->get(); |
| 75 |
|
| 76 |
$formattedTemplateTasks = []; |
| 77 |
foreach ($templateTasks as $task) { |
| 78 |
$formattedTemplateTasks[] = [ |
| 79 |
'id' => strval($task->id), |
| 80 |
'title' => $task->title |
| 81 |
]; |
| 82 |
} |
| 83 |
return $formattedTemplateTasks; |
| 84 |
} |
| 85 |
|
| 86 |
public function getDefaultBoardTemplates($records, $search, $includeIds) |
| 87 |
{ |
| 88 |
if (!defined('FLUENT_BOARDS_PRO')) { |
| 89 |
return []; |
| 90 |
} |
| 91 |
|
| 92 |
$templateService = new \FluentBoardsPro\App\Services\TemplateService(); |
| 93 |
$templates = $templateService->getDefaultTemplates(); |
| 94 |
|
| 95 |
$formattedTemplates = []; |
| 96 |
foreach ($templates as $template) { |
| 97 |
// Filter by search if provided |
| 98 |
if (!empty($search)) { |
| 99 |
$title = strtolower($template['title'] ?? ''); |
| 100 |
$description = strtolower($template['description'] ?? ''); |
| 101 |
$searchLower = strtolower($search); |
| 102 |
|
| 103 |
if (strpos($title, $searchLower) === false && strpos($description, $searchLower) === false) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
$formattedTemplates[] = [ |
| 109 |
'id' => $template['id'], |
| 110 |
'title' => $template['title'] |
| 111 |
]; |
| 112 |
} |
| 113 |
|
| 114 |
return $formattedTemplates; |
| 115 |
} |
| 116 |
|
| 117 |
public function getUserBoardTemplates($records, $search, $includeIds) |
| 118 |
{ |
| 119 |
if (!defined('FLUENT_BOARDS_PRO')) { |
| 120 |
return []; |
| 121 |
} |
| 122 |
|
| 123 |
$templateService = new \FluentBoardsPro\App\Services\TemplateService(); |
| 124 |
$templates = $templateService->getUserTemplates(); |
| 125 |
|
| 126 |
$formattedTemplates = []; |
| 127 |
foreach ($templates as $template) { |
| 128 |
// Filter by search if provided |
| 129 |
if (!empty($search)) { |
| 130 |
$title = strtolower($template['title'] ?? ''); |
| 131 |
$description = strtolower($template['description'] ?? ''); |
| 132 |
$searchLower = strtolower($search); |
| 133 |
|
| 134 |
if (strpos($title, $searchLower) === false && strpos($description, $searchLower) === false) { |
| 135 |
continue; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
$formattedTemplates[] = [ |
| 140 |
'id' => strval($template['id']), |
| 141 |
'title' => $template['title'] |
| 142 |
]; |
| 143 |
} |
| 144 |
|
| 145 |
return $formattedTemplates; |
| 146 |
} |
| 147 |
|
| 148 |
public function getBoardMembers($records, $search, $includeIds) |
| 149 |
{ |
| 150 |
// Get all WordPress users |
| 151 |
$args = [ |
| 152 |
'orderby' => 'display_name', |
| 153 |
'order' => 'ASC', |
| 154 |
]; |
| 155 |
|
| 156 |
// Add search filter if provided |
| 157 |
if (!empty($search)) { |
| 158 |
$args['search'] = '*' . $search . '*'; |
| 159 |
$args['search_columns'] = ['user_login', 'user_email', 'display_name']; |
| 160 |
} |
| 161 |
|
| 162 |
// Include specific IDs if provided |
| 163 |
if (!empty($includeIds)) { |
| 164 |
$args['include'] = $includeIds; |
| 165 |
} |
| 166 |
|
| 167 |
$users = get_users($args); |
| 168 |
|
| 169 |
$formattedMembers = []; |
| 170 |
foreach ($users as $user) { |
| 171 |
$formattedMembers[] = [ |
| 172 |
'id' => strval($user->ID), |
| 173 |
'title' => $user->display_name . ' (' . $user->user_email . ')' |
| 174 |
]; |
| 175 |
} |
| 176 |
|
| 177 |
return $formattedMembers; |
| 178 |
} |
| 179 |
} |
| 180 |
|