Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions assets/controllers/elements/select_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ export default class extends Controller {

}

return '<div>' + escape(data.text) + '</div>';
if (data.class) {
return '<div><span class="' + escape(data.class) + '">' + escape(data.text) + '</span></div>';
} else {
return '<div>' + escape(data.text) + '</div>';
}
}

renderOption(data, escape) {
Expand All @@ -109,7 +113,11 @@ export default class extends Controller {
return '<div>&nbsp;</div>';
}

return '<div>' + escape(data.text) + '</div>';
if (data.class) {
return '<div><span class="' + escape(data.class) + '">' + escape(data.text) + '</span></div>';
} else {
return '<div>' + escape(data.text) + '</div>';
}
}

disconnect() {
Expand Down
46 changes: 46 additions & 0 deletions migrations/Version20260903120000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use App\Migration\AbstractMultiPlatformMigration;
use Doctrine\DBAL\Schema\Schema;

final class Version20260903120000 extends AbstractMultiPlatformMigration
{
public function getDescription(): string
{
return 'Add nullable color column to part_custom_states table (semantic Bootstrap color used to render the state as a badge)';
}

public function mySQLUp(Schema $schema): void
{
$this->addSql('ALTER TABLE part_custom_states ADD color VARCHAR(20) DEFAULT NULL');
}

public function mySQLDown(Schema $schema): void
{
$this->addSql('ALTER TABLE part_custom_states DROP COLUMN color');
}

public function sqLiteUp(Schema $schema): void
{
$this->addSql('ALTER TABLE part_custom_states ADD COLUMN color VARCHAR(20) DEFAULT NULL');
}

public function sqLiteDown(Schema $schema): void
{
$this->addSql('ALTER TABLE part_custom_states DROP COLUMN color');
}

public function postgreSQLUp(Schema $schema): void
{
$this->addSql('ALTER TABLE part_custom_states ADD color VARCHAR(20) DEFAULT NULL');
}

public function postgreSQLDown(Schema $schema): void
{
$this->addSql('ALTER TABLE part_custom_states DROP COLUMN color');
}
}
17 changes: 17 additions & 0 deletions src/DataTables/Helpers/PartDataTableHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use App\Entity\ProjectSystem\Project;
use App\Entity\Attachments\Attachment;
use App\Entity\Parts\Part;
use App\Entity\Parts\PartCustomState;
use App\Services\Attachments\AttachmentURLGenerator;
use App\Services\Attachments\PartPreviewGenerator;
use App\Services\EntityURLGenerator;
Expand Down Expand Up @@ -170,6 +171,22 @@ public function renderEdaStatus(Part $context): string
return sprintf('<a href="%s" data-turbo="false">%s</a>', $editUrl, $statusIcon);
}

/**
* Renders the custom state of a part as the colored badge it is configured with.
* Returns an empty string if the part has no custom state.
*/
public function renderPartCustomState(?PartCustomState $state): string
{
if ($state === null) {
return '';
}

return sprintf('<span class="badge %s">%s</span>',
htmlspecialchars($state->getBadgeClass()),
htmlspecialchars($state->getName())
);
}

