Ver Fonte

FD3-660 form subscriber y vista edit cablemodem

Espinoza Guillermo há 7 anos atrás
pai
commit
f35ac9aa5b

+ 45 - 0
src/CablemodemBundle/Controller/CablemodemController.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace CablemodemBundle\Controller;
+
+use HostBundle\Entity\HostType;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\JsonResponse;
+use FOS\RestBundle\Controller\Annotations\RouteResource;
+use Buzz\Message\RequestInterface as HttpRequestInterface;
+
+/**
+ * @RouteResource("Cablemodem")
+ */
+class CablemodemController extends Controller
+{
+
+    /**
+     * @param Request $request
+     *
+     * @return JsonResponse
+     *
+     * @Route("/host/ipv4/ip_range", name="ajax_host_ipv4_range")
+     */
+    public function ajaxHostIPV4RangeAction(Request $request)
+    {
+        $result = [];
+        try {
+            $shortname = $request->get('shortname');
+            $webservice = $this->get('webservice');
+            $url = $this->getParameter('url_dhcp') . '/api/hosts/ipv4s/ranges.json';
+            
+            $result = $webservice->makeGetRequest($url, HttpRequestInterface::METHOD_POST, compact('shortname'));
+            $result = json_decode($result, true);
+        } catch (\Exception $ex) {
+            $result = [
+                'error' => $ex->getMessage(),
+            ];
+        }
+        
+        return new JsonResponse($result);
+    }
+
+}

+ 114 - 0
src/CablemodemBundle/Form/FixedIPSubscriber.php

@@ -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);   
+        }
+    }
+    
+}

+ 86 - 0
src/CablemodemBundle/Resources/views/CRUD/edit.html.twig

@@ -0,0 +1,86 @@
+{% extends "@SonataAdmin/CRUD/edit.html.twig" %}
+
+{% block javascripts %}
+
+{{ parent() }}
+
+<script type="text/javascript">
+
+$(document).ready(function() {
+
+    $('#{{ admin.uniqid ~ "_checkFixedIP" }}').on('ifChecked ifUnchecked', showSelect);
+    $('#{{ admin.uniqid ~ "_checkCpeFixedIP" }}').on('ifChecked ifUnchecked', showSelect);
+    $('#{{ admin.uniqid ~ "_checkMtaFixedIP" }}').on('ifChecked ifUnchecked', showSelect);
+    
+    var fixedIPIds = [
+        '#{{ admin.uniqid ~ "_fixedIP" }}',
+        '#{{ admin.uniqid ~ "_cpeFixedIP" }}',
+        '#{{ admin.uniqid ~ "_mtaFixedIP" }}',
+    ];
+    for (i = 0; i < fixedIPIds.length; i++) {
+        if ($(fixedIPIds[i]).val()) {
+            updateFixedIPs(fixedIPIds[i]);
+        }
+    }
+            
+});
+
+// Muestra y actualiza los select dependiendo del checkbox que se seleccione
+function showSelect(e, aux)
+{
+    var form_group = $(e.target).parents('.form-group').next('.form-group');
+    var select = form_group.find('select');
+    var selectContainer = form_group.find('.select2-container');
+        
+    if ($(e.target).prop('checked')) {
+        updateFixedIPs('#' + select.attr('id'));
+        
+        select.removeClass('hidden');
+        selectContainer.removeClass('hidden');
+    } else {
+        select.addClass('hidden');
+        select.val('');
+        selectContainer.addClass('hidden');
+    }
+}
+
+// Según el id del select, que depende del hostType se busca por Ajax y REST en DHCP
+// El rango de IPs que se pueden asignar
+function updateFixedIPs(id)
+{
+    var $hostTypeName = '';
+    
+    if (id === '#{{ admin.uniqid ~ "_fixedIP" }}') {
+        $hostTypeName = 'cablemodem';
+    } else if (id === '#{{ admin.uniqid ~ "_cpeFixedIP" }}') {
+        $hostTypeName = 'cpe';
+    } else {
+        $hostTypeName = 'mta';
+    }
+    
+    if ($hostTypeName) {
+        $.ajax({
+            url: '{{ path ('ajax_host_ipv4_range') }}',
+            type: 'POST',
+            async: false,
+            data: {
+                shortname: $hostTypeName,
+            },
+            success: function(data) {
+                var $select = $(id);
+                $select.html('<option value=""></option>');
+                if (data.ips) {
+                    $.each(data.ips, function (index, value) {
+                        $select.append('<option value="' + value + '">' + value + '</option>');
+                    });
+                }
+                
+                return false;
+            },
+        });
+    }
+}
+
+</script>
+
+{% endblock javascripts %}