1 | <?php |
||
22 | class QueryBuilderProvider implements Provider |
||
23 | { |
||
24 | /** |
||
25 | * @var QueryBuilder The original query, as passed to us. |
||
26 | */ |
||
27 | private $originalQuery; |
||
28 | |||
29 | /** |
||
30 | * @var QueryBuilder The query, before limits are applied |
||
31 | */ |
||
32 | private $queryBeforeLimits; |
||
33 | |||
34 | /** |
||
35 | * @var QueryBuilder The underlying query |
||
36 | */ |
||
37 | private $query; |
||
38 | |||
39 | /** |
||
40 | * @var QueryConfiguration |
||
41 | */ |
||
42 | private $queryConfiguration; |
||
43 | |||
44 | /** |
||
45 | * @var callable the default global function to check if a row should be included |
||
46 | */ |
||
47 | private $defaultGlobalSearchFunction; |
||
|
|||
48 | |||
49 | /** |
||
50 | * @var callable the default global order function |
||
51 | */ |
||
52 | private $defaultGlobalOrderFunction; |
||
53 | |||
54 | /** |
||
55 | * @var array an array of callables with local search functions to check if the row should be included |
||
56 | */ |
||
57 | private $columnSearchFunction = []; |
||
58 | |||
59 | /** |
||
60 | * @var array an array that will hold the search functions for each column |
||
61 | */ |
||
62 | private $columnConfiguration = []; |
||
63 | |||
64 | /** |
||
65 | * CollectionProvider constructor. |
||
66 | * @param QueryBuilder $query The query to base the built query on |
||
67 | */ |
||
68 | public function __construct(QueryBuilder $query) |
||
69 | { |
||
70 | $this->originalQuery = $query; |
||
71 | $this->query = clone $query; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Here the DTQueryConfiguration is passed to prepare the provider for the processing of the request. |
||
76 | * This will only be called when the DTProvider needs to handle the request. |
||
77 | * It will never be called when the DTProvider does not need to handle the request. |
||
78 | * |
||
79 | * @param QueryConfiguration $queryConfiguration |
||
80 | * @param ColumnConfiguration[] $columnConfiguration |
||
81 | * @return mixed |
||
82 | */ |
||
83 | public function prepareForProcessing(QueryConfiguration $queryConfiguration, array $columnConfiguration) |
||
84 | { |
||
85 | $this->queryConfiguration = $queryConfiguration; |
||
86 | $this->columnConfiguration = $columnConfiguration; |
||
87 | |||
88 | // compile the query first |
||
89 | $this->compileQuery(); |
||
90 | |||
91 | // sort |
||
92 | $this->sortQuery(); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * This method should process all configurations and prepare the underlying data for the view. It will arrange the |
||
97 | * data and provide the results in a DTData object. |
||
98 | * It will be called after {@link #prepareForProcessing} has been called and needs to return the processed data in |
||
99 | * a DTData object so the Composer can further handle the data. |
||
100 | * |
||
101 | * @return ResponseData The processed data |
||
102 | * |
||
103 | */ |
||
104 | public function process() |
||
105 | { |
||
106 | // check if the query configuration is set |
||
107 | if (is_null($this->queryConfiguration) || empty($this->columnConfiguration)) { |
||
108 | throw new \InvalidArgumentException("Provider was not configured. Did you call prepareForProcessing first?"); |
||
109 | } |
||
110 | |||
111 | // limit |
||
112 | $this->queryBeforeLimits = clone $this->query; |
||
113 | $this->limitQuery(); |
||
114 | |||
115 | // # of items in filtered & ordered data set |
||
116 | $dataCount = $this->queryBeforeLimits->count(); |
||
117 | |||
118 | // the data for the response |
||
119 | $columns = $this->compileColumnNames(); |
||
120 | $response = new Collection($this->query->get($columns)); |
||
121 | |||
122 | // slice the result into the right size |
||
123 | return new ResponseData( |
||
124 | $response, |
||
125 | $this->getTotalNumberOfRows(), |
||
126 | $dataCount |
||
127 | ); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Get the total number of rows for the original query. |
||
132 | * @return int |
||
133 | */ |
||
134 | private function getTotalNumberOfRows() |
||
135 | { |
||
136 | return $this->originalQuery->count(); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Will compile the collection into the final collection where operations like search and order can be applied. |
||
141 | * |
||
142 | * @return QueryBuilder |
||
143 | * @throws DatatableException |
||
144 | */ |
||
145 | private function compileQuery() |
||
146 | { |
||
147 | if ($this->queryConfiguration->isGlobalSearch()) { |
||
148 | $this->compileGlobalQuery(); |
||
149 | } elseif ($this->queryConfiguration->isColumnSearch()) { |
||
150 | $this->compileColumnQuery(); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * When a global (single) search has been done against data in the datatable. |
||
156 | * |
||
157 | * @return QueryBuilder |
||
158 | * @throws DatatableException |
||
159 | */ |
||
160 | private function compileGlobalQuery() |
||
161 | { |
||
162 | foreach ($this->columnConfiguration as $i => $col) { |
||
163 | $this->createQueryForColumn($col, $this->queryConfiguration->searchValue()); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * When a global query is being performed (ie, a query against a single column) |
||
169 | * |
||
170 | * @return QueryBuilder |
||
171 | * @throws DatatableException |
||
172 | */ |
||
173 | private function compileColumnQuery() |
||
174 | { |
||
175 | $searchColumns = $this->queryConfiguration->searchColumns(); |
||
176 | |||
177 | foreach ($searchColumns as $i => $col) { |
||
178 | $column = $this->getColumnFromName($col->columnName()); |
||
179 | |||
180 | if (!isset($column)) |
||
181 | continue; |
||
182 | |||
183 | $this->createQueryForColumn($column, $col->searchValue()); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Create the query w/ QueryBuilder |
||
189 | * @param ColumnConfiguration $column |
||
190 | * @param $searchValue |
||
191 | * @return QueryBuilder |
||
192 | * @throws DatatableException |
||
193 | */ |
||
194 | private function createQueryForColumn(ColumnConfiguration $column, $searchValue) |
||
195 | { |
||
196 | $searchType = $column->getSearch(); |
||
197 | |||
198 | if ($searchType == DefaultSearchable::NONE()) { |
||
199 | // Don't do anything, this is not a searchable field |
||
200 | return $this->query; |
||
201 | } elseif ($searchType == DefaultSearchable::NORMAL()) { |
||
202 | $this->query->orWhere($column->getName(), 'LIKE', '%' . $searchValue . '%'); |
||
203 | } elseif ($searchType == DefaultSearchable::REGEX()) { |
||
204 | $this->query->orWhere($column->getName(), 'REGEXP', $searchValue); |
||
205 | } else { |
||
206 | throw new DatatableException('An unsupported DefaultSearchable was provided.'); |
||
207 | } |
||
208 | |||
209 | return $this->query; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Get the requested column configuration from the name of a column |
||
214 | * @param string $name |
||
215 | * @return ColumnConfiguration |
||
216 | * @throws DatatableException when a column is not found |
||
217 | */ |
||
218 | private function getColumnFromName($name) |
||
219 | { |
||
220 | foreach ($this->columnConfiguration as $i => $col) { |
||
221 | if ($col->getName() == $name) { |
||
222 | return $col; |
||
223 | } |
||
224 | } |
||
225 | |||
226 | // This exception should never happen. If it does, something is |
||
227 | // wrong w/ the relationship between searchable columns and the |
||
228 | // configuration. |
||
229 | throw new DatatableException("A requested column was not found in the columnConfiguration."); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Get a list of all the column names for the SELECT query. |
||
234 | */ |
||
235 | private function compileColumnNames() |
||
244 | |||
245 | /** |
||
246 | * Will sort the query based on the given datatable query configuration. |
||
247 | */ |
||
248 | private function sortQuery() |
||
249 | { |
||
250 | if ($this->queryConfiguration->hasOrderColumn()) { |
||
251 | $orderColumns = $this->queryConfiguration->orderColumns(); |
||
252 | |||
253 | foreach($orderColumns as $order) { |
||
258 | |||
259 | /** |
||
260 | * Will limit a query based on the start and length given |
||
261 | */ |
||
262 | private function limitQuery() |
||
267 | } |
This check marks private properties in classes that are never used. Those properties can be removed.