<?php
// src/AppBundle/Admin/ImageAdmin.php
namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

class ImageAdmin extends AbstractAdmin
{	
    protected function configureFormFields(FormMapper $formMapper)
    {
        // get the current Image instance
        $image = $this->getSubject();

        // use $fileFieldOptions so we can add other options to the field
        $fileFieldOptions = array('required' => false, 'label'=>"Selectionnez une image sur votre ordinateur");
        if ($image && ($webPath = $image->getUrl()) && ($this->isCurrentRoute('edit')) ) {
            // get the container so the full path to the image can be set
            $container = $this->getConfigurationPool()->getContainer();
            $fullPath = $container->get('request_stack')->getCurrentRequest()->getBasePath().$webPath;

            // add a 'help' option containing the preview's img tag
            $fileFieldOptions['help'] = '<img src="'.$fullPath.'" style="float: right; position: relative; padding-bottom: 0px; margin-right: 6%; top: -90px; margin-bottom: -70px;" class="col-sm-3" />';
        }
		
		$formMapper 
            ->add('file', 'file', $fileFieldOptions)
            ->add('filename', 'text', array
				(
				'required' => false, 
				'label'=>"ou entrez le lien d'une image sur le web",
				'attr' => array('style' => 'width: 66.6667% !important;')
				)
			);
    }
	
	// This method configures the filters, used to filter and sort the list of models;
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
       $datagridMapper->add('id');
	   $datagridMapper->add('filename');
    }
	//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->add('id');
        $listMapper->addIdentifier('filename');
    }

    public function prePersist($image)
    {
        $this->manageFileUpload($image);
    }

    public function preUpdate($image)
    {
        $this->manageFileUpload($image);
    }

    private function manageFileUpload($image)
    {
        if ($image->getFile()) {
            $image->refreshUpdated();
        }
    }
	
	public function toString($object)
    {
        return ($object->getAlt()!=null)
            ? $object->getAlt()
            : 'Image'; // shown in the breadcrumb on the create view
    }
	
	/*public function getObjectMetadata($object)
    {
        $provider = $this->pool->getProvider($object->getProviderName());

        $url = $provider->generatePublicUrl($object, $provider->getFormatName($object, 'admin'));

        return new Metadata($object->getName(), $object->getDescription(), $url);
    }*/
}
?>