Completed
Push — master ( 2f0dc6...f53cc0 )
by Jan
05:37
created

AttachmentDataTable::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 7
rs 10
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\DataTables;
33
34
35
use App\DataTables\Column\LocaleDateTimeColumn;
36
use App\Entity\Attachments\Attachment;
37
use App\Entity\Attachments\FootprintAttachment;
38
use App\Entity\Parts\Part;
39
use App\Services\AttachmentHelper;
40
use App\Services\ElementTypeNameGenerator;
41
use App\Services\EntityURLGenerator;
42
use Doctrine\ORM\QueryBuilder;
43
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
44
use Omines\DataTablesBundle\Column\BoolColumn;
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 AttachmentDataTable implements DataTableTypeInterface
51
{
52
53
    protected $translator;
54
    protected $entityURLGenerator;
55
    protected $attachmentHelper;
56
    protected $elementTypeNameGenerator;
57
58
    public function __construct(TranslatorInterface $translator, EntityURLGenerator $entityURLGenerator,
59
                                AttachmentHelper $attachmentHelper, ElementTypeNameGenerator $elementTypeNameGenerator)
60
    {
61
        $this->translator = $translator;
62
        $this->entityURLGenerator = $entityURLGenerator;
63
        $this->attachmentHelper = $attachmentHelper;
64
        $this->elementTypeNameGenerator = $elementTypeNameGenerator;
65
    }
66
67
    protected function getQuery(QueryBuilder $builder)
68
    {
69
        $builder->distinct()->select('attachment')
70
            ->addSelect('attachment_type')
71
            //->addSelect('element')
72
            ->from(Attachment::class, 'attachment')
73
            ->leftJoin('attachment.attachment_type', 'attachment_type');
74
        //->leftJoin('attachment.element', 'element');
75
    }
76
77
    /**
78
     * @param DataTable $dataTable
79
     * @param array $options
80
     */
81
    public function configure(DataTable $dataTable, array $options)
82
    {
83
        $dataTable->add('picture', TextColumn::class, [
84
            'label' => '',
85
            'render' => function ($value, Attachment $context) {
86
                if ($context->isPicture()
87
                    && !$context->isExternal()
88
                    && $this->attachmentHelper->isFileExisting($context)) {
89
                    return sprintf(
90
                        '<img alt="%s" src="%s" class="%s">',
91
                        'Part image',
92
                        $this->entityURLGenerator->viewURL($context),
93
                        'img-fluid hoverpic'
94
                    );
95
                }
96
97
                return '';
98
            }
99
        ]);
100
101
        $dataTable->add('name', TextColumn::class, [
102
            'label' => $this->translator->trans('attachment.edit.name'),
103
            'render' => function ($value, Attachment $context) {
104
                //Link to external source
105
                if ($context->isExternal()) {
106
                    return sprintf(
107
                        '<a href="%s" class="link-external">%s</a>',
108
                        htmlspecialchars($context->getURL()),
109
                        htmlspecialchars($value)
110
                    );
111
                }
112
113
                if ($this->attachmentHelper->isFileExisting($context)) {
114
                    return sprintf(
115
                        '<a href="%s" target="_blank" data-no-ajax>%s</a>',
116
                        $this->entityURLGenerator->viewURL($context),
117
                        htmlspecialchars($value)
118
                    );
119
                }
120
121
                return $value;
122
            }
123
        ]);
124
125
        $dataTable->add('attachment_type', TextColumn::class, [
126
            'field' => 'attachment_type.name',
127
            'render' => function ($value, Attachment $context) {
128
                return sprintf(
129
                    '<a href="%s">%s</a>',
130
                    $this->entityURLGenerator->editURL($context->getAttachmentType()),
131
                    htmlspecialchars($value)
132
                );
133
            }
134
        ]);
135
136
        $dataTable->add('element', TextColumn::class, [
137
            'label' => $this->translator->trans('attachment.table.element'),
138
            //'propertyPath' => 'element.name',
139
            'render' => function ($value, Attachment $context) {
140
                return sprintf(
141
                    '<a href="%s">%s</a>',
142
                    $this->entityURLGenerator->infoURL($context->getElement()),
0 ignored issues
show
Bug introduced by
It seems like $context->getElement() can also be of type null; however, parameter $entity of App\Services\EntityURLGenerator::infoURL() does only seem to accept App\Entity\Base\DBElement, maybe add an additional type check? ( Ignorable by Annotation )

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

142
                    $this->entityURLGenerator->infoURL(/** @scrutinizer ignore-type */ $context->getElement()),
Loading history...
143
                    $this->elementTypeNameGenerator->getTypeNameCombination($context->getElement(), true)
0 ignored issues
show
Bug introduced by
It seems like $context->getElement() can also be of type null; however, parameter $entity of App\Services\ElementType...etTypeNameCombination() does only seem to accept App\Entity\Base\NamedDBElement, maybe add an additional type check? ( Ignorable by Annotation )

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

143
                    $this->elementTypeNameGenerator->getTypeNameCombination(/** @scrutinizer ignore-type */ $context->getElement(), true)
Loading history...
144
                );
145
            }
146
        ]);
147
148
        $dataTable->add('filename', TextColumn::class, [
149
            'propertyPath' => 'filename'
150
        ]);
151
152
        $dataTable->add('filesize', TextColumn::class, [
153
            'render' => function ($value, Attachment $context) {
154
                if ($this->attachmentHelper->isFileExisting($context)) {
155
                    return $this->attachmentHelper->getHumanFileSize($context);
156
                }
157
                if ($context->isExternal()) {
158
                    return '<i>' . $this->translator->trans('attachment.external') . '</i>';
159
                }
160
161
                return sprintf(
162
                    '<span class="badge badge-warning">
163
                        <i class="fas fa-exclamation-circle fa-fw"></i>%s
164
                        </span>',
165
                    $this->translator->trans('attachment.file_not_found')
166
                );
167
            }
168
        ]);
169
170
        $dataTable
171
            ->add('addedDate', LocaleDateTimeColumn::class, [
172
                'label' => $this->translator->trans('part.table.addedDate'),
173
                'visible' => false
174
            ])
175
            ->add('lastModified', LocaleDateTimeColumn::class, [
176
                'label' => $this->translator->trans('part.table.lastModified'),
177
                'visible' => false
178
            ]);
179
180
        $dataTable->add('show_in_table', BoolColumn::class, [
181
            'label' => $this->translator->trans('attachment.edit.show_in_table'),
182
            'trueValue' => $this->translator->trans('true'),
183
            'falseValue' => $this->translator->trans('false'),
184
            'nullValue' => '',
185
            'visible' => false
186
        ]);
187
188
        $dataTable->add('isPicture', BoolColumn::class, [
189
            'label' => $this->translator->trans('attachment.edit.isPicture'),
190
            'trueValue' => $this->translator->trans('true'),
191
            'falseValue' => $this->translator->trans('false'),
192
            'nullValue' => '',
193
            'visible' => false,
194
            'propertyPath' => 'picture'
195
        ]);
196
197
        $dataTable->add('is3DModel', BoolColumn::class, [
198
            'label' => $this->translator->trans('attachment.edit.is3DModel'),
199
            'trueValue' => $this->translator->trans('true'),
200
            'falseValue' => $this->translator->trans('false'),
201
            'nullValue' => '',
202
            'visible' => false,
203
            'propertyPath' => '3dmodel'
204
        ]);
205
206
        $dataTable->add('isBuiltin', BoolColumn::class, [
207
            'label' => $this->translator->trans('attachment.edit.isBuiltin'),
208
            'trueValue' => $this->translator->trans('true'),
209
            'falseValue' => $this->translator->trans('false'),
210
            'nullValue' => '',
211
            'visible' => false,
212
            'propertyPath' => 'builtin'
213
        ]);
214
215
        $dataTable->createAdapter(ORMAdapter::class, [
216
            'entity' => Attachment::class,
217
            'query' => function (QueryBuilder $builder) {
218
                $this->getQuery($builder);
219
            },
220
        ]);
221
    }
222
}