mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-04 23:44:33 +02:00
Implement searching logic using the new filter system and improved searching logic
This should also fix issue #169 as ordernr is now really searched. Also support for searching in MPN was added.
This commit is contained in:
parent
c854f8049d
commit
c3f144447f
4 changed files with 402 additions and 156 deletions
|
@ -50,6 +50,7 @@ use App\DataTables\Column\PartAttachmentsColumn;
|
|||
use App\DataTables\Column\PrettyBoolColumn;
|
||||
use App\DataTables\Column\TagsColumn;
|
||||
use App\DataTables\Filters\PartFilter;
|
||||
use App\DataTables\Filters\PartSearchFilter;
|
||||
use App\Entity\Parts\Category;
|
||||
use App\Entity\Parts\Footprint;
|
||||
use App\Entity\Parts\Manufacturer;
|
||||
|
@ -104,49 +105,12 @@ final class PartsDataTable implements DataTableTypeInterface
|
|||
public function configureOptions(OptionsResolver $optionsResolver): void
|
||||
{
|
||||
$optionsResolver->setDefaults([
|
||||
'category' => null,
|
||||
'footprint' => null,
|
||||
'manufacturer' => null,
|
||||
'storelocation' => null,
|
||||
'supplier' => null,
|
||||
'tag' => null,
|
||||
'search' => null,
|
||||
'filter' => null
|
||||
'filter' => null,
|
||||
'search' => null
|
||||
]);
|
||||
|
||||
$optionsResolver->setAllowedTypes('category', ['null', Category::class]);
|
||||
$optionsResolver->setAllowedTypes('footprint', ['null', Footprint::class]);
|
||||
$optionsResolver->setAllowedTypes('manufacturer', ['null', Manufacturer::class]);
|
||||
$optionsResolver->setAllowedTypes('supplier', ['null', Supplier::class]);
|
||||
$optionsResolver->setAllowedTypes('tag', ['null', 'string']);
|
||||
$optionsResolver->setAllowedTypes('search', ['null', 'string']);
|
||||
|
||||
//Configure search options
|
||||
$optionsResolver->setDefault('search_options', static function (OptionsResolver $resolver): void {
|
||||
$resolver->setDefaults([
|
||||
'name' => true,
|
||||
'category' => true,
|
||||
'description' => true,
|
||||
'store_location' => true,
|
||||
'comment' => true,
|
||||
'ordernr' => true,
|
||||
'supplier' => false,
|
||||
'manufacturer' => false,
|
||||
'footprint' => false,
|
||||
'tags' => false,
|
||||
'regex' => false,
|
||||
]);
|
||||
$resolver->setAllowedTypes('name', 'bool');
|
||||
$resolver->setAllowedTypes('category', 'bool');
|
||||
$resolver->setAllowedTypes('description', 'bool');
|
||||
$resolver->setAllowedTypes('store_location', 'bool');
|
||||
$resolver->setAllowedTypes('comment', 'bool');
|
||||
$resolver->setAllowedTypes('supplier', 'bool');
|
||||
$resolver->setAllowedTypes('manufacturer', 'bool');
|
||||
$resolver->setAllowedTypes('footprint', 'bool');
|
||||
$resolver->setAllowedTypes('tags', 'bool');
|
||||
$resolver->setAllowedTypes('regex', 'bool');
|
||||
});
|
||||
$optionsResolver->setAllowedTypes('filter', [PartFilter::class, 'null']);
|
||||
$optionsResolver->setAllowedTypes('search', [PartSearchFilter::class, 'null']);
|
||||
}
|
||||
|
||||
public function configure(DataTable $dataTable, array $options): void
|
||||
|
@ -365,101 +329,17 @@ final class PartsDataTable implements DataTableTypeInterface
|
|||
|
||||
private function buildCriteria(QueryBuilder $builder, array $options): void
|
||||
{
|
||||
//Apply the search criterias first
|
||||
if ($options['search'] instanceof PartSearchFilter) {
|
||||
$search = $options['search'];
|
||||
$search->apply($builder);
|
||||
}
|
||||
|
||||
//We do the most stuff here in the filter class
|
||||
if (isset($options['filter']) && $options['filter'] instanceof PartFilter) {
|
||||
if ($options['filter'] instanceof PartFilter) {
|
||||
$filter = $options['filter'];
|
||||
$filter->apply($builder);
|
||||
}
|
||||
|
||||
if (!empty($options['search'])) {
|
||||
if (!$options['search_options']['regex']) {
|
||||
//Dont show results, if no things are selected
|
||||
$builder->andWhere('0=1');
|
||||
$defined = false;
|
||||
if ($options['search_options']['name']) {
|
||||
$builder->orWhere('part.name LIKE :search');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['description']) {
|
||||
$builder->orWhere('part.description LIKE :search');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['comment']) {
|
||||
$builder->orWhere('part.comment LIKE :search');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['category']) {
|
||||
$builder->orWhere('category.name LIKE :search');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['manufacturer']) {
|
||||
$builder->orWhere('manufacturer.name LIKE :search');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['footprint']) {
|
||||
$builder->orWhere('footprint.name LIKE :search');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['tags']) {
|
||||
$builder->orWhere('part.tags LIKE :search');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['store_location']) {
|
||||
$builder->orWhere('storelocations.name LIKE :search');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['supplier']) {
|
||||
$builder->orWhere('suppliers.name LIKE :search');
|
||||
$defined = true;
|
||||
}
|
||||
|
||||
if ($defined) {
|
||||
$builder->setParameter('search', '%'.$options['search'].'%');
|
||||
}
|
||||
} else { //Use REGEX
|
||||
$builder->andWhere('0=1');
|
||||
$defined = false;
|
||||
if ($options['search_options']['name']) {
|
||||
$builder->orWhere('REGEXP(part.name, :search) = 1');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['description']) {
|
||||
$builder->orWhere('REGEXP(part.description, :search) = 1');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['comment']) {
|
||||
$builder->orWhere('REGEXP(part.comment, :search) = 1');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['category']) {
|
||||
$builder->orWhere('REGEXP(category.name, :search) = 1');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['manufacturer']) {
|
||||
$builder->orWhere('REGEXP(manufacturer.name, :search) = 1');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['footprint']) {
|
||||
$builder->orWhere('REGEXP(footprint.name, :search) = 1');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['tags']) {
|
||||
$builder->orWhere('REGEXP(part.tags, :search) = 1');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['store_location']) {
|
||||
$builder->orWhere('REGEXP(storelocations.name, :search) = 1');
|
||||
$defined = true;
|
||||
}
|
||||
if ($options['search_options']['supplier']) {
|
||||
$builder->orWhere('REGEXP(suppliers.name, :search) = 1');
|
||||
$defined = true;
|
||||
}
|
||||
|
||||
if ($defined) {
|
||||
$builder->setParameter('search', $options['search']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue