-
-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathPartSearchFilter.php
More file actions
365 lines (300 loc) · 10 KB
/
PartSearchFilter.php
File metadata and controls
365 lines (300 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\DataTables\Filters;
use App\DataTables\Filters\Constraints\AbstractConstraint;
use Doctrine\ORM\QueryBuilder;
use Doctrine\DBAL\ParameterType;
class PartSearchFilter implements FilterInterface
{
/** @var boolean Whether to use regex for searching */
protected bool $regex = false;
/** @var bool Use name field for searching */
protected bool $name = true;
/** @var bool Use id field for searching */
protected bool $dbId = false;
/** @var bool Use category name for searching */
protected bool $category = true;
/** @var bool Use description for searching */
protected bool $description = true;
/** @var bool Use tags for searching */
protected bool $tags = true;
/** @var bool Use storelocation name for searching */
protected bool $storelocation = true;
/** @var bool Use comment field for searching */
protected bool $comment = true;
/** @var bool Use ordernr for searching */
protected bool $ordernr = true;
/** @var bool Use manufacturer product name for searching */
protected bool $mpn = true;
/** @var bool Use supplier name for searching */
protected bool $supplier = false;
/** @var bool Use manufacturer name for searching */
protected bool $manufacturer = false;
/** @var bool Use footprint name for searching */
protected bool $footprint = false;
/** @var bool Use Internal Part number for searching */
protected bool $ipn = true;
/** @var int Helper variable for hacky array_map variable injection */
protected int $it = 0;
public function __construct(
/** @var string The string to query for */
protected string $keyword
)
{
// Transform keyword and trim excess spaces
$keyword = trim(str_replace('+', ' ', $keyword));
}
protected function getFieldsToSearch(): array
{
$fields_to_search = [];
if($this->name) {
$fields_to_search[] = 'part.name';
}
if($this->category) {
$fields_to_search[] = '_category.name';
}
if($this->description) {
$fields_to_search[] = 'part.description';
}
if ($this->comment) {
$fields_to_search[] = 'part.comment';
}
if($this->tags) {
$fields_to_search[] = 'part.tags';
}
if($this->storelocation) {
$fields_to_search[] = '_storelocations.name';
}
if($this->ordernr) {
$fields_to_search[] = '_orderdetails.supplierpartnr';
}
if($this->mpn) {
$fields_to_search[] = 'part.manufacturer_product_number';
}
if($this->supplier) {
$fields_to_search[] = '_suppliers.name';
}
if($this->manufacturer) {
$fields_to_search[] = '_manufacturer.name';
}
if($this->footprint) {
$fields_to_search[] = '_footprint.name';
}
if ($this->ipn) {
$fields_to_search[] = 'part.ipn';
}
return $fields_to_search;
}
public function apply(QueryBuilder $queryBuilder): void
{
$fields_to_search = $this->getFieldsToSearch();
$is_numeric = preg_match('/^\d+$/', $this->keyword) === 1;
// Add exact ID match only when the keyword is numeric
$search_dbId = $is_numeric && (bool)$this->dbId;
//If we have nothing to search for, do nothing
if (($fields_to_search === [] && !$search_dbId) || $this->keyword === '') {
return;
}
$expressions = [];
$params = [];
//Use equal expression to just search for exact numeric matches
if ($search_dbId) {
$expressions[] = $queryBuilder->expr()->eq('part.id', ':id_exact');
$params[] = new \Doctrine\ORM\Query\Parameter('id_exact', (int) $this->keyword,
ParameterType::INTEGER);
}
if ($this->regex) {
//Convert the fields to search to a list of expressions
$expressions = array_map(function (string $field): string {
return sprintf("REGEXP(%s, :search_query) = TRUE", $field);
}, $fields_to_search);
//For regex, we pass the query as is, save html special chars
$params[] = new \Doctrine\ORM\Query\Parameter('search_query', $this->keyword);
} else {
//Escape % and _ characters in the keyword
$this->keyword = str_replace(['%', '_'], ['\%', '\_'], $this->keyword);
//Split keyword on spaces, but limit token count
$tokens = explode(' ', $this->keyword, 5);
//Perform search of every single token in every selected field
//AND-combine the results (all tokens must be present in any of the results)
for ($i = 0; $i < sizeof($tokens); $i++) {
$this->it = $i;
$tokens[$i] = trim($tokens[$i]);
//Skip empty words (e.g. because of multiple spaces)
if ($tokens[$i] === '') {
continue;
}
//Convert the fields to search to a list of expressions
$expressions = array_map(function (string $field): string {
return sprintf("ILIKE(%s, :search_query%u) = TRUE", $field, $this->it);
}, $fields_to_search);
//Aggregate the parameters for consolidated commission
$params[] = new \Doctrine\ORM\Query\Parameter('search_query' . $i,
'%' . $tokens[$i] . '%');
}
}
$queryBuilder->setParameters(
new \Doctrine\Common\Collections\ArrayCollection($params)
);
//Guard condition
if (!empty($expressions)) {
//Add Or concatenation of the expressions to our query
$queryBuilder->andWhere(
$queryBuilder->expr()->orX(...$expressions)
);
}
}
public function getKeyword(): string
{
return $this->keyword;
}
public function setKeyword(string $keyword): PartSearchFilter
{
$this->keyword = $keyword;
return $this;
}
public function isRegex(): bool
{
return $this->regex;
}
public function setRegex(bool $regex): PartSearchFilter
{
$this->regex = $regex;
return $this;
}
public function isName(): bool
{
return $this->name;
}
public function setName(bool $name): PartSearchFilter
{
$this->name = $name;
return $this;
}
public function isDbId(): bool
{
return $this->dbId;
}
public function setDbId(bool $dbId): PartSearchFilter
{
$this->dbId = $dbId;
return $this;
}
public function isCategory(): bool
{
return $this->category;
}
public function setCategory(bool $category): PartSearchFilter
{
$this->category = $category;
return $this;
}
public function isDescription(): bool
{
return $this->description;
}
public function setDescription(bool $description): PartSearchFilter
{
$this->description = $description;
return $this;
}
public function isTags(): bool
{
return $this->tags;
}
public function setTags(bool $tags): PartSearchFilter
{
$this->tags = $tags;
return $this;
}
public function isStorelocation(): bool
{
return $this->storelocation;
}
public function setStorelocation(bool $storelocation): PartSearchFilter
{
$this->storelocation = $storelocation;
return $this;
}
public function isOrdernr(): bool
{
return $this->ordernr;
}
public function setOrdernr(bool $ordernr): PartSearchFilter
{
$this->ordernr = $ordernr;
return $this;
}
public function isMpn(): bool
{
return $this->mpn;
}
public function setMpn(bool $mpn): PartSearchFilter
{
$this->mpn = $mpn;
return $this;
}
public function isIPN(): bool
{
return $this->ipn;
}
public function setIPN(bool $ipn): PartSearchFilter
{
$this->ipn = $ipn;
return $this;
}
public function isSupplier(): bool
{
return $this->supplier;
}
public function setSupplier(bool $supplier): PartSearchFilter
{
$this->supplier = $supplier;
return $this;
}
public function isManufacturer(): bool
{
return $this->manufacturer;
}
public function setManufacturer(bool $manufacturer): PartSearchFilter
{
$this->manufacturer = $manufacturer;
return $this;
}
public function isFootprint(): bool
{
return $this->footprint;
}
public function setFootprint(bool $footprint): PartSearchFilter
{
$this->footprint = $footprint;
return $this;
}
public function isComment(): bool
{
return $this->comment;
}
public function setComment(bool $comment): PartSearchFilter
{
$this->comment = $comment;
return $this;
}
}