<?php
// src/AppBundle/Admin/EventAdmin.php
namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

use Oh\GoogleMapFormTypeBundle\Form\Type\GoogleMapType;


class EventAdmin extends AbstractAdmin
{
	//configure which fields are displayed on the edit and create actions. 
	//The FormMapper behaves similar to the FormBuilder of the Symfony Form component;
    protected function configureFormFields(FormMapper $formMapper)
    {
		$formMapper // the tab call is optional
                ->with('Informations', array(
                    'class'       => 'col-md-6',
                    'box_class'   => 'box box-solid',
                    'description' => '',
                ))
					->add('title', 'text')
					->add('date_start','datetime', array(
							'placeholder' => array(
								'year' => 'Année', 'month' => 'Mois', 'day' => 'Jour',
								//'hour' => 'Heure', 'minute' => 'Minute', 'second' => 'Seconde',
							),
							'date_format' => 'd M y',
							'model_timezone' => 'Europe/Paris',
							'years' => range(Date('Y'),Date('Y')+10)
					))
					->add('date_end','datetime', array(
							'placeholder' => array(
								'year' => 'Année', 'month' => 'Mois', 'day' => 'Jour',
								//'hour' => 'Heure', 'minute' => 'Minute', 'second' => 'Seconde',
							),
							'date_format' => 'd M y',
							'model_timezone' => 'Europe/Paris',
							'years' => range(Date('Y'),Date('Y')+10),
							'required'=>false
					))
					->add('description','textarea',array('attr' => array('rows' => '14')))
					->add('organizer')
					
					->add('registration')
					->add('link')
					->add('price')
				
				->add('authorname','text',array('label'=>'Nom du contact'))
				->add('authoremail','text',array( 'label'=>'Email'))
				->add('authorphonenumber','text',array('label'=>'Numéro de téléphone','required'=>false))
				->add('published',null,array( 'data' => true,'label'=>'Publié'))
                ->end();
	   
		$formMapper->with('Localisation', array(
                    'class'       => 'col-md-6',
                    'box_class'   => 'box box-solid location-box',
                    'description' => '',
                ))
				//->add('location_adresse')
				->add('latlng', GoogleMapType::class, array(
				'label' 		 => 'Posistion sur google map',
				'map_width'      => '100%',     // the width of the map
				'map_height'     => '350px',     // the height of the map
				'default_lat'    => 46.2043907,    // the starting position on the map
				'default_lng'    => 6.143157699, // the starting position on the map
				))
				->add('street')
				->add('number')
				->add('zip')
				->add('city')
		->end();
    }

	// This method configures the filters, used to filter and sort the list of models;
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
		$datagridMapper->add('id');
	   $datagridMapper->add('published');
	   $datagridMapper->add('title');
       $datagridMapper->add('slug');
       $datagridMapper->add('date_start');
       $datagridMapper->add('date_end');
       $datagridMapper->add('city');
       $datagridMapper->add('organizer');
    }
	//Here you specify which fields are shown when all models are listed 
	//(the addIdentifier() method means that this field will link to the show/edit page of this particular model).
    protected function configureListFields(ListMapper $listMapper)
    {
       $listMapper->addIdentifier('id');
	   $listMapper->addIdentifier('title')
					->add('published',null,array('label'=>'Publié','editable' => true))
					->add('slug', 'string',array('label' => 'lien', 'template' => 'AppBundle:Admin:link_event.html.twig','path'=>'application_page_event') );
	   	 /*$listMapper->add('slug', 'url', array('route' => array(
						'name' => 'application_page_event',
						'absolute' => true,
						'identifier_parameter_name' => 'slug')
				,));
	*/
		$listMapper->add('date_start','datetime', array(
                'pattern' => constant('IntlDateFormatter::SHORT'),
                'locale' => 'fr',
                'timezone' => 'Europe/Paris',
		));
		$listMapper->add('date_end','datetime', array(
                'pattern' => constant('IntlDateFormatter::SHORT'),
                'locale' => 'fr',
                'timezone' => 'Europe/Paris',
		));
        $listMapper->add('city');
        $listMapper->add('organizer')
				->add('authorname', 'string',array('label' => 'Posté par'))
				->add('authoremail', 'string',array('label' => 'e-mail'));
    }
	
	public function toString($object)
    {
        return ($object->getTitle()!=null)? $object->getTitle(): 'Evenement';
    }
}
?>