2019-03-05 23:52:45 +01: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).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* 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-03-05 23:52:45 +01:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-03-05 23:52:45 +01:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-03-05 23:52:45 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\DataTables;
|
|
|
|
|
2020-01-17 21:38:36 +01:00
|
|
|
use App\DataTables\Adapter\FetchJoinORMAdapter;
|
2019-09-07 15:23:03 +02:00
|
|
|
use App\DataTables\Column\EntityColumn;
|
2019-09-07 12:48:49 +02:00
|
|
|
use App\DataTables\Column\LocaleDateTimeColumn;
|
2019-11-10 19:43:55 +01:00
|
|
|
use App\DataTables\Column\MarkdownColumn;
|
2019-11-10 19:16:39 +01:00
|
|
|
use App\DataTables\Column\PartAttachmentsColumn;
|
2019-11-10 19:38:36 +01:00
|
|
|
use App\DataTables\Column\TagsColumn;
|
2019-09-05 22:27:18 +02:00
|
|
|
use App\Entity\Parts\Category;
|
2019-09-08 13:59:35 +02:00
|
|
|
use App\Entity\Parts\Footprint;
|
|
|
|
use App\Entity\Parts\Manufacturer;
|
2019-08-12 18:04:53 +02:00
|
|
|
use App\Entity\Parts\Part;
|
2019-09-08 17:30:58 +02:00
|
|
|
use App\Entity\Parts\Storelocation;
|
|
|
|
use App\Entity\Parts\Supplier;
|
2019-09-08 18:19:29 +02:00
|
|
|
use App\Services\AmountFormatter;
|
2019-10-05 20:30:27 +02:00
|
|
|
use App\Services\Attachments\AttachmentURLGenerator;
|
2019-10-03 14:53:50 +02:00
|
|
|
use App\Services\Attachments\PartPreviewGenerator;
|
2019-03-05 23:52:45 +01:00
|
|
|
use App\Services\EntityURLGenerator;
|
2020-01-02 22:55:28 +01:00
|
|
|
use App\Services\Trees\NodesListBuilder;
|
2019-03-05 23:52:45 +01:00
|
|
|
use Doctrine\ORM\QueryBuilder;
|
|
|
|
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\SearchCriteriaProvider;
|
2019-09-06 18:25:24 +02:00
|
|
|
use Omines\DataTablesBundle\Column\BoolColumn;
|
|
|
|
use Omines\DataTablesBundle\Column\MapColumn;
|
2019-03-05 23:52:45 +01:00
|
|
|
use Omines\DataTablesBundle\Column\TextColumn;
|
|
|
|
use Omines\DataTablesBundle\DataTable;
|
|
|
|
use Omines\DataTablesBundle\DataTableTypeInterface;
|
2019-09-06 18:25:24 +02:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2019-03-05 23:52:45 +01:00
|
|
|
|
2020-01-05 22:49:00 +01:00
|
|
|
final class PartsDataTable implements DataTableTypeInterface
|
2019-03-05 23:52:45 +01:00
|
|
|
{
|
2020-02-01 16:17:20 +01:00
|
|
|
private $translator;
|
|
|
|
private $treeBuilder;
|
|
|
|
private $amountFormatter;
|
|
|
|
private $previewGenerator;
|
|
|
|
private $attachmentURLGenerator;
|
2020-01-05 22:49:00 +01:00
|
|
|
/**
|
|
|
|
* @var EntityURLGenerator
|
|
|
|
*/
|
2020-02-01 16:17:20 +01:00
|
|
|
private $urlGenerator;
|
2019-03-05 23:52:45 +01:00
|
|
|
|
2019-09-08 18:19:29 +02:00
|
|
|
public function __construct(EntityURLGenerator $urlGenerator, TranslatorInterface $translator,
|
2020-01-02 22:55:28 +01:00
|
|
|
NodesListBuilder $treeBuilder, AmountFormatter $amountFormatter,
|
2019-11-10 19:16:39 +01:00
|
|
|
PartPreviewGenerator $previewGenerator, AttachmentURLGenerator $attachmentURLGenerator)
|
2019-03-05 23:52:45 +01:00
|
|
|
{
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2019-09-06 18:25:24 +02:00
|
|
|
$this->translator = $translator;
|
2019-09-08 17:39:24 +02:00
|
|
|
$this->treeBuilder = $treeBuilder;
|
2019-09-08 18:19:29 +02:00
|
|
|
$this->amountFormatter = $amountFormatter;
|
2019-10-03 14:53:50 +02:00
|
|
|
$this->previewGenerator = $previewGenerator;
|
2019-10-05 20:30:27 +02:00
|
|
|
$this->attachmentURLGenerator = $attachmentURLGenerator;
|
2019-03-05 23:52:45 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
public function configure(DataTable $dataTable, array $options): void
|
2019-03-05 23:52:45 +01:00
|
|
|
{
|
2019-09-06 18:25:24 +02:00
|
|
|
$dataTable
|
2019-10-03 14:53:50 +02:00
|
|
|
->add('picture', TextColumn::class, [
|
|
|
|
'label' => '',
|
|
|
|
'render' => function ($value, Part $context) {
|
2019-10-03 18:03:56 +02:00
|
|
|
$preview_attachment = $this->previewGenerator->getTablePreviewAttachment($context);
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null === $preview_attachment) {
|
2019-10-03 14:53:50 +02:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf(
|
2019-10-06 15:44:19 +02:00
|
|
|
'<img alt="%s" src="%s" data-thumbnail="%s" class="%s">',
|
2019-10-03 14:53:50 +02:00
|
|
|
'Part image',
|
2019-10-05 20:30:27 +02:00
|
|
|
$this->attachmentURLGenerator->getThumbnailURL($preview_attachment),
|
2019-10-06 15:44:19 +02:00
|
|
|
$this->attachmentURLGenerator->getThumbnailURL($preview_attachment, 'thumbnail_md'),
|
2019-10-03 14:53:50 +02:00
|
|
|
'img-fluid hoverpic'
|
|
|
|
);
|
2019-11-09 00:47:20 +01:00
|
|
|
},
|
2019-10-03 14:53:50 +02:00
|
|
|
])
|
2019-09-06 18:25:24 +02:00
|
|
|
->add('name', TextColumn::class, [
|
|
|
|
'label' => $this->translator->trans('part.table.name'),
|
|
|
|
'render' => function ($value, Part $context) {
|
2019-09-08 14:23:49 +02:00
|
|
|
return sprintf(
|
|
|
|
'<a href="%s">%s</a>',
|
|
|
|
$this->urlGenerator->infoURL($context),
|
|
|
|
$context->getName()
|
|
|
|
);
|
2019-09-06 18:25:24 +02:00
|
|
|
},
|
|
|
|
])
|
|
|
|
->add('id', TextColumn::class, [
|
|
|
|
'label' => $this->translator->trans('part.table.id'),
|
2019-11-09 00:47:20 +01:00
|
|
|
'visible' => false,
|
2019-09-06 18:25:24 +02:00
|
|
|
])
|
2019-11-10 19:43:55 +01:00
|
|
|
->add('description', MarkdownColumn::class, [
|
2019-09-06 18:25:24 +02:00
|
|
|
'label' => $this->translator->trans('part.table.description'),
|
|
|
|
])
|
2019-09-07 15:23:03 +02:00
|
|
|
->add('category', EntityColumn::class, [
|
|
|
|
'label' => $this->translator->trans('part.table.category'),
|
2019-09-08 16:56:05 +02:00
|
|
|
'property' => 'category',
|
2019-09-06 18:25:24 +02:00
|
|
|
])
|
2019-09-07 15:23:03 +02:00
|
|
|
->add('footprint', EntityColumn::class, [
|
|
|
|
'property' => 'footprint',
|
2019-11-09 00:47:20 +01:00
|
|
|
'label' => $this->translator->trans('part.table.footprint'),
|
2019-09-07 13:12:24 +02:00
|
|
|
])
|
2019-09-07 15:23:03 +02:00
|
|
|
->add('manufacturer', EntityColumn::class, [
|
|
|
|
'property' => 'manufacturer',
|
2019-11-09 00:47:20 +01:00
|
|
|
'label' => $this->translator->trans('part.table.manufacturer'),
|
2019-09-07 13:12:24 +02:00
|
|
|
])
|
2019-09-08 17:52:50 +02:00
|
|
|
->add('storelocation', TextColumn::class, [
|
|
|
|
'label' => $this->translator->trans('part.table.storeLocations'),
|
|
|
|
'render' => function ($value, Part $context) {
|
2019-11-09 00:47:20 +01:00
|
|
|
$tmp = [];
|
2019-09-08 17:52:50 +02:00
|
|
|
foreach ($context->getPartLots() as $lot) {
|
|
|
|
//Ignore lots without storelocation
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null === $lot->getStorageLocation()) {
|
2019-09-08 17:52:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$tmp[] = sprintf(
|
|
|
|
'<a href="%s">%s</a>',
|
|
|
|
$this->urlGenerator->listPartsURL($lot->getStorageLocation()),
|
|
|
|
$lot->getStorageLocation()->getName()
|
|
|
|
);
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-09-08 17:52:50 +02:00
|
|
|
return implode('<br>', $tmp);
|
2019-11-09 00:47:20 +01:00
|
|
|
},
|
2019-09-08 17:52:50 +02:00
|
|
|
])
|
2019-09-06 18:25:24 +02:00
|
|
|
->add('amount', TextColumn::class, [
|
|
|
|
'label' => $this->translator->trans('part.table.amount'),
|
2019-09-08 18:19:29 +02:00
|
|
|
'render' => function ($value, Part $context) {
|
|
|
|
$amount = $context->getAmountSum();
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-09-08 18:19:29 +02:00
|
|
|
return $this->amountFormatter->format($amount, $context->getPartUnit());
|
2019-11-09 00:47:20 +01:00
|
|
|
},
|
2019-09-06 18:25:24 +02:00
|
|
|
])
|
|
|
|
->add('minamount', TextColumn::class, [
|
2019-09-07 13:12:24 +02:00
|
|
|
'label' => $this->translator->trans('part.table.minamount'),
|
2019-09-08 18:19:29 +02:00
|
|
|
'visible' => false,
|
|
|
|
'render' => function ($value, Part $context) {
|
|
|
|
return $this->amountFormatter->format($value, $context->getPartUnit());
|
2019-11-09 00:47:20 +01:00
|
|
|
},
|
2019-09-07 13:12:24 +02:00
|
|
|
])
|
|
|
|
->add('partUnit', TextColumn::class, [
|
|
|
|
'field' => 'partUnit.name',
|
|
|
|
'label' => $this->translator->trans('part.table.partUnit'),
|
2019-11-09 00:47:20 +01:00
|
|
|
'visible' => false,
|
2019-09-06 18:25:24 +02:00
|
|
|
])
|
2019-09-07 12:48:49 +02:00
|
|
|
->add('addedDate', LocaleDateTimeColumn::class, [
|
2019-09-06 18:25:24 +02:00
|
|
|
'label' => $this->translator->trans('part.table.addedDate'),
|
2019-11-09 00:47:20 +01:00
|
|
|
'visible' => false,
|
2019-09-06 18:25:24 +02:00
|
|
|
])
|
2019-09-07 12:48:49 +02:00
|
|
|
->add('lastModified', LocaleDateTimeColumn::class, [
|
2019-09-06 18:25:24 +02:00
|
|
|
'label' => $this->translator->trans('part.table.lastModified'),
|
2019-11-09 00:47:20 +01:00
|
|
|
'visible' => false,
|
2019-09-06 18:25:24 +02:00
|
|
|
])
|
|
|
|
->add('needs_review', BoolColumn::class, [
|
|
|
|
'label' => $this->translator->trans('part.table.needsReview'),
|
|
|
|
'trueValue' => $this->translator->trans('true'),
|
|
|
|
'falseValue' => $this->translator->trans('false'),
|
|
|
|
'nullValue' => '',
|
2019-11-09 00:47:20 +01:00
|
|
|
'visible' => false,
|
2019-09-06 18:25:24 +02:00
|
|
|
])
|
|
|
|
->add('favorite', BoolColumn::class, [
|
|
|
|
'label' => $this->translator->trans('part.table.favorite'),
|
|
|
|
'trueValue' => $this->translator->trans('true'),
|
|
|
|
'falseValue' => $this->translator->trans('false'),
|
|
|
|
'nullValue' => '',
|
2019-11-09 00:47:20 +01:00
|
|
|
'visible' => false,
|
2019-09-06 18:25:24 +02:00
|
|
|
])
|
|
|
|
->add('manufacturing_status', MapColumn::class, [
|
|
|
|
'label' => $this->translator->trans('part.table.manufacturingStatus'),
|
|
|
|
'visible' => false,
|
|
|
|
'default' => $this->translator->trans('m_status.unknown'),
|
|
|
|
'map' => [
|
|
|
|
'' => $this->translator->trans('m_status.unknown'),
|
|
|
|
'announced' => $this->translator->trans('m_status.announced'),
|
|
|
|
'active' => $this->translator->trans('m_status.active'),
|
|
|
|
'nrfnd' => $this->translator->trans('m_status.nrfnd'),
|
|
|
|
'eol' => $this->translator->trans('m_status.eol'),
|
2019-11-09 00:47:20 +01:00
|
|
|
'discontinued' => $this->translator->trans('m_status.discontinued'),
|
|
|
|
],
|
2019-09-06 18:25:24 +02:00
|
|
|
])
|
|
|
|
->add('manufacturer_product_number', TextColumn::class, [
|
|
|
|
'label' => $this->translator->trans('part.table.mpn'),
|
2019-11-09 00:47:20 +01:00
|
|
|
'visible' => false,
|
2019-09-06 18:25:24 +02:00
|
|
|
])
|
|
|
|
->add('mass', TextColumn::class, [
|
|
|
|
'label' => $this->translator->trans('part.table.mass'),
|
2019-11-09 00:47:20 +01:00
|
|
|
'visible' => false,
|
2019-09-06 18:25:24 +02:00
|
|
|
])
|
2019-11-10 19:38:36 +01:00
|
|
|
->add('tags', TagsColumn::class, [
|
2019-09-06 18:25:24 +02:00
|
|
|
'label' => $this->translator->trans('part.table.tags'),
|
2019-11-09 00:47:20 +01:00
|
|
|
'visible' => false,
|
2019-09-06 18:25:24 +02:00
|
|
|
])
|
2019-11-10 19:16:39 +01:00
|
|
|
->add('attachments', PartAttachmentsColumn::class, [
|
|
|
|
'label' => $this->translator->trans('part.table.attachments'),
|
|
|
|
'visible' => false,
|
|
|
|
])
|
2019-09-06 18:25:24 +02:00
|
|
|
|
2020-01-26 21:54:21 +01:00
|
|
|
->addOrderBy('name')
|
2020-01-17 21:38:36 +01:00
|
|
|
->createAdapter(FetchJoinORMAdapter::class, [
|
|
|
|
'simple_total_query' => true,
|
2020-01-05 15:46:58 +01:00
|
|
|
'query' => function (QueryBuilder $builder): void {
|
2019-09-07 13:12:24 +02:00
|
|
|
$this->getQuery($builder);
|
|
|
|
},
|
2019-03-05 23:52:45 +01:00
|
|
|
'entity' => Part::class,
|
|
|
|
'criteria' => [
|
2020-01-05 15:46:58 +01:00
|
|
|
function (QueryBuilder $builder) use ($options): void {
|
2019-09-05 22:27:18 +02:00
|
|
|
$this->buildCriteria($builder, $options);
|
|
|
|
},
|
2019-11-09 00:47:20 +01:00
|
|
|
new SearchCriteriaProvider(),
|
|
|
|
],
|
2019-03-05 23:52:45 +01: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
|
|
|
{
|
|
|
|
$builder->distinct()->select('part')
|
2020-01-26 21:54:21 +01:00
|
|
|
->addSelect('category')
|
|
|
|
->addSelect('footprint')
|
|
|
|
->addSelect('manufacturer')
|
|
|
|
->addSelect('partUnit')
|
|
|
|
->addSelect('master_picture_attachment')
|
|
|
|
->addSelect('footprint_attachment')
|
|
|
|
->addSelect('partLots')
|
|
|
|
->addSelect('orderdetails')
|
|
|
|
->addSelect('attachments')
|
|
|
|
->addSelect('storelocations')
|
2020-01-05 15:46:58 +01:00
|
|
|
->from(Part::class, 'part')
|
|
|
|
->leftJoin('part.category', 'category')
|
|
|
|
->leftJoin('part.master_picture_attachment', 'master_picture_attachment')
|
|
|
|
->leftJoin('part.partLots', 'partLots')
|
|
|
|
->leftJoin('partLots.storage_location', 'storelocations')
|
|
|
|
->leftJoin('part.footprint', 'footprint')
|
|
|
|
->leftJoin('footprint.master_picture_attachment', 'footprint_attachment')
|
|
|
|
->leftJoin('part.manufacturer', 'manufacturer')
|
|
|
|
->leftJoin('part.orderdetails', 'orderdetails')
|
|
|
|
->leftJoin('part.attachments', 'attachments')
|
|
|
|
->leftJoin('part.partUnit', 'partUnit');
|
|
|
|
}
|
|
|
|
|
2020-02-01 16:17:20 +01:00
|
|
|
private function buildCriteria(QueryBuilder $builder, array $options): void
|
2020-01-05 15:46:58 +01:00
|
|
|
{
|
|
|
|
$em = $builder->getEntityManager();
|
|
|
|
|
|
|
|
if (isset($options['category'])) {
|
|
|
|
$category = $options['category'];
|
|
|
|
$list = $this->treeBuilder->typeToNodesList(Category::class, $category);
|
|
|
|
$list[] = $category;
|
|
|
|
|
|
|
|
$builder->andWhere('part.category IN (:cid)')->setParameter('cid', $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['footprint'])) {
|
|
|
|
$category = $options['footprint'];
|
|
|
|
$list = $this->treeBuilder->typeToNodesList(Footprint::class, $category);
|
|
|
|
$list[] = $category;
|
|
|
|
|
|
|
|
$builder->andWhere('part.footprint IN (:cid)')->setParameter('cid', $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['manufacturer'])) {
|
|
|
|
$category = $options['manufacturer'];
|
|
|
|
$list = $this->treeBuilder->typeToNodesList(Manufacturer::class, $category);
|
|
|
|
$list[] = $category;
|
|
|
|
|
|
|
|
$builder->andWhere('part.manufacturer IN (:cid)')->setParameter('cid', $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['storelocation'])) {
|
|
|
|
$location = $options['storelocation'];
|
|
|
|
$list = $this->treeBuilder->typeToNodesList(Storelocation::class, $location);
|
|
|
|
$list[] = $location;
|
|
|
|
|
|
|
|
$builder->andWhere('partLots.storage_location IN (:cid)')->setParameter('cid', $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['supplier'])) {
|
|
|
|
$supplier = $options['supplier'];
|
|
|
|
$list = $this->treeBuilder->typeToNodesList(Supplier::class, $supplier);
|
|
|
|
$list[] = $supplier;
|
|
|
|
|
|
|
|
$builder->andWhere('orderdetails.supplier IN (:cid)')->setParameter('cid', $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['tag'])) {
|
|
|
|
$builder->andWhere('part.tags LIKE :tag')->setParameter('tag', '%'.$options['tag'].'%');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['search'])) {
|
|
|
|
$builder->AndWhere('part.name LIKE :search')->orWhere('part.description LIKE :search')->orWhere('part.comment LIKE :search')
|
|
|
|
->setParameter('search', '%'.$options['search'].'%');
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|