<?php
namespace App\Form;
use App\Entity\BusinessCardOption;
use App\Entity\Imposition;
use App\Entity\Machine;
use App\Entity\MachinePaperSizeMatrix;
use App\Entity\PaperSize;
use App\Entity\Product;
use App\Entity\Task;
use App\Form\DataTransformer\PrintingTypeToNumberTransformer;
use App\Repository\MachinePaperSizeMatrixRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BusinessCardOptionType extends AbstractType
{
private $printingTypeToNumberTransformer;
private EntityManagerInterface $entityManager;
public function __construct(PrintingTypeToNumberTransformer $printingTypeToNumberTransformer, EntityManagerInterface $em)
{
$this->printingTypeToNumberTransformer = $printingTypeToNumberTransformer;
$this->entityManager = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name', TextType::class, [
'required' => false,
'attr' =>[
'class' => 'form-control-sm',
],
])
->add('printingType', EntityType::class,[
'choices' => [],
'class' => Machine::class,
'expanded' => false,
'required' => true,
'multiple' => false,
'empty_data' => null,
'choice_label' => 'name',
'attr' => [
'class' => 'printingTypeSelect',
'required' => 'required',
]
])
->add('machine', EntityType::class,[
'choices' => [],
'class' => Machine::class,
'expanded' => false,
'required' => true,
'multiple' => false,
'choice_label' => 'name',
'by_reference' => true,
'attr' => [
'class' => 'machineSelect',
'required' => 'required'
]
])
->add('paperSize', EntityType::class,[
'choices' => [],
'class' => PaperSize::class,
'expanded' => false,
'required' => false,
'choice_label' => 'name',
'by_reference' => true,
'attr' => [
'class' => 'paperSizeSelect',
'required' => 'required'
]
])
->add('finishedSizeX', IntegerType::class,[
/*'row_attr' => [
'class' => 'input-group',
],*/
'label' => 'Finished Size X',
'attr' =>[
'class' => 'finishedSizeX form-control-sm text-end',
'min' => 1
],
'required' => true
])
->add('finishedSizeY', IntegerType::class,[
'label' => 'Finished Size Y',
'attr' =>[
'class' => 'finishedSizeY form-control-sm text-end',
'min' => 1
],
'required' => true
])
->add('imposition', EntityType::class,[
'choices' => [],
'class' => Imposition::class,
'expanded' => false,
'required' => false,
'choice_label' => 'name',
'by_reference' => true,
'attr' => [
'class' => 'impositionSelect',
'required' => 'required'
]
])
->add('paperSearch', EntityType::class,[
'mapped' => false,
'choices' => [],
'class' => Product::class,
'expanded' => false,
'required' => false,
'choice_label' => 'name',
'by_reference' => true,
'attr' => [
'class' => 'paperSearchSelect',
/*'required' => 'required'*/
]
])
->add('paperType', EntityType::class,[
'choices' => [],
'class' => Product::class,
'expanded' => false,
'required' => false,
'choice_label' => 'name',
'by_reference' => true,
'attr' => [
'class' => 'paperTypeSelect',
/*'required' => 'required'*/
]
])
->add('sideOne', ChoiceType::class, [
'choices' => [
],
'attr' => [
'class' => 'sideOneSelect form-select-sm',
'style' => 'width:70%;height:32px',
'required' => 'required'
],
'required' => true
])
->add('sideTwo', ChoiceType::class, [
'choices' => [
],
'attr' => [
'class' => 'sideTwoSelect form-select-sm',
'style' => 'width:70%;height:32px',
'required' => 'required'
],
'required' => true
])
->add('searchTask',ChoiceType::class, [
'mapped' => false,
'label' => false,
'attr' => [
'class' => 'taskSelect'
],
'required' => false
])
/* ->add('tasks', EntityType::class,[
'choices' => [],
'class' => Task::class,
'multiple' => true,
]);*/
->add('tasks', CollectionType::class,[
'entry_type' => BusinessCardOptionTaskType::class,
'entry_options' => [
'label' => false
],
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
'label' => false,
'required' => false,
])
;
/* $builder->get('printingType')
->addModelTransformer(
$this->printingTypeToNumberTransformer
);*/
/* $builder->get('printingType')
->addViewTransformer(
$this->printingTypeToNumberTransformer
);*/
/*$builder->get('tasks')
->addModelTransformer(new CallbackTransformer(
function ($tasksAsArray): string {
// transform the array to a string
if ($tasksAsArray)
return implode(', ', $tasksAsArray);
},
function ($tasksAsString): array {
// transform the string back to an array
return explode(', ', $tasksAsString);
}
))
;*/
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function(FormEvent $event) use ($options){
$data = $event->getData();
$form = $event->getForm();
if (!$data) {
if ($options['testing'] === true) {
//For testing
$printingType = $this->entityManager->getRepository(Machine::class)->find(4);
$machine = $this->entityManager->getRepository(Machine::class)->find(2);
$paperSize = $this->entityManager->getRepository(PaperSize::class)->find(1);
$imposition = $this->entityManager->getRepository(Imposition::class)->find(1);
$paper = $this->entityManager->getRepository(Product::class)->find(5398);
//Side one
/** @var $machinePaperSizeMatrix MachinePaperSizeMatrix*/
$machinePaperSizeMatrix = $this->entityManager->getRepository(MachinePaperSizeMatrix::class)->findOneBy([
'paperSize' => $paperSize->getId(),
'machine' => $machine->getId()
]);
$sideOne = [
$machinePaperSizeMatrix->getColourClickLabel() => $machinePaperSizeMatrix->getColourClickLabel(),
$machinePaperSizeMatrix->getBlackOnlyClickLabel() => $machinePaperSizeMatrix->getBlackOnlyClickLabel()
];
$sideTwo = [
'none' => 'none',
$machinePaperSizeMatrix->getColourClickLabel() => $machinePaperSizeMatrix->getColourClickLabel(),
$machinePaperSizeMatrix->getBlackOnlyClickLabel() => $machinePaperSizeMatrix->getBlackOnlyClickLabel()
];
$form->add('printingType', EntityType::class, [
'choices' => [$printingType],
'class' => Machine::class,
'choice_label' => function (Machine $machine): string {
return $machine->getCalculatorDisplayName();
},
'attr' => [
'class' => 'printingTypeSelect'
]
])->add('machine', EntityType::class, [
'choices' => [$machine],
'class' => Machine::class,
'expanded' => false,
'required' => true,
'choice_label' => function (Machine $machine): string {
return $machine->getCalculatorDisplayName();
},
'by_reference' => true,
'attr' => [
'class' => 'machineSelect'
]
])->add('paperSize', EntityType::class, [
'choices' => [$paperSize],
'class' => PaperSize::class,
'expanded' => false,
'choice_label' => function (PaperSize $paperSize): string {
return $paperSize->getCalculatorDisplayName();
},
'attr' => [
'class' => 'paperSizeSelect'
]
])->add('imposition', EntityType::class, [
'choices' => [$imposition],
'class' => Imposition::class,
'expanded' => false,
'choice_label' => function (Imposition $imposition): string {
return $imposition->getCalculatorDisplayName();
},
'attr' => [
'class' => 'impositionSelect'
]
]);
//
$form->add('paperSearch', EntityType::class, [
'choices' => !is_null($paper)?[$paper]:[],
'mapped' => false,
'class' => Product::class,
'required' => false,
'expanded' => false,
'choice_label' => function (Product $product): string {
return $product->getCalculatorPaperSearchDisplayName();
},
'by_reference' => true,
'attr' => [
'class' => 'paperSearchSelect'
]
]);
$form->add('sideOne', ChoiceType::class, [
'choices' => [$sideOne],
'required' => true,
'attr' => [
'class' => 'sideOneSelect form-select-sm',
'style' => 'width:70%;height:32px'
]
]);
$form->add('sideTwo', ChoiceType::class, [
'choices' => [$sideTwo],
'required' => true,
'attr' => [
'class' => 'sideTwoSelect form-select-sm',
'style' => 'width:70%;height:32px'
]
]);
}else{
$form->add('paperSearch', EntityType::class, [
'choices' => [],
'mapped' => false,
'class' => Product::class,
'required' => false,
'expanded' => false,
'by_reference' => true,
'attr' => [
'class' => 'paperSearchSelect'
]
]);
$form->add('searchTask',ChoiceType::class, [
'choices' => [],
'mapped' => false,
'label' => false,
'attr' => [
'class' => 'taskSelect'
],
'required' => false
]);
}
}else{
$form->add('printingType', EntityType::class,[
'choices' => [$data->getPrintingType()],
'class' => Machine::class,
'expanded' => false,
'required' => true,
'choice_label' => function (Machine $machine): string {
return $machine->getCalculatorDisplayName();
},
'by_reference' => true,
'attr' => [
'class' => 'printingTypeSelect'
]
]);
$form->add('machine', EntityType::class,[
'choices' => [$data->getMachine()],
'class' => Machine::class,
'expanded' => false,
'required' => true,
'choice_label' => function (Machine $machine): string {
return $machine->getCalculatorDisplayName();
},
'by_reference' => true,
'attr' => [
'class' => 'machineSelect'
]
])->add('paperSize', EntityType::class,[
'choices' => [$data->getPaperSize()],
'class' => PaperSize::class,
'expanded' => false,
'required' => false,
'choice_label' => function (PaperSize $paperSize): string {
return $paperSize->getCalculatorDisplayName();
},
'by_reference' => true,
'attr' => [
'class' => 'paperSizeSelect'
]
])
->add('imposition', EntityType::class,[
'choices' => [$data->getImposition()],
'class' => Imposition::class,
'expanded' => false,
'required' => true,
'choice_label' => 'name',
'by_reference' => true,
'attr' => [
'class' => 'impositionSelect'
]
]);
//
$paperChoices =
$form->add('paperType', EntityType::class,[
'choices' => !is_null($data->getPaperType())?[$data->getPaperType()]:[],
'class' => Product::class,
'expanded' => false,
'required' => false,
'choice_label' => function (Product $product): string {
return $product->getCalculatorPaperDisplayName();
},
'by_reference' => true,
'attr' => [
'class' => 'paperTypeSelect'
]
]);
$form->add('sideOne', ChoiceType::class, [
'choices' => [$data->getSideOne() => $data->getSideOne()],
'required' => true,
'attr' => [
'class' => 'sideOneSelect form-select-sm',
'style' => 'width:70%;height:32px'
]
]);
$form->add('sideTwo', ChoiceType::class, [
'choices' => [$data->getSideTwo() => $data->getSideTwo()],
'required' => true,
'attr' => [
'class' => 'sideTwoSelect form-select-sm',
'style' => 'width:70%;height:32px'
]
]);
$form->add('paperSearch', EntityType::class, [
'choices' => [],
'mapped' => false,
'class' => Product::class,
'required' => false,
'expanded' => false,
'by_reference' => true,
'attr' => [
'class' => 'paperSearchSelect'
]
]);
}
});
/* $builder->addEventListener(
FormEvents::SUBMIT,
function(FormEvent $event) use ($options){
$test = $event->getData();
});*/
$builder->addEventListener(
FormEvents::PRE_SUBMIT,
function(FormEvent $event) use ($options){
$data = $event->getData();
$form = $event->getForm();
//Set printing Type
if (isset($data['printingType']) && $data['printingType'] != null){
$printingType = $this->entityManager
->getRepository(Machine::class)
// query for the issue with this id
->find($data['printingType']);
$form->add('printingType', EntityType::class,[
'choices' => [$printingType],
'class' => Machine::class,
]);
}
//Machine
if (isset($data['machine']) && $data['machine'] != null){
$machine = $this->entityManager
->getRepository(Machine::class)
// query for the issue with this id
->find($data['machine'])
;
$form->add('machine', EntityType::class,[
'choices' => [$machine],
'class' => Machine::class,
]);
}
//Imposition
if (isset($data['imposition']) && $data['imposition'] != null){
$imposition = $this->entityManager
->getRepository(Imposition::class)
// query for the issue with this id
->find($data['imposition']);
$form->add('imposition', EntityType::class,[
'choices' => [$imposition],
'class' => Imposition::class,
]);
}
//PaperSize
if (isset($data['paperSize']) && $data['paperSize'] != null){
$paperSize = $this->entityManager
->getRepository(PaperSize::class)
// query for the issue with this id
->find($data['paperSize']);
$form->add('paperSize', EntityType::class,[
'choices' => [$paperSize],
'class' => PaperSize::class,
]);
}
//Paper type - Product
if (isset($data['paperType']) && $data['paperType'] != null){
$paperType = $this->entityManager
->getRepository(Product::class)
// query for the issue with this id
->find($data['paperType']);
$form->add('paperType', EntityType::class,[
'choices' => [$paperType],
'class' => Product::class,
]);
}
if (isset($data['tasks']) && $data['tasks'] != null){
$tasks = $this->entityManager
->getRepository(Task::class)
// query for the issue with this id
->findBy(['id' => $data['tasks']]);
$form->remove('tasks');
$form->add('tasks', EntityType::class,[
'choices' => [$tasks],
'class' => Task::class,
'multiple' => true
]);
}else{
//No task selected
$form->add('tasks', EntityType::class,[
'choices' => [],
'class' => Task::class,
'multiple' => true
]);
//$form->remove('tasks');
}
/*$form->remove('paperSearch');
$form->remove('searchTask');*/
if (isset($data['sideOne']) && $data['sideOne'] != null){
$form->add('sideOne', TextType::class,[
'data' => $data['sideOne']
]);
}
if (isset($data['sideTwo']) && $data['sideTwo'] != null){
$form->add('sideTwo', TextType::class,[
'data' => $data['sideTwo']
]);
}
$form->remove('paperSearch');
$form->remove('searchTask');
});
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => BusinessCardOption::class,
'allow_extra_fields' => true,
'testing' => false
]);
}
}