Skip to content

Commit

Permalink
Update to symfony 3 and remove jquery dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel authored and Daniel committed Jul 9, 2016
1 parent b8b6150 commit 0893981
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 284 deletions.
3 changes: 2 additions & 1 deletion Document/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Addressable\Bundle\Document;

use Addressable\Bundle\Model\AddressableInterface;
use Addressable\Bundle\Validator\Constraints as AddressValidator;
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
use Symfony\Component\Validator\Constraints as Assert;
Expand All @@ -11,7 +12,7 @@
*
* @PHPCR\Document()
*/
class Address
class Address implements AddressableInterface
{
/**
* @PHPCR\Id()
Expand Down
3 changes: 2 additions & 1 deletion Entity/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Addressable\Bundle\Entity;

use Addressable\Bundle\Model\AddressableInterface;
use Addressable\Bundle\Validator\Constraints as AddressValidator;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
Expand All @@ -10,7 +11,7 @@
* Entity representing an address.
* You must extend this class and add an id and entity annotation.
*/
class Address
class Address implements AddressableInterface
{
/**
* @ORM\Column(type="string")
Expand Down
23 changes: 6 additions & 17 deletions Form/Type/AddressMapType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Form type for adding a map to select a full address.
Expand All @@ -33,16 +32,15 @@ public function buildForm(FormBuilderInterface $builder, array $options)
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'google_api_key' => '',
'map_width' => '100%', // the width of the map
'map_height' => 300, // the height of the map
'map_height' => '300px', // 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
'include_jquery' => false, // whether to include jquery
'include_gmaps_js' => true, // whether to include maps script
'include_current_position_action' => false, // whether to include the current position button
'include_current_position_action' => true, // whether to include the current position button
'street_number_field' => array(
'name' => 'streetNumber',
'type' => 'text',
Expand Down Expand Up @@ -100,6 +98,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['google_api_key'] = $options['google_api_key'];
// fields
$view->vars['lat_field'] = $options['latitude_field']['name'];
$view->vars['lng_field'] = $options['longitude_field']['name'];
Expand All @@ -113,16 +112,6 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$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['include_current_position_action'] = $options['include_current_position_action'];
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'address_map_type';
}
}
72 changes: 41 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Under development.

Next steps:

- Add Bing map option/alternative
- Geo-location awareness (search by radius, distance calculator, route mapping, etc)


Expand All @@ -37,25 +36,14 @@ Register the bundle in your `app/AppKernel.php`:
new Addressable\Bundle\AddressableBundle(),
```

Add the bundle to assetic in your config file:

```yaml
# app/config/config.yml

# Assetic Configuration
assetic:
bundles: [ 'AddressableBundle' ]
```
Include the twig template for the type layout.

```yaml
# app/config/config.yml

twig:
form:
resources:
- 'AddressableBundle:Form:fields.html.twig'
form_themes:
- AddressableBundle:Form:fields.html.twig
```
Now your entity or document must:
Expand All @@ -68,9 +56,8 @@ Now your entity or document must:
use Addressable\Bundle\Model\AddressableInterface;
use Addressable\Bundle\Model\Traits\ORM\AddressableTrait;

class YourEntity implementes AddressableInterface
class YourEntity implements AddressableInterface
{

use AddressableTrait;

/**
Expand All @@ -79,7 +66,6 @@ Now your entity or document must:
protected $yourOtherField;

...

}
```

Expand All @@ -89,24 +75,28 @@ Once your entity is setup, we can add the address map selector to your forms in

```php

// if you are using standard symfony form type
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('address', AddressMapType::class, array(
'google_api_key' => 'yourKeyHere'
))
...
}

// if you are using Sonata Admin
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Location')
->add('address', 'address_map_type', array())
->add('address', AddressMapType::class, array(
'google_api_key' => 'yourKeyHere'
))
->end()
...
}

// if you are using standard symfony form type
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('address', 'address_map_type', array())
...
}

```

Note: if using address as a child or relation remember to set the 'data_class' options pointing to the Address object.
Expand All @@ -119,15 +109,14 @@ We can override several options:
```php
->add(
'address',
'addressable_type',
AddressMapType::class,
array(
'google_api_key' => 'yourKeyHere',
'map_width' => '100%', // the width of the map
'map_height' => 300, // the height of the map
'map_height' => '300px', // 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
'include_jquery' => false, // whether to include jquery
'include_gmaps_js' => true, // whether to include maps script
'include_current_position_action' => false, // whether to include the set current position button
'include_current_position_action' => true, // whether to include the set current position button
'street_number_field' => array(
'name' => 'streetNumber',
'type' => 'text',
Expand Down Expand Up @@ -181,6 +170,27 @@ We can override several options:
);
```

Further Customization
---------------------

If you don't want the bundle to use it's own script you can override the *address_map_scripts* block to be empty; and
then simply copy and paste the javascript in vendor/daa/addressable-bundle/Resources/public/js/address_map.js
to your own js files.

To add additional functionality after address updates, simply override the block *address_map_callback* and extend to add
the additional functionality (or make it empty and define var gmap_callback in your js code).

```
{% block address_map_callback %}
<script>
var gmap_callback = function(location, gmap){
// your callback code here
}
</script>
{% endblock %}
```


Screenshot
----------
Expand Down
5 changes: 0 additions & 5 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
services:

# form type
form.type.address_map_type:
class: Addressable\Bundle\Form\Type\AddressMapType
tags:
- { name: form.type, alias: address_map_type }
Loading

0 comments on commit 0893981

Please sign in to comment.