<?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 InterviewAdmin 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 Principales', array(
                    'class'       => 'col-md-6',
                    'box_class'   => 'box box-solid',
                    'description' => '',
                ));
			
			$formMapper->add('title', 'text');
			$formMapper->add('subtitle');
			
			$formMapper->add('author');
			
			$formMapper->add('resume');
			$formMapper->add('quotation');
			$formMapper->add('questionanswers', 'sonata_type_collection', array(
				'required' => true,
                'type_options' => array(
                    // Prevents the "Delete" option from being displayed
                    'delete' => true,
                    /*'delete_options' => array(
                        // You may otherwise choose to put the field but hide it
                        'type'         => '',
                        // In that case, you need to fill in the options as well
                        'type_options' => array(
                            'mapped'   => false,
                            'required' => false,
                        )
                    )*/
                )
            ), array(
                'edit' => 'inline',
                'inline' => 'table',
                'sortable' => 'position',
            ));
			
			$formMapper->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');
	   $datagridMapper->add('published');
	   $datagridMapper->add('title');
       $datagridMapper->add('slug')
			->add('author')
			->add('address');
    }
	//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')
			->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_interview') )
			->add('author')
			->add('address','string', array('label'=>'Adresse'));
    }
	
	public function toString($object)
    {
        return ($object->getTitle()!=null)
            ? $object->getTitle()
            : 'Interview'; // shown in the breadcrumb on the create view
    }
	
	/*
	*  PRE PERSIST
	*/
	public function prePersist($object)
	{
		$this->preUpdate($object);
	}
	
	public function preUpdate($object)
	{
		$object->setQuestionanswers($object->getQuestionanswers());
		$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);
                    }
                }
            }
        }
    }
}
?>