<?php
declare(strict_types=1);
namespace App\Form\Type\ProjectSimulation;
use App\Entity\ProjectSimulation\ProjectSimulation;
use App\Form\Type\DepartmentType;
use App\Model\Department;
use Sylius\Bundle\AddressingBundle\Form\Type\CountryType;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Positive;
use Symfony\Component\Validator\Constraints\Type;
final class ProjectSimulationStepType extends AbstractType
{
private ChannelContextInterface $channelContext;
private LocaleContextInterface $localeContext;
public function __construct(ChannelContextInterface $channelContext, LocaleContextInterface $localeContext)
{
$this->channelContext = $channelContext;
$this->localeContext = $localeContext;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
//begin step1
->add('buildingType', ChoiceType::class, [
'label' => 'app.form.project_simulation.building_type',
'expanded' => true,
'choices' => [
'Maison' => 'house',
'Appartement' => 'appartment',
],
'attr' => array(
'placeholder' => date('Y'),
'class' => 'building-type'
),
'choice_attr' => ['test' => 'test']
])
->add('situation', ChoiceType::class, [
'label' => 'app.form.project_simulation.situation',
'expanded' => true,
'choices' => [
'Propriétaire du logement' => 'proprio',
'Locataire du logement' => 'locataire',
],
'attr' => array(
'class' => 'situation',
'data-attr' => 'test'
),
'choice_attr' => [
'Propriétaire du logement' => ['data-color' => 'Red'],
'Locataire du logement' => ['data-color' => 'Yellow'],
]
])
->add('department', ChoiceType::class, [
'label' => 'app.form.project_simulation.department',
'attr' => ['class' => 'input-like'],
'choices' => array_flip(Department::DEPARTMENTS)
])
->add('yearOfConstruction', IntegerType::class, [
'label' => 'app.form.project_simulation.year_of_construction',
'constraints' => [
new NotBlank(['groups' => ['sylius']]),
new Type(['type' => 'numeric', 'groups' => ['sylius']]),
],
'attr' => array(
'placeholder' => date('Y'),
'class' => 'input-control',
'min' => 1800,
'max' => date('Y')
),
])
->add('livingSpace', IntegerType::class, [
'label' => 'app.form.project_simulation.living_space',
'constraints' => [
new NotBlank(['groups' => ['sylius']]),
new Type(['type' => 'numeric', 'groups' => ['sylius']]),
new Positive()
],
'attr' => array(
'placeholder' => 100,
'class' => 'input-control',
'min' => 0
),
])
//end step1
//begin step2
->add('roofOrientation', ChoiceType::class, [
'label' => 'app.form.project_simulation.roof_orientation',
'expanded' => true,
'choices' => [
'Ouest' => 'Ouest',
'Sud-Ouest' => 'Sud-Ouest',
'Sud' => 'Sud',
'Sud-Est' => 'Sud-Est',
'Est' => 'Est',
],
])
->add('approximateRoofArea', ChoiceType::class, [
'label' => 'app.form.project_simulation.approximate_roof_area',
'expanded' => true,
'choices' => [
'15m²' => 15,
'30m²' => 30,
'50m²' => 50,
'50m²+°' => 1,
],
])
->add('roofPitch', ChoiceType::class, [
'label' => 'app.form.project_simulation.roof_pitch',
'expanded' => true,
// value = value en pourcentage à prendre en compte dans le calcul
'choices' => $this->getRoofPitchOptions()
])
//end step2
//begin step3
->add('annualConsumptionKwh', IntegerType::class, [
'label' => 'app.form.project_simulation.annual_consumption_kwh',
'constraints' => [
new NotBlank(['groups' => ['sylius']]),
new Type(['type' => 'numeric', 'groups' => ['sylius']]),
],
'attr' => array(
'placeholder' => 1000,
'class' => 'input-control'
),
])
->add('panelSize', ChoiceType::class, [
'label' => 'app.form.project_simulation.panel_size',
'choices' => $this->getPanelSizeArray(),
])
->add('typeOfHeating', ChoiceType::class, [
'label' => 'app.form.project_simulation.type_of_heating',
'expanded' => true,
'choices' => [
'Gaz / Fioul / Bois' => 'gaz_fioul_bois',
'Radiateur électrique' => 'radiateur_electrique',
'Pompe à chaleur AIR/EAU' => 'pompe_a_chaleur',
'Climatisation split AIR/AIR' => 'climatisation'
],
])
->add('typeOfDomesticHotWater', ChoiceType::class, [
'label' => 'app.form.project_simulation.type_of_domestic_hot_water',
'expanded' => true,
'choices' => [
'Thermodynamique' => 'thermodynamique',
'Gaz / Fioul / Bois' => 'gaz_fioul_bois',
'Ballon électrique' => 'ballon_electrique',
'Autre' => 'autre',
],
])
->add('airConditioner', ChoiceType::class, [
'expanded' => true,
'label' => 'app.form.project_simulation.air_conditioner',
'choices' => [
'Oui' => true,
'Non' => false
]
])
->add('pool', ChoiceType::class, [
'expanded' => true,
'label' => 'app.form.project_simulation.pool',
'choices' => [
'Oui' => true,
'Non' => false
]
])
//end step3
->addEventListener(FormEvents::POST_SUBMIT, [$this, 'onPostSubmit'])
;
}
public function onPostSubmit(FormEvent $event): void
{
/** @var ProjectSimulation $data */
$data = $event->getData();
$data->setLocaleCode($this->localeContext->getLocaleCode());
$data->setChannel($this->channelContext->getChannel());
}
public function getBlockPrefix(): string
{
return 'app_project_simulation';
}
private function getRoofPitchOptions() {
$array = [];
foreach (ProjectSimulation::ROOF_PITCH_COEFFICIENT as $key => $value) {
$array[$key] = $key;
}
return $array;
}
private function getPanelSizeArray(): array {
$panelSizes = [];
$i = 15;
while ($i <= 60) {
$panelSizes[$i] = $i;
$i += 5;
}
return $panelSizes;
}
}