|
@@ -0,0 +1,114 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace CablemodemBundle\Form;
|
|
|
+
|
|
|
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
|
+use Symfony\Component\Form\FormEvent;
|
|
|
+use Symfony\Component\Form\FormEvents;
|
|
|
+use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
|
|
+use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
|
|
+
|
|
|
+class FixedIPSubscriber implements EventSubscriberInterface
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var Object
|
|
|
+ */
|
|
|
+ private $subject;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var array Campos a modificar del form
|
|
|
+ * [ checkbok => choice ]
|
|
|
+ */
|
|
|
+ private $fields = [
|
|
|
+ 'checkFixedIP' => 'fixedIP',
|
|
|
+ 'checkCpeFixedIP' => 'cpeFixedIP',
|
|
|
+ 'checkMtaFixedIP' => 'mtaFixedIP',
|
|
|
+ ];
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param Object $subject
|
|
|
+ */
|
|
|
+ public function __construct($subject)
|
|
|
+ {
|
|
|
+ $this->subject = $subject;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function getSubscribedEvents()
|
|
|
+ {
|
|
|
+ return array(
|
|
|
+ FormEvents::PRE_SET_DATA => 'onPreSetData',
|
|
|
+ FormEvents::PRE_SUBMIT => 'onPreSubmit',
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param FormEvent $event
|
|
|
+ */
|
|
|
+ public function onPreSetData(FormEvent $event)
|
|
|
+ {
|
|
|
+ $data = $event->getData();
|
|
|
+ $form = $event->getForm();
|
|
|
+
|
|
|
+ foreach ($this->fields as $checkbox => $field) {
|
|
|
+ $choiceOptions = [
|
|
|
+ 'label' => false,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $checkboxOptions = [
|
|
|
+ 'mapped' => false,
|
|
|
+ 'required' => false,
|
|
|
+ 'translation_domain' => 'CablemodemBundle',
|
|
|
+ ];
|
|
|
+
|
|
|
+ $method = 'get' . ucfirst($field);
|
|
|
+ $value = $this->subject->$method();
|
|
|
+ if ($value) {
|
|
|
+ $choiceOptions['choices'] = [
|
|
|
+ $value => $value,
|
|
|
+ ];
|
|
|
+ $checkboxOptions['data'] = true;
|
|
|
+ } else {
|
|
|
+ $choiceOptions['attr']['class'] = 'hidden';
|
|
|
+ }
|
|
|
+ $form->add($checkbox, CheckboxType::class, $checkboxOptions);
|
|
|
+ $form->add($field, ChoiceType::class, $choiceOptions);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param FormEvent $event
|
|
|
+ */
|
|
|
+ public function onPreSubmit(FormEvent $event)
|
|
|
+ {
|
|
|
+ $data = $event->getData();
|
|
|
+ $form = $event->getForm();
|
|
|
+
|
|
|
+ foreach ($this->fields as $checkbox => $field) {
|
|
|
+ $choiceOptions = [
|
|
|
+ 'label' => false,
|
|
|
+ 'attr' => [
|
|
|
+ 'class' => 'hidden',
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+
|
|
|
+ $checkboxOptions = [
|
|
|
+ 'mapped' => false,
|
|
|
+ 'required' => false,
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (isset($data[$field]) && $data[$field]) {
|
|
|
+ $choiceOptions['choices'] = [
|
|
|
+ $data[$field]
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $form->add($checkbox, CheckboxType::class, $checkboxOptions);
|
|
|
+ $form->add($field, ChoiceType::class, $choiceOptions);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|