PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.5 2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 All 69 releases
fluent-support / app / Services / TicketQueryService.php

TicketQueryService.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.4.5, at app/Services/TicketQueryService.php

145 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // One closure keeps the OR chain from swallowing the AND-ed agent scopes added later.
46 $ticketsQuery->where(function ($groupsQuery) use ($filtersGroups) {
47 foreach ($filtersGroups as $groupIndex => $group) {
48 $method = 'orWhere';
49 if ($groupIndex == 0) {
50 $method = 'where';
51 }
52 $groupsQuery->{$method}(function ($q) use ($group) {
53 foreach ($group as $providerName => $items) {
54 //Call doSearchForAdvancedFilter/filterTicketByUser dynamically from pro
55 do_action_ref_array('fluent_support\tickets_filter_' . $providerName, [&$q, $items]);
56 }
57 });
58 }
59 });
60 } else {
61
62 if($simpleFilters = $this->args['simple_filters']) {
63 $ticketsQuery->applyFilters($simpleFilters);
64 }
65
66 if($customerId = $this->args['customer_id']) {
67 $ticketsQuery->where('customer_id', $customerId);
68 }
69
70 if ($search = $this->args['search']) {
71 $ticketsQuery->searchBy($search);
72 }
73 }
74
75 $ticketsQuery->orderBy(sanitize_sql_orderby($this->args['sort_by']), sanitize_sql_orderby($this->args['sort_type']));
76 do_action_ref_array('fluent_support\main_tickets_query', [&$ticketsQuery, $this->args]);
77 $this->model = $ticketsQuery;
78 }
79
80 public function paginate()
81 {
82 return $this->model->paginate();
83 }
84
85 public function get()
86 {
87 $model = $this->model;
88
89 if($limit = $this->args['limit']) {
90 $model = $model->limit($limit);
91 }
92 if($offset = $this->args['offset']) {
93 $model = $model->offset($offset);
94 }
95
96 return $model->get();
97 }
98
99 public function getModel()
100 {
101 return $this->model;
102 }
103
104 /**
105 *formatAdvancedFilters method will format the requested variable for advanced filtering
106 */
107 private function formatAdvancedFilters()
108 {
109 $filters = $this->args['filters_groups_raw'];
110
111 $groups = [];
112 //$filterGroup mean all items under And, OR
113 foreach ($filters as $filterGroup) {
114 $group = [];
115 //$filterItem is individual item under a group(And/OR)
116 foreach ($filterGroup as $filterItem) {
117 $source = $filterItem['source'] ?? [];
118 if (!is_array($source) || count($source) != 2 || empty($source[0]) || empty($source[1]) || empty($filterItem['operator'])) {
119 continue;
120 }
121
122 $provider = $source[0];
123
124 if (!isset($group[$provider])) {
125 $group[$provider] = [];
126 }
127
128 $property = $source[1];
129
130 $group[$provider][] = [
131 'property' => $property,
132 'operator' => $filterItem['operator'],
133 'value' => $filterItem['value']
134 ];
135 }
136
137 if ($group) {
138 $groups[] = $group;
139 }
140 }
141
142 $this->args['filters_groups'] = $groups;
143 }
144 }
145