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-12-17 01:19:52 +01:00
|
|
|
use App\DataTables\Column\RowClassColumn;
|
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;
|
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;
|
2024-08-24 15:48:50 +02:00
|
|
|
use Omines\DataTablesBundle\Column\NumberColumn;
|
2019-10-04 18:06:37 +02:00
|
|
|
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
|
|
|
{
|
2023-06-11 14:15:46 +02:00
|
|
|
public function __construct(private readonly TranslatorInterface $translator, private readonly EntityURLGenerator $entityURLGenerator, private readonly AttachmentManager $attachmentHelper, private readonly AttachmentURLGenerator $attachmentURLGenerator, private readonly ElementTypeNameGenerator $elementTypeNameGenerator)
|
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:19:52 +01:00
|
|
|
$dataTable->add('dont_matter', RowClassColumn::class, [
|
2023-06-11 14:15:46 +02:00
|
|
|
'render' => function ($value, Attachment $context): string {
|
2025-02-22 17:29:14 +01:00
|
|
|
//Mark attachments yellow which have an internal file linked that doesn't exist
|
|
|
|
if($context->hasInternal() && !$this->attachmentHelper->isInternalFileExisting($context)){
|
2022-12-17 01:09:47 +01:00
|
|
|
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',
|
2023-06-11 14:15:46 +02:00
|
|
|
'render' => function ($value, Attachment $context): string {
|
2019-10-04 18:06:37 +02:00
|
|
|
if ($context->isPicture()
|
2025-02-22 17:29:14 +01:00
|
|
|
&& $this->attachmentHelper->isInternalFileExisting($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
|
|
|
]);
|
|
|
|
|
2024-08-24 15:48:50 +02:00
|
|
|
$dataTable->add('id', NumberColumn::class, [
|
|
|
|
'label' => $this->translator->trans('part.table.id'),
|
|
|
|
'visible' => false,
|
|
|
|
]);
|
|
|
|
|
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',
|
2024-06-17 21:38:16 +02:00
|
|
|
'orderField' => 'NATSORT(attachment.name)',
|
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',
|
2024-06-17 21:38:16 +02:00
|
|
|
'orderField' => 'NATSORT(attachment_type.name)',
|
2023-06-11 14:15:46 +02:00
|
|
|
'render' => fn($value, Attachment $context): string => sprintf(
|
|
|
|
'<a href="%s">%s</a>',
|
|
|
|
$this->entityURLGenerator->editURL($context->getAttachmentType()),
|
|
|
|
htmlspecialchars((string) $value)
|
|
|
|
),
|
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',
|
2023-06-11 14:15:46 +02:00
|
|
|
'render' => fn($value, Attachment $context): string => sprintf(
|
|
|
|
'<a href="%s">%s</a>',
|
|
|
|
$this->entityURLGenerator->infoURL($context->getElement()),
|
|
|
|
$this->elementTypeNameGenerator->getTypeNameCombination($context->getElement(), true)
|
|
|
|
),
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
|
2025-02-22 17:29:14 +01:00
|
|
|
$dataTable->add('internal_link', TextColumn::class, [
|
|
|
|
'label' => 'attachment.table.internal_file',
|
2019-11-09 00:47:20 +01:00
|
|
|
'propertyPath' => 'filename',
|
2025-02-22 17:41:41 +01:00
|
|
|
'orderField' => 'NATSORT(attachment.original_filename)',
|
2025-02-22 17:29:14 +01:00
|
|
|
'render' => function ($value, Attachment $context) {
|
|
|
|
if ($this->attachmentHelper->isInternalFileExisting($context)) {
|
|
|
|
return sprintf(
|
|
|
|
'<a href="%s" target="_blank" data-no-ajax>%s</a>',
|
|
|
|
$this->entityURLGenerator->viewURL($context),
|
|
|
|
htmlspecialchars($value)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable->add('external_link', TextColumn::class, [
|
|
|
|
'label' => 'attachment.table.external_link',
|
|
|
|
'propertyPath' => 'host',
|
2025-02-22 17:41:41 +01:00
|
|
|
'orderField' => 'attachment.external_path',
|
2025-02-22 17:29:14 +01:00
|
|
|
'render' => function ($value, Attachment $context) {
|
|
|
|
if ($context->hasExternal()) {
|
|
|
|
return sprintf(
|
2025-02-22 17:41:41 +01:00
|
|
|
'<a href="%s" class="link-external" title="%s" target="_blank" rel="noopener">%s</a>',
|
2025-02-22 17:29:14 +01:00
|
|
|
htmlspecialchars((string) $context->getExternalPath()),
|
2025-02-22 17:41:41 +01:00
|
|
|
htmlspecialchars((string) $context->getExternalPath()),
|
|
|
|
htmlspecialchars($value),
|
2025-02-22 17:29:14 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
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) {
|
2025-02-22 17:29:14 +01:00
|
|
|
if (!$context->hasInternal()) {
|
2022-12-17 01:09:47 +01:00
|
|
|
return sprintf(
|
|
|
|
'<span class="badge bg-primary">
|
|
|
|
<i class="fas fa-globe fa-fw"></i>%s
|
|
|
|
</span>',
|
2025-02-22 17:29:14 +01:00
|
|
|
$this->translator->trans('attachment.external_only')
|
2022-12-17 01:09:47 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-02-22 17:29:14 +01:00
|
|
|
if ($this->attachmentHelper->isInternalFileExisting($context)) {
|
|
|
|
return sprintf(
|
|
|
|
'<span class="badge bg-secondary">
|
|
|
|
<i class="fas fa-hdd fa-fw"></i> %s
|
|
|
|
</span>',
|
|
|
|
$this->attachmentHelper->getHumanFileSize($context)
|
|
|
|
);
|
2019-10-04 18:06:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-03-03 19:57:31 +01:00
|
|
|
throw new \RuntimeException('filter must be an instance of AttachmentFilter!');
|
2022-09-11 02:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$filter = $options['filter'];
|
|
|
|
$filter->apply($builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|