Browse Source

[WIP] Campo location en form y show action

Guillermo Espinoza 8 years ago
parent
commit
24973d05c0

+ 20 - 0
Entity/Interfaces/LocationInterface.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace MapBundle\Entity\Interfaces;
+
+use MapBundle\Entity\Location;
+
+interface LocationInterface
+{
+    
+    /**
+     * @return Location
+     */
+    public function getLocation();
+    
+    /**
+     * @param Location $location
+     */
+    public function setLocation(Location $location = null);
+    
+}

+ 85 - 0
Entity/Location.php

@@ -0,0 +1,85 @@
+<?php
+
+namespace MapBundle\Entity;
+
+use Doctrine\ORM\Mapping as ORM;
+use DeviceBundle\Interfaces\DeviceInterface;
+use ExtraDataBundle\Entity\Traits\ExtraDataTrait;
+
+/**
+ * @ORM\Table
+ * @ORM\Entity
+ */
+class Location
+{
+
+    use ExtraDataTrait;
+
+    /**
+     * @var int
+     *
+     * @ORM\Column(name="id", type="integer")
+     * @ORM\Id
+     * @ORM\GeneratedValue(strategy="AUTO")
+     */
+    private $id;
+
+    /**
+     * @var int
+     *
+     * @ORM\Column(type="integer", nullable=true)
+     */
+    private $mapId;
+
+
+    /**
+     * Get id
+     *
+     * @return int
+     */
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    /**
+     * @return DeviceInterface
+     */
+    public function getDevice()
+    {
+        return $this->device;
+    }
+
+    /**
+     * @param DeviceInterface $device
+     */
+    public function setDevice(DeviceInterface $device)
+    {
+        $this->device = $device;
+    }
+    
+    /**
+     * Set mapId
+     *
+     * @param integer $mapId
+     *
+     * @return Location
+     */
+    public function setMapId($mapId = null)
+    {
+        $this->mapId = $mapId;
+
+        return $this;
+    }
+
+    /**
+     * Get mapId
+     *
+     * @return int
+     */
+    public function getMapId()
+    {
+        return $this->mapId;
+    }
+
+}

+ 35 - 0
Entity/Traits/LocationTrait.php

@@ -0,0 +1,35 @@
+<?php
+
+namespace MapBundle\Entity\Traits;
+
+use Doctrine\ORM\Mapping as ORM;
+use MapBundle\Entity\Location;
+
+trait LocationTrait
+{
+
+    /**
+     * @var Location
+     * 
+     * @ORM\OneToOne(targetEntity="MapBundle\Entity\Location", fetch="EXTRA_LAZY", cascade="all")
+     */
+    private $location;
+
+
+    /**
+     * @return Location
+     */
+    public function getLocation()
+    {
+        return $this->location;
+    }
+
+    /**
+     * @param Location $location
+     */
+    public function setLocation(Location $location = null)
+    {
+        $this->location = $location;
+    }
+
+}

+ 71 - 0
EventListener/LocationListener.php

@@ -0,0 +1,71 @@
+<?php
+
+namespace MapBundle\EventListener;
+
+use MapBundle\Entity\Interfaces\LocationInterface;
+use MapBundle\Form\Type\LocationType;
+use Sonata\AdminBundle\Event\ConfigureEvent;
+
+class LocationListener
+{
+
+    /**
+     * @param ConfigureEvent $event
+     */
+    public function configureFormFields(ConfigureEvent $event)
+    {
+        $mapper = $event->getMapper();
+        $subject = $mapper->getAdmin()->getSubject();
+        if ($subject && $subject instanceof LocationInterface) {
+            if ($mapper->hasOpenTab()) {
+                $mapper
+                        ->end()
+                        ->end();
+            }
+            $options = array(
+                'translation_domain' => 'MapBundle'
+            );
+            $mapper
+                    ->tab('Map', $options)
+                    ->with('Location', $options)
+                    ->add('location', LocationType::class, array(
+                        'label' => false,
+                    ))
+                    ->end()
+                    ->end()
+            ;
+        }
+    }
+
+    /**
+     * @param ConfigureEvent $event
+     */
+    public function configureShowFields(ConfigureEvent $event)
+    {
+        $mapper = $event->getMapper();
+        $subject = $mapper->getAdmin()->getSubject();
+        if ($subject && $subject instanceof LocationInterface) {
+            if ($mapper->hasOpenTab()) {
+                $mapper
+                        ->end();
+            }
+            $options = array(
+                'translation_domain' => 'MapBundle',
+                'class' => 'tab-map'
+            );
+            if (!$mapper->has('location')) {
+                $mapper
+                        ->tab('Map', $options)
+                        ->with('Location', $options)
+                        ->add('location', null, array(
+                            'template' => 'LeafletBundle:CRUD:map_show_field.html.twig',
+                            'translation_domain' => 'MapBundle',
+                        ))
+                        ->end()
+                        ->end()
+                ;
+            }
+        }
+    }
+
+}

