Przeglądaj źródła

Se crea el importador de ONUs desde el mapa.

Maximiliano Schvindt 7 lat temu
rodzic
commit
1caf09b95c

+ 17 - 0
src/FTTHBundle/Admin/ONUAdmin.php

@@ -112,4 +112,21 @@ class ONUAdmin extends WorkflowBaseAdmin
             ->end();
     }
 
+    /**
+     * @param string $action
+     * @param Object $object
+     * 
+     * @return array
+     */
+    public function configureActionButtons($action, $object = null)
+    {
+        $actions = parent::configureActionButtons($action, $object);
+        
+        if($action == "list") {
+            $actions['import_onu'] = array('template' => 'FTTHBundle:ONU:import_button.html.twig');
+        } 
+
+        return $actions;
+    }
+
 }

+ 144 - 0
src/FTTHBundle/Controller/ONUController.php

@@ -0,0 +1,144 @@
+<?php
+
+namespace FTTHBundle\Controller;
+
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\JsonResponse;
+use Symfony\Component\HttpFoundation\Response;
+use MapBundle\Entity\Location;
+use FTTHBundle\Entity\ONU;
+
+use Symfony\Component\Form\Extension\Core\Type\TextType;
+use Symfony\Component\Form\Extension\Core\Type\DateType;
+use Symfony\Component\Form\Extension\Core\Type\SubmitType;
+use WebserviceBundle\Form\Type\RemoteClientType;
+use MapBundle\Form\Type\RemoteMapType;
+
+class ONUController extends Controller
+{
+
+    public function importAction() 
+    {
+        //$em = $this->get('doctrine')->getManager();
+        $adminPool = $this->get('sonata.admin.pool');
+        $webservice = $this->get("webservice");
+
+        $urlMaps = $this->getParameter('remote_get_map_url');
+
+        /* Chequeamos que existe el objectType ONU - Esto debe venir definido por defecto luego */
+        $urlObjectTypes = $this->getParameter('remote_get_object_type_url');
+        $filters = array('name' => 'ONU');
+        $_types = $webservice->getData($urlObjectTypes, $filters, array(), null, null);
+        $disabled = false;
+        $flashbag = $this->get('session')->getFlashBag();
+        if(empty($_types)) {
+            $flashbag->add("error", "No existen ONUs definidas en el módulo de Mapas.");
+            $disabled = true;
+            $objectTypeId = 0;
+        } else {
+            $objectTypeId = $_types[0]['id'];
+        }
+        
+        $form = $this->createFormBuilder()
+        ->add('mapId', RemoteMapType::class, array('label' => 'Mapa', 'disabled' => $disabled))
+        ->add('clientId', RemoteClientType::class, array('label' => 'Cliente', 'disabled' => $disabled))
+        ->getForm();
+        
+        $flashbag->add("warning", "Seleccione el Mapa desde el que se importarán las ONUs y el cliente al que se asociarán.");
+        
+        return $this->render('FTTHBundle:ONU:onu_import.html.twig', array(
+            'base_template' => $adminPool->getTemplate('layout'),
+            'admin_pool' => $adminPool,
+            'admin' => $adminPool->getAdminByClass("FTTHBundle\Entity\ONU"),
+            'objectTypeId' => $objectTypeId,
+            'form' => $form->createView(),
+            'disabled' => $disabled
+        ));
+
+    }
+
+    /*
+     * Queda pendiente enviar un comando para actualizar masivamente los devices
+     * 
+     */
+    public function onuSaveAction(Request $request) 
+    {
+        $em = $this->get('doctrine')->getManager();        
+        $webservice = $this->get("webservice");
+
+        $mapId = $request->get('mapId');
+        $clientId = $request->get('clientId');
+        
+        if(is_null($mapId) || empty($mapId) || is_null($clientId) || empty($clientId)) {
+            $response = new Response(
+                json_encode(array()),
+                Response::HTTP_OK,
+                array('content-type' => 'application/json')
+            );
+    
+            return $response;
+        }
+        
+        $limit = $request->get('limit');
+        $offset = $request->get('offset');
+        $objectTypeId = $request->get('objectTypeId');
+
+        $urlObjectTypes = $this->getParameter('remote_get_objects_url');
+        
+        $filters = array('type' => $objectTypeId, 'map' => $mapId);
+        $_onus = $webservice->getData($urlObjectTypes, $filters, array(), $limit, $offset);
+
+        $return = array();
+        foreach($_onus as $onu) {
+            $object = $em->getRepository("FTTHBundle:ONU")->findOneByPonSerialNumber($onu['text']);
+            if(is_null($object)) {
+                $object = new ONU();
+                $object->setClientId(1);
+                $object->setTenancyId(1);
+                $object->setPonSerialNumber($onu['text']);
+                $em->persist($object);
+                $return[] = "ONU create {$onu['text']}";
+            }
+            $location = $object->getLocation();
+            
+            $data = json_decode($onu['vector']['data'],true);
+            $mapLocation = $data[0];
+
+            $mapLocation['zoom'] = 17;
+            $mapLocation['onu'] = "{$object->getPonSerialNumber()}";
+            $mapLocation['map_object_id'] = $onu['id'];
+            
+            $action = "update";
+            if(is_null($location)) {
+                $location = new Location();
+                $em->persist($location);
+                $object->setLocation($location);
+                $em->persist($object);
+                $em->flush();
+                $action = "create";
+            }
+            
+            $return[] = "LOCATION (id {$location->getId()}) {$action} - ONU (id {$object->getId()}) {$onu['text']} - ".json_encode($mapLocation);
+            
+            $location->setJsonExtraData($mapLocation);
+            $location->setObjectTypeId($objectTypeId);
+            $location->setMapId($mapId);
+            
+            $em->persist($location);
+        }
+
+        $em->flush();
+
+        $response = new Response(
+            json_encode($return),
+            Response::HTTP_OK,
+            array('content-type' => 'application/json')
+        );
+
+        return $response;
+
+    }
+
+}

