mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
Moved column sorting and visibility logic to its own (non-shared) helper service
This commit is contained in:
parent
0753b7137f
commit
1369091b90
3 changed files with 249 additions and 224 deletions
|
@ -216,7 +216,10 @@ services:
|
||||||
####################################################################################################################
|
####################################################################################################################
|
||||||
App\DataTables\PartsDataTable:
|
App\DataTables\PartsDataTable:
|
||||||
arguments:
|
arguments:
|
||||||
$default_part_columns: '%partdb.table.default_part_columns%'
|
$visible_columns: '%partdb.table.default_part_columns%'
|
||||||
|
|
||||||
|
App\DataTables\Helpers\ColumnSortHelper:
|
||||||
|
shared: false # Service has a state so not share it between different tables
|
||||||
|
|
||||||
####################################################################################################################
|
####################################################################################################################
|
||||||
# Label system
|
# Label system
|
||||||
|
|
130
src/DataTables/Helpers/ColumnSortHelper.php
Normal file
130
src/DataTables/Helpers/ColumnSortHelper.php
Normal file
|
@ -0,0 +1,130 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 - 2023 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
|
||||||
|
namespace App\DataTables\Helpers;
|
||||||
|
|
||||||
|
use Omines\DataTablesBundle\DataTable;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
|
class ColumnSortHelper
|
||||||
|
{
|
||||||
|
private array $columns = [];
|
||||||
|
|
||||||
|
public function __construct(LoggerInterface $logger)
|
||||||
|
{
|
||||||
|
$this->logger = $logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new column which can be sorted and visibility controlled by the user. The basic syntax is similar to
|
||||||
|
* the DataTable add method, but with additional options.
|
||||||
|
* @param string $name
|
||||||
|
* @param string $type
|
||||||
|
* @param array $options
|
||||||
|
* @param string|null $alias If an alias is set here, the column will be available under this alias in the config
|
||||||
|
* string instead of the name.
|
||||||
|
* @param bool $visibility_configurable If set to false, this column can not be visibility controlled by the user
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function add(string $name, string $type, array $options = [], ?string $alias = null,
|
||||||
|
bool $visibility_configurable = true): self
|
||||||
|
{
|
||||||
|
//Alias allows us to override the name of the column in the env variable
|
||||||
|
$this->columns[$alias ?? $name] = [
|
||||||
|
'name' => $name,
|
||||||
|
'type' => $type,
|
||||||
|
'options' => $options,
|
||||||
|
'visibility_configurable' => $visibility_configurable
|
||||||
|
];
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all columns saved inside this helper
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function reset(): void
|
||||||
|
{
|
||||||
|
$this->columns = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply the visibility configuration to the given DataTable and configure the columns.
|
||||||
|
* @param DataTable $dataTable
|
||||||
|
* @param string|array $visible_columns Either a list or a comma separated string of column names, which should
|
||||||
|
* be visible by default. If a column is not listed here, it will be hidden by default.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function applyVisibilityAndConfigureColumns(DataTable $dataTable, string|array $visible_columns): void
|
||||||
|
{
|
||||||
|
//If the config is given as a string, convert it to an array first
|
||||||
|
if (!is_array($visible_columns)) {
|
||||||
|
$visible_columns = array_map(trim(...), explode(",", $visible_columns));
|
||||||
|
}
|
||||||
|
|
||||||
|
$processed_columns = [];
|
||||||
|
|
||||||
|
//First add all columns which visibility is not configurable
|
||||||
|
foreach ($this->columns as $col_id => $col_data) {
|
||||||
|
if (!$col_data['visibility_configurable']) {
|
||||||
|
$this->addColumnEntry($dataTable, $this->columns[$col_id], null);
|
||||||
|
$processed_columns[] = $col_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Afterwards the columns, which should be visible by default
|
||||||
|
foreach ($visible_columns as $col_id) {
|
||||||
|
if (!isset($this->columns[$col_id]) || !$this->columns[$col_id]['visibility_configurable']) {
|
||||||
|
$this->logger->warning("Configuration option TABLE_PART_DEFAULT_COLUMNS specify invalid column '$col_id'. Column is skipped.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (in_array($col_id, $processed_columns, true)) {
|
||||||
|
$this->logger->warning("Configuration option TABLE_PART_DEFAULT_COLUMNS specify column '$col_id' multiple time. Only first occurrence is used.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$this->addColumnEntry($dataTable, $this->columns[$col_id], true);
|
||||||
|
$processed_columns[] = $col_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
//and the remaining non-visible columns
|
||||||
|
foreach ($this->columns as $col_id => $col_data) {
|
||||||
|
if (in_array($col_id, $processed_columns)) {
|
||||||
|
// column already processed
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$this->addColumnEntry($dataTable, $this->columns[$col_id], false);
|
||||||
|
$processed_columns[] = $col_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addColumnEntry(DataTable $dataTable, array $entry, ?bool $visible): void
|
||||||
|
{
|
||||||
|
$options = $entry['options'] ?? [];
|
||||||
|
if (!is_null($visible)) {
|
||||||
|
$options["visible"] = $visible;
|
||||||
|
}
|
||||||
|
$dataTable->add($entry['name'], $entry['type'], $options);
|
||||||
|
}
|
||||||
|
}
|
|
@ -25,6 +25,7 @@ namespace App\DataTables;
|
||||||
use App\DataTables\Adapters\FetchResultsAtOnceORMAdapter;
|
use App\DataTables\Adapters\FetchResultsAtOnceORMAdapter;
|
||||||
use App\DataTables\Adapters\TwoStepORMAdapater;
|
use App\DataTables\Adapters\TwoStepORMAdapater;
|
||||||
use App\DataTables\Column\EnumColumn;
|
use App\DataTables\Column\EnumColumn;
|
||||||
|
use App\DataTables\Helpers\ColumnSortHelper;
|
||||||
use App\Doctrine\Helpers\FieldHelper;
|
use App\Doctrine\Helpers\FieldHelper;
|
||||||
use App\Entity\Parts\ManufacturingStatus;
|
use App\Entity\Parts\ManufacturingStatus;
|
||||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||||
|
@ -33,7 +34,6 @@ use Doctrine\ORM\Query;
|
||||||
use Doctrine\ORM\Tools\Pagination\Paginator;
|
use Doctrine\ORM\Tools\Pagination\Paginator;
|
||||||
use Omines\DataTablesBundle\Adapter\Doctrine\Event\ORMAdapterQueryEvent;
|
use Omines\DataTablesBundle\Adapter\Doctrine\Event\ORMAdapterQueryEvent;
|
||||||
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapterEvents;
|
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapterEvents;
|
||||||
use Psr\Log\LoggerInterface;
|
|
||||||
use Symfony\Bundle\SecurityBundle\Security;
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
use App\Entity\Parts\Storelocation;
|
use App\Entity\Parts\Storelocation;
|
||||||
use App\DataTables\Column\EntityColumn;
|
use App\DataTables\Column\EntityColumn;
|
||||||
|
@ -63,8 +63,15 @@ use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
|
|
||||||
final class PartsDataTable implements DataTableTypeInterface
|
final class PartsDataTable implements DataTableTypeInterface
|
||||||
{
|
{
|
||||||
public function __construct(private readonly EntityURLGenerator $urlGenerator, private readonly TranslatorInterface $translator, private readonly AmountFormatter $amountFormatter, private readonly PartDataTableHelper $partDataTableHelper, private readonly Security $security, private readonly string $default_part_columns, protected LoggerInterface $logger)
|
public function __construct(
|
||||||
{
|
private readonly EntityURLGenerator $urlGenerator,
|
||||||
|
private readonly TranslatorInterface $translator,
|
||||||
|
private readonly AmountFormatter $amountFormatter,
|
||||||
|
private readonly PartDataTableHelper $partDataTableHelper,
|
||||||
|
private readonly Security $security,
|
||||||
|
private readonly string $visible_columns,
|
||||||
|
private readonly ColumnSortHelper $csh,
|
||||||
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function configureOptions(OptionsResolver $optionsResolver): void
|
public function configureOptions(OptionsResolver $optionsResolver): void
|
||||||
|
@ -84,9 +91,9 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
$this->configureOptions($resolver);
|
$this->configureOptions($resolver);
|
||||||
$options = $resolver->resolve($options);
|
$options = $resolver->resolve($options);
|
||||||
|
|
||||||
$dataTable
|
$this->csh
|
||||||
//Color the table rows depending on the review and favorite status
|
//Color the table rows depending on the review and favorite status
|
||||||
->add('dont_matter', RowClassColumn::class, [
|
->add('row_color', RowClassColumn::class, [
|
||||||
'render' => function ($value, Part $context): string {
|
'render' => function ($value, Part $context): string {
|
||||||
if ($context->isNeedsReview()) {
|
if ($context->isNeedsReview()) {
|
||||||
return 'table-secondary';
|
return 'table-secondary';
|
||||||
|
@ -97,127 +104,94 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
|
|
||||||
return ''; //Default coloring otherwise
|
return ''; //Default coloring otherwise
|
||||||
},
|
},
|
||||||
])
|
], visibility_configurable: false)
|
||||||
|
->add('select', SelectColumn::class, visibility_configurable: false)
|
||||||
->add('select', SelectColumn::class)
|
|
||||||
->add('picture', TextColumn::class, [
|
->add('picture', TextColumn::class, [
|
||||||
'label' => '',
|
'label' => '',
|
||||||
'className' => 'no-colvis',
|
'className' => 'no-colvis',
|
||||||
'render' => fn($value, Part $context) => $this->partDataTableHelper->renderPicture($context),
|
'render' => fn($value, Part $context) => $this->partDataTableHelper->renderPicture($context),
|
||||||
]);
|
], visibility_configurable: false)
|
||||||
|
->add('name', TextColumn::class, [
|
||||||
|
|
||||||
// internal array of $dataTable->add(...) parameters. Parameters will be later passed to the method
|
|
||||||
// after sorting columns according to TABLE_PART_DEFAULT_COLUMNS option.
|
|
||||||
$columns = [];
|
|
||||||
|
|
||||||
$columns['name'] = [
|
|
||||||
'name', TextColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.name'),
|
'label' => $this->translator->trans('part.table.name'),
|
||||||
'render' => fn($value, Part $context) => $this->partDataTableHelper->renderName($context),
|
'render' => fn($value, Part $context) => $this->partDataTableHelper->renderName($context),
|
||||||
]
|
])
|
||||||
];
|
->add('id', TextColumn::class, [
|
||||||
$columns['id'] = [
|
|
||||||
'id', TextColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.id'),
|
'label' => $this->translator->trans('part.table.id'),
|
||||||
'visible' => false,
|
'visible' => false,
|
||||||
]
|
])
|
||||||
];
|
->add('ipn', TextColumn::class, [
|
||||||
$columns['ipn'] = [
|
|
||||||
'ipn', TextColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.ipn'),
|
'label' => $this->translator->trans('part.table.ipn'),
|
||||||
'visible' => false,
|
'visible' => false,
|
||||||
]
|
])
|
||||||
];
|
->add('description', MarkdownColumn::class, [
|
||||||
$columns['description'] = [
|
|
||||||
'description', MarkdownColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.description'),
|
'label' => $this->translator->trans('part.table.description'),
|
||||||
]
|
]);
|
||||||
];
|
|
||||||
|
|
||||||
if ($this->security->isGranted('@categories.read')) {
|
if ($this->security->isGranted('@categories.read')) {
|
||||||
$columns['category'] = [
|
$this->csh->add('category', EntityColumn::class, [
|
||||||
'category', EntityColumn::class,
|
'label' => $this->translator->trans('part.table.category'),
|
||||||
[
|
'property' => 'category',
|
||||||
'label' => $this->translator->trans('part.table.category'),
|
]);
|
||||||
'property' => 'category',
|
|
||||||
]
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->security->isGranted('@footprints.read')) {
|
if ($this->security->isGranted('@footprints.read')) {
|
||||||
$columns['footprint'] = [
|
$this->csh->add('footprint', EntityColumn::class, [
|
||||||
'footprint', EntityColumn::class,
|
'property' => 'footprint',
|
||||||
[
|
'label' => $this->translator->trans('part.table.footprint'),
|
||||||
'property' => 'footprint',
|
]);
|
||||||
'label' => $this->translator->trans('part.table.footprint'),
|
|
||||||
]
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
if ($this->security->isGranted('@manufacturers.read')) {
|
if ($this->security->isGranted('@manufacturers.read')) {
|
||||||
$columns['manufacturer'] = [
|
$this->csh->add('manufacturer', EntityColumn::class, [
|
||||||
'manufacturer', EntityColumn::class,
|
'property' => 'manufacturer',
|
||||||
[
|
'label' => $this->translator->trans('part.table.manufacturer'),
|
||||||
'property' => 'manufacturer',
|
]);
|
||||||
'label' => $this->translator->trans('part.table.manufacturer'),
|
|
||||||
]
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
if ($this->security->isGranted('@storelocations.read')) {
|
if ($this->security->isGranted('@storelocations.read')) {
|
||||||
$columns['storelocation'] = [
|
$this->csh->add('storelocation', TextColumn::class, [
|
||||||
'storelocation', TextColumn::class,
|
'label' => $this->translator->trans('part.table.storeLocations'),
|
||||||
[
|
'orderField' => 'storelocations.name',
|
||||||
'label' => $this->translator->trans('part.table.storeLocations'),
|
'render' => function ($value, Part $context): string {
|
||||||
'orderField' => 'storelocations.name',
|
$tmp = [];
|
||||||
'render' => function ($value, Part $context): string {
|
foreach ($context->getPartLots() as $lot) {
|
||||||
$tmp = [];
|
//Ignore lots without storelocation
|
||||||
foreach ($context->getPartLots() as $lot) {
|
if (!$lot->getStorageLocation() instanceof Storelocation) {
|
||||||
//Ignore lots without storelocation
|
continue;
|
||||||
if (!$lot->getStorageLocation() instanceof Storelocation) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$tmp[] = sprintf(
|
|
||||||
'<a href="%s" title="%s">%s</a>',
|
|
||||||
$this->urlGenerator->listPartsURL($lot->getStorageLocation()),
|
|
||||||
htmlspecialchars($lot->getStorageLocation()->getFullPath()),
|
|
||||||
htmlspecialchars($lot->getStorageLocation()->getName())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
$tmp[] = sprintf(
|
||||||
|
'<a href="%s" title="%s">%s</a>',
|
||||||
|
$this->urlGenerator->listPartsURL($lot->getStorageLocation()),
|
||||||
|
htmlspecialchars($lot->getStorageLocation()->getFullPath()),
|
||||||
|
htmlspecialchars($lot->getStorageLocation()->getName())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return implode('<br>', $tmp);
|
return implode('<br>', $tmp);
|
||||||
},
|
},
|
||||||
]
|
]);
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$columns['amount'] = [
|
$this->csh->add('amount', TextColumn::class, [
|
||||||
'amount', TextColumn::class,
|
'label' => $this->translator->trans('part.table.amount'),
|
||||||
[
|
'render' => function ($value, Part $context) {
|
||||||
'label' => $this->translator->trans('part.table.amount'),
|
$amount = $context->getAmountSum();
|
||||||
'render' => function ($value, Part $context) {
|
$expiredAmount = $context->getExpiredAmountSum();
|
||||||
$amount = $context->getAmountSum();
|
|
||||||
$expiredAmount = $context->getExpiredAmountSum();
|
|
||||||
|
|
||||||
$ret = '';
|
$ret = '';
|
||||||
|
|
||||||
if ($context->isAmountUnknown()) {
|
if ($context->isAmountUnknown()) {
|
||||||
//When all amounts are unknown, we show a question mark
|
//When all amounts are unknown, we show a question mark
|
||||||
if ($amount === 0.0) {
|
if ($amount === 0.0) {
|
||||||
$ret .= sprintf('<b class="text-primary" title="%s">?</b>',
|
$ret .= sprintf('<b class="text-primary" title="%s">?</b>',
|
||||||
$this->translator->trans('part_lots.instock_unknown'));
|
$this->translator->trans('part_lots.instock_unknown'));
|
||||||
} else { //Otherwise mark it with greater equal and the (known) amount
|
} else { //Otherwise mark it with greater equal and the (known) amount
|
||||||
$ret .= sprintf('<b class="text-primary" title="%s">≥</b>',
|
$ret .= sprintf('<b class="text-primary" title="%s">≥</b>',
|
||||||
$this->translator->trans('part_lots.instock_unknown')
|
$this->translator->trans('part_lots.instock_unknown')
|
||||||
);
|
);
|
||||||
$ret .= htmlspecialchars($this->amountFormatter->format($amount, $context->getPartUnit()));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$ret .= htmlspecialchars($this->amountFormatter->format($amount, $context->getPartUnit()));
|
$ret .= htmlspecialchars($this->amountFormatter->format($amount, $context->getPartUnit()));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
$ret .= htmlspecialchars($this->amountFormatter->format($amount, $context->getPartUnit()));
|
||||||
|
}
|
||||||
|
|
||||||
//If we have expired lots, we show them in parentheses behind
|
//If we have expired lots, we show them in parentheses behind
|
||||||
if ($expiredAmount > 0) {
|
if ($expiredAmount > 0) {
|
||||||
|
@ -233,158 +207,80 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
$ret);
|
$ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $ret;
|
return $ret;
|
||||||
},
|
},
|
||||||
'orderField' => 'amountSum'
|
'orderField' => 'amountSum'
|
||||||
]
|
])
|
||||||
];
|
->add('minamount', TextColumn::class, [
|
||||||
$columns['minamount'] = [
|
|
||||||
'minamount', TextColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.minamount'),
|
'label' => $this->translator->trans('part.table.minamount'),
|
||||||
'visible' => false,
|
'visible' => false,
|
||||||
'render' => fn($value, Part $context): string => htmlspecialchars($this->amountFormatter->format($value, $context->getPartUnit())),
|
'render' => fn($value, Part $context): string => htmlspecialchars($this->amountFormatter->format($value,
|
||||||
]
|
$context->getPartUnit())),
|
||||||
];
|
]);
|
||||||
|
|
||||||
if ($this->security->isGranted('@footprints.read')) {
|
if ($this->security->isGranted('@footprints.read')) {
|
||||||
$columns['partUnit'] = [
|
$this->csh->add('partUnit', TextColumn::class, [
|
||||||
'partUnit', TextColumn::class,
|
'field' => 'partUnit.name',
|
||||||
[
|
'label' => $this->translator->trans('part.table.partUnit'),
|
||||||
'field' => 'partUnit.name',
|
'visible' => false,
|
||||||
'label' => $this->translator->trans('part.table.partUnit'),
|
]);
|
||||||
'visible' => false,
|
|
||||||
]
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$columns['addedDate'] = [
|
$this->csh->add('addedDate', LocaleDateTimeColumn::class, [
|
||||||
'addedDate', LocaleDateTimeColumn::class,
|
'label' => $this->translator->trans('part.table.addedDate'),
|
||||||
[
|
'visible' => false,
|
||||||
'label' => $this->translator->trans('part.table.addedDate'),
|
])
|
||||||
'visible' => false,
|
->add('lastModified', LocaleDateTimeColumn::class, [
|
||||||
]
|
|
||||||
];
|
|
||||||
$columns['lastModified'] = [
|
|
||||||
'lastModified', LocaleDateTimeColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.lastModified'),
|
'label' => $this->translator->trans('part.table.lastModified'),
|
||||||
'visible' => false,
|
'visible' => false,
|
||||||
]
|
])
|
||||||
];
|
->add('needs_review', PrettyBoolColumn::class, [
|
||||||
$columns['needs_review'] = [
|
|
||||||
'needs_review', PrettyBoolColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.needsReview'),
|
'label' => $this->translator->trans('part.table.needsReview'),
|
||||||
'visible' => false,
|
'visible' => false,
|
||||||
]
|
])
|
||||||
];
|
->add('favorite', PrettyBoolColumn::class, [
|
||||||
$columns['favorite'] = [
|
|
||||||
'favorite', PrettyBoolColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.favorite'),
|
'label' => $this->translator->trans('part.table.favorite'),
|
||||||
'visible' => false,
|
'visible' => false,
|
||||||
]
|
])
|
||||||
];
|
->add('manufacturing_status', EnumColumn::class, [
|
||||||
$columns['manufacturing_status'] = [
|
|
||||||
'manufacturing_status', EnumColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.manufacturingStatus'),
|
'label' => $this->translator->trans('part.table.manufacturingStatus'),
|
||||||
'visible' => false,
|
'visible' => false,
|
||||||
'class' => ManufacturingStatus::class,
|
'class' => ManufacturingStatus::class,
|
||||||
'render' => function(?ManufacturingStatus $status, Part $context): string {
|
'render' => function (?ManufacturingStatus $status, Part $context): string {
|
||||||
if (!$status) {
|
if (!$status) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->translator->trans($status->toTranslationKey());
|
return $this->translator->trans($status->toTranslationKey());
|
||||||
} ,
|
},
|
||||||
]
|
])
|
||||||
];
|
->add('manufacturer_product_number', TextColumn::class, [
|
||||||
$columns['manufacturer_product_number'] = [
|
|
||||||
'manufacturer_product_number', TextColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.mpn'),
|
'label' => $this->translator->trans('part.table.mpn'),
|
||||||
'visible' => false,
|
'visible' => false,
|
||||||
]
|
])
|
||||||
];
|
->add('mass', SIUnitNumberColumn::class, [
|
||||||
$columns['mass'] = [
|
|
||||||
'mass', SIUnitNumberColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.mass'),
|
'label' => $this->translator->trans('part.table.mass'),
|
||||||
'visible' => false,
|
'visible' => false,
|
||||||
'unit' => 'g'
|
'unit' => 'g'
|
||||||
]
|
])
|
||||||
];
|
->add('tags', TagsColumn::class, [
|
||||||
$columns['tags'] = [
|
|
||||||
'tags', TagsColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.tags'),
|
'label' => $this->translator->trans('part.table.tags'),
|
||||||
'visible' => false,
|
'visible' => false,
|
||||||
]
|
])
|
||||||
];
|
->add('attachments', PartAttachmentsColumn::class, [
|
||||||
$columns['attachments'] = [
|
|
||||||
'attachments', PartAttachmentsColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.attachments'),
|
'label' => $this->translator->trans('part.table.attachments'),
|
||||||
'visible' => false,
|
'visible' => false,
|
||||||
]
|
])
|
||||||
];
|
->add('edit', IconLinkColumn::class, [
|
||||||
$columns['edit'] = [
|
|
||||||
'edit', IconLinkColumn::class,
|
|
||||||
[
|
|
||||||
'label' => $this->translator->trans('part.table.edit'),
|
'label' => $this->translator->trans('part.table.edit'),
|
||||||
'visible' => false,
|
'visible' => false,
|
||||||
'href' => fn($value, Part $context) => $this->urlGenerator->editURL($context),
|
'href' => fn($value, Part $context) => $this->urlGenerator->editURL($context),
|
||||||
'disabled' => fn($value, Part $context) => !$this->security->isGranted('edit', $context),
|
'disabled' => fn($value, Part $context) => !$this->security->isGranted('edit', $context),
|
||||||
'title' => $this->translator->trans('part.table.edit.title'),
|
'title' => $this->translator->trans('part.table.edit.title'),
|
||||||
]
|
]);
|
||||||
];
|
|
||||||
|
|
||||||
$visible_columns_ids = array_map("trim", explode(",", $this->default_part_columns));
|
//Apply the user configured order and visibility and add the columns to the table
|
||||||
$allowed_configurable_columns_ids = ["name", "id", "ipn", "description", "category", "footprint", "manufacturer",
|
$this->csh->applyVisibilityAndConfigureColumns($dataTable, $this->visible_columns);
|
||||||
"storelocation", "amount", "minamount", "partUnit", "addedDate", "lastModified", "needs_review", "favorite",
|
|
||||||
"manufacturing_status", "manufacturer_product_number", "mass", "tags", "attachments", "edit"
|
|
||||||
];
|
|
||||||
$processed_columns = [];
|
|
||||||
|
|
||||||
foreach ($visible_columns_ids as $col_id) {
|
|
||||||
if (!in_array($col_id, $allowed_configurable_columns_ids) || !isset($columns[$col_id])) {
|
|
||||||
$this->logger->warning("Configuration option TABLE_PART_DEFAULT_COLUMNS specify invalid column '$col_id'. Collumn is skipped.");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (in_array($col_id, $processed_columns)) {
|
|
||||||
$this->logger->warning("Configuration option TABLE_PART_DEFAULT_COLUMNS specify column '$col_id' multiple time. Only first occurence is used.");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$options = [];
|
|
||||||
if (count($columns[$col_id]) >= 3) {
|
|
||||||
$options = $columns[$col_id][2];
|
|
||||||
}
|
|
||||||
$options["visible"] = true;
|
|
||||||
$dataTable->add($col_id, $columns[$col_id][1], $options);
|
|
||||||
|
|
||||||
$processed_columns[] = $col_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
// add remaining non-visible columns
|
|
||||||
foreach ($allowed_configurable_columns_ids as $col_id) {
|
|
||||||
if (in_array($col_id, $processed_columns)) {
|
|
||||||
// column already processed
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$options = [];
|
|
||||||
if (count($columns[$col_id]) >= 3) {
|
|
||||||
$options = $columns[$col_id][2];
|
|
||||||
}
|
|
||||||
$options["visible"] = false;
|
|
||||||
$dataTable->add($col_id, $columns[$col_id][1], $options);
|
|
||||||
|
|
||||||
$processed_columns[] = $col_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
$dataTable->addOrderBy('name')
|
$dataTable->addOrderBy('name')
|
||||||
->createAdapter(TwoStepORMAdapater::class, [
|
->createAdapter(TwoStepORMAdapater::class, [
|
||||||
|
@ -415,7 +311,7 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
->addSelect(
|
->addSelect(
|
||||||
'(
|
'(
|
||||||
SELECT IFNULL(SUM(partLot.amount), 0.0)
|
SELECT IFNULL(SUM(partLot.amount), 0.0)
|
||||||
FROM '. PartLot::class. ' partLot
|
FROM '.PartLot::class.' partLot
|
||||||
WHERE partLot.part = part.id
|
WHERE partLot.part = part.id
|
||||||
AND partLot.instock_unknown = false
|
AND partLot.instock_unknown = false
|
||||||
AND (partLot.expiration_date IS NULL OR partLot.expiration_date > CURRENT_DATE())
|
AND (partLot.expiration_date IS NULL OR partLot.expiration_date > CURRENT_DATE())
|
||||||
|
@ -436,8 +332,7 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
->leftJoin('part.parameters', 'parameters')
|
->leftJoin('part.parameters', 'parameters')
|
||||||
|
|
||||||
//This must be the only group by, or the paginator will not work correctly
|
//This must be the only group by, or the paginator will not work correctly
|
||||||
->addGroupBy('part.id')
|
->addGroupBy('part.id');
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getDetailQuery(QueryBuilder $builder, array $filter_results): void
|
private function getDetailQuery(QueryBuilder $builder, array $filter_results): void
|
||||||
|
@ -467,7 +362,7 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
->addSelect(
|
->addSelect(
|
||||||
'(
|
'(
|
||||||
SELECT IFNULL(SUM(partLot.amount), 0.0)
|
SELECT IFNULL(SUM(partLot.amount), 0.0)
|
||||||
FROM '. PartLot::class. ' partLot
|
FROM '.PartLot::class.' partLot
|
||||||
WHERE partLot.part = part.id
|
WHERE partLot.part = part.id
|
||||||
AND partLot.instock_unknown = false
|
AND partLot.instock_unknown = false
|
||||||
AND (partLot.expiration_date IS NULL OR partLot.expiration_date > CURRENT_DATE())
|
AND (partLot.expiration_date IS NULL OR partLot.expiration_date > CURRENT_DATE())
|
||||||
|
@ -486,7 +381,6 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
->leftJoin('part.attachments', 'attachments')
|
->leftJoin('part.attachments', 'attachments')
|
||||||
->leftJoin('part.partUnit', 'partUnit')
|
->leftJoin('part.partUnit', 'partUnit')
|
||||||
->leftJoin('part.parameters', 'parameters')
|
->leftJoin('part.parameters', 'parameters')
|
||||||
|
|
||||||
->where('part.id IN (:ids)')
|
->where('part.id IN (:ids)')
|
||||||
->setParameter('ids', $ids)
|
->setParameter('ids', $ids)
|
||||||
|
|
||||||
|
@ -503,8 +397,7 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
->addGroupBy('suppliers')
|
->addGroupBy('suppliers')
|
||||||
->addGroupBy('attachments')
|
->addGroupBy('attachments')
|
||||||
->addGroupBy('partUnit')
|
->addGroupBy('partUnit')
|
||||||
->addGroupBy('parameters')
|
->addGroupBy('parameters');
|
||||||
;
|
|
||||||
|
|
||||||
//Get the results in the same order as the IDs were passed
|
//Get the results in the same order as the IDs were passed
|
||||||
FieldHelper::addOrderByFieldParam($builder, 'part.id', 'ids');
|
FieldHelper::addOrderByFieldParam($builder, 'part.id', 'ids');
|
||||||
|
@ -523,6 +416,5 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
$filter = $options['filter'];
|
$filter = $options['filter'];
|
||||||
$filter->apply($builder);
|
$filter->apply($builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue