| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Ticket; |
| 6 |
|
| 7 |
class TicketQueryService |
| 8 |
{ |
| 9 |
private $args = []; |
| 10 |
|
| 11 |
private $model = null; |
| 12 |
|
| 13 |
public function __construct($args = []) |
| 14 |
{ |
| 15 |
$this->args = wp_parse_args($args, [ |
| 16 |
'with' => [], |
| 17 |
'filter_type' => 'simple', |
| 18 |
'filters_groups' => [], |
| 19 |
'filters_groups_raw' => [], |
| 20 |
'sort_by' => 'id', |
| 21 |
'sort_type' => 'DESC', |
| 22 |
'limit' => false, |
| 23 |
'offset' => false, |
| 24 |
'simple_filters' => [], |
| 25 |
'customer_id' => '', |
| 26 |
'search' => '' |
| 27 |
]); |
| 28 |
$this->setupQuery(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
*setupQuery method will prepare query to get the tickets based on the selected filter options |
| 33 |
*/ |
| 34 |
private function setupQuery() |
| 35 |
{ |
| 36 |
//If filter conditions added in advanced filtering |
| 37 |
if ($this->args['filters_groups_raw']) { |
| 38 |
$this->formatAdvancedFilters(); |
| 39 |
} |
| 40 |
|
| 41 |
$ticketsQuery = Ticket::with($this->args['with']); |
| 42 |
|
| 43 |
if ($this->args['filter_type'] == 'advanced') { |
| 44 |
$filtersGroups = $this->args['filters_groups']; |
| 45 |
foreach ($filtersGroups as $groupIndex => $group) { |
| 46 |
$method = 'orWhere'; |
| 47 |
if ($groupIndex == 0) { |
| 48 |
$method = 'where'; |
| 49 |
} |
| 50 |
$ticketsQuery->{$method}(function ($q) use ($group) { |
| 51 |
foreach ($group as $providerName => $items) { |
| 52 |
//Call doSearchForAdvancedFilter/filterTicketByUser dynamically from pro |
| 53 |
do_action_ref_array('fluent_support\tickets_filter_' . $providerName, [&$q, $items]); |
| 54 |
} |
| 55 |
}); |
| 56 |
} |
| 57 |
} else { |
| 58 |
|
| 59 |
if($simpleFilters = $this->args['simple_filters']) { |
| 60 |
$ticketsQuery->applyFilters($simpleFilters); |
| 61 |
} |
| 62 |
|
| 63 |
if($customerId = $this->args['customer_id']) { |
| 64 |
$ticketsQuery->where('customer_id', $customerId); |
| 65 |
} |
| 66 |
|
| 67 |
if ($search = $this->args['search']) { |
| 68 |
$ticketsQuery->searchBy($search); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
$ticketsQuery->orderBy(sanitize_sql_orderby($this->args['sort_by']), sanitize_sql_orderby($this->args['sort_type'])); |
| 73 |
do_action_ref_array('fluent_support\main_tickets_query', [&$ticketsQuery, $this->args]); |
| 74 |
$this->model = $ticketsQuery; |
| 75 |
} |
| 76 |
|
| 77 |
public function paginate() |
| 78 |
{ |
| 79 |
return $this->model->paginate(); |
| 80 |
} |
| 81 |
|
| 82 |
public function get() |
| 83 |
{ |
| 84 |
$model = $this->model; |
| 85 |
|
| 86 |
if($limit = $this->args['limit']) { |
| 87 |
$model = $model->limit($limit); |
| 88 |
} |
| 89 |
if($offset = $this->args['offset']) { |
| 90 |
$model = $model->offset($offset); |
| 91 |
} |
| 92 |
|
| 93 |
return $model->get(); |
| 94 |
} |
| 95 |
|
| 96 |
public function getModel() |
| 97 |
{ |
| 98 |
return $this->model; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
*formatAdvancedFilters method will format the requested variable for advanced filtering |
| 103 |
*/ |
| 104 |
private function formatAdvancedFilters() |
| 105 |
{ |
| 106 |
$filters = $this->args['filters_groups_raw']; |
| 107 |
|
| 108 |
$groups = []; |
| 109 |
//$filterGroup mean all items under And, OR |
| 110 |
foreach ($filters as $filterGroup) { |
| 111 |
$group = []; |
| 112 |
//$filterItem is individual item under a group(And/OR) |
| 113 |
foreach ($filterGroup as $filterItem) { |
| 114 |
$source = $filterItem['source'] ?? []; |
| 115 |
if (!is_array($source) || count($source) != 2 || empty($source[0]) || empty($source[1]) || empty($filterItem['operator'])) { |
| 116 |
continue; |
| 117 |
} |
| 118 |
|
| 119 |
$provider = $source[0]; |
| 120 |
|
| 121 |
if (!isset($group[$provider])) { |
| 122 |
$group[$provider] = []; |
| 123 |
} |
| 124 |
|
| 125 |
$property = $source[1]; |
| 126 |
|
| 127 |
$group[$provider][] = [ |
| 128 |
'property' => $property, |
| 129 |
'operator' => $filterItem['operator'], |
| 130 |
'value' => $filterItem['value'] |
| 131 |
]; |
| 132 |
} |
| 133 |
|
| 134 |
if ($group) { |
| 135 |
$groups[] = $group; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
$this->args['filters_groups'] = $groups; |
| 140 |
} |
| 141 |
} |
| 142 |
|