+ 10 - 0
src/FTTHBundle/Entity/ONU.php

@@ -308,6 +308,16 @@ class ONU implements DeviceInterface, TenancyIdTraitInterface, LocationInterface
         return $this->updated;
     }
 
+    /**
+     * Set updated
+     *
+     */
+    public function setUpdated($updated)
+    {
+        $this->updated = $updated;
+        return $this;
+    }
+
     /**
      * Get administrativeState
      *

+ 7 - 0
src/FTTHBundle/Resources/config/routing/admin.xml

@@ -4,4 +4,11 @@
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
 
+    <route id="onu_import" path="/admin/ftth/onu/import">
+        <default key="_controller">FTTHBundle:ONU:import</default>
+    </route>
+    
+    <route id="onu_save" path="/admin/ftth/onu/onu_save">
+        <default key="_controller">FTTHBundle:ONU:onuSave</default>
+    </route>
 </routes>

+ 3 - 0
src/FTTHBundle/Resources/translations/FTTHBundle.es.yml

@@ -173,3 +173,6 @@ Incorrect State: Estado erróneo
 Undefined State: Estado no definido
 Undefined Workflow: Workflow no definido
 Incorrect Workflow: El workflow no existe o no es compatible con este objeto
+link_action_import_from_map: Importar desde Mapa
+No existen ONUs definidas en el módulo de Mapas.: No existen ONUs definidas en el módulo de Mapas.
+Seleccione el Mapa desde el que se importarán las ONUs y el cliente al que se asociarán.: Seleccione el Mapa desde el que se importarán las ONUs y el cliente al que se asociarán.

+ 6 - 0
src/FTTHBundle/Resources/views/ONU/import_button.html.twig

@@ -0,0 +1,6 @@
+<li>
+    <a class="sonata-action-element" href="{{ path('onu_import') }}">
+        <i class="fa fa-download" aria-hidden="true"></i>
+        {{ 'link_action_import_from_map'|trans({}, 'FTTHBundle') }}
+    </a>
+</li>

+ 156 - 0
src/FTTHBundle/Resources/views/ONU/onu_import.html.twig

@@ -0,0 +1,156 @@
+{% extends 'SonataAdminBundle:CRUD:base_list.html.twig' %}
+
+{% block stylesheets %}
+{{ parent() }}
+
+<style>
+footer {z-index: 1100;}
+.container-fluid {padding-left: 0px; padding-right: 0px;}
+.container-fluid div.row {padding-left: 15px; padding-right: 15px;}
+.bold-title {font-weight: bold}
+#console-message {box-shadow: none; border-top:0px; max-height: 330px; overflow: auto;}
+</style>
+{% endblock %}
+
+{% block actions %}
+ {% endblock %}
+
+{% block tab_menu %}
+<ol class="nav navbar-top-links breadcrumb">
+    <li>
+        <a href="{{url('sonata_admin_dashboard')}}"><i class="fa fa-home"></i></a>
+    </li>
+    <li class="active"><span>{{ 'Importar ONUs'|trans({}, 'FTTHBundle') }}</span></li>
+</ol>
+{% endblock %}
+
+{% block title %}
+{% endblock %}
+
+{% block navbar_title %}
+{% endblock %}
+
+{% block list_filters_actions %}
+<ul class="nav navbar-nav navbar-right">
+    <li><a class="sonata-action-element" href="{{url('admin_ftth_onu_list')}}"><i class="fa fa-list" aria-hidden="true"></i>
+        {{ 'link_action_list'|trans({}, 'SonataAdminBundle') }}
+    </a></li>
+</ul>
+{% endblock %}
+
+{% block list_filters %}
+{% endblock %}
+
+{% block list_table %}
+{% endblock %}
+
+{% block sonata_wrapper %}
+{{ parent() }}
+<footer class="footer">
+    <span id="statusBar" class="statusBar">Listo.</span>
+</footer>
+{% endblock %}
+
+{% block content %}
+
+<div id='wrapper' class="box box-primary">
+    <div id='page-content-wrapper'>
+        <div class="row">
+            <div class="col-md-12">
+                <div class="box box-primary" style="border-top:0px">
+                    <div class="box-header">
+                        <h4 class="box-title">
+                            {{ 'Importador'|trans({}, 'FTTHBundle') }}
+                        </h4>
+                    </div>
+                    <div class="box-body">  
+                        <div class="sonata-ba-collapsed-fields">
+                            <div class="form-group clearfix">
+                                {{ form_widget(form) }}
+                            </div>
+                        </div>
+                    </div>
+                </div>
+            </div>
+            <div class="col-md-12" style="padding: 25px; padding-top: 0px">
+                <div class="box box-primary box-body" id="console-message"></div>
+            </div>
+        </div>
+
+    </div>
+</div>
+
+<div class="sonata-ba-form-actions well well-small form-actions">
+    {% if disabled %}
+        <button class="btn btn-success" type="submit" id="btn_import" disabled="disabled">
+    {% else %}
+        <button class="btn btn-success" type="submit" id="btn_import">
+    {% endif %}
+    <i class="fa fa-download" aria-hidden="true"></i>&nbsp;{{ 'Importar'|trans({}, 'FTTHBundle') }}
+    </button>
+</div>
+
+
+<script>
+
+$(document).ready(function() {
+
+    var limit = 100;
+    var offset = 0;
+
+    $("#btn_import").change(function() {
+        offset = 0;
+    });
+
+    $("#btn_import").click(function(){
+        $("#btn_import").attr('disabled','disabled');
+        mapId = $("#mapId").val();
+        clientId = $("#form_clientId").val();
+
+        if(!mapId || !clientId) {
+            $("#btn_import").removeAttr('disabled');
+            alert("{{ 'Debe seleccionar un mapa y cliente'|trans({}, 'FTTHBundle') }}");
+            return;
+        }
+
+        count = 0;
+
+        $("#console-message").prepend("<span class='bold-title'>Iniciando importación, espere un momento.</span><br />");
+        
+        do {
+            result = importData(mapId, limit, offset);
+            offset += limit;
+            count++;
+            result.forEach(function(row) {
+                $("#console-message").prepend(row + "<br />");
+            });
+            $("#console-message").prepend("<span class='bold-title'>Importando nuevos elementos, espere un momento.</span><br />");
+            
+        } while (result && result.length);
+        
+        $("#console-message").prepend("<span class='bold-title'>Fin :)</span><br />");
+        $("#btn_import").removeAttr('disabled');
+        offset = 0;
+    }); 
+});
+
+function importData(mapId, limit, offset) {
+
+    var r = false;
+    $.ajax({
+        method: "POST",
+        async: false,
+        cache: false,
+        url: "{{url('onu_save')}}?_=" + new Date().getTime(),
+        data: { mapId: mapId, clientId: clientId, limit: limit, offset: offset, objectTypeId: {{objectTypeId}} }
+    }).done(function( result ) {
+        r = result;
+    });
+
+    return r;
+
+
+}
+
+</script>
+{% endblock %}