| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Api\Classes; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Agent; |
| 6 |
use FluentSupport\App\Models\Customer; |
| 7 |
use FluentSupport\App\Models\MailBox; |
| 8 |
use FluentSupport\App\Models\Ticket; |
| 9 |
use FluentSupport\App\Services\Helper; |
| 10 |
use FluentSupport\App\Services\Tickets\ResponseService; |
| 11 |
|
| 12 |
/** |
| 13 |
* Tickets class for PHP API |
| 14 |
* Example Usage: $ticketsApi = FluentSupportApi('tickets'); |
| 15 |
* @package FluentSupport\App\Api\Classes |
| 16 |
* |
| 17 |
* @version 1.0.0 |
| 18 |
*/ |
| 19 |
class Tickets |
| 20 |
{ |
| 21 |
private $instance = null; |
| 22 |
|
| 23 |
private $allowedInstanceMethods = [ |
| 24 |
'all', |
| 25 |
'get', |
| 26 |
'find', |
| 27 |
'first', |
| 28 |
'paginate' |
| 29 |
]; |
| 30 |
|
| 31 |
public function __construct(Ticket $instance) |
| 32 |
{ |
| 33 |
$this->instance = $instance; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* getTickets method will return all tickets |
| 38 |
* |
| 39 |
* @return object |
| 40 |
*/ |
| 41 |
public function getTickets() |
| 42 |
{ |
| 43 |
$ticketsQuery = Ticket::with([ |
| 44 |
'customer' => function ($query) { |
| 45 |
$query->select(['first_name', 'last_name', 'id', 'email']); |
| 46 |
}, 'agent' => function ($query) { |
| 47 |
$query->select(['first_name', 'last_name', 'id', 'email']); |
| 48 |
} |
| 49 |
]) |
| 50 |
->orderBy('id', 'DESC'); |
| 51 |
$tickets = $ticketsQuery->paginate(); |
| 52 |
|
| 53 |
if (defined('FLUENTSUPPORTPRO')) { |
| 54 |
foreach ($tickets as $ticket) { |
| 55 |
$ticket->custom_fields = $ticket->customData(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return $tickets; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* getTicket method will return a specific ticket by id |
| 64 |
* @param int $id |
| 65 |
* @return object|false |
| 66 |
*/ |
| 67 |
|
| 68 |
public function getTicket(int $id) |
| 69 |
{ |
| 70 |
if (is_numeric($id)) { |
| 71 |
$ticket = Ticket::findOrFail($id); |
| 72 |
if (defined('FLUENTSUPPORTPRO')) { |
| 73 |
$ticket->custom_fields = $ticket->customData(); |
| 74 |
} |
| 75 |
return $ticket; |
| 76 |
} |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* addResponse method add response to a ticket by agent and ticket ID |
| 82 |
* @param array $data |
| 83 |
* @param int $agentId |
| 84 |
* @param int $ticketId |
| 85 |
* @return array|boolean |
| 86 |
*/ |
| 87 |
|
| 88 |
public function addResponse(array $data, int $agentId, int $ticketId) |
| 89 |
{ |
| 90 |
if (!$agentId || !$ticketId || !$data['content']) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
$agent = Agent::findOrFail($agentId); |
| 95 |
$ticket = Ticket::findOrFail($ticketId); |
| 96 |
|
| 97 |
if ($agent && $ticket) { |
| 98 |
return (new ResponseService())->createResponse($data, $agent, $ticket); |
| 99 |
} |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* createTicket method will create a new ticket |
| 105 |
* @param array $data |
| 106 |
* @return object| boolean |
| 107 |
*/ |
| 108 |
|
| 109 |
public function createTicket(array $data) |
| 110 |
{ |
| 111 |
|
| 112 |
$customerId = $data['customer_id'] ?? null; |
| 113 |
|
| 114 |
if (!$customerId || !($customer = Customer::find($customerId))) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
if (!$data['title'] || !$data['content']) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
if(!$data['mailbox_id']){ |
| 123 |
$defaultMailbox = Helper::getDefaultMailBox(); |
| 124 |
if(!$defaultMailbox){ |
| 125 |
return false; |
| 126 |
} |
| 127 |
$data['mailbox_id'] = $defaultMailbox->id; |
| 128 |
}else{ |
| 129 |
$mailbox = MailBox::find($data['mailbox_id']); |
| 130 |
if (!$mailbox) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
do_action('fluent_support/before_ticket_create', $data, $customer); |
| 136 |
|
| 137 |
$createdTicket = Ticket::create($data); |
| 138 |
|
| 139 |
do_action('fluent_support/ticket_created', $createdTicket, $customer); |
| 140 |
|
| 141 |
if (defined('FLUENTSUPPORTPRO') && !empty($data['custom_fields'])) { |
| 142 |
$createdTicket->syncCustomFields($data['custom_fields']); |
| 143 |
$createdTicket->custom_fields = $createdTicket->customData(); |
| 144 |
} |
| 145 |
|
| 146 |
return $createdTicket; |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
public function getInstance() |
| 151 |
{ |
| 152 |
return $this->instance; |
| 153 |
} |
| 154 |
|
| 155 |
public function __call($method, $params) |
| 156 |
{ |
| 157 |
if (in_array($method, $this->allowedInstanceMethods)) { |
| 158 |
return call_user_func_array([$this->instance, $method], $params); |
| 159 |
} |
| 160 |
|
| 161 |
throw new \Exception(sprintf("Method %s does not exist.", esc_html($method))); |
| 162 |
} |
| 163 |
} |
| 164 |
|