|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics) |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
|
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace App\DataTables\Filters; |
|
22
|
|
|
|
|
23
|
|
|
use Doctrine\ORM\Query\Expr; |
|
24
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
25
|
|
|
|
|
26
|
|
|
class PartSearchFilter implements FilterInterface |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** @var string The string to query for */ |
|
30
|
|
|
protected string $keyword; |
|
31
|
|
|
|
|
32
|
|
|
/** @var boolean Whether to use regex for searching */ |
|
33
|
|
|
protected bool $regex = false; |
|
34
|
|
|
|
|
35
|
|
|
/** @var bool Use name field for searching */ |
|
36
|
|
|
protected bool $name = true; |
|
37
|
|
|
|
|
38
|
|
|
/** @var bool Use category name for searching */ |
|
39
|
|
|
protected bool $category = true; |
|
40
|
|
|
|
|
41
|
|
|
/** @var bool Use description for searching */ |
|
42
|
|
|
protected bool $description = true; |
|
43
|
|
|
|
|
44
|
|
|
/** @var bool Use tags for searching */ |
|
45
|
|
|
protected bool $tags = true; |
|
46
|
|
|
|
|
47
|
|
|
/** @var bool Use storelocation name for searching */ |
|
48
|
|
|
protected bool $storelocation = true; |
|
49
|
|
|
|
|
50
|
|
|
/** @var bool Use comment field for searching */ |
|
51
|
|
|
protected bool $comment = true; |
|
52
|
|
|
|
|
53
|
|
|
/** @var bool Use ordernr for searching */ |
|
54
|
|
|
protected bool $ordernr = true; |
|
55
|
|
|
|
|
56
|
|
|
/** @var bool Use manufacturer product name for searching */ |
|
57
|
|
|
protected bool $mpn = true; |
|
58
|
|
|
|
|
59
|
|
|
/** @var bool Use supplier name for searching */ |
|
60
|
|
|
protected bool $supplier = false; |
|
61
|
|
|
|
|
62
|
|
|
/** @var bool Use manufacturer name for searching */ |
|
63
|
|
|
protected bool $manufacturer = false; |
|
64
|
|
|
|
|
65
|
|
|
/** @var bool Use footprint name for searching */ |
|
66
|
|
|
protected bool $footprint = false; |
|
67
|
|
|
|
|
68
|
|
|
public function __construct(string $query) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->keyword = $query; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function getFieldsToSearch(): array |
|
74
|
|
|
{ |
|
75
|
|
|
$fields_to_search = []; |
|
76
|
|
|
|
|
77
|
|
|
if($this->name) { |
|
78
|
|
|
$fields_to_search[] = 'part.name'; |
|
79
|
|
|
} |
|
80
|
|
|
if($this->category) { |
|
81
|
|
|
$fields_to_search[] = 'category.name'; |
|
82
|
|
|
} |
|
83
|
|
|
if($this->description) { |
|
84
|
|
|
$fields_to_search[] = 'part.description'; |
|
85
|
|
|
} |
|
86
|
|
|
if($this->tags) { |
|
87
|
|
|
$fields_to_search[] = 'part.tags'; |
|
88
|
|
|
} |
|
89
|
|
|
if($this->storelocation) { |
|
90
|
|
|
$fields_to_search[] = 'storelocations.name'; |
|
91
|
|
|
} |
|
92
|
|
|
if($this->ordernr) { |
|
93
|
|
|
$fields_to_search[] = 'orderdetails.supplierpartnr'; |
|
94
|
|
|
} |
|
95
|
|
|
if($this->mpn) { |
|
96
|
|
|
$fields_to_search[] = 'part.manufacturer_product_url'; |
|
97
|
|
|
} |
|
98
|
|
|
if($this->supplier) { |
|
99
|
|
|
$fields_to_search[] = 'suppliers.name'; |
|
100
|
|
|
} |
|
101
|
|
|
if($this->manufacturer) { |
|
102
|
|
|
$fields_to_search[] = 'manufacturer.name'; |
|
103
|
|
|
} |
|
104
|
|
|
if($this->footprint) { |
|
105
|
|
|
$fields_to_search[] = 'footprint.name'; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $fields_to_search; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function apply(QueryBuilder $queryBuilder): void |
|
112
|
|
|
{ |
|
113
|
|
|
$fields_to_search = $this->getFieldsToSearch(); |
|
114
|
|
|
|
|
115
|
|
|
//If we have nothing to search for, do nothing |
|
116
|
|
|
if (empty($fields_to_search) || empty($this->keyword)) { |
|
117
|
|
|
return; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
//Convert the fields to search to a list of expressions |
|
121
|
|
|
$expressions = array_map(function (string $field) { |
|
122
|
|
|
if ($this->regex) { |
|
123
|
|
|
return sprintf("REGEXP(%s, :search_query) = 1", $field); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return sprintf("%s LIKE :search_query", $field); |
|
127
|
|
|
}, $fields_to_search); |
|
128
|
|
|
|
|
129
|
|
|
//Add Or concatation of the expressions to our query |
|
130
|
|
|
$queryBuilder->andWhere( |
|
131
|
|
|
$queryBuilder->expr()->orX(...$expressions) |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
|
|
//For regex we pass the query as is, for like we add % to the start and end as wildcards |
|
135
|
|
|
if ($this->regex) { |
|
136
|
|
|
$queryBuilder->setParameter('search_query', $this->keyword); |
|
137
|
|
|
} else { |
|
138
|
|
|
$queryBuilder->setParameter('search_query', '%' . $this->keyword . '%'); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getKeyword(): string |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->keyword; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param string $keyword |
|
152
|
|
|
* @return PartSearchFilter |
|
153
|
|
|
*/ |
|
154
|
|
|
public function setKeyword(string $keyword): PartSearchFilter |
|
155
|
|
|
{ |
|
156
|
|
|
$this->keyword = $keyword; |
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return bool |
|
162
|
|
|
*/ |
|
163
|
|
|
public function isRegex(): bool |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->regex; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param bool $regex |
|
170
|
|
|
* @return PartSearchFilter |
|
171
|
|
|
*/ |
|
172
|
|
|
public function setRegex(bool $regex): PartSearchFilter |
|
173
|
|
|
{ |
|
174
|
|
|
$this->regex = $regex; |
|
175
|
|
|
return $this; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @return bool |
|
180
|
|
|
*/ |
|
181
|
|
|
public function isName(): bool |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->name; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param bool $name |
|
188
|
|
|
* @return PartSearchFilter |
|
189
|
|
|
*/ |
|
190
|
|
|
public function setName(bool $name): PartSearchFilter |
|
191
|
|
|
{ |
|
192
|
|
|
$this->name = $name; |
|
193
|
|
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @return bool |
|
198
|
|
|
*/ |
|
199
|
|
|
public function isCategory(): bool |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->category; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param bool $category |
|
206
|
|
|
* @return PartSearchFilter |
|
207
|
|
|
*/ |
|
208
|
|
|
public function setCategory(bool $category): PartSearchFilter |
|
209
|
|
|
{ |
|
210
|
|
|
$this->category = $category; |
|
211
|
|
|
return $this; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @return bool |
|
216
|
|
|
*/ |
|
217
|
|
|
public function isDescription(): bool |
|
218
|
|
|
{ |
|
219
|
|
|
return $this->description; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @param bool $description |
|
224
|
|
|
* @return PartSearchFilter |
|
225
|
|
|
*/ |
|
226
|
|
|
public function setDescription(bool $description): PartSearchFilter |
|
227
|
|
|
{ |
|
228
|
|
|
$this->description = $description; |
|
229
|
|
|
return $this; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @return bool |
|
234
|
|
|
*/ |
|
235
|
|
|
public function isTags(): bool |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->tags; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @param bool $tags |
|
242
|
|
|
* @return PartSearchFilter |
|
243
|
|
|
*/ |
|
244
|
|
|
public function setTags(bool $tags): PartSearchFilter |
|
245
|
|
|
{ |
|
246
|
|
|
$this->tags = $tags; |
|
247
|
|
|
return $this; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* @return bool |
|
252
|
|
|
*/ |
|
253
|
|
|
public function isStorelocation(): bool |
|
254
|
|
|
{ |
|
255
|
|
|
return $this->storelocation; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* @param bool $storelocation |
|
260
|
|
|
* @return PartSearchFilter |
|
261
|
|
|
*/ |
|
262
|
|
|
public function setStorelocation(bool $storelocation): PartSearchFilter |
|
263
|
|
|
{ |
|
264
|
|
|
$this->storelocation = $storelocation; |
|
265
|
|
|
return $this; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @return bool |
|
270
|
|
|
*/ |
|
271
|
|
|
public function isOrdernr(): bool |
|
272
|
|
|
{ |
|
273
|
|
|
return $this->ordernr; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @param bool $ordernr |
|
278
|
|
|
* @return PartSearchFilter |
|
279
|
|
|
*/ |
|
280
|
|
|
public function setOrdernr(bool $ordernr): PartSearchFilter |
|
281
|
|
|
{ |
|
282
|
|
|
$this->ordernr = $ordernr; |
|
283
|
|
|
return $this; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @return bool |
|
288
|
|
|
*/ |
|
289
|
|
|
public function isMpn(): bool |
|
290
|
|
|
{ |
|
291
|
|
|
return $this->mpn; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* @param bool $mpn |
|
296
|
|
|
* @return PartSearchFilter |
|
297
|
|
|
*/ |
|
298
|
|
|
public function setMpn(bool $mpn): PartSearchFilter |
|
299
|
|
|
{ |
|
300
|
|
|
$this->mpn = $mpn; |
|
301
|
|
|
return $this; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* @return bool |
|
306
|
|
|
*/ |
|
307
|
|
|
public function isSupplier(): bool |
|
308
|
|
|
{ |
|
309
|
|
|
return $this->supplier; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* @param bool $supplier |
|
314
|
|
|
* @return PartSearchFilter |
|
315
|
|
|
*/ |
|
316
|
|
|
public function setSupplier(bool $supplier): PartSearchFilter |
|
317
|
|
|
{ |
|
318
|
|
|
$this->supplier = $supplier; |
|
319
|
|
|
return $this; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* @return bool |
|
324
|
|
|
*/ |
|
325
|
|
|
public function isManufacturer(): bool |
|
326
|
|
|
{ |
|
327
|
|
|
return $this->manufacturer; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* @param bool $manufacturer |
|
332
|
|
|
* @return PartSearchFilter |
|
333
|
|
|
*/ |
|
334
|
|
|
public function setManufacturer(bool $manufacturer): PartSearchFilter |
|
335
|
|
|
{ |
|
336
|
|
|
$this->manufacturer = $manufacturer; |
|
337
|
|
|
return $this; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* @return bool |
|
342
|
|
|
*/ |
|
343
|
|
|
public function isFootprint(): bool |
|
344
|
|
|
{ |
|
345
|
|
|
return $this->footprint; |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* @param bool $footprint |
|
350
|
|
|
* @return PartSearchFilter |
|
351
|
|
|
*/ |
|
352
|
|
|
public function setFootprint(bool $footprint): PartSearchFilter |
|
353
|
|
|
{ |
|
354
|
|
|
$this->footprint = $footprint; |
|
355
|
|
|
return $this; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* @return bool |
|
360
|
|
|
*/ |
|
361
|
|
|
public function isComment(): bool |
|
362
|
|
|
{ |
|
363
|
|
|
return $this->comment; |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
/** |
|
367
|
|
|
* @param bool $comment |
|
368
|
|
|
* @return PartSearchFilter |
|
369
|
|
|
*/ |
|
370
|
|
|
public function setComment(bool $comment): PartSearchFilter |
|
371
|
|
|
{ |
|
372
|
|
|
$this->comment = $comment; |
|
373
|
|
|
return $this; |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
|
|
377
|
|
|
} |