src/Form/Type/ProjectSimulation/ProjectSimulationStepType.php line 24

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\Type\ProjectSimulation;
  4. use App\Entity\ProjectSimulation\ProjectSimulation;
  5. use App\Form\Type\DepartmentType;
  6. use App\Model\Department;
  7. use Sylius\Bundle\AddressingBundle\Form\Type\CountryType;
  8. use Sylius\Component\Channel\Context\ChannelContextInterface;
  9. use Sylius\Component\Locale\Context\LocaleContextInterface;
  10. use Symfony\Component\Form\AbstractType;
  11. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  12. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\Form\FormEvent;
  15. use Symfony\Component\Form\FormEvents;
  16. use Symfony\Component\Validator\Constraints\NotBlank;
  17. use Symfony\Component\Validator\Constraints\Positive;
  18. use Symfony\Component\Validator\Constraints\Type;
  19. final class ProjectSimulationStepType extends AbstractType
  20. {
  21.     private ChannelContextInterface $channelContext;
  22.     private LocaleContextInterface $localeContext;
  23.     public function __construct(ChannelContextInterface $channelContextLocaleContextInterface $localeContext)
  24.     {
  25.         $this->channelContext $channelContext;
  26.         $this->localeContext $localeContext;
  27.     }
  28.     public function buildForm(FormBuilderInterface $builder, array $options): void
  29.     {
  30.         $builder
  31.             //begin step1
  32.             ->add('buildingType'ChoiceType::class, [
  33.                 'label' => 'app.form.project_simulation.building_type',
  34.                 'expanded' => true,
  35.                 'choices'  => [
  36.                     'Maison' => 'house',
  37.                     'Appartement' => 'appartment',
  38.                 ],
  39.                 'attr' => array(
  40.                     'placeholder' => date('Y'),
  41.                     'class' => 'building-type'
  42.                 ),
  43.                 'choice_attr' => ['test' => 'test']
  44.             ])
  45.             ->add('situation'ChoiceType::class, [
  46.                 'label' => 'app.form.project_simulation.situation',
  47.                 'expanded' => true,
  48.                 'choices'  => [
  49.                     'Propriétaire du logement' => 'proprio',
  50.                     'Locataire du logement' => 'locataire',
  51.                 ],
  52.                 'attr' => array(
  53.                     'class' => 'situation',
  54.                     'data-attr' => 'test'
  55.                 ),
  56.                 'choice_attr' => [
  57.                     'Propriétaire du logement' => ['data-color' => 'Red'],
  58.                     'Locataire du logement' => ['data-color' => 'Yellow'],
  59.                 ]
  60.             ])
  61.             ->add('department'ChoiceType::class, [
  62.                 'label' => 'app.form.project_simulation.department',
  63.                 'attr' => ['class' => 'input-like'],
  64.                 'choices' => array_flip(Department::DEPARTMENTS)
  65.             ])
  66.             ->add('yearOfConstruction'IntegerType::class, [
  67.                 'label' => 'app.form.project_simulation.year_of_construction',
  68.                 'constraints' => [
  69.                     new NotBlank(['groups' => ['sylius']]),
  70.                     new Type(['type' => 'numeric''groups' => ['sylius']]),
  71.                 ],
  72.                 'attr' => array(
  73.                     'placeholder' => date('Y'),
  74.                     'class' => 'input-control',
  75.                     'min' => 1800,
  76.                     'max' => date('Y')
  77.                 ),
  78.             ])
  79.             ->add('livingSpace'IntegerType::class, [
  80.                 'label' => 'app.form.project_simulation.living_space',
  81.                 'constraints' => [
  82.                     new NotBlank(['groups' => ['sylius']]),
  83.                     new Type(['type' => 'numeric''groups' => ['sylius']]),
  84.                     new Positive()
  85.                 ],
  86.                 'attr' => array(
  87.                     'placeholder' => 100,
  88.                     'class' => 'input-control',
  89.                     'min' => 0
  90.                 ),
  91.             ])
  92.             //end step1
  93.             //begin step2
  94.             ->add('roofOrientation'ChoiceType::class, [
  95.                 'label' => 'app.form.project_simulation.roof_orientation',
  96.                 'expanded' => true,
  97.                 'choices'  => [
  98.                     'Ouest' => 'Ouest',
  99.                     'Sud-Ouest' => 'Sud-Ouest',
  100.                     'Sud' => 'Sud',
  101.                     'Sud-Est' => 'Sud-Est',
  102.                     'Est' => 'Est',
  103.                 ],
  104.             ])
  105.             ->add('approximateRoofArea'ChoiceType::class, [
  106.                 'label' => 'app.form.project_simulation.approximate_roof_area',
  107.                 'expanded' => true,
  108.                 'choices'  => [
  109.                     '15m²' => 15,
  110.                     '30m²' => 30,
  111.                     '50m²' => 50,
  112.                     '50m²+°' => 1,
  113.                 ],
  114.             ])
  115.             ->add('roofPitch'ChoiceType::class, [
  116.                 'label' => 'app.form.project_simulation.roof_pitch',
  117.                 'expanded' => true,
  118.                 // value = value en pourcentage à prendre en compte dans le calcul
  119.                 'choices'  => $this->getRoofPitchOptions()
  120.             ])
  121.             //end step2
  122.             //begin step3
  123.             ->add('annualConsumptionKwh'IntegerType::class, [
  124.                 'label' => 'app.form.project_simulation.annual_consumption_kwh',
  125.                 'constraints' => [
  126.                     new NotBlank(['groups' => ['sylius']]),
  127.                     new Type(['type' => 'numeric''groups' => ['sylius']]),
  128.                 ],
  129.                 'attr' => array(
  130.                     'placeholder' => 1000,
  131.                     'class' => 'input-control'
  132.                 ),
  133.             ])
  134.             ->add('panelSize'ChoiceType::class, [
  135.                 'label' => 'app.form.project_simulation.panel_size',
  136.                 'choices'  => $this->getPanelSizeArray(),
  137.             ])
  138.             ->add('typeOfHeating'ChoiceType::class, [
  139.                 'label' => 'app.form.project_simulation.type_of_heating',
  140.                 'expanded' => true,
  141.                 'choices'  => [
  142.                     'Gaz / Fioul / Bois' => 'gaz_fioul_bois',
  143.                     'Radiateur électrique' => 'radiateur_electrique',
  144.                     'Pompe à chaleur AIR/EAU' => 'pompe_a_chaleur',
  145.                     'Climatisation split AIR/AIR' => 'climatisation'
  146.                 ],
  147.             ])
  148.             ->add('typeOfDomesticHotWater'ChoiceType::class, [
  149.                 'label' => 'app.form.project_simulation.type_of_domestic_hot_water',
  150.                 'expanded' => true,
  151.                 'choices'  => [
  152.                     'Thermodynamique' => 'thermodynamique',
  153.                     'Gaz / Fioul / Bois' => 'gaz_fioul_bois',
  154.                     'Ballon électrique' => 'ballon_electrique',
  155.                     'Autre' => 'autre',
  156.                 ],
  157.             ])
  158.             ->add('airConditioner'ChoiceType::class, [
  159.                 'expanded' => true,
  160.                 'label' => 'app.form.project_simulation.air_conditioner',
  161.                 'choices' => [
  162.                     'Oui' => true,
  163.                     'Non' => false
  164.                 ]
  165.             ])
  166.             ->add('pool'ChoiceType::class, [
  167.                 'expanded' => true,
  168.                 'label' => 'app.form.project_simulation.pool',
  169.                 'choices' => [
  170.                     'Oui' => true,
  171.                     'Non' => false
  172.                 ]
  173.             ])
  174.             //end step3
  175.             ->addEventListener(FormEvents::POST_SUBMIT, [$this'onPostSubmit'])
  176.         ;
  177.     }
  178.     public function onPostSubmit(FormEvent $event): void
  179.     {
  180.         /** @var ProjectSimulation $data */
  181.         $data $event->getData();
  182.         $data->setLocaleCode($this->localeContext->getLocaleCode());
  183.         $data->setChannel($this->channelContext->getChannel());
  184.     }
  185.     public function getBlockPrefix(): string
  186.     {
  187.         return 'app_project_simulation';
  188.     }
  189.     private function getRoofPitchOptions() {
  190.         $array = [];
  191.         foreach (ProjectSimulation::ROOF_PITCH_COEFFICIENT as $key => $value) {
  192.             $array[$key] = $key;
  193.         }
  194.         return $array;
  195.     }
  196.     private function getPanelSizeArray(): array {
  197.         $panelSizes = [];
  198.         $i 15;
  199.         while ($i <= 60) {
  200.             $panelSizes[$i] = $i;
  201.             $i += 5;
  202.         }
  203.         return $panelSizes;
  204.     }
  205. }