diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 9a273b6..3a6486d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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 diff --git a/Form/Type/GoogleMapType.php b/Form/Type/GoogleMapType.php index 25cbe22..2fe4263 100755 --- a/Form/Type/GoogleMapType.php +++ b/Form/Type/GoogleMapType.php @@ -1,13 +1,21 @@ 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? )); @@ -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'; + } } diff --git a/README.md b/README.md index 28c09ac..e905d4c 100755 --- a/README.md +++ b/README.md @@ -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`: @@ -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 diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 6cac9f7..f9343a7 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -12,6 +12,7 @@ + "%form.type.oh_google_maps.class%" \ No newline at end of file diff --git a/Resources/public/js/jquery.ohgooglemaps.js b/Resources/public/js/jquery.ohgooglemaps.js index cbe40b6..b0a49cb 100755 --- a/Resources/public/js/jquery.ohgooglemaps.js +++ b/Resources/public/js/jquery.ohgooglemaps.js @@ -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); }, @@ -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); }); @@ -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); } } diff --git a/Resources/views/Form/google_maps.html.twig b/Resources/views/Form/google_maps.html.twig index b4df551..7a6df2e 100755 --- a/Resources/views/Form/google_maps.html.twig +++ b/Resources/views/Form/google_maps.html.twig @@ -2,7 +2,9 @@
{% block oh_google_maps_html %}
-
Current location + +
+ Current location
@@ -17,13 +19,14 @@ {% endif %} {% if include_gmaps_js %} - + + {% endif %} - {% javascripts - '@OhGoogleMapFormTypeBundle/Resources/public/js/jquery.ohgooglemaps.js' - %} - - {% endjavascripts %} + {#{% javascripts#} + {#'@OhGoogleMapFormTypeBundle/Resources/public/js/jquery.ohgooglemaps.js'#} + {#%}#} + {##} + {#{% endjavascripts %}#} {% endblock %} {% block oh_google_maps_javascript %} {% block oh_google_maps_callback %} @@ -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 }} }); }); diff --git a/composer.json b/composer.json index ad1adad..46e61bf 100755 --- a/composer.json +++ b/composer.json @@ -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": {