Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$treeBuilder = new TreeBuilder('oh_google_map_form_type');

$rootNode = $treeBuilder->root('oh_google_map_form_type');
$rootNode = $treeBuilder->getRootNode();


$rootNode
->children()
->scalarNode('api_key')->defaultNull()->end()
->scalarNode('include_gmaps_js')->defaultFalse()->end()
->end();

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
Expand Down
86 changes: 65 additions & 21 deletions Form/Type/GoogleMapType.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
<?php

/**
* Autosearch from request - ?mapSearchInput=Warszawa ul. nowogrodzka
*/
namespace Oh\GoogleMapFormTypeBundle\Form\Type;

use AppBundle\Model\AppConfig;
use Mea\CoreBundle\Form\Type\StringType;
use Mea\CoreBundle\Service\ErrorService;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class GoogleMapType extends AbstractType
Expand All @@ -18,29 +26,57 @@ class GoogleMapType extends AbstractType
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add($options['lat_name'], $options['type'], array_merge($options['options'], $options['lat_options']))
->add($options['lng_name'], $options['type'], array_merge($options['options'], $options['lng_options']))
;

unset($options['options']['compound']);

if($options['editable'])
$builder
->add($options['lat_name'], HiddenType::class, array_merge($options['options'], $options['lat_options']))
->add($options['lng_name'],HiddenType::class, array_merge($options['options'], $options['lng_options']))
;

else{

$entity = $builder->getData();

ErrorService::getInstance()->addDebug($builder);

$builder
->add($options['lat_name'],HiddenType::class, array_merge($options['options'], $options['lat_options']))
->add($options['lng_name'], HiddenType::class, array_merge($options['options'], $options['lng_options']))
// ->add('Coordinates',StringType::class,[
// 'data'=>'sdfdfs',
// 'required'=>false,
// 'mapped'=>false,
// ])
;

}
}

/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'type' => 'text', // the types to render the lat and lng fields as
'options' => array(), // the options for both the fields
'editable' => true,
'type' => TextType::class, // the types to render the lat and lng fields as
'compound'=>true,
'options' => array(
'compound'=>true
), // the options for both the fields
'lat_options' => array(), // the options for just the lat field
'lng_options' => array(), // the options for just the lng field
'lat_name' => 'lat', // the name of the lat field
'lng_name' => 'lng', // the name of the lng field
'error_bubbling' => false,
'zoom_callback' => false, //form callback
'map_width' => 300, // the width of the map
'map_height' => 300, // the height of the map
'default_lat' => 51.5, // the starting position on the map
'default_lng' => -0.1245, // the starting position on the map
'default_lat' => AppConfig::LOCATION_DEFAULT_LAT, // the starting position on the map
'default_lng' => AppConfig::LOCATION_DEFAULT_LNG, // the starting position on the map
'default_zoom' => AppConfig::LOCATION_DEFAULT_ZOOM, // the starting position on the map
'include_jquery' => false, // jquery needs to be included above the field (ie not at the bottom of the page)
'include_gmaps_js'=>true // is this the best place to include the google maps javascript?
));
Expand All @@ -51,23 +87,31 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['lat_name'] = $options['lat_name'];
$view->vars['lng_name'] = $options['lng_name'];
$view->vars['map_width'] = $options['map_width'];
$view->vars['map_height'] = $options['map_height'];
$view->vars['default_lat'] = $options['default_lat'];
$view->vars['default_lng'] = $options['default_lng'];
$view->vars['include_jquery'] = $options['include_jquery'];
$view->vars['include_gmaps_js'] = $options['include_gmaps_js'];
$view->vars['editable'] = $options['editable'];
$view->vars['lat_name'] = $options['lat_name'];
$view->vars['lng_name'] = $options['lng_name'];
$view->vars['map_width'] = $options['map_width'];
$view->vars['map_height'] = $options['map_height'];
$view->vars['default_lat'] = $options['default_lat'];
$view->vars['default_lng'] = $options['default_lng'];
$view->vars['default_zoom'] = $options['default_zoom'];
$view->vars['include_jquery'] = $options['include_jquery'];
$view->vars['include_gmaps_js'] = $options['include_gmaps_js'];
$view->vars['zoom_callback'] = $options['zoom_callback'];




}

public function getParent()
{
return 'form';
return TextType::class;
}

public function getName()
public function getBlockPrefix()
{
return 'oh_google_maps';
return 'oh_google_maps';

}
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Installation

This bundle is compatible with Symfony 2.1. Add the following to your `composer.json`:

"oh/google-map-form-type-bundle": "dev-master"
"grekpg/OhGoogleMapFormTypeBundle": "dev-master"

