Pārlūkot izejas kodu

FD3-756 seteo fixed ip cuando cambia el host type

Espinoza Guillermo 6 gadi atpakaļ
vecāks
revīzija
d85b27f229

+ 42 - 26
src/HostBundle/Entity/Host.php

@@ -19,6 +19,7 @@ use WorkflowBundle\Entity\Traits\WorkflowTrait;
  * @UniqueEntity("mac")
  *
  * @Assert\Callback("validateFixedAddress")
+ * @Assert\Callback("validateHostType")
  */
 class Host implements WorkflowInterface
 {
@@ -106,7 +107,7 @@ class Host implements WorkflowInterface
     /**
      * @var integer $ipv4Address
      *
-     * @ORM\Column(name="ipv4_address", type="integer", nullable=true)
+     * @ORM\Column(name="ipv4_address", type="integer", nullable=true, options={"unsigned":true})
      */
     protected $ipv4Address;
 
@@ -267,31 +268,6 @@ class Host implements WorkflowInterface
         return $this->associatedHosts;
     }
     
-    /**
-     * @param ExecutionContextInterface $context
-     */
-    public function validateFixedAddress(ExecutionContextInterface $context)
-    {
-        global $kernel;
-        
-        if (!$kernel->getContainer()) {
-            // fix para los tests
-            return;
-        }
-        $em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
-        $hostRepository = $em->getRepository(self::class);
-        $hosts = $hostRepository->findAll();
-        $options = json_decode($this->getOptions(), true);
-        $fixedAddress = isset($options['fixed_address']) ? $options['fixed_address'] : $this->getFixedAddress();
-        $fixedAddress = $fixedAddress ?: '';
-        foreach ($hosts as $host) {
-            if ($host->getId() != $this->id && $fixedAddress === $host->getFixedAddress()) {
-                $context->addViolation('options.fixed_address.not_unique');
-                break;
-            }
-        }
-    }
-
     /**
      * @return integer
      */
@@ -343,5 +319,45 @@ class Host implements WorkflowInterface
 
         return $this;
     }
+        
+    /**
+     * @param ExecutionContextInterface $context
+     */
+    public function validateFixedAddress(ExecutionContextInterface $context)
+    {
+        global $kernel;
+        
+        if (!$kernel->getContainer()) {
+            // fix para los tests
+            return;
+        }
+        $em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
+        $hostRepository = $em->getRepository(self::class);
+        $hosts = $hostRepository->findAll();
+        $options = json_decode($this->getOptions(), true);
+        $fixedAddress = isset($options['fixed_address']) ? $options['fixed_address'] : $this->getFixedAddress();
+        $fixedAddress = $fixedAddress ?: '';
+        foreach ($hosts as $host) {
+            if ($host->getId() != $this->id && $fixedAddress === $host->getFixedAddress()) {
+                $context->addViolation('options.fixed_address.not_unique');
+                break;
+            }
+        }
+    }
+    
+    /**
+     * Si el host type es distinto de cablemodem tiene que tener host asociado
+     * @param ExecutionContextInterface $context
+     */
+    public function validateHostType(ExecutionContextInterface $context)
+    {
+        $hostType = $this->hostType;
+        if (!$hostType) {
+            return;
+        }
+        if ($hostType->getName() !== 'Cablemodem' && is_null($this->host)) {
+            $context->addViolation('host.host_null');
+        }
+    }
 
 }

+ 14 - 0
src/HostBundle/EventListener/AdminDHCPOption.php

@@ -8,6 +8,8 @@ use HostBundle\Services\HostService;
 use Sonata\AdminBundle\Event\ConfigureEvent;
 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
 use Symfony\Component\Form\Extension\Core\Type\TextType;
+use Symfony\Component\Form\FormEvent;
+use Symfony\Component\Form\FormEvents;
 
 class AdminDHCPOption
 {
@@ -67,6 +69,18 @@ class AdminDHCPOption
                 $mapper->add($opt, TextType::class, $fieldOptions);
             }
             $mapper->end()->end();
+            
+            // fix seteo de fixed IP al editar el tipo de Host
+            $mapper->getFormBuilder()->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
+                $data = $event->getData();
+                $form = $event->getForm();
+                
+                if (isset($data['fixed_address'])) {
+                    $form->add('fixed_address', null, [
+                        'data' => $data['fixed_address'],
+                    ]);
+                }
+            });
         }
     }
 

+ 2 - 0
src/HostBundle/Resources/translations/validators.es.yml

@@ -0,0 +1,2 @@
+options.fixed_address.not_unique: La IP fija seleccionada ya esta asociada a otro host
+host.host_null: El tipo de host requiere que se asocie un host de tipo Cablemodem

+ 16 - 1
src/HostBundle/Resources/views/CRUD/edit.html.twig

@@ -24,14 +24,29 @@ $(document).ready(function() {
     
 });
 
+// Al seleccionar tipo Cablemodem oculto el campo Host
+// de lo contrario lo muestro y selecciono la primer option
+// luego actualizo las fixed IP según el tipo de host
 function showHostField()
 {
     var $hostTypeField = $("select[id$='hostType'] :selected");
     var $hostField = $("div.form-group[id$='host']");
-
+    var $hostSelect = $("select[id$='host']");
+    
     if ($hostTypeField.html() === 'Cablemodem') {
+        if ($hostSelect.find("option[value='']").length == 0) {
+            $hostSelect.prepend('<option value=""></option>');
+        }
+        $hostSelect.val(null).trigger('change');
+        
         $hostField.hide();
     } else {
+        if ($hostSelect.find("option[value='']").length) {
+            $hostSelect.find("option[value='']").remove();
+        }
+        var val = $hostSelect.find("option").first().attr('value');
+        $hostSelect.val(val).trigger('change');
+        
         $hostField.show();
     }