public function renderAmount(Part $context): string
{
$amount = $context->getAmountSum();
Expand Down
13 changes: 3 additions & 10 deletions src/DataTables/PartsDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,11 @@ public function configure(DataTable $dataTable, array $options): void
return $tmp;
}
])
->add('partCustomState', TextColumn::class, [
->add('partCustomState', HTMLColumn::class, [
'label' => $this->translator->trans('part.table.partCustomState'),
'orderField' => 'NATSORT(_partCustomState.name)',
'data' => function(Part $context): string {
$partCustomState = $context->getPartCustomState();

if ($partCustomState === null) {
return '';
}

return $partCustomState->getName();
}
'data' => fn(Part $context): string
=> $this->partDataTableHelper->renderPartCustomState($context->getPartCustomState()),
])
->add('addedDate', LocaleDateTimeColumn::class, [
'label' => $this->translator->trans('part.table.addedDate'),
Expand Down
8 changes: 8 additions & 0 deletions src/DataTables/ProjectBomEntriesDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ public function configure(DataTable $dataTable, array $options): void
},
])

->add('partCustomState', HTMLColumn::class, [
'label' => $this->translator->trans('part.table.partCustomState'),
'orderField' => 'NATSORT(partCustomState.name)',
'visible' => false,
'data' => fn (ProjectBOMEntry $context): string
=> $this->partDataTableHelper->renderPartCustomState($context->getPart()?->getPartCustomState()),
])

->add('mountnames', HTMLColumn::class, [
'label' => 'project.bom.mountnames',
'data' => function (ProjectBOMEntry $context) {
Expand Down
38 changes: 35 additions & 3 deletions src/Entity/Parts/PartCustomState.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@

namespace App\Entity\Parts;

use ApiPlatform\Metadata\ApiProperty;
use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\PartCustomStateAttachment;
use ApiPlatform\Doctrine\Common\Filter\DateFilterInterface;
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
Expand All @@ -39,9 +37,12 @@
use ApiPlatform\Metadata\Post;
use ApiPlatform\Serializer\Filter\PropertyFilter;
use App\ApiPlatform\Filter\LikeFilter;
use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\PartCustomStateAttachment;
use App\Entity\Base\AbstractPartsContainingDBElement;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Parameters\PartCustomStateParameter;
use App\Helpers\BootstrapColor;
use App\Mcp\DTO\ElementByIdInput;
use App\Mcp\DTO\StructuralElementOverview;
use App\Mcp\DTO\StructuralElementSearchInput;
Expand All @@ -50,6 +51,7 @@
use App\State\Mcp\ListStructuralElementsProcessor;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Validator\Constraints as Assert;
Expand Down Expand Up @@ -108,6 +110,14 @@ class PartCustomState extends AbstractPartsContainingDBElement
#[Groups(['part_custom_state:read', 'part_custom_state:write', 'full', 'import'])]
protected string $comment = '';

/**
* @var BootstrapColor|null The semantic color this state is rendered as a badge with.
* Null keeps the default, uncolored appearance Part-DB used before this field existed.
*/
#[ORM\Column(type: Types::STRING, length: 20, nullable: true, enumType: BootstrapColor::class)]
#[Groups(['part_custom_state:read', 'part_custom_state:write', 'full', 'import'])]
protected ?BootstrapColor $color = null;

#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent', cascade: ['persist'])]
#[ORM\OrderBy(['name' => 'ASC'])]
protected Collection $children;
Expand Down Expand Up @@ -152,4 +162,26 @@ public function __construct()
$this->attachments = new ArrayCollection();
$this->parameters = new ArrayCollection();
}

public function getColor(): ?BootstrapColor
{
return $this->color;
}

public function setColor(?BootstrapColor $color): self
{
$this->color = $color;

return $this;
}

/**
* Returns the CSS class this state is rendered as a badge with, everywhere it is shown.
* Without a configured color this is the color Part-DB used before the color existed, so an unconfigured
* state keeps looking exactly the way it did.
*/
public function getBadgeClass(): string
{
return $this->color?->toBadgeClass() ?? 'bg-primary';
}
}
22 changes: 22 additions & 0 deletions src/Form/AdminPages/PartCustomStateAdminForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@

namespace App\Form\AdminPages;

use App\Entity\Base\AbstractNamedDBElement;
use App\Entity\Parts\PartCustomState;
use App\Form\Type\BootstrapColorType;
use App\Helpers\BootstrapColor;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\FormBuilderInterface;

class PartCustomStateAdminForm extends BaseEntityAdminForm
{
protected function additionalFormElements(FormBuilderInterface $builder, array $options, AbstractNamedDBElement $entity): void
{
if (!$entity instanceof PartCustomState) {
return;
}

$is_new = null === $entity->getID();

$builder->add('color', BootstrapColorType::class, [
'required' => false,
'label' => 'part_custom_state.color.label',
'help' => 'part_custom_state.color.help',
'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity),
]);
}
}
46 changes: 46 additions & 0 deletions src/Form/Type/BootstrapColorType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 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\Form\Type;

use App\Helpers\BootstrapColor;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class BootstrapColorType extends AbstractType
{
public function getParent(): string
{
return EnumType::class;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'class' => BootstrapColor::class,
'choice_label' => fn (BootstrapColor $color) => $color->toTranslationKey(),
'choice_attr' => fn (BootstrapColor $color) => ['data-class' => 'badge '. $color->toBadgeClass()],
]);
}
}
53 changes: 53 additions & 0 deletions src/Helpers/BootstrapColor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2022 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\Helpers;