Register the bundle in your `app/AppKernel.php`:

Expand Down Expand Up @@ -36,7 +36,9 @@ Usage

This bundle contains a new FormType called GoogleMapType which can be used in your forms like so:

$builder->add('latlng', 'oh_google_maps');
$builder->add('latlng', GoogleMapType::class,array(
'zoom_callback'=>'function(zoom){ $("#zoom").val(zoom); }' //optional zoom change listener
));

On your model you will have to process the latitude and longitude array

Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<!-- oh_google_maps form type -->
<service id="form.type.oh_google_maps" class="%form.type.oh_google_maps.class%">
<tag name="form.type" alias="oh_google_maps" />
<property name="test">"%form.type.oh_google_maps.class%"</property>
</service>
</services>
</container>
9 changes: 8 additions & 1 deletion Resources/public/js/jquery.ohgooglemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'lat_field' : null,
'lng_field' : null,
'callback' : function (location, gmap) {},
'zoom_callback' : function (zoom) {},
'error_callback' : function(status) {
$this.settings.search_error_el.text(status);
},
Expand Down Expand Up @@ -51,6 +52,10 @@

});

google.maps.event.addListener(this.map, 'zoom_changed', function() {
$this.settings.zoom_callback($this.map.getZoom());
});

google.maps.event.addListener(this.map, 'click', function(event) {
$this.insertMarker(event.latLng);
});
Expand Down Expand Up @@ -158,8 +163,10 @@
'lat_field' : null,
'lng_field' : null,
'callback' : function (location, gmap) {},
'zoom_callback' : function (zoom) {},
'error_callback' : function(status) {
$this.settings.search_error_el.text(status);
//todo: what is this ? console.log(status);
//$this.settings.search_error_el.text(status);
}
}

Expand Down
20 changes: 12 additions & 8 deletions Resources/views/Form/google_maps.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<div {{ block('widget_container_attributes') }}>
{% block oh_google_maps_html %}
<div id="{{ id }}_container">
<input type="text" id="{{ id }}_input" /><button id="{{ id }}_search_button" class="btn">Search</button><br /><a href="#" id="{{ id }}_current_position">Current location</a>
<input type="text" id="{{ id }}_input" />
<button id="{{ id }}_search_button" class="btn">Search</button><br />
<a href="#" id="{{ id }}_current_position">Current location</a>
<div id="{{ id }}_map_canvas" class="gmap" style="width: {{ map_width }}px; height: {{ map_height }}px"></div>
<div id="{{ id }}_error"></div>
</div>
Expand All @@ -17,13 +19,14 @@
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
{% endif %}
{% if include_gmaps_js %}
<script type="text/javascript" src="//maps.google.com/maps/api/js?sensor=true"></script>
<script>alert('override template ad add key $KEY');</script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=$KEY"></script>
{% endif %}
{% javascripts
'@OhGoogleMapFormTypeBundle/Resources/public/js/jquery.ohgooglemaps.js'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{#{% javascripts#}
{#'@OhGoogleMapFormTypeBundle/Resources/public/js/jquery.ohgooglemaps.js'#}
{#%}#}
{#<script type="text/javascript" src="{{ asset_url }}"></script>#}
{#{% endjavascripts %}#}
{% endblock %}
{% block oh_google_maps_javascript %}
{% block oh_google_maps_callback %}
Expand All @@ -46,7 +49,8 @@
'default_zoom' : {% if value is defined and value and value.lat and value.lng %}15{% else %}5{% endif %},
'lat_field' : $('#{{ attribute(form, lat_name).vars.id }}'),
'lng_field' : $('#{{ attribute(form, lng_name).vars.id }}'),
'callback' : oh_google_maps_callback
'callback' : oh_google_maps_callback,
'zoom_callback' : {{ zoom_callback|default('function(){}')|raw }}
});
});

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"name": "oh/google-map-form-type-bundle",
"name": "grekpg/ohgooglemapformtypebundle",
"type": "symfony-bundle",
"description": "Set latitude and longitude values on a form using Google Maps",
"keywords": ["google maps", "symfony", "form"],
"homepage": "https://github.com/ollieLtd/OhGoogleMapFormTypeBundle",
"homepage": "https://github.com/grekpg/OhGoogleMapFormTypeBundle",
"license": "MIT",
"authors": [
{
"name": "Ollie Harridge",
"email": "code@oll.ie"
"name": "grek",
"email": "grek@g.pl"
}
],
"require": {
"php": ">=5.3.2",
"symfony/framework-bundle": "~2.1"
"symfony/framework-bundle": "*"
},
"autoload": {
"psr-0": {
Expand Down