| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Ticket; |
| 6 |
use FluentSupport\App\Services\FluentBoardsService; |
| 7 |
use FluentSupport\App\Services\Helper; |
| 8 |
use FluentSupport\Framework\Http\Request\Request; |
| 9 |
|
| 10 |
class FluentBoardsController extends Controller |
| 11 |
{ |
| 12 |
public function getBoards() |
| 13 |
{ |
| 14 |
if (!function_exists('FluentBoardsApi')) { |
| 15 |
return $this->sendError([ |
| 16 |
'message' => __('Fluent Boards plugin is not installed or activated.', 'fluent-support') |
| 17 |
], 400); |
| 18 |
} |
| 19 |
|
| 20 |
$boards = FluentBoardsApi('boards')->getBoards(); |
| 21 |
$formattedBoards = []; |
| 22 |
|
| 23 |
foreach ($boards as $board) { |
| 24 |
$formattedBoards[] = [ |
| 25 |
'id' => $board->id, |
| 26 |
'title' => $board->title, |
| 27 |
'tasks' => [], |
| 28 |
]; |
| 29 |
} |
| 30 |
|
| 31 |
return ['boards' => $formattedBoards]; |
| 32 |
} |
| 33 |
|
| 34 |
public function getStages(Request $request) |
| 35 |
{ |
| 36 |
if (!function_exists('FluentBoardsApi')) { |
| 37 |
return $this->sendError([ |
| 38 |
'message' => __('Fluent Boards plugin is not installed or activated.', 'fluent-support') |
| 39 |
], 400); |
| 40 |
} |
| 41 |
|
| 42 |
$boardId = $request->getSafe('board_id', 'intval'); |
| 43 |
$boardStages = FluentBoardsApi('boards')->getStagesByBoard($boardId); |
| 44 |
|
| 45 |
$formattedStages = []; |
| 46 |
if (!empty($boardStages)) { |
| 47 |
foreach ($boardStages[0]->stages as $stage) { |
| 48 |
$formattedStages[] = [ |
| 49 |
'id' => $stage->id, |
| 50 |
'title' => $stage->title, |
| 51 |
]; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
return ['stages' => $formattedStages]; |
| 56 |
} |
| 57 |
|
| 58 |
public function createTask(Request $request, FluentBoardsService $fluentBoardsService) |
| 59 |
{ |
| 60 |
if (!function_exists('FluentBoardsApi')) { |
| 61 |
return $this->sendError([ |
| 62 |
'message' => __('Fluent Boards plugin is not installed or activated.', 'fluent-support') |
| 63 |
], 400); |
| 64 |
} |
| 65 |
|
| 66 |
try { |
| 67 |
$sourceId = $request->getSafe('source_id', 'intval'); |
| 68 |
|
| 69 |
$ticket = Ticket::findOrFail($sourceId); |
| 70 |
|
| 71 |
$this->ensureCanAccessTicket($ticket); |
| 72 |
|
| 73 |
$taskData = [ |
| 74 |
'source_id' => $sourceId, |
| 75 |
'board_id' => $request->getSafe('board_id', 'intval'), |
| 76 |
'stage_id' => $request->getSafe('stage_id', 'intval'), |
| 77 |
'crm_contact_id' => $request->getSafe('crm_contact_id', 'intval') ?: null, |
| 78 |
'title' => $request->getSafe('title', 'sanitize_text_field'), |
| 79 |
'description' => $request->getSafe('description', 'wp_kses_post'), |
| 80 |
'source' => $request->getSafe('source', 'sanitize_text_field'), |
| 81 |
'started_at' => $request->getSafe('started_at', 'sanitize_text_field'), |
| 82 |
'due_at' => $request->getSafe('due_at', 'sanitize_text_field'), |
| 83 |
'labels' => [], |
| 84 |
'assignees' => [], |
| 85 |
]; |
| 86 |
|
| 87 |
$task = FluentBoardsApi('tasks')->create($taskData); |
| 88 |
|
| 89 |
if (!$task) { |
| 90 |
return $this->sendError(__('Failed to create task.', 'fluent-support')); |
| 91 |
} |
| 92 |
|
| 93 |
$fluentBoardsService->addInternalNote($task); |
| 94 |
$fluentBoardsService->addComment($task); |
| 95 |
|
| 96 |
return [ |
| 97 |
'message' => __('Task successfully added to Fluent Boards', 'fluent-support'), |
| 98 |
'task' => $task |
| 99 |
]; |
| 100 |
} catch (\Exception $e) { |
| 101 |
return $this->sendError([ |
| 102 |
'message' => Helper::getSafeErrorMessage($e) |
| 103 |
]); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|