PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.4
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.4
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 1.5.5 All 68 releases
fluent-support / app / Services / TicketQueryService.php

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

142 lines 4.2 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 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($this->args['sort_by'], $this->args['sort_type']);
73
74 do_action_ref_array('fluent_support\main_tickets_query', [&$ticketsQuery, $this->args]);
75 $this->model = $ticketsQuery;
76 }
77
78 public function paginate()
79 {
80 return $this->model->paginate();
81 }
82
83 public function get()
84 {
85 $model = $this->model;
86
87 if($limit = $this->args['limit']) {
88 $model = $model->limit($limit);
89 }
90 if($offset = $this->args['offset']) {
91 $model = $model->offset($offset);
92 }
93
94 return $model->get();
95 }
96
97 public function getModel()
98 {
99 return $this->model;
100 }
101
102 /**
103 *formatAdvancedFilters method will format the requested variable for advanced filtering
104 */
105 private function formatAdvancedFilters()
106 {
107 $filters = $this->args['filters_groups_raw'];
108
109 $groups = [];
110 //$filterGroup mean all items under And, OR
111 foreach ($filters as $filterGroup) {
112 $group = [];
113 //$filterItem is individual item under a group(And/OR)
114 foreach ($filterGroup as $filterItem) {
115 if (count($filterItem['source']) != 2 || empty($filterItem['source'][0]) || empty($filterItem['source'][1]) || empty($filterItem['operator'])) {
116 continue;
117 }
118
119 $provider = $filterItem['source'][0];
120
121 if (!isset($group[$provider])) {
122 $group[$provider] = [];
123 }
124
125 $property = $filterItem['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