+ 37 - 0
Form/Type/LocationType.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace MapBundle\Form\Type;
+
+use LeafletBundle\Form\Type\LeafletMapType;
+use MapBundle\Entity\Location;
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormBuilderInterface;
+use Symfony\Component\OptionsResolver\OptionsResolver;
+
+class LocationType extends AbstractType
+{
+
+    /**
+     * {@inheritdoc}
+     */
+    public function buildForm(FormBuilderInterface $builder, array $options)
+    {
+        $builder
+                ->add('extraData')
+                ->add('map', LeafletMapType::class)
+                ;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function configureOptions(OptionsResolver $resolver)
+    {
+        $resolver->setDefaults(array(
+            'required' => false,
+            'translation_domain' => 'MapBundle',
+            'data_class' => Location::class,
+        ));
+    }
+
+}

+ 22 - 1
README.md

@@ -1,6 +1,7 @@
 # MapBundle
 
 - [Instalación](#instalacion)
+- [Mapa](#mapa)
 
 ## Instalación
 
@@ -36,4 +37,24 @@ app/config/config.yml:
 ```yml
 imports:
     - { resource: "@MapBundle/Resources/config/services.yml" }
-```
+```
+
+Requiere además tener instalado los bundles:
+
+- http://infra.flowdat.com:10080/VendorSoftwareFlowdat3/LeafletBundle
+- http://infra.flowdat.com:10080/VendorSoftwareFlowdat3/ExtraDataBundle
+
+## Mapa
+
+Para agregar un campo de tipo Location, el cual agrega un tab en el formulario 
+para agregar coordenadas en el mapa, agregar por ej para la entidad ONU:
+
+```php
+use MapBundle\Entity\Interfaces\LocationInterface;
+use MapBundle\Entity\Traits\LocationTrait;
+
+class ONU implements LocationInterface
+{
+
+    use LocationTrait; 
+```

+ 15 - 3
Resources/config/services.yml

@@ -1,4 +1,16 @@
+parameters:
+    googlemaps_api_key: AIzaSyCq1Gbl9TX6KamfOmXZecuJee1837TdtpI
+    
+twig:
+    globals:
+        script_google_maps: 'https://maps.googleapis.com/maps/api/js?v=3&key=%googlemaps_api_key%'
+    form_themes:
+        - 'MapBundle:Form/Type:location_widget.html.twig'
+        - 'LeafletBundle:Form/Type:leaflet_map_widget.html.twig'
+
 services:
-#    map.example:
-#        class: MapBundle\Example
-#        arguments: ["@service_id", "plain_value", "%parameter%"]
+    map.location_listener:
+       class: MapBundle\EventListener\LocationListener
+       tags:
+           - { name: kernel.event_listener, event: sonata.admin.event.configure.form, method: configureFormFields }
+           - { name: kernel.event_listener, event: sonata.admin.event.configure.show, method: configureShowFields }

+ 6 - 0
Resources/translations/MapBundle.es.yml

@@ -0,0 +1,6 @@
+Map: Mapa
+Location: Ubicación
+form:
+    label_location: Ubicación
+show:
+    label_location: Ubicación

+ 6 - 0
Resources/views/Form/Type/location_widget.html.twig

@@ -0,0 +1,6 @@
+{% block location_widget %}
+    {% spaceless %}
+        {{ form_row(form.extraData) }}
+        {{ form_row(form.map) }}
+    {% endspaceless %}
+{% endblock %}