Completed
Push — master ( f40214...4c5b5b )
by Jan
03:55
created

PartsDataTable::buildCriteria()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 8
nop 2
dl 0
loc 21
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * part-db version 0.1
4
 * Copyright (C) 2005 Christoph Lechner
5
 * http://www.cl-projects.de/.
6
 *
7
 * part-db version 0.2+
8
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
9
 * http://code.google.com/p/part-db/
10
 *
11
 * Part-DB Version 0.4+
12
 * Copyright (C) 2016 - 2019 Jan Böhmer
13
 * https://github.com/jbtronics
14
 *
15
 * This program is free software; you can redistribute it and/or
16
 * modify it under the terms of the GNU General Public License
17
 * as published by the Free Software Foundation; either version 2
18
 * of the License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program; if not, write to the Free Software
27
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
28
 */
29
30
namespace App\DataTables;
31
32
use App\Entity\Parts\Category;
33
use App\Entity\Parts\Part;
34
use App\Services\EntityURLGenerator;
35
use Doctrine\ORM\QueryBuilder;
36
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\SearchCriteriaProvider;
37
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
38
use Omines\DataTablesBundle\Column\TextColumn;
39
use Omines\DataTablesBundle\DataTable;
40
use Omines\DataTablesBundle\DataTableTypeInterface;
41
42
class PartsDataTable implements DataTableTypeInterface
43
{
44
    /**
45
     * @var EntityURLGenerator
46
     */
47
    protected $urlGenerator;
48
49
    public function __construct(EntityURLGenerator $urlGenerator)
50
    {
51
        $this->urlGenerator = $urlGenerator;
52
    }
53
54
    protected function buildCriteria(QueryBuilder $builder, array $options)
55
    {
56
        if (isset($options['category'])) {
57
            $em = $builder->getEntityManager();
58
            $category = $options['category'];
59
            $repo = $em->getRepository(Category::class);
60
            $list = $repo->toNodesList($category);
61
            $list[] = $category;
62
63
            $builder->andWhere('part.category IN (:cid)')->setParameter('cid', $list);
64
        }
65
66
        if (isset($options['tag'])) {
67
            $builder->andWhere('part.tags LIKE :tag')->setParameter('tag', '%' . $options['tag'] . '%');
68
        }
69
70
        dump($options['search']);
71
72
        if (isset($options['search'])) {
73
            $builder->AndWhere('part.name LIKE :search')->orWhere('part.description LIKE :search')->orWhere('part.comment LIKE :search')
74
                ->setParameter('search', '%' . $options['search'] . '%');
75
        }
76
    }
77
78
    /**
79
     * @param DataTable $dataTable
80
     * @param array     $options
81
     */
82
    public function configure(DataTable $dataTable, array $options)
83
    {
84
        $dataTable//->add("id", TextColumn::class)
85
        ->add('name', TextColumn::class, ['label' => 'name.label',
86
            'render' => function ($value, Part $context) {
87
                return $this->urlGenerator->infoHTML($context);
88
            }, ])
89
            ->add('description', TextColumn::class, ['label' => 'description.label'])
90
            ->add('category', TextColumn::class, ['field' => 'category.name', 'label' => 'category.label'])
91
            ->add('amountSum', TextColumn::class, ['label' => 'instock.label_short'])
92
            ->add('minamount', TextColumn::class, ['label' => 'mininstock.label_short'])
93
            //->add('storelocation', TextColumn::class, ['field' => 'storelocation.name', 'label' => 'storelocation.label'])
94
            ->addOrderBy('name')
95
            ->createAdapter(ORMAdapter::class, [
96
                'entity' => Part::class,
97
                'criteria' => [
98
                    function (QueryBuilder $builder) use ($options) {
99
                        $this->buildCriteria($builder, $options);
100
                    },
101
                    new SearchCriteriaProvider()
102
                ]
103
            ]);
104
    }
105
}
106