/**
* The semantic Bootstrap color a PartCustomState can be rendered with.
* This is a closed whitelist: no free-form CSS classes or colors can be stored.
*/
enum BootstrapColor: string
{
case PRIMARY = 'primary';
case SECONDARY = 'secondary';
case INFO = 'info';
case SUCCESS = 'success';
case WARNING = 'warning';
case DANGER = 'danger';
case LIGHT = 'light';
case DARK = 'dark';

public function toTranslationKey(): string
{
return 'part_custom_state.color.' . $this->value;
}

/**
* Maps this color to the fixed Bootstrap badge class it is rendered with.
* This is the only place that translates a stored color into a CSS class.
*/
public function toBadgeClass(): string
{
return 'text-bg-' . $this->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@

/**
* Shared denormalize()/supportsDenormalization() implementation for AbstractResourceIriNormalizer and
* AbstractResourceIriDenormalizer. The using class must provide an $inner (de)normalizer and an
* IriConverterInterface $iriConverter property.
* AbstractResourceIriDenormalizer. The using class must provide an $inner (de)normalizer, an
* IriConverterInterface $iriConverter property and a ResourceClassResolverInterface $resourceClassResolver
* property.
*
* It works around a bug in API Platform's AbstractItemNormalizer where IRI strings for abstract resource classes
* with a discriminator map fail deserialization when objectToPopulate is null (the discriminator is checked before
Expand All @@ -40,7 +41,18 @@ trait AbstractResourceIriDenormalizationTrait
{
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
{
if (is_string($data) || (is_array($data) && isset($data['@id']) && is_string($data['@id']))) {
// API Platform's AbstractItemNormalizer has a bug: when objectToPopulate is null and data is an IRI
// string, it tries to resolve the discriminator class from [$iri_string] before reaching the IRI
// check (line 271). For abstract resource classes with a discriminator map (e.g. Attachment), this
// fails because the array has no _type key. Fix by resolving IRI strings directly.
// See: https://github.com/Part-DB/Part-DB-server/issues/1370
//
// $type must actually be an API resource for this to make sense: this normalizer also runs for plain
// value objects (e.g. backed enums) nested inside a resource, and a string value there is the enum's
// scalar value, not an IRI - treating it as one silently swallows the value instead of letting the
// regular (enum) normalizer handle it.
if ($this->resourceClassResolver->isResourceClass($type)
&& (is_string($data) || (is_array($data) && isset($data['@id']) && is_string($data['@id'])))) {
$iri = is_array($data) ? $data['@id'] : $data;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace App\Serializer\APIPlatform;

use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Serializer\ItemDenormalizer;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
Expand All @@ -45,6 +46,7 @@ class AbstractResourceIriDenormalizer implements DenormalizerInterface, Serializ
public function __construct(
private readonly ItemDenormalizer $inner,
private readonly IriConverterInterface $iriConverter,
private readonly ResourceClassResolverInterface $resourceClassResolver,
) {
}

Expand Down
Loading
Loading