<commit_before><?php
/**
 * This file is part of the Clastic package.
 *
 * (c) <NAME> <<EMAIL>>
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Clastic\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
 * UserType
 *
 * @author <NAME> <<EMAIL>>
 */
class UserFormType extends AbstractType
{
    private $isNew;

    /**
     * @param bool $isNew
     */
    public function __construct($isNew)
    {
        $this->isNew = $isNew;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                $builder->create('tabs', 'tabs', array('inherit_data' => true))
                    ->add($this->createGeneralTab($builder))
                    ->add($this->createPasswordTab($builder))
                    ->add($this->createRoleTab($builder))
                    ->add($this->createActionTab($builder))
            );
    }

    private function getAvailableRoles(FormBuilderInterface $builder)
    {
        $roles = array(
            'ROLE_ADMIN' => 'Admin',
            'ROLE_USER' => 'User',
        );

        if ($builder->getData() && $builder->getData()->getId() === 1) {
            $roles['ROLE_SUPER_ADMIN'] = sprintf('%s (super)', $roles['ROLE_ADMIN']);
            unset($roles['ROLE_ADMIN']);
        }

        return $roles;
    }

    private function createGeneralTab(FormBuilderInterface $builder)
    {
        return $this->createTab(
                $builder,
                'general',
                array('label' => 'user.form.tab.general.label')
            )
            ->add('username', 'text', array(
                'label' => 'user.form.tab.general.field.username',
            ))
            ->add('email', 'text', array(
                'label' => 'user.form.tab.general.field.email',
            ))
            ->add('enabled', 'checkbox', array(
                'value' => true,
                'label' => 'user.form.tab.general.field.enabled',
            ));
    }

    private function createPasswordTab(FormBuilderInterface $builder)
    {
        return $this->createTab($builder, 'password', array('label' => 'user.form.tab.password.label'))
            ->add('plainPassword', 'repeated', array(
                'type' => 'password',
                'required' => $this->isNew,
                'first_options' => array('label' => 'user.form.tab.password.field.password'),
                'second_options' => array('label' => 'user.form.tab.password.field.password_repeat'),
                'invalid_message' => 'The passwords don\'t match',
            ));
    }

    private function createRoleTab(FormBuilderInterface $builder)
    {
        return $this->createTab($builder, 'role', array('label' => 'user.form.tab.role.label'))
            ->add('roles', 'multi_select', array(
                'choices' => $this->getAvailableRoles($builder),
                'label' => 'user.form.tab.role.field.roles'
            ));
    }

    private function createActionTab(FormBuilderInterface $builder)
    {
        return $builder->create('actions', 'tabs_tab_actions', array(
                'mapped' => false,<commit_msg>[User] Allow an empty value on the enabled field.<commit_after><?php
/**
 * This file is part of the Clastic package.
 *
 * (c) <NAME> <<EMAIL>>
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Clastic\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
 * UserType
 *
 * @author <NAME> <<EMAIL>>
 */
class UserFormType extends AbstractType
{
    private $isNew;

    /**
     * @param bool $isNew
     */
    public function __construct($isNew)
    {
        $this->isNew = $isNew;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                $builder->create('tabs', 'tabs', array('inherit_data' => true))
                    ->add($this->createGeneralTab($builder))
                    ->add($this->createPasswordTab($builder))
                    ->add($this->createRoleTab($builder))
                    ->add($this->createActionTab($builder))
            );
    }

    private function getAvailableRoles(FormBuilderInterface $builder)
    {
        $roles = array(
            'ROLE_ADMIN' => 'Admin',
            'ROLE_USER' => 'User',
        );

        if ($builder->getData() && $builder->getData()->getId() === 1) {
            $roles['ROLE_SUPER_ADMIN'] = sprintf('%s (super)', $roles['ROLE_ADMIN']);
            unset($roles['ROLE_ADMIN']);
        }

        return $roles;
    }

    private function createGeneralTab(FormBuilderInterface $builder)
    {
        return $this->createTab(
                $builder,
                'general',
                array('label' => 'user.form.tab.general.label')
            )
            ->add('username', 'text', array(
                'label' => 'user.form.tab.general.field.username',
            ))
            ->add('email', 'text', array(
                'label' => 'user.form.tab.general.field.email',
            ))
            ->add('enabled', 'checkbox', array(
                'value' => true,
                'label' => 'user.form.tab.general.field.enabled',
                'required' => false,
            ));
    }

    private function createPasswordTab(FormBuilderInterface $builder)
    {
        return $this->createTab($builder, 'password', array('label' => 'user.form.tab.password.label'))
            ->add('plainPassword', 'repeated', array(
                'type' => 'password',
                'required' => $this->isNew,
                'first_options' => array('label' => 'user.form.tab.password.field.password'),
                'second_options' => array('label' => 'user.form.tab.password.field.password_repeat'),
                'invalid_message' => 'The passwords don\'t match',
            ));
    }

    private function createRoleTab(FormBuilderInterface $builder)
    {
        return $this->createTab($builder, 'role', array('label' => 'user.form.tab.role.label'))
            ->add('roles', 'multi_select', array(
                'choices' => $this->getAvailableRoles($builder),
                'label' => 'user.form.tab.role.field.roles'
            ));
    }

    private function createActionTab(FormBuilderInterface $builder)
    {
        return $builder->create('actions', 'tabs_tab_actions', array(
                'mapped' => false,
{'fraction_non_alphanumeric': 0.15053456878118318, 'fraction_numerical': 0.0002851033499643621, 'mean_word_length': 2.094838994265549, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 106, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1329204', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1915, 'n_words': 369}