PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.5.4
Fluent Support – Helpdesk & Customer Support Ticket System v1.5.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.5.4, at app/Services/TicketQueryService.php

132 lines 3.6 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 private function setupQuery()
32 {
33 if ($this->args['filters_groups_raw']) {
34 $this->formatAdvancedFilters();
35 }
36
37 $ticketsQuery = Ticket::with($this->args['with']);
38
39 if ($this->args['filter_type'] == 'advanced') {
40 $filtersGroups = $this->args['filters_groups'];
41 foreach ($filtersGroups as $groupIndex => $group) {
42 $method = 'orWhere';
43 if ($groupIndex == 0) {
44 $method = 'where';
45 }
46 $ticketsQuery->{$method}(function ($q) use ($group) {
47 foreach ($group as $providerName => $items) {
48 do_action_ref_array('fluent_support\tickets_filter_' . $providerName, [&$q, $items]);
49 }
50 });
51 }
52 } else {
53
54 if($simpleFilters = $this->args['simple_filters']) {
55 $ticketsQuery->applyFilters($simpleFilters);
56 }
57
58 if($customerId = $this->args['customer_id']) {
59 $ticketsQuery->where('customer_id', $customerId);
60 }
61
62 if ($search = $this->args['search']) {
63 $ticketsQuery->searchBy($search);
64 }
65 }
66
67 $ticketsQuery->orderBy($this->args['sort_by'], $this->args['sort_type']);
68
69 $this->model = $ticketsQuery;
70 }
71
72 public function paginate()
73 {
74 return $this->model->paginate();
75 }
76
77 public function get()
78 {
79 $model = $this->model;
80
81 if($limit = $this->args['limit']) {
82 $model = $model->limit($limit);
83 }
84 if($offset = $this->args['offset']) {
85 $model = $model->offset($offset);
86 }
87
88 return $model->get();
89 }
90
91 public function getModel()
92 {
93 return $this->model;
94 }
95
96 private function formatAdvancedFilters()
97 {
98 $filters = $this->args['filters_groups_raw'];
99
100 $groups = [];
101
102 foreach ($filters as $filterGroup) {
103 $group = [];
104 foreach ($filterGroup as $filterItem) {
105 if (count($filterItem['source']) != 2 || empty($filterItem['source'][0]) || empty($filterItem['source'][1]) || empty($filterItem['operator'])) {
106 continue;
107 }
108
109 $provider = $filterItem['source'][0];
110
111 if (!isset($group[$provider])) {
112 $group[$provider] = [];
113 }
114
115 $property = $filterItem['source'][1];
116
117 $group[$provider][] = [
118 'property' => $property,
119 'operator' => $filterItem['operator'],
120 'value' => $filterItem['value']
121 ];
122 }
123
124 if ($group) {
125 $groups[] = $group;
126 }
127 }
128
129 $this->args['filters_groups'] = $groups;
130 }
131 }
132