Issues (257)

src/DataTables/ProjectBomEntriesDataTable.php (2 issues)

Severity
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;
22
23
use App\DataTables\Column\EntityColumn;
24
use App\DataTables\Column\LocaleDateTimeColumn;
25
use App\DataTables\Column\MarkdownColumn;
26
use App\DataTables\Column\SelectColumn;
27
use App\DataTables\Helpers\PartDataTableHelper;
28
use App\Entity\Attachments\Attachment;
29
use App\Entity\Parts\Part;
30
use App\Entity\ProjectSystem\ProjectBOMEntry;
31
use App\Services\EntityURLGenerator;
32
use App\Services\Formatters\AmountFormatter;
33
use Doctrine\ORM\QueryBuilder;
34
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\SearchCriteriaProvider;
35
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
36
use Omines\DataTablesBundle\Column\TextColumn;
37
use Omines\DataTablesBundle\DataTable;
38
use Omines\DataTablesBundle\DataTableTypeInterface;
39
use Symfony\Contracts\Translation\TranslatorInterface;
40
41
class ProjectBomEntriesDataTable implements DataTableTypeInterface
42
{
43
    protected TranslatorInterface $translator;
44
    protected PartDataTableHelper $partDataTableHelper;
45
    protected EntityURLGenerator $entityURLGenerator;
46
    protected AmountFormatter $amountFormatter;
47
48
    public function __construct(TranslatorInterface $translator, PartDataTableHelper $partDataTableHelper,
49
        EntityURLGenerator $entityURLGenerator, AmountFormatter $amountFormatter)
50
    {
51
        $this->translator = $translator;
52
        $this->partDataTableHelper = $partDataTableHelper;
53
        $this->entityURLGenerator = $entityURLGenerator;
54
        $this->amountFormatter = $amountFormatter;
55
    }
56
57
58
    public function configure(DataTable $dataTable, array $options)
59
    {
60
        $dataTable
61
            //->add('select', SelectColumn::class)
62
            ->add('picture', TextColumn::class, [
63
                'label' => '',
64
                'className' => 'no-colvis',
65
                'render' => function ($value, ProjectBOMEntry $context) {
66
                    if($context->getPart() === null) {
67
                        return '';
68
                    }
69
                    return $this->partDataTableHelper->renderPicture($context->getPart());
70
                },
71
            ])
72
73
            ->add('id', TextColumn::class, [
74
                'label' => $this->translator->trans('part.table.id'),
75
                'visible' => false,
76
            ])
77
78
            ->add('quantity', TextColumn::class, [
79
                'label' => $this->translator->trans('project.bom.quantity'),
80
                'className' => 'text-center',
81
                'render' => function ($value, ProjectBOMEntry $context) {
82
                    //If we have a non-part entry, only show the rounded quantity
83
                    if ($context->getPart() === null) {
84
                        return round($context->getQuantity());
85
                    }
86
                    //Otherwise use the unit of the part to format the quantity
87
                    return $this->amountFormatter->format($context->getQuantity(), $context->getPart()->getPartUnit());
88
                },
89
            ])
90
91
            ->add('name', TextColumn::class, [
92
                'label' => $this->translator->trans('part.table.name'),
93
                'orderable' => false,
94
                'render' => function ($value, ProjectBOMEntry $context) {
95
                    if($context->getPart() === null) {
96
                        return $context->getName();
97
                    }
98
                    if($context->getPart() !== null) {
99
                        $tmp = $this->partDataTableHelper->renderName($context->getPart());
100
                        if(!empty($context->getName())) {
101
                            $tmp .= '<br><b>'.htmlspecialchars($context->getName()).'</b>';
102
                        }
103
                        return $tmp;
104
                    }
105
                    throw new \Exception('This should never happen!');
106
                },
107
            ])
108
109
            ->add('description', MarkdownColumn::class, [
110
                'label' => $this->translator->trans('part.table.description'),
111
                'data' => function (ProjectBOMEntry $context) {
112
                    if($context->getPart() !== null) {
113
                        return $context->getPart()->getDescription();
114
                    }
115
                    //For non-part BOM entries show the comment field
116
                    return $context->getComment();
117
                },
118
            ])
119
120
121
            ->add('category', EntityColumn::class, [
122
                'label' => $this->translator->trans('part.table.category'),
123
                'property' => 'part.category',
124
            ])
125
            ->add('footprint', EntityColumn::class, [
126
                'property' => 'part.footprint',
127
                'label' => $this->translator->trans('part.table.footprint'),
128
            ])
129
130
            ->add('manufacturer', EntityColumn::class, [
131
                'property' => 'part.manufacturer',
132
                'label' => $this->translator->trans('part.table.manufacturer'),
133
            ])
134
135
            ->add('mountnames', TextColumn::class, [
136
                'label' => 'project.bom.mountnames',
137
                'render' => function ($value, ProjectBOMEntry $context) {
138
                    $html = '';
139
140
                    foreach (explode(',', $context->getMountnames()) as $mountname) {
141
                        $html .= sprintf('<span class="badge badge-secondary bg-secondary">%s</span> ', htmlspecialchars($mountname));
142
                    }
143
                    return $html;
144
                },
145
            ])
146
147
148
            ->add('addedDate', LocaleDateTimeColumn::class, [
149
                'label' => $this->translator->trans('part.table.addedDate'),
150
                'visible' => false,
151
            ])
152
            ->add('lastModified', LocaleDateTimeColumn::class, [
153
                'label' => $this->translator->trans('part.table.lastModified'),
154
                'visible' => false,
155
            ])
156
        ;
157
158
        $dataTable->createAdapter(ORMAdapter::class, [
159
            'entity' => Attachment::class,
160
            'query' => function (QueryBuilder $builder) use ($options): void {
161
                $this->getQuery($builder, $options);
162
            },
163
            'criteria' => [
164
                function (QueryBuilder $builder) use ($options): void {
165
                    $this->buildCriteria($builder, $options);
166
                },
167
                new SearchCriteriaProvider(),
168
            ],
169
        ]);
170
    }
171
172
    private function getQuery(QueryBuilder $builder, array $options): void
173
    {
174
        $builder->select('bom_entry')
175
            ->addSelect('part')
176
            ->from(ProjectBOMEntry::class, 'bom_entry')
177
            ->leftJoin('bom_entry.part', 'part')
178
            ->where('bom_entry.project = :project')
179
            ->setParameter('project', $options['project'])
180
        ;
181
    }
182
183
    private function buildCriteria(QueryBuilder $builder, array $options): void
0 ignored issues
show
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

183
    private function buildCriteria(QueryBuilder $builder, /** @scrutinizer ignore-unused */ array $options): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $builder is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

183
    private function buildCriteria(/** @scrutinizer ignore-unused */ QueryBuilder $builder, array $options): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
184
    {
185
186
    }
187
}