- Remove TypeMachine, TypeMachineComponentRequirement, TypeMachinePieceRequirement, TypeMachineProductRequirement entities and related repositories/state processor - Replace MachineSkeletonController with MachineStructureController - Link CustomField directly to Machine instead of TypeMachine - Add migration to drop TypeMachine tables and migrate custom fields to machines - Fix ModelType serialization: Annotation\Groups → Attribute\Groups (Symfony 8 compat) and add product:read, composant:read, piece:read groups for embedded category display - Fix Profile: same Annotation → Attribute import - Fix SearchFilter: partial → ipartial on Comment and Document - Update frontend submodule (remove skeleton pages/components, simplify machine creation) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
73 lines
2.5 KiB
PHP
73 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\CustomField;
|
|
use App\Entity\CustomFieldValue;
|
|
use App\Entity\Machine;
|
|
use App\Repository\CustomFieldValueRepository;
|
|
use App\Repository\MachineRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
#[Route('/api/machines')]
|
|
class MachineCustomFieldsController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly MachineRepository $machineRepository,
|
|
private readonly CustomFieldValueRepository $customFieldValueRepository,
|
|
) {}
|
|
|
|
#[Route('/{id}/add-custom-fields', name: 'machine_add_custom_fields', methods: ['POST'])]
|
|
public function addMissingCustomFields(string $id): JsonResponse
|
|
{
|
|
$this->denyAccessUnlessGranted('ROLE_GESTIONNAIRE');
|
|
|
|
$machine = $this->machineRepository->find($id);
|
|
if (!$machine instanceof Machine) {
|
|
return $this->json(['success' => false, 'error' => 'Machine not found.'], 404);
|
|
}
|
|
|
|
foreach ($machine->getCustomFields() as $customField) {
|
|
if (!$customField instanceof CustomField) {
|
|
continue;
|
|
}
|
|
$existing = $this->customFieldValueRepository->findOneBy([
|
|
'machine' => $machine,
|
|
'customField' => $customField,
|
|
]);
|
|
if ($existing instanceof CustomFieldValue) {
|
|
continue;
|
|
}
|
|
|
|
$value = new CustomFieldValue();
|
|
$value->setMachine($machine);
|
|
$value->setCustomField($customField);
|
|
$value->setValue($customField->getDefaultValue() ?? '');
|
|
$this->entityManager->persist($value);
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
|
|
$values = $this->customFieldValueRepository->findBy(['machine' => $machine]);
|
|
|
|
return $this->json([
|
|
'success' => true,
|
|
'machineId' => $machine->getId(),
|
|
'customFieldValues' => array_map(
|
|
static fn (CustomFieldValue $value) => [
|
|
'id' => $value->getId(),
|
|
'value' => $value->getValue(),
|
|
'customFieldId' => $value->getCustomField()->getId(),
|
|
],
|
|
$values
|
|
),
|
|
]);
|
|
}
|
|
}
|