Completed
Push — master ( 8dc9c7...c3fd32 )
by Jan
05:27
created

PartsDataTable::buildCriteria()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 24
c 1
b 0
f 0
nc 32
nop 2
dl 0
loc 38
rs 8.9137
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\DataTables\Column\EntityColumn;
33
use App\DataTables\Column\LocaleDateTimeColumn;
34
use App\Entity\Parts\Category;
35
use App\Entity\Parts\Footprint;
36
use App\Entity\Parts\Manufacturer;
37
use App\Entity\Parts\Part;
38
use App\Services\EntityURLGenerator;
39
use Doctrine\ORM\QueryBuilder;
40
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\SearchCriteriaProvider;
41
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
42
use Omines\DataTablesBundle\Column\BoolColumn;
43
use Omines\DataTablesBundle\Column\DateTimeColumn;
44
use Omines\DataTablesBundle\Column\MapColumn;
45
use Omines\DataTablesBundle\Column\TextColumn;
46
use Omines\DataTablesBundle\DataTable;
47
use Omines\DataTablesBundle\DataTableTypeInterface;
48
use Symfony\Contracts\Translation\TranslatorInterface;
49
50
class PartsDataTable implements DataTableTypeInterface
51
{
52
    /**
53
     * @var EntityURLGenerator
54
     */
55
    protected $urlGenerator;
56
    protected $translator;
57
58
    public function __construct(EntityURLGenerator $urlGenerator, TranslatorInterface $translator)
59
    {
60
        $this->urlGenerator = $urlGenerator;
61
        $this->translator = $translator;
62
    }
63
64
    protected function getQuery(QueryBuilder $builder)
65
    {
66
        $builder->select('part')
67
            ->addSelect('category')
68
            ->addSelect('footprint')
69
            ->addSelect('manufacturer')
70
            ->addSelect('partUnit')
71
            ->from(Part::class, 'part')
72
            ->leftJoin('part.category', 'category')
73
            ->leftJoin('part.footprint', 'footprint')
74
            ->leftJoin('part.manufacturer', 'manufacturer')
75
            ->leftJoin('part.partUnit', 'partUnit');
76
    }
77
78
    protected function buildCriteria(QueryBuilder $builder, array $options)
79
    {
80
        $em = $builder->getEntityManager();
81
82
        if (isset($options['category'])) {
83
            $category = $options['category'];
84
            $repo = $em->getRepository(Category::class);
85
            $list = $repo->toNodesList($category);
86
            $list[] = $category;
87
88
            $builder->andWhere('part.category IN (:cid)')->setParameter('cid', $list);
89
        }
90
91
        if (isset($options['footprint'])) {
92
            $category = $options['footprint'];
93
            $repo = $em->getRepository(Footprint::class);
94
            $list = $repo->toNodesList($category);
95
            $list[] = $category;
96
97
            $builder->andWhere('part.footprint IN (:cid)')->setParameter('cid', $list);
98
        }
99
100
        if (isset($options['manufacturer'])) {
101
            $category = $options['manufacturer'];
102
            $repo = $em->getRepository(Manufacturer::class);
103
            $list = $repo->toNodesList($category);
104
            $list[] = $category;
105
106
            $builder->andWhere('part.manufacturer IN (:cid)')->setParameter('cid', $list);
107
        }
108
109
        if (isset($options['tag'])) {
110
            $builder->andWhere('part.tags LIKE :tag')->setParameter('tag', '%' . $options['tag'] . '%');
111
        }
112
113
        if (isset($options['search'])) {
114
            $builder->AndWhere('part.name LIKE :search')->orWhere('part.description LIKE :search')->orWhere('part.comment LIKE :search')
115
                ->setParameter('search', '%' . $options['search'] . '%');
116
        }
117
    }
118
119
    /**
120
     * @param DataTable $dataTable
121
     * @param array     $options
122
     */
123
    public function configure(DataTable $dataTable, array $options)
124
    {
125
        $dataTable
126
            ->add('name', TextColumn::class, [
127
                'label' => $this->translator->trans('part.table.name'),
128
                'render' => function ($value, Part $context) {
129
                    return $this->urlGenerator->infoHTML($context);
130
                },
131
            ])
132
            ->add('id', TextColumn::class, [
133
                'label' => $this->translator->trans('part.table.id'),
134
                'visible' => false
135
            ])
136
            ->add('description', TextColumn::class, [
137
                'label' => $this->translator->trans('part.table.description'),
138
            ])
139
            ->add('category', EntityColumn::class, [
140
                'label' => $this->translator->trans('part.table.category'),
141
                'property' => 'category'
142
            ])
143
            ->add('footprint', EntityColumn::class, [
144
                'property' => 'footprint',
145
                'label' => $this->translator->trans('part.table.footprint')
146
            ])
147
            ->add('manufacturer', EntityColumn::class, [
148
                'property' => 'manufacturer',
149
                'label' => $this->translator->trans('part.table.manufacturer')
150
            ])
151
            //->add('footprint', TextColumn::class, ['field' => 'footprint.name'])
152
            //->add('manufacturer', TextColumn::class, ['field' => 'manufacturer.name' ])
153
            //->add('amountSum', TextColumn::class, ['label' => 'instock.label_short'])
154
            ->add('amount', TextColumn::class, [
155
                'label' => $this->translator->trans('part.table.amount'),
156
                'propertyPath' => 'amountSum'
157
            ])
158
            ->add('minamount', TextColumn::class, [
159
                'label' => $this->translator->trans('part.table.minamount'),
160
                'visible' => false
161
            ])
162
            ->add('partUnit', TextColumn::class, [
163
                'field' => 'partUnit.name',
164
                'label' => $this->translator->trans('part.table.partUnit'),
165
                'visible' => false
166
            ])
167
            ->add('addedDate', LocaleDateTimeColumn::class, [
168
                'label' => $this->translator->trans('part.table.addedDate'),
169
                'visible' => false
170
            ])
171
            ->add('lastModified', LocaleDateTimeColumn::class, [
172
                'label' => $this->translator->trans('part.table.lastModified'),
173
                'visible' => false
174
            ])
175
            ->add('needs_review', BoolColumn::class, [
176
                'label' => $this->translator->trans('part.table.needsReview'),
177
                'trueValue' => $this->translator->trans('true'),
178
                'falseValue' => $this->translator->trans('false'),
179
                'nullValue' => '',
180
                'visible' => false
181
            ])
182
            ->add('favorite', BoolColumn::class, [
183
                'label' => $this->translator->trans('part.table.favorite'),
184
                'trueValue' => $this->translator->trans('true'),
185
                'falseValue' => $this->translator->trans('false'),
186
                'nullValue' => '',
187
                'visible' => false
188
            ])
189
            ->add('manufacturing_status', MapColumn::class, [
190
                'label' => $this->translator->trans('part.table.manufacturingStatus'),
191
                'visible' => false,
192
                'default' => $this->translator->trans('m_status.unknown'),
193
                'map' => [
194
                    '' => $this->translator->trans('m_status.unknown'),
195
                    'announced' => $this->translator->trans('m_status.announced'),
196
                    'active' => $this->translator->trans('m_status.active'),
197
                    'nrfnd' => $this->translator->trans('m_status.nrfnd'),
198
                    'eol' => $this->translator->trans('m_status.eol'),
199
                    'discontinued' => $this->translator->trans('m_status.discontinued')
200
                ]
201
            ])
202
            ->add('manufacturer_product_number', TextColumn::class, [
203
                'label' => $this->translator->trans('part.table.mpn'),
204
                'visible' => false
205
            ])
206
            ->add('mass', TextColumn::class, [
207
                'label' => $this->translator->trans('part.table.mass'),
208
                'visible' => false
209
            ])
210
            ->add('tags', TextColumn::class, [
211
                'label' => $this->translator->trans('part.table.tags'),
212
                'visible' => false
213
            ])
214
215
            ->addOrderBy('name')
216
            ->createAdapter(ORMAdapter::class, [
217
                'query' => function(QueryBuilder $builder) {
218
                    $this->getQuery($builder);
219
                },
220
                'entity' => Part::class,
221
                'criteria' => [
222
                    function (QueryBuilder $builder) use ($options) {
223
                        $this->buildCriteria($builder, $options);
224
                    },
225
                    new SearchCriteriaProvider()
226
                ]
227
            ]);
228
    }
229
}
230