<?php
namespace Nordpeis\Bundle\CalculatorBundle\Form\Type;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CalculatorType extends AbstractType
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'attr' => array(
'name' => $this->getName() . 'Form',
'novalidate' => 'novalidate',
),
));
}
public function getName()
{
return 'calculator';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$producers = $this->getProducers();
$fireplaces = $this->getFireplaces();
$pipes = $this->getPipes();
// $coverPlates = $this->getCoverPlates();
$floors = $this->getFloors();
$roofTypes = $this->getRoofTypes();
$colors = $this->getColors();
$ventilationModules = $this->getVentilationModules();
// $pipehats = $this->getPipeHats();
$builder->add(
'producer',
ChoiceType::class,
array(
'label' => 'Producer',
'placeholder' => '- select -',
'choices' => $producers,
'attr' => isset($options['data']['producer']) ? array(
'ng-init' => "calculator.producer='" . $options['data']['producer'] . "'",
) : array(),
)
);
$builder->add(
'fireplace',
ChoiceType::class,
array(
'label' => 'Fireplace',
'placeholder' => '- select -',
'choices' => [],
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.producer && hasFireplace',
'ng-required' => 'calculator.producer && hasFireplace',
'ng-options' => "fireplace.type for fireplace in fireplaces | orderBy:'name' track by fireplace.id",
)
)
);
$builder->add(
'pipe',
ChoiceType::class,
array(
'multiple' => false,
'expanded' => true,
'label' => 'Pipe dimension',
'choices' => [],
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace && hasPipeOptions',
'ng-required' => 'calculator.fireplace && hasPipeOptions',
'ng-repeat' => 'pipe in pipes',
'ng-checked' => 'calculator.pipe == pipe.id',
'ng-label' => 'pipe.type',
'ng-value' => 'pipe.id',
'value' => 'pipe.id',
'ng-radio' => true,
),
)
);
$builder->add(
'mounting',
ChoiceType::class,
array(
'label' => 'Mounting',
'placeholder' => '- select -',
'choices' => array(),
'required' => 'true',
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace',
// 'ng-change' => 'mountingChange()',
'ng-options' => "scheme.name for scheme in schemes | orderBy:'name' track by scheme.id",
),
)
);
$builder->add(
'is_oppheng',
CheckboxType::class,
array(
'label' => 'Oppheng?',
'required' => false,
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace && calculator.mounting.mount_option',
),
)
);
$builder->add(
'cover_plate',
ChoiceType::class,
array(
'multiple' => false,
'expanded' => true,
'label' => 'Cover plates',
'choices' => [],
'attr' => array(
'ng-disabled' => 'loadingFireplace',
// 'ng-show' => 'calculator.fireplace && calculator.mounting && calculator.mounting.mounting_id !== 4',
'ng-required' => 'calculator.fireplace && calculator.mounting && calculator.mounting.mounting_id !== 4',
'ng-repeat' => 'coverPlate in coverPlates',
'ng-checked' => 'calculator.cover_plate == coverPlate.id',
'ng-label' => 'coverPlate.type',
'ng-value' => 'coverPlate.id',
'value' => 'coverPlate.id',
'ng-radio' => true,
),
)
);
$builder->add(
'floor',
ChoiceType::class,
array(
'label' => 'Floors',
'placeholder' => '- select -',
'choices' => $floors,
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace',
),
)
);
$builder->add(
'upper_floor_living_room',
CheckboxType::class,
array(
'label' => 'Is the upper floor a living room?',
'required' => false,
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace && hasUpperFloorLivingRoom',
),
)
);
$builder->add(
'roof',
ChoiceType::class,
array(
'label' => 'Roof type',
'placeholder' => '- select -',
'choices' => $roofTypes,
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace',
),
)
);
$builder->add(
'pipehat',
ChoiceType::class,
array(
'label' => 'Pipehat',
'placeholder' => '- select -',
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace',
'ng-options' => 'pipehat.type for pipehat in pipehats track by pipehat.id',
),
)
);
$builder->add(
'show_example',
ButtonType::class,
array(
'label' => 'Click to view an example image',
'attr' => array(
'class' => 'btn btn-block btn-default generated-image',
'data-toggle' => "modal",
'data-target' => "#myModal",
'ng-show' => 'calculator.fireplace',
),
)
);
$builder->add(
'fireplace_height',
IntegerType::class,
array(
'label' => 'Fireplace height (x)',
'required' => false,
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace && (calculator.mounting.no_fireplace || calculator.mounting.needs_x)',
'ng-required' => 'calculator.fireplace && (calculator.mounting.no_fireplace || calculator.mounting.needs_x)',
'min' => '0',
),
)
);
$builder->add(
'ventilation_duct',
ChoiceType::class,
array(
'label' => 'Ventilation Duct',
'required' => false,
'placeholder' => '- select -',
'choices' => $ventilationModules,
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace && calculator.mounting.no_fireplace && (calculator.mounting.type == 4 || calculator.mounting.type == 5)',
'ng-required' => 'calculator.fireplace && calculator.mounting.no_fireplace && (calculator.mounting.type == 4 || calculator.mounting.type == 5)',
),
)
);
$builder->add(
'a_size',
IntegerType::class,
array(
'label' => 'A (in mm)',
'required' => false,
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace',
'ng-pattern' => "/^[1-9]\d*$/",
),
)
);
$builder->add(
'g_size',
IntegerType::class,
array(
'label' => 'G (in degrees)',
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace',
'ng-pattern' => "/^[1-9]\d*$/",
),
)
);
$builder->add(
'calculate_h_size',
ButtonType::class,
array(
'label' => 'Calculate H',
'attr' => array(
'class' => 'btn btn-primary',
'ng-show' => 'calculator.fireplace',
'ng-disabled' => '!calculator.a_size || calculator.g_size == null || loadingFireplace',
'ng-click' => 'calculateHSize()',
),
)
);
$builder->add(
'h_size',
IntegerType::class,
array(
'label' => 'H (in mm)',
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace',
'ng-pattern' => "/^[1-9]\d*$/",
),
)
);
$builder->add(
'l1_size',
IntegerType::class,
array(
'label' => 'L1 (in mm)',
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace',
'ng-pattern' => "/^[1-9]\d*$/",
// 'ng-change' => 'changeL1()'
),
)
);
$builder->add(
'l2_size',
IntegerType::class,
array(
'label' => 'L2 (in mm)',
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-required' => '!calculator.mounting.ext',
'ng-show' => 'calculator.fireplace && !calculator.mounting.ext',
'ng-pattern' => "/^[1-9]\d*$/"
// 'ng-change' => 'changeL2()'
),
)
);
$builder->add(
'l3_size',
ChoiceType::class,
array(
'label' => 'L3 (in mm)',
'placeholder' => '- select -',
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace && calculator.mounting.wall',
'ng-required' => 'calculator.fireplace && calculator.mounting.wall',
'ng-options' => 'option.text for option in l3Options track by option.value',
),
)
);
$builder->add(
'has_angle',
CheckboxType::class,
array(
'label' => 'Has angle?',
'required' => false,
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace',
'ng_change' => 'changeHasAngle()',
),
)
);
$builder->add(
'v1_size',
IntegerType::class,
array(
'label' => 'V1 (in mm)',
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace && calculator.has_angle',
'ng-required' => 'calculator.fireplace && calculator.has_angle',
'ng-change' => 'changeV1()',
'min' => '0',
),
)
);
$builder->add(
'cc_size',
ChoiceType::class,
array(
'label' => 'C/C (in mm)',
'placeholder' => '- select -',
'attr' => array(
'ng-disabled' => 'loadingFireplace || !calculator.mounting || !calculator.has_angle || !calculator.l1_size || !calculator.l2_size || !calculator.v1_size',
'ng-show' => 'calculator.fireplace && calculator.has_angle',
'ng-required' => 'calculator.fireplace && calculator.has_angle',
'ng-options' => 'option.text for option in ccOptions[calculator.mounting.type][floor] track by option.value',
'ng-change' => 'changeCC()',
),
)
);
$builder->add(
'color_1_floor',
ChoiceType::class,
array(
'label' => 'Color up to ceiling on the first floor',
'placeholder' => false,
'choices' => $colors,
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace',
'ng-selected' => 'loadingFireplace',
),
)
);
$builder->add(
'color_2_floor',
ChoiceType::class,
array(
'label' => 'Color after first floor',
'required' => false,
'placeholder' => false,
'choices' => $colors,
'attr' => array(
'ng-disabled' => 'loadingFireplace',
'ng-show' => 'calculator.fireplace && hasSecondFloor',
'ng-required' => 'calculator.fireplace && hasSecondFloor',
),
)
);
}
private function getFireplaces()
{
$repository = $this->entityManager->getRepository('NordpeisCalculatorBundle:Fireplace');
$query = $this->entityManager->createQuery(
'SELECT f from NordpeisCalculatorBundle:Fireplace f
JOIN NordpeisCalculatorBundle:Scheme s WITH s.fireplace = f
WHERE s.hidden = :hidden'
)->setParameter('hidden', false);
$fireplaces = array();
foreach ($query->getResult() as $item) {
$fireplaces[$item->getName()] = $item->getId();
}
ksort($fireplaces);
return $fireplaces;
}
private function getProducers()
{
$repository = $this->entityManager->getRepository('NordpeisCalculatorBundle:Producer');
$query = $this->entityManager->createQuery(
'SELECT p from NordpeisCalculatorBundle:Producer p
WHERE p.hidden = :hidden'
)->setParameter('hidden', false);
$producers = array();
foreach ($query->getResult() as $item) {
$producers[$item->getName()] = $item->getId();
}
ksort($producers);
return $producers;
}
private function getPipes()
{
$repository = $this->entityManager->getRepository('NordpeisCalculatorBundle:Pipe');
$response = $repository->findAll();
$pipes = array();
foreach ($response as $item) {
$pipes[$item->getDiameter()] = $item->getId();
}
return $pipes;
}
private function getCoverPlates()
{
$repository = $this->entityManager->getRepository('NordpeisCalculatorBundle:CoverPlate');
$response = $repository->findAll();
$coverPlates = array();
foreach ($response as $item) {
$coverPlates[$item->getName()] = $item->getId();
}
ksort($coverPlates);
return $coverPlates;
}
private function getFloors()
{
$repository = $this->entityManager->getRepository('NordpeisCalculatorBundle:Floor');
$response = $repository->findAll();
$floors = array();
foreach ($response as $item) {
$floors[$item->getName()] = $item->getId();
}
return $floors;
}
private function getRoofTypes()
{
$repository = $this->entityManager->getRepository('NordpeisCalculatorBundle:RoofType');
$response = $repository->findAll();
$roofTypes = array();
foreach ($response as $item) {
$type = $item->getType();
if ($type == 'All') {
$type = 'Other';
}
$roofTypes[$type] = $item->getId();
}
ksort($roofTypes);
return $roofTypes;
}
private function getColors()
{
$repository = $this->entityManager->getRepository('NordpeisCalculatorBundle:Color');
$response = $repository->findAll();
$colors = array();
foreach ($response as $item) {
// Temporary fix for the Silver color
if($item->getId() !== 4 ) {
$colors[$item->getColor()] = $item->getId();
}
}
return $colors;
}
private function getVentilationModules()
{
$moduleCategoryRepository = $this->entityManager->getRepository('NordpeisCalculatorBundle:ModuleCategory');
$moduleCategory = $moduleCategoryRepository->findOneByCategory('Ventilation2');
$query = $this->entityManager->createQuery(
'SELECT m from NordpeisCalculatorBundle:Module m
WHERE m.managedByFireplace = :managedByFireplace
AND m.moduleCategory = :moduleCategory'
)->setParameter('managedByFireplace', true)
->setParameter('moduleCategory', $moduleCategory);
$modules = array();
foreach ($query->getResult() as $item) {
$modules[$item->getName()] = $item->getId();
}
return $modules;
}
}