<?php
// src/AppBundle/Admin/CompagnyAdmin.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 ApplicantAdmin 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
			->with('Informations', array(
                    'class'       => 'col-md-6',
                    'box_class'   => 'box box-solid',
                    'description' => '',
                ))
			->add('name', 'text')
			->add('firstname', 'text')
			->add('jobTitle','text')
			->add('resume','textarea',array('attr' => array('rows' => '14')))
			->add('sector','entity', array(
				'class' => 'AppBundle\Entity\Sector',
				'choice_label' => 'name',
				'required'=>false
			))
			->add('linkedInProfilURL')
			
			->add('birthdate','date', array(
							'placeholder' => array(
								'year' => 'Année', 'month' => 'Mois', 'day' => 'Jour'
							),
							'format' => 'd M y',
							'model_timezone' => 'Europe/Paris',
				'required' => false
					))
			->add('gender','choice',array('expanded'=>true, 'multiple'=>false,'choices' => array('Femme' =>true,'Homme' => false)))
			
			->add('photo', 'sonata_type_model', array(
				'property' => 'alt',
				'template' => 'appBundle:FieldType:imageChoiceField.html.twig',
				'btn_delete' => false,
				'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('address')
				->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
				))
		->end();
    }

	// This method configures the filters, used to filter and sort the list of models;
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
       $datagridMapper->add('id')->add('published');
       $datagridMapper->add('firstname');
	   $datagridMapper->add('name');
    }
	//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')
			->add('published',null,array('label'=>'Publié','editable' => true))
			->add('slug', 'string',array('label' => 'lien', 'template' => 'AppBundle:Admin:link_event.html.twig','path'=>'application_page_applicant') )
			->addIdentifier('name','string', array('label'=>'Nom'))
			->addIdentifier('firstname','string', array('label'=>'Prénom'))
			->add('jobTitle','string', array('label'=>'Poste recherchés'));
    }
	
	public function toString($object)
    {
        return ($object->getName()!=null)
            ? $object->getName()
            : 'Job'; // shown in the breadcrumb on the create view
    }
/*
	*  PRE PERSIST
	*/
	public function prePersist($object)
	{
		$this->preUpdate($object);
	}
	
	public function preUpdate($object)
	{
		$this->manageEmbeddedImageAdmins($object);
	}

	 private function manageEmbeddedImageAdmins($object)
    {
        // Cycle through each field
        foreach ($this->getFormFieldDescriptions() as $fieldName => $fieldDescription) {
            // detect embedded Admins that manage Images
            if ($fieldDescription->getType() === 'sonata_type_admin' &&
                ($associationMapping = $fieldDescription->getAssociationMapping()) &&
                $associationMapping['targetEntity'] === 'AppBundle\Entity\Image'
            ) {
                $getter = 'get'.$fieldName;
                $setter = 'set'.$fieldName;

                /** @var Image $image */
                $image = $company->$getter();

                if ($image) {
                    if ($image->getFile()) {
                        // update the Image to trigger file management
                        $image->refreshUpdated();
                    } elseif (!$image->getFile() && !$image->getUrl()) {
                        // prevent Sf/Sonata trying to create and persist an empty Image
                        $object->$setter(null);
                    }
                }
            }
        }
    }
	
}
?>