-
-
Save habibun/dbdd58e8d7ae364ba1a1a95666758a11 to your computer and use it in GitHub Desktop.
s
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Eit\Bundle\UserBundle\Form; | |
use Doctrine\ORM\EntityRepository; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
/** | |
* Class UserType. | |
*/ | |
class UserType extends AbstractType | |
{ | |
/** | |
* @param FormBuilderInterface $builder | |
* @param array $options | |
*/ | |
private $roles; | |
/** | |
* @param array $roles | |
*/ | |
public function __construct($roles) | |
{ | |
$this->roles = $roles; | |
} | |
/** | |
* @param FormBuilderInterface $builder | |
* @param array $options | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$user = $options['data']; | |
$builder = $builder | |
->add('firstName', 'text', array( | |
'label' => 'user.form.firstName', | |
)) | |
->add('lastName', 'text', array( | |
'label' => 'user.form.lastName', | |
'required' => false, | |
)) | |
->add('username', 'text', array( | |
'label' => 'user.form.username', | |
)) | |
->add('email', 'email', array( | |
'label' => 'user.form.email', | |
)) | |
->add('group', 'entity', array( | |
'class' => 'EitAdminBundle:Group', | |
'label' => 'user.form.group', | |
'placeholder' => 'choice.group', | |
'required' => false, | |
)) | |
->add('companies', 'entity', array( | |
'class' => 'EitAdminBundle:Company', | |
'label' => 'user.form.companies', | |
'multiple' => true, | |
'required' => false, | |
)) | |
->add('locations', 'entity', array( | |
'class' => 'EitAdminBundle:Location', | |
'label' => 'user.form.locations', | |
'multiple' => true, | |
'query_builder' => function (EntityRepository $er) { | |
return $er->createQueryBuilder('l') | |
->where('l.systemGenerated = false') | |
; | |
}, | |
'required' => false, | |
)) | |
->add('marketOrganogram', 'entity', array( | |
'class' => 'Eit\Bundle\SaleBundle\Entity\MarketOrganogram', | |
'label' => 'user.form.market_organogram', | |
'placeholder' => 'choice.market_organogram', | |
'query_builder' => function (EntityRepository $er) { | |
return $er->createQueryBuilder('mo') | |
->where('mo.status = true') | |
; | |
}, | |
'required' => false, | |
)) | |
->add('image', 'file', array( | |
'label' => 'user.form.image', | |
'required' => false, | |
'data_class' => null, | |
)) | |
->add('enabled', 'checkbox', array( | |
'label' => 'user.form.enabled', | |
'required' => false, | |
)) | |
->add('locked', 'checkbox', array( | |
'label' => 'user.form.locked', | |
'required' => false, | |
)) | |
->add('roles', 'choice', array( | |
'choices' => $this->flattenArray($this->roles), | |
'label' => 'user.form.roles', | |
'multiple' => true, | |
'required' => false, | |
)) | |
->add('submit', 'submit', array( | |
'label' => 'save', | |
'attr' => array( | |
'class' => 'btn btn-default btn-sm', | |
), | |
)) | |
; | |
if (null == $user->getId()) { | |
$builder->add('plainPassword', 'repeated', array( | |
'type' => 'password', | |
'first_options' => array('label' => 'user.form.password', 'attr' => array('class' => 'input-sm')), | |
'second_options' => array('label' => 'user.form.passwordConfirmation', 'attr' => array('class' => 'input-sm')), | |
'invalid_message' => 'fos_user.password.mismatch', | |
)); | |
} | |
} | |
/** | |
* @param OptionsResolverInterface $resolver | |
*/ | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'Eit\Bundle\UserBundle\Entity\User', | |
'csrf_protection' => false, | |
'method' => 'POST', | |
)); | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return 'eit_bundle_userbundle_user'; | |
} | |
private function flattenArray(array $data) | |
{ | |
$returnData = array(); | |
foreach ($data as $key => $value) { | |
$tempValue = str_replace('ROLE_', '', $key); | |
$tempValue = ucwords(strtolower(str_replace('_', ' ', $tempValue))); | |
$returnData[$key] = $tempValue; | |
} | |
return $returnData; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment