<?php
// src/AppBundle/Admin/BlogPostAdmin.php
namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
//use Sonata\AdminBundle\Admin\AbstractAdmin;

use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;


use FOS\UserBundle\Util\LegacyFormHelper;
use Sonata\AdminBundle\Route\RouteCollection;
use FOS\UserBundle\Form\Type\ChangePasswordFormType;
use FOS\UserBundle\Model\UserManagerInterface;

use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class UserAdmin extends Admin
{
	protected function configureRoutes(RouteCollection $collection)
    {
        $collection->add('editPass', $this->getRouterIdParameter().'/editPassword');
    }

	protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('Informations générales')
                ->add('username',null, array('label' => 'Identifiant'))
                ->add('email',null, array('label' => 'Adresse e-mail'));
		if ($this->isCurrentRoute('editPass')) {
			$formMapper
				->add('plainPassword', 'repeated', array(
						'type' => 'password',
						'options' => array('translation_domain' => 'FOSUserBundle'),
						'first_options' => array('label' => 'form.new_password'),
						'second_options' => array('label' => 'form.new_password_confirmation'),
						'invalid_message' => 'fos_user.password.mismatch',
				));
		}elseif ($this->isCurrentRoute('create')) {
                $formMapper->add('plainPassword','password' );
		} 
		if (!$this->isCurrentRoute('editPass')) {
		$formMapper->end()
			->with('Informations Avancées')
                ->add('roles', 'choice', array(
					  'choices' => array('Utilisateur' => 'ROLE_USER','Administrateur' => 'ROLE_ADMIN','Super administrateur' => 'ROLE_SUPER_ADMIN'),
					  'required' => true,
					  'multiple'=>true,
					))
                ->add('locked', null, array('required' => false))
                ->add('expired', null, array('required' => false))
                ->add('enabled', null, array('required' => false))
                ->add('credentialsExpired', null, array('required' => false))
            ->end()
        ;
		}
    } 
	
	protected function configureListFields(ListMapper $listMapper)
    {
    	$listMapper
			->add('username',null,array('label'=>'Nom'))
			->add('email',null,array('label'=>'Email'))
			//->add('category',null,array('label'=>'Catégorie','associated_property'=>'name'))
			//->add('roles',null,array('label'=>'Groupe'))
			
			// add custom action links
            ->add('_action', 'actions', array(
                'actions' => array(
                    'show' => array(),
                    'edit' => array(),
					'editPassword' => array('template' => 'AppBundle:CRUD:list__action_editPassword.html.twig'),
                    'delete' => array(),
                    
                )
            ))
			;
    }
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper->add('username');
    }
	
	public function toString($object)
    {
		return $object->getUsername();
    }


	public function prePersist($user)
    {
        parent::prePersist($user);
        $this->getUserManager()->updateCanonicalFields($user);
		$this->getUserManager()->updatePassword($user);
		$this->getUserManager()->updateUser($user);

    }
    public function preUpdate($user)
    {
        parent::preUpdate($user);
        $this->getUserManager()->updateCanonicalFields($user);
		$this->getUserManager()->updatePassword($user);
		$this->getUserManager()->updateUser($user);

    }

    public function setUserManager(UserManagerInterface $userManager)
    {
        $this->userManager = $userManager;
    }

    /**
     * @return UserManagerInterface
     */
    public function getUserManager()
    {
        return $this->userManager;
    }
	
    
    public function setRoles(Array $roles)
    {
		$this->roles = $roles;
    }
	/**
     * @return $container->getParameter('security.role_hierarchy.roles')
	 **/
    public function getRoles()
    {
        return $this->roles;
    }
	
}