<commit_before><?php
/**
 * @package     Mautic
 * @copyright   2014 Mautic Contributors. All rights reserved.
 * @author      Mautic
 * @link        http://mautic.org
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */

namespace Mautic\ApiBundle\Form\Type;

use Mautic\ApiBundle\Form\Validator\Constraints\OAuthCallback;
use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\CoreBundle\Form\EventListener\CleanFormSubscriber;
use Mautic\CoreBundle\Form\EventListener\FormExitSubscriber;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormError;
use Mautic\CoreBundle\Form\DataTransformer as Transformers;

/**
 * Class ClientType
 */
class ClientType extends AbstractType
{

    /**
     * @var \Symfony\Bundle\FrameworkBundle\Translation\Translator
     */
    private $translator;

    /**
     * @var \Symfony\Component\Validator\Validator
     */
    private $validator;

    /**
     * @var bool|mixed
     */
    private $apiMode;

    /**
     * @param MauticFactory $factory
     */
    public function __construct(MauticFactory $factory)
    {
        $this->translator = $factory->getTranslator();
        $this->validator  = $factory->getValidator();
        $this->apiMode    = $factory->getParameter('api_mode');
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addEventSubscriber(new CleanFormSubscriber());
        $builder->addEventSubscriber(new FormExitSubscriber('api.client', $options));

        $builder->add('name', 'text', array(
            'label'      => 'mautic.core.name',
            'label_attr' => array('class' => 'control-label'),
            'attr'       => array('class' => 'form-control')
        ));


        if ($this->apiMode == 'oauth2') {
            $arrayStringTransformer = new Transformers\ArrayStringTransformer();
            $builder->add(
                $builder->create('redirectUris', 'text', array(
                    'label'      => 'mautic.api.client.redirecturis',
                    'label_attr' => array('class' => 'control-label'),
                    'attr'       => array(
                        'class'   => 'form-control',
                        'tooltip' => 'mautic.api.client.form.help.requesturis',
                    )
                ))
                    ->addViewTransformer($arrayStringTransformer)
            );

            $builder->add('publicId', 'text', array(
                'label'      => 'mautic.api.client.form.clientid',
                'label_attr' => array('class' => 'control-label'),
                'attr'       => array('class' => 'form-control'),
                'disabled'   => true,
                'required'   => false,
                'mapped'     => false,
                'data'       => $options['data']->getPublicId()
            ));

            $builder->add('secret', 'text', array(
                'label'      => 'mautic.api.client.form.clientsecret',
                'label_attr' => array('class' => 'control-label'),
                'attr'       => array('class' => 'form-control'),
                'disabled'   => true,
                'required'   => false
            ));

            $translator = $this->translator;
            $validator  = $this->validator;

            $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) use ($translator, $validator) {
                $form = $event->getForm();
                $data = $event->getData();

                if ($form->has('redirectUris')) {
                    foreach ($data->getRedirectUris() as $uri) {
                        $urlConstraint = new OAuthCallback();
                        $urlConstraint->message = $translator->trans('mautic.api.client.redirecturl.invalid', array('%url%' => $uri), 'validators');

                        $errors = $validator->validateValue(
                            $uri,
                            $urlConstraint
                        );

                        if (!empty($errors)) {
                            foreach ($errors as $error) {
                                $form['redirectUris']->addError(new FormError($error->getMessage()));
                            }
                        }
                    }
                }
            });
        } else {
            $builder->add(
                $builder->create('callback', 'text', array(
                'label'      => 'mautic.api.client.form.callback',
                'label_attr' => array('class' => 'control-label'),
                'attr'       => array(
                    'class'   => 'form-control',
                    'tooltip' => 'mautic.api.client.form.help.callback',
                ),
                'required'   => false
            ))->addModelTransformer(new Transformers\NullToEmptyTransformer()));

            $builder->add('consumerKey', 'text', array(
                'label'      => 'mautic.api.client.form.consumerkey',
                'label_attr' => array('class' => 'control-label'),
                'attr'       => array('class' => 'form-control'),
                'disabled'   => true,
                'required'   => false,
                'mapped'     => false,
                'data'       => $options['data']->getConsumerKey()
            ));

            $builder->add('consumerSecret', 'text', array(
                'label'      => 'mautic.api.client.form.consumersecret',
                'label_attr' => array('class' => 'control-label'),
                'attr'       => array('class' => 'form-control'),
                'disabled'   => true,
                'required'   => false
            ));

            $translator = $this->translator;
            $validator  = $this->validator;

            $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) use ($translator, $validator) {
                $form = $event->getForm();
                $data = $event->getData();

                if ($form->has('callback')) {
                    $uri           = $data->getCallback();
                    $urlConstraint = new OAuthCallback();
                    $urlConstraint->message = $translator->trans('mautic.api.client.redirecturl.invalid', array('%url%' => $uri), 'validators');

                    $errors = $validator->validateValue(
                        $uri,
                        $urlConstraint
                    );

                    if (!empty($errors)) {
                        foreach ($errors as $error) {
                            $form['callback']->addError(new FormError($error->getMessage()));
                        }
                    }
                }
            });
        }

        $builder->add('buttons', 'form_buttons');

        if (!empty($options["action"])) {
            $builder->setAction($options["action"]);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $dataClass = ($this->apiMode == 'oauth2') ? 'Mautic\ApiBundle\Entity\oAuth2\Client' : 'Mautic\ApiBundle\Entity\oAuth1\Consumer';
        $resolver->setDefaults(array(
            'data_class' => $dataClass
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return "client";
    }
}<commit_msg>Fixed not being able to copy API client/consumer keys in Firefox<commit_after><?php
/**
 * @package     Mautic
 * @copyright   2014 Mautic Contributors. All rights reserved.
 * @author      Mautic
 * @link        http://mautic.org
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */

namespace Mautic\ApiBundle\Form\Type;

use Mautic\ApiBundle\Form\Validator\Constraints\OAuthCallback;
use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\CoreBundle\Form\EventListener\CleanFormSubscriber;
use Mautic\CoreBundle\Form\EventListener\FormExitSubscriber;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormError;
use Mautic\CoreBundle\Form\DataTransformer as Transformers;

/**
 * Class ClientType
 */
class ClientType extends AbstractType
{

    /**
     * @var \Symfony\Bundle\FrameworkBundle\Translation\Translator
     */
    private $translator;

    /**
     * @var \Symfony\Component\Validator\Validator
     */
    private $validator;

    /**
     * @var bool|mixed
     */
    private $apiMode;

    /**
     * @param MauticFactory $factory
     */
    public function __construct(MauticFactory $factory)
    {
        $this->translator = $factory->getTranslator();
        $this->validator  = $factory->getValidator();
        $this->apiMode    = $factory->getParameter('api_mode');
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addEventSubscriber(new CleanFormSubscriber());
        $builder->addEventSubscriber(new FormExitSubscriber('api.client', $options));

        $builder->add(
            'name',
            'text',
            array(
                'label'      => 'mautic.core.name',
                'label_attr' => array('class' => 'control-label'),
                'attr'       => array('class' => 'form-control')
            )
        );


        if ($this->apiMode == 'oauth2') {
            $arrayStringTransformer = new Transformers\ArrayStringTransformer();
            $builder->add(
                $builder->create(
                    'redirectUris',
                    'text',
                    array(
                        'label'      => 'mautic.api.client.redirecturis',
                        'label_attr' => array('class' => 'control-label'),
                        'attr'       => array(
                            'class'   => 'form-control',
                            'tooltip' => 'mautic.api.client.form.help.requesturis',
                        )
                    )
                )
                    ->addViewTransformer($arrayStringTransformer)
            );

            $builder->add(
                'publicId',
                'text',
                array(
                    'label'      => 'mautic.api.client.form.clientid',
                    'label_attr' => array('class' => 'control-label'),
                    'attr'       => array('class' => 'form-control'),
                    'disabled'   => true,
                    'required'   => false,
                    'mapped'     => false,
                    'data'       => $options['data']->getPublicId()
                )
            );

            $builder->add(
                'secret',
                'text',
                array(
                    'label'      => 'mautic.api.client.form.clientsecret',
                    'label_attr' => array('class' => 'control-label'),
                    'attr'       => array('class' => 'form-control'),
                    'disabled'   => true,
                    'required'   => false
                )
            );

            $translator = $this->translator;
            $validator  = $this->validator;

            $builder->addEventListener(
                FormEvents::POST_SUBMIT,
                function (FormEvent $event) use ($translator, $validator) {
                    $form = $event->getForm();
                    $data = $event->getData();

                    if ($form->has('redirectUris')) {
                        foreach ($data->getRedirectUris() as $uri) {
                            $urlConstraint          = new OAuthCallback();
                            $urlConstraint->message = $translator->trans(
                                'mautic.api.client.redirecturl.invalid',
                                array('%url%' => $uri),
                                'validators'
                            );

                            $errors = $validator->validateValue(
                                $uri,
                                $urlConstraint
                            );

                            if (!empty($errors)) {
                                foreach ($errors as $error) {
                                    $form['redirectUris']->addError(new FormError($error->getMessage()));
                                }
                            }
                        }
                    }
                }
            );
        } else {
            $builder->add(
                $builder->create(
                    'callback',
                    'text',
                    array(
                        'label'      => 'mautic.api.client.form.callback',
                        'label_attr' => array('class' => 'control-label'),
                        'attr'       => array(
                            'class'   => 'form-control',
                            'tooltip' => 'mautic.api.client.form.help.callback',
                        ),
                        'required'   => false
                    )
                )->addModelTransformer(new Transformers\NullToEmptyTransformer())
            );

            $builder->add(
                'consumerKey',
                'text',
                array(
                    'label'      => 'mautic.api.client.form.consumerkey',
                    'label_attr' => array('class' => 'control-label'),
                    'attr'       => array(
                        'class'   => 'form-control',
                        'onclick' => 'this.setSelectionRange(0, this.value.length);'
                    ),
                    'read_only'  => true,
                    'required'   => false,
                    'mapped'     => false,
                    'data'       => $options['data']->getConsumerKey()
                )
            );

            $builder->add(
                'consumerSecret',
                'text',
                array(
                    'label'      => 'mautic.api.client.form.consumersecret',
                    'label_attr' => array('class' => 'control-label'),
                    'attr'       => array(
                        'class'   => 'form-control',
                        'onclick' => 'this.setSelectionRange(0, this.value.length);'
                    ),
                    'read_only'  => true,
                    'required'   => false
                )
            );

            $translator = $this->translator;
            $validator  = $this->validator;

            $builder->addEventListener(
                FormEvents::POST_SUBMIT,
                function (FormEvent $event) use ($translator, $validator) {
                    $form = $event->getForm();
                    $data = $event->getData();

                    if ($form->has('callback')) {
                        $uri                    = $data->getCallback();
                        $urlConstraint          = new OAuthCallback();
                        $urlConstraint->message = $translator->trans('mautic.api.client.redirecturl.invalid', array('%url%' => $uri), 'validators');

                        $errors = $validator->validateValue(
                            $uri,
                            $urlConstraint
                        );

                        if (!empty($errors)) {
                            foreach ($errors as $error) {
                                $form['callback']->addError(new FormError($error->getMessage()));
                            }
                        }
                    }
                }
            );
        }

        $builder->add('buttons', 'form_buttons');

        if (!empty($options["action"])) {
            $builder->setAction($options["action"]);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $dataClass = ($this->apiMode == 'oauth2') ? 'Mautic\ApiBundle\Entity\oAuth2\Client' : 'Mautic\ApiBundle\Entity\oAuth1\Consumer';
        $resolver->setDefaults(
            array(
                'data_class' => $dataClass
            )
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return "client";
    }
}
{'fraction_non_alphanumeric': 0.13625172641566083, 'fraction_numerical': 0.0014411817690506215, 'mean_word_length': 1.363610559182515, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 211, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3824533', 'n_tokens_mistral': 4399, 'n_tokens_neox': 4180, 'n_words': 657}