src/Nordpeis/Bundle/CalculatorBundle/Form/Type/CalculatorType.php line 14

Open in your IDE?
  1. <?php
  2. namespace Nordpeis\Bundle\CalculatorBundle\Form\Type;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\Extension\Core\Type\ButtonType;
  6. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  7. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  8. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11. class CalculatorType extends AbstractType
  12. {
  13.     private $entityManager;
  14.     public function __construct(EntityManagerInterface $entityManager)
  15.     {
  16.         $this->entityManager $entityManager;
  17.     }
  18.     public function configureOptions(OptionsResolver $resolver)
  19.     {
  20.         $resolver->setDefaults(array(
  21.             'attr' => array(
  22.                 'name' => $this->getName() . 'Form',
  23.                 'novalidate' => 'novalidate',
  24.             ),
  25.         ));
  26.     }
  27.     public function getName()
  28.     {
  29.         return 'calculator';
  30.     }
  31.     public function buildForm(FormBuilderInterface $builder, array $options)
  32.     {
  33.         $producers $this->getProducers();
  34.         $fireplaces $this->getFireplaces();
  35.         $pipes $this->getPipes();
  36.         // $coverPlates = $this->getCoverPlates();
  37.         $floors $this->getFloors();
  38.         $roofTypes $this->getRoofTypes();
  39.         $colors $this->getColors();
  40.         $ventilationModules $this->getVentilationModules();
  41.         // $pipehats = $this->getPipeHats();
  42.         $builder->add(
  43.             'producer',
  44.             ChoiceType::class,
  45.             array(
  46.                 'label' => 'Producer',
  47.                 'placeholder' => '- select -',
  48.                 'choices' => $producers,
  49.                 'attr' => isset($options['data']['producer']) ? array(
  50.                     'ng-init' => "calculator.producer='" $options['data']['producer'] . "'",
  51.                 ) : array(),
  52.             )
  53.         );
  54.         $builder->add(
  55.             'fireplace',
  56.             ChoiceType::class,
  57.             array(
  58.                 'label' => 'Fireplace',
  59.                 'placeholder' => '- select -',
  60.                 'choices' => [],
  61.                 'attr' => array(
  62.                     'ng-disabled' => 'loadingFireplace',
  63.                     'ng-show' => 'calculator.producer && hasFireplace',
  64.                     'ng-required' => 'calculator.producer && hasFireplace',
  65.                     'ng-options' => "fireplace.type for fireplace in fireplaces | orderBy:'name' track by fireplace.id",
  66.                 )
  67.             )
  68.         );
  69.         $builder->add(
  70.             'pipe',
  71.             ChoiceType::class,
  72.             array(
  73.                 'multiple' => false,
  74.                 'expanded' => true,
  75.                 'label' => 'Pipe dimension',
  76.                 'choices' => [],
  77.                 'attr' => array(
  78.                     'ng-disabled' => 'loadingFireplace',
  79.                     'ng-show' => 'calculator.fireplace && hasPipeOptions',
  80.                     'ng-required' => 'calculator.fireplace && hasPipeOptions',
  81.                     'ng-repeat' => 'pipe in pipes',
  82.                     'ng-checked' => 'calculator.pipe == pipe.id',
  83.                     'ng-label' => 'pipe.type',
  84.                     'ng-value' => 'pipe.id',
  85.                     'value' => 'pipe.id',
  86.                     'ng-radio' => true,
  87.                 ),
  88.             )
  89.         );
  90.         $builder->add(
  91.             'mounting',
  92.             ChoiceType::class,
  93.             array(
  94.                 'label' => 'Mounting',
  95.                 'placeholder' => '- select -',
  96.                 'choices' => array(),
  97.                 'required' => 'true',
  98.                 'attr' => array(
  99.                     'ng-disabled' => 'loadingFireplace',
  100.                     'ng-show' => 'calculator.fireplace',
  101.                     // 'ng-change' => 'mountingChange()',
  102.                     'ng-options' => "scheme.name for scheme in schemes | orderBy:'name' track by scheme.id",
  103.                 ),
  104.             )
  105.         );
  106.         $builder->add(
  107.             'is_oppheng',
  108.             CheckboxType::class,
  109.             array(
  110.                 'label' => 'Oppheng?',
  111.                 'required' => false,
  112.                 'attr' => array(
  113.                     'ng-disabled' => 'loadingFireplace',
  114.                     'ng-show' => 'calculator.fireplace && calculator.mounting.mount_option',
  115.                 ),
  116.             )
  117.         );
  118.         $builder->add(
  119.             'cover_plate',
  120.             ChoiceType::class,
  121.             array(
  122.                 'multiple' => false,
  123.                 'expanded' => true,
  124.                 'label' => 'Cover plates',
  125.                 'choices' => [],
  126.                 'attr' => array(
  127.                     'ng-disabled' => 'loadingFireplace',
  128.                     // 'ng-show' => 'calculator.fireplace && calculator.mounting && calculator.mounting.mounting_id !== 4',
  129.                     'ng-required' => 'calculator.fireplace && calculator.mounting && calculator.mounting.mounting_id !== 4',
  130.                     'ng-repeat' => 'coverPlate in coverPlates',
  131.                     'ng-checked' => 'calculator.cover_plate == coverPlate.id',
  132.                     'ng-label' => 'coverPlate.type',
  133.                     'ng-value' => 'coverPlate.id',
  134.                     'value' => 'coverPlate.id',
  135.                     'ng-radio' => true,
  136.                 ),
  137.             )
  138.         );
  139.         $builder->add(
  140.             'floor',
  141.             ChoiceType::class,
  142.             array(
  143.                 'label' => 'Floors',
  144.                 'placeholder' => '- select -',
  145.                 'choices' => $floors,
  146.                 'attr' => array(
  147.                     'ng-disabled' => 'loadingFireplace',
  148.                     'ng-show' => 'calculator.fireplace',
  149.                 ),
  150.             )
  151.         );
  152.         $builder->add(
  153.             'upper_floor_living_room',
  154.             CheckboxType::class,
  155.             array(
  156.                 'label' => 'Is the upper floor a living room?',
  157.                 'required' => false,
  158.                 'attr' => array(
  159.                     'ng-disabled' => 'loadingFireplace',
  160.                     'ng-show' => 'calculator.fireplace && hasUpperFloorLivingRoom',
  161.                 ),
  162.             )
  163.         );
  164.         $builder->add(
  165.             'roof',
  166.             ChoiceType::class,
  167.             array(
  168.                 'label' => 'Roof type',
  169.                 'placeholder' => '- select -',
  170.                 'choices' => $roofTypes,
  171.                 'attr' => array(
  172.                     'ng-disabled' => 'loadingFireplace',
  173.                     'ng-show' => 'calculator.fireplace',
  174.                 ),
  175.             )
  176.         );
  177.         $builder->add(
  178.             'pipehat',
  179.             ChoiceType::class,
  180.             array(
  181.                 'label' => 'Pipehat',
  182.                 'placeholder' => '- select -',
  183.                 'attr' => array(
  184.                     'ng-disabled' => 'loadingFireplace',
  185.                     'ng-show' => 'calculator.fireplace',
  186.                     'ng-options' => 'pipehat.type for pipehat in pipehats track by pipehat.id',
  187.                 ),
  188.             )
  189.         );
  190.         $builder->add(
  191.             'show_example',
  192.             ButtonType::class,
  193.             array(
  194.                 'label' => 'Click to view an example image',
  195.                 'attr' => array(
  196.                     'class' => 'btn btn-block btn-default generated-image',
  197.                     'data-toggle' => "modal",
  198.                     'data-target' => "#myModal",
  199.                     'ng-show' => 'calculator.fireplace',
  200.                 ),
  201.             )
  202.         );
  203.         $builder->add(
  204.             'fireplace_height',
  205.             IntegerType::class,
  206.             array(
  207.                 'label' => 'Fireplace height (x)',
  208.                 'required' => false,
  209.                 'attr' => array(
  210.                     'ng-disabled' => 'loadingFireplace',
  211.                     'ng-show' => 'calculator.fireplace && (calculator.mounting.no_fireplace || calculator.mounting.needs_x)',
  212.                     'ng-required' => 'calculator.fireplace && (calculator.mounting.no_fireplace || calculator.mounting.needs_x)',
  213.                     'min' => '0',
  214.                 ),
  215.             )
  216.         );
  217.         $builder->add(
  218.             'ventilation_duct',
  219.             ChoiceType::class,
  220.             array(
  221.                 'label' => 'Ventilation Duct',
  222.                 'required' => false,
  223.                 'placeholder' => '- select -',
  224.                 'choices' => $ventilationModules,
  225.                 'attr' => array(
  226.                     'ng-disabled' => 'loadingFireplace',
  227.                     'ng-show' => 'calculator.fireplace && calculator.mounting.no_fireplace && (calculator.mounting.type == 4 || calculator.mounting.type == 5)',
  228.                     'ng-required' => 'calculator.fireplace && calculator.mounting.no_fireplace && (calculator.mounting.type == 4 || calculator.mounting.type == 5)',
  229.                 ),
  230.             )
  231.         );
  232.         $builder->add(
  233.             'a_size',
  234.             IntegerType::class,
  235.             array(
  236.                 'label' => 'A (in mm)',
  237.                 'required' => false,
  238.                 'attr' => array(
  239.                     'ng-disabled' => 'loadingFireplace',
  240.                     'ng-show' => 'calculator.fireplace',
  241.                     'ng-pattern' => "/^[1-9]\d*$/",
  242.                 ),
  243.             )
  244.         );
  245.         $builder->add(
  246.             'g_size',
  247.             IntegerType::class,
  248.             array(
  249.                 'label' => 'G (in degrees)',
  250.                 'attr' => array(
  251.                     'ng-disabled' => 'loadingFireplace',
  252.                     'ng-show' => 'calculator.fireplace',
  253.                     'ng-pattern' => "/^[1-9]\d*$/",
  254.                 ),
  255.             )
  256.         );
  257.         $builder->add(
  258.             'calculate_h_size',
  259.             ButtonType::class,
  260.             array(
  261.                 'label' => 'Calculate H',
  262.                 'attr' => array(
  263.                     'class' => 'btn btn-primary',
  264.                     'ng-show' => 'calculator.fireplace',
  265.                     'ng-disabled' => '!calculator.a_size || calculator.g_size == null || loadingFireplace',
  266.                     'ng-click' => 'calculateHSize()',
  267.                 ),
  268.             )
  269.         );
  270.         $builder->add(
  271.             'h_size',
  272.             IntegerType::class,
  273.             array(
  274.                 'label' => 'H (in mm)',
  275.                 'attr' => array(
  276.                     'ng-disabled' => 'loadingFireplace',
  277.                     'ng-show' => 'calculator.fireplace',
  278.                     'ng-pattern' => "/^[1-9]\d*$/",
  279.                 ),
  280.             )
  281.         );
  282.         $builder->add(
  283.             'l1_size',
  284.             IntegerType::class,
  285.             array(
  286.                 'label' => 'L1 (in mm)',
  287.                 'attr' => array(
  288.                     'ng-disabled' => 'loadingFireplace',
  289.                     'ng-show' => 'calculator.fireplace',
  290.                     'ng-pattern' => "/^[1-9]\d*$/",
  291.                     // 'ng-change' => 'changeL1()'
  292.                 ),
  293.             )
  294.         );
  295.         $builder->add(
  296.             'l2_size',
  297.             IntegerType::class,
  298.             array(
  299.                 'label' => 'L2 (in mm)',
  300.                 'attr' => array(
  301.                     'ng-disabled' => 'loadingFireplace',
  302.                     'ng-required' => '!calculator.mounting.ext',
  303.                     'ng-show' => 'calculator.fireplace && !calculator.mounting.ext',
  304.                     'ng-pattern' => "/^[1-9]\d*$/"
  305.                     // 'ng-change' => 'changeL2()'
  306.                 ),
  307.             )
  308.         );
  309.         $builder->add(
  310.             'l3_size',
  311.             ChoiceType::class,
  312.             array(
  313.                 'label' => 'L3 (in mm)',
  314.                 'placeholder' => '- select -',
  315.                 'attr' => array(
  316.                     'ng-disabled' => 'loadingFireplace',
  317.                     'ng-show' => 'calculator.fireplace && calculator.mounting.wall',
  318.                     'ng-required' => 'calculator.fireplace && calculator.mounting.wall',
  319.                     'ng-options' => 'option.text for option in l3Options track by option.value',
  320.                 ),
  321.             )
  322.         );
  323.         $builder->add(
  324.             'has_angle',
  325.             CheckboxType::class,
  326.             array(
  327.                 'label' => 'Has angle?',
  328.                 'required' => false,
  329.                 'attr' => array(
  330.                     'ng-disabled' => 'loadingFireplace',
  331.                     'ng-show' => 'calculator.fireplace',
  332.                     'ng_change' => 'changeHasAngle()',
  333.                 ),
  334.             )
  335.         );
  336.         $builder->add(
  337.             'v1_size',
  338.             IntegerType::class,
  339.             array(
  340.                 'label' => 'V1 (in mm)',
  341.                 'attr' => array(
  342.                     'ng-disabled' => 'loadingFireplace',
  343.                     'ng-show' => 'calculator.fireplace && calculator.has_angle',
  344.                     'ng-required' => 'calculator.fireplace && calculator.has_angle',
  345.                     'ng-change' => 'changeV1()',
  346.                     'min' => '0',
  347.                 ),
  348.             )
  349.         );
  350.         $builder->add(
  351.             'cc_size',
  352.             ChoiceType::class,
  353.             array(
  354.                 'label' => 'C/C (in mm)',
  355.                 'placeholder' => '- select -',
  356.                 'attr' => array(
  357.                     'ng-disabled' => 'loadingFireplace || !calculator.mounting || !calculator.has_angle || !calculator.l1_size || !calculator.l2_size || !calculator.v1_size',
  358.                     'ng-show' => 'calculator.fireplace && calculator.has_angle',
  359.                     'ng-required' => 'calculator.fireplace && calculator.has_angle',
  360.                     'ng-options' => 'option.text for option in ccOptions[calculator.mounting.type][floor] track by option.value',
  361.                     'ng-change' => 'changeCC()',
  362.                 ),
  363.             )
  364.         );
  365.         $builder->add(
  366.             'color_1_floor',
  367.             ChoiceType::class,
  368.             array(
  369.                 'label' => 'Color up to ceiling on the first floor',
  370.                 'placeholder' => false,
  371.                 'choices' => $colors,
  372.                 'attr' => array(
  373.                     'ng-disabled' => 'loadingFireplace',
  374.                     'ng-show' => 'calculator.fireplace',
  375.                     'ng-selected' => 'loadingFireplace',
  376.                 ),
  377.             )
  378.         );
  379.         $builder->add(
  380.             'color_2_floor',
  381.             ChoiceType::class,
  382.             array(
  383.                 'label' => 'Color after first floor',
  384.                 'required' => false,
  385.                 'placeholder' => false,
  386.                 'choices' => $colors,
  387.                 'attr' => array(
  388.                     'ng-disabled' => 'loadingFireplace',
  389.                     'ng-show' => 'calculator.fireplace && hasSecondFloor',
  390.                     'ng-required' => 'calculator.fireplace && hasSecondFloor',
  391.                 ),
  392.             )
  393.         );
  394.     }
  395.     private function getFireplaces()
  396.     {
  397.         $repository $this->entityManager->getRepository('NordpeisCalculatorBundle:Fireplace');
  398.         $query $this->entityManager->createQuery(
  399.             'SELECT f from NordpeisCalculatorBundle:Fireplace f
  400.             JOIN NordpeisCalculatorBundle:Scheme s WITH s.fireplace = f
  401.             WHERE s.hidden = :hidden'
  402.         )->setParameter('hidden'false);
  403.         $fireplaces = array();
  404.         foreach ($query->getResult() as $item) {
  405.             $fireplaces[$item->getName()] = $item->getId();
  406.         }
  407.         ksort($fireplaces);
  408.         return $fireplaces;
  409.     }
  410.     private function getProducers()
  411.     {
  412.         $repository $this->entityManager->getRepository('NordpeisCalculatorBundle:Producer');
  413.         $query $this->entityManager->createQuery(
  414.             'SELECT p from NordpeisCalculatorBundle:Producer p
  415.             WHERE p.hidden = :hidden'
  416.         )->setParameter('hidden'false);
  417.         $producers = array();
  418.         foreach ($query->getResult() as $item) {
  419.             $producers[$item->getName()] = $item->getId();
  420.         }
  421.         ksort($producers);
  422.         return $producers;
  423.     }
  424.     private function getPipes()
  425.     {
  426.         $repository $this->entityManager->getRepository('NordpeisCalculatorBundle:Pipe');
  427.         $response $repository->findAll();
  428.         $pipes = array();
  429.         foreach ($response as $item) {
  430.             $pipes[$item->getDiameter()] = $item->getId();
  431.         }
  432.         return $pipes;
  433.     }
  434.     private function getCoverPlates()
  435.     {
  436.         $repository $this->entityManager->getRepository('NordpeisCalculatorBundle:CoverPlate');
  437.         $response $repository->findAll();
  438.         $coverPlates = array();
  439.         foreach ($response as $item) {
  440.             $coverPlates[$item->getName()] = $item->getId();
  441.         }
  442.         ksort($coverPlates);
  443.         return $coverPlates;
  444.     }
  445.     private function getFloors()
  446.     {
  447.         $repository $this->entityManager->getRepository('NordpeisCalculatorBundle:Floor');
  448.         $response $repository->findAll();
  449.         $floors = array();
  450.         foreach ($response as $item) {
  451.             $floors[$item->getName()] = $item->getId();
  452.         }
  453.         return $floors;
  454.     }
  455.     private function getRoofTypes()
  456.     {
  457.         $repository $this->entityManager->getRepository('NordpeisCalculatorBundle:RoofType');
  458.         $response $repository->findAll();
  459.         $roofTypes = array();
  460.         foreach ($response as $item) {
  461.             $type $item->getType();
  462.             if ($type == 'All') {
  463.                 $type 'Other';
  464.             }
  465.             $roofTypes[$type] = $item->getId();
  466.         }
  467.         ksort($roofTypes);
  468.         return $roofTypes;
  469.     }
  470.     private function getColors()
  471.     {
  472.         $repository $this->entityManager->getRepository('NordpeisCalculatorBundle:Color');
  473.         $response $repository->findAll();
  474.         $colors = array();
  475.         foreach ($response as $item) {
  476.             // Temporary fix for the Silver color 
  477.             if($item->getId() !== ) {
  478.                 $colors[$item->getColor()] = $item->getId();
  479.             }
  480.         }
  481.         return $colors;
  482.     }
  483.     private function getVentilationModules()
  484.     {
  485.         $moduleCategoryRepository $this->entityManager->getRepository('NordpeisCalculatorBundle:ModuleCategory');
  486.         $moduleCategory $moduleCategoryRepository->findOneByCategory('Ventilation2');
  487.         $query $this->entityManager->createQuery(
  488.             'SELECT m from NordpeisCalculatorBundle:Module m
  489.                 WHERE m.managedByFireplace = :managedByFireplace
  490.                 AND m.moduleCategory = :moduleCategory'
  491.         )->setParameter('managedByFireplace'true)
  492.             ->setParameter('moduleCategory'$moduleCategory);
  493.         $modules = array();
  494.         foreach ($query->getResult() as $item) {
  495.             $modules[$item->getName()] = $item->getId();
  496.         }
  497.         return $modules;
  498.     }
  499. }