2019-10-04 18:06:37 +02:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-10-04 18:06:37 +02:00
|
|
|
namespace App\DataTables;
|
|
|
|
|
|
|
|
use App\DataTables\Column\LocaleDateTimeColumn;
|
2022-09-09 00:46:12 +02:00
|
|
|
use App\DataTables\Column\PrettyBoolColumn;
|
2022-09-11 02:00:22 +02:00
|
|
|
use App\DataTables\Filters\AttachmentFilter;
|
2019-10-04 18:06:37 +02:00
|
|
|
use App\Entity\Attachments\Attachment;
|
2022-12-17 01:09:47 +01:00
|
|
|
use App\Entity\LogSystem\AbstractLogEntry;
|
2019-10-19 23:29:51 +02:00
|
|
|
use App\Services\Attachments\AttachmentManager;
|
2019-10-05 20:30:27 +02:00
|
|
|
use App\Services\Attachments\AttachmentURLGenerator;
|
2019-10-04 18:06:37 +02:00
|
|
|
use App\Services\ElementTypeNameGenerator;
|
|
|
|
use App\Services\EntityURLGenerator;
|
|
|
|
use Doctrine\ORM\QueryBuilder;
|
2022-09-11 02:00:22 +02:00
|
|
|
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\SearchCriteriaProvider;
|
2019-10-04 18:06:37 +02:00
|
|
|
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
|
|
|
|
use Omines\DataTablesBundle\Column\TextColumn;
|
|
|
|
use Omines\DataTablesBundle\DataTable;
|
|
|
|
use Omines\DataTablesBundle\DataTableTypeInterface;
|
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
|
|
2020-01-05 22:49:00 +01:00
|
|
|
final class AttachmentDataTable implements DataTableTypeInterface
|
2019-10-04 18:06:37 +02:00
|
|
|
{
|
2022-09-18 22:59:31 +02:00
|
|
|
private TranslatorInterface $translator;
|
|
|
|
private EntityURLGenerator $entityURLGenerator;
|
|
|
|
private AttachmentManager $attachmentHelper;
|
|
|
|
private ElementTypeNameGenerator $elementTypeNameGenerator;
|
|
|
|
private AttachmentURLGenerator $attachmentURLGenerator;
|
2019-10-04 18:06:37 +02:00
|
|
|
|
|
|
|
public function __construct(TranslatorInterface $translator, EntityURLGenerator $entityURLGenerator,
|
2019-10-19 23:29:51 +02:00
|
|
|
AttachmentManager $attachmentHelper, AttachmentURLGenerator $attachmentURLGenerator,
|
2019-10-05 20:30:27 +02:00
|
|
|
ElementTypeNameGenerator $elementTypeNameGenerator)
|
2019-10-04 18:06:37 +02:00
|
|
|
{
|
|
|
|
$this->translator = $translator;
|
|
|
|
$this->entityURLGenerator = $entityURLGenerator;
|
|
|
|
$this->attachmentHelper = $attachmentHelper;
|
|
|
|
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
|
2019-10-05 20:30:27 +02:00
|
|
|
$this->attachmentURLGenerator = $attachmentURLGenerator;
|
2019-10-04 18:06:37 +02:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
public function configure(DataTable $dataTable, array $options): void
|
2019-10-04 18:06:37 +02:00
|
|
|
{
|
2022-12-17 01:09:47 +01:00
|
|
|
$dataTable->add('$$rowClass', TextColumn::class, [
|
|
|
|
'label' => '',
|
|
|
|
'className' => 'no-colvis',
|
|
|
|
'visible' => false,
|
|
|
|
'render' => function ($value, Attachment $context) {
|
|
|
|
//Mark attachments with missing files yellow
|
|
|
|
if(!$this->attachmentHelper->isFileExisting($context)){
|
|
|
|
return 'table-warning';
|
|
|
|
}
|
|
|
|
|
|
|
|
return ''; //Default coloring otherwise
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
2019-10-04 18:06:37 +02:00
|
|
|
$dataTable->add('picture', TextColumn::class, [
|
|
|
|
'label' => '',
|
2022-09-11 00:46:12 +02:00
|
|
|
'className' => 'no-colvis',
|
2019-10-04 18:06:37 +02:00
|
|
|
'render' => function ($value, Attachment $context) {
|
|
|
|
if ($context->isPicture()
|
2020-08-21 21:36:22 +02:00
|
|
|
&& !$context->isExternal()
|
2019-10-04 18:06:37 +02:00
|
|
|
&& $this->attachmentHelper->isFileExisting($context)) {
|
2020-05-27 21:54:08 +02:00
|
|
|
$title = htmlspecialchars($context->getName());
|
|
|
|
if ($context->getFilename()) {
|
2020-08-21 21:36:22 +02:00
|
|
|
$title .= ' ('.htmlspecialchars($context->getFilename()).')';
|
2020-05-27 21:54:08 +02:00
|
|
|
}
|
|
|
|
|
2019-10-04 18:06:37 +02:00
|
|
|
return sprintf(
|
2022-07-29 23:54:49 +02:00
|
|
|
'<img alt="%s" src="%s" data-thumbnail="%s" class="%s" data-title="%s" data-controller="elements--hoverpic">',
|
2019-10-04 18:06:37 +02:00
|
|
|
'Part image',
|
2019-10-05 20:30:27 +02:00
|
|
|
$this->attachmentURLGenerator->getThumbnailURL($context),
|
2019-10-06 15:44:19 +02:00
|
|
|
$this->attachmentURLGenerator->getThumbnailURL($context, 'thumbnail_md'),
|
2020-05-27 21:54:08 +02:00
|
|
|
'img-fluid hoverpic',
|
|
|
|
$title
|
2019-10-04 18:06:37 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
2019-11-09 00:47:20 +01:00
|
|
|
},
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable->add('name', TextColumn::class, [
|
2020-01-04 22:51:09 +01:00
|
|
|
'label' => 'attachment.edit.name',
|
2019-10-04 18:06:37 +02:00
|
|
|
'render' => function ($value, Attachment $context) {
|
|
|
|
//Link to external source
|
|
|
|
if ($context->isExternal()) {
|
|
|
|
return sprintf(
|
|
|
|
'<a href="%s" class="link-external">%s</a>',
|
|
|
|
htmlspecialchars($context->getURL()),
|
|
|
|
htmlspecialchars($value)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->attachmentHelper->isFileExisting($context)) {
|
|
|
|
return sprintf(
|
|
|
|
'<a href="%s" target="_blank" data-no-ajax>%s</a>',
|
|
|
|
$this->entityURLGenerator->viewURL($context),
|
|
|
|
htmlspecialchars($value)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
2019-11-09 00:47:20 +01:00
|
|
|
},
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable->add('attachment_type', TextColumn::class, [
|
2020-01-04 22:51:09 +01:00
|
|
|
'label' => 'attachment.table.type',
|
2019-10-04 18:06:37 +02:00
|
|
|
'field' => 'attachment_type.name',
|
|
|
|
'render' => function ($value, Attachment $context) {
|
|
|
|
return sprintf(
|
|
|
|
'<a href="%s">%s</a>',
|
|
|
|
$this->entityURLGenerator->editURL($context->getAttachmentType()),
|
|
|
|
htmlspecialchars($value)
|
|
|
|
);
|
2019-11-09 00:47:20 +01:00
|
|
|
},
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable->add('element', TextColumn::class, [
|
2020-01-04 22:51:09 +01:00
|
|
|
'label' => 'attachment.table.element',
|
2019-10-04 18:06:37 +02:00
|
|
|
//'propertyPath' => 'element.name',
|
|
|
|
'render' => function ($value, Attachment $context) {
|
|
|
|
return sprintf(
|
|
|
|
'<a href="%s">%s</a>',
|
|
|
|
$this->entityURLGenerator->infoURL($context->getElement()),
|
|
|
|
$this->elementTypeNameGenerator->getTypeNameCombination($context->getElement(), true)
|
|
|
|
);
|
2019-11-09 00:47:20 +01:00
|
|
|
},
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable->add('filename', TextColumn::class, [
|
2020-01-04 22:51:09 +01:00
|
|
|
'label' => $this->translator->trans('attachment.table.filename'),
|
2019-11-09 00:47:20 +01:00
|
|
|
'propertyPath' => 'filename',
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable->add('filesize', TextColumn::class, [
|
2020-01-04 22:51:09 +01:00
|
|
|
'label' => $this->translator->trans('attachment.table.filesize'),
|
2019-10-04 18:06:37 +02:00
|
|
|
'render' => function ($value, Attachment $context) {
|
2022-12-17 01:09:47 +01:00
|
|
|
if ($context->isExternal()) {
|
|
|
|
return sprintf(
|
|
|
|
'<span class="badge bg-primary">
|
|
|
|
<i class="fas fa-globe fa-fw"></i>%s
|
|
|
|
</span>',
|
|
|
|
$this->translator->trans('attachment.external')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-04 18:06:37 +02:00
|
|
|
if ($this->attachmentHelper->isFileExisting($context)) {
|
|
|
|
return $this->attachmentHelper->getHumanFileSize($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf(
|
2022-08-21 20:39:18 +02:00
|
|
|
'<span class="badge bg-warning">
|
2019-10-04 18:06:37 +02:00
|
|
|
<i class="fas fa-exclamation-circle fa-fw"></i>%s
|
|
|
|
</span>',
|
|
|
|
$this->translator->trans('attachment.file_not_found')
|
|
|
|
);
|
2019-11-09 00:47:20 +01:00
|
|
|
},
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable
|
|
|
|
->add('addedDate', LocaleDateTimeColumn::class, [
|
2020-01-04 22:51:09 +01:00
|
|
|
'label' => 'part.table.addedDate',
|
2019-11-09 00:47:20 +01:00
|
|
|
'visible' => false,
|
2019-10-04 18:06:37 +02:00
|
|
|
])
|
|
|
|
->add('lastModified', LocaleDateTimeColumn::class, [
|
2020-01-04 22:51:09 +01:00
|
|
|
'label' => 'part.table.lastModified',
|
2019-11-09 00:47:20 +01:00
|
|
|
'visible' => false,
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
|
2022-09-09 00:46:12 +02:00
|
|
|
$dataTable->add('show_in_table', PrettyBoolColumn::class, [
|
2020-01-04 22:51:09 +01:00
|
|
|
'label' => 'attachment.edit.show_in_table',
|
2019-11-09 00:47:20 +01:00
|
|
|
'visible' => false,
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
|
2022-09-09 00:46:12 +02:00
|
|
|
$dataTable->add('isPicture', PrettyBoolColumn::class, [
|
2020-01-04 22:51:09 +01:00
|
|
|
'label' => 'attachment.edit.isPicture',
|
2019-10-04 18:06:37 +02:00
|
|
|
'visible' => false,
|
2019-11-09 00:47:20 +01:00
|
|
|
'propertyPath' => 'picture',
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
|
2022-09-09 00:46:12 +02:00
|
|
|
$dataTable->add('is3DModel', PrettyBoolColumn::class, [
|
2020-01-04 22:51:09 +01:00
|
|
|
'label' => 'attachment.edit.is3DModel',
|
2019-10-04 18:06:37 +02:00
|
|
|
'visible' => false,
|
2019-11-09 00:47:20 +01:00
|
|
|
'propertyPath' => '3dmodel',
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
|
2022-09-09 00:46:12 +02:00
|
|
|
$dataTable->add('isBuiltin', PrettyBoolColumn::class, [
|
2020-01-04 22:51:09 +01:00
|
|
|
'label' => 'attachment.edit.isBuiltin',
|
2019-10-04 18:06:37 +02:00
|
|
|
'visible' => false,
|
2019-11-09 00:47:20 +01:00
|
|
|
'propertyPath' => 'builtin',
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable->createAdapter(ORMAdapter::class, [
|
|
|
|
'entity' => Attachment::class,
|
2020-01-05 15:46:58 +01:00
|
|
|
'query' => function (QueryBuilder $builder): void {
|
2019-10-04 18:06:37 +02:00
|
|
|
$this->getQuery($builder);
|
|
|
|
},
|
2022-09-11 02:00:22 +02:00
|
|
|
'criteria' => [
|
|
|
|
function (QueryBuilder $builder) use ($options): void {
|
|
|
|
$this->buildCriteria($builder, $options);
|
|
|
|
},
|
|
|
|
new SearchCriteriaProvider(),
|
|
|
|
],
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
}
|
2020-01-05 15:46:58 +01:00
|
|
|
|
2020-02-01 16:17:20 +01:00
|
|
|
private function getQuery(QueryBuilder $builder): void
|
2020-01-05 15:46:58 +01:00
|
|
|
{
|
2022-09-25 14:43:15 +02:00
|
|
|
$builder->select('attachment')
|
2020-01-05 15:46:58 +01:00
|
|
|
->addSelect('attachment_type')
|
|
|
|
//->addSelect('element')
|
|
|
|
->from(Attachment::class, 'attachment')
|
|
|
|
->leftJoin('attachment.attachment_type', 'attachment_type');
|
|
|
|
//->leftJoin('attachment.element', 'element');
|
|
|
|
}
|
2022-09-11 02:00:22 +02:00
|
|
|
|
|
|
|
private function buildCriteria(QueryBuilder $builder, array $options): void
|
|
|
|
{
|
|
|
|
//We do the most stuff here in the filter class
|
|
|
|
if (isset($options['filter'])) {
|
|
|
|
if(!$options['filter'] instanceof AttachmentFilter) {
|
|
|
|
throw new \Exception('filter must be an instance of AttachmentFilter!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$filter = $options['filter'];
|
|
|
|
$filter->apply($builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|