<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\ConfigBundle\Controller;

use Mautic\CoreBundle\Controller\FormController;
use Mautic\ConfigBundle\Event\ConfigEvent;
use Mautic\ConfigBundle\Event\ConfigBuilderEvent;
use Mautic\ConfigBundle\ConfigEvents;
use Mautic\CoreBundle\Helper\EncryptionHelper;
use Symfony\Component\Form\FormError;

/**
 * Class ConfigController
 */
class ConfigController extends FormController
{

    /**
     * Controller action for editing the application configuration
     *
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function editAction ()
    {
        //admin only allowed
        if (!$this->factory->getUser()->isAdmin()) {
            return $this->accessDenied();
        }

        $event      = new ConfigBuilderEvent($this->factory);
        $dispatcher = $this->get('event_dispatcher');
        $dispatcher->dispatch(ConfigEvents::CONFIG_ON_GENERATE, $event);
        $formConfigs            = $event->getForms();
        $formThemes             = $event->getFormThemes();
        $doNotChange            = $this->factory->getParameter('security.restrictedConfigFields');
        $doNotChangeDisplayMode = $this->factory->getParameter('security.restrictedConfigFields.displayMode', 'remove');

        $this->mergeParamsWithLocal($formConfigs, $doNotChange);

        /* @type \Mautic\ConfigBundle\Model\ConfigModel $model */
        $model = $this->factory->getModel('config');

        // Create the form
        $action = $this->generateUrl('mautic_config_action', array('objectAction' => 'edit'));
        $form   = $model->createForm($formConfigs, $this->get('form.factory'), array(
            'action'                 => $action,
            'doNotChange'            => $doNotChange,
            'doNotChangeDisplayMode' => $doNotChangeDisplayMode
        ));

        /** @var \Mautic\InstallBundle\Configurator\Configurator $configurator */
        $configurator = $this->get('mautic.configurator');
        $isWritabale  = $configurator->isFileWritable();

        // Check for a submitted form and process it
        if ($this->request->getMethod() == 'POST') {
            if (!$cancelled = $this->isFormCancelled($form)) {
                $isValid = false;
                if ($isWritabale && $isValid = $this->isFormValid($form)) {

                    // Bind request to the form
                    $post     = $this->request->request;
                    $formData = $form->getData();

                    // Dispatch pre-save event. Bundles may need to modify some field values like passwords before save
                    $configEvent = new ConfigEvent($formData, $post);
                    $dispatcher->dispatch(ConfigEvents::CONFIG_PRE_SAVE, $configEvent);
                    $formValues = $configEvent->getConfig();

                    // Prevent these from getting overwritten with empty values
                    $unsetIfEmpty = $configEvent->getPreservedFields();

                    // Merge each bundle's updated configuration into the local configuration
                    foreach ($formValues as $object) {
                        $checkThese = array_intersect(array_keys($object), $unsetIfEmpty);
                        foreach ($checkThese as $checkMe) {
                            if (empty($object[$checkMe])) {
                                unset($object[$checkMe]);
                            }
                        }

                        $configurator->mergeParameters($object);
                    }

                    try {
                        // Ensure the config has a secret key
                        $params = $configurator->getParameters();
                        if (empty($params['secret_key'])) {
                            $configurator->mergeParameters(array('secret_key' => EncryptionHelper::generateKey()));
                        }

                        $configurator->write();

                        $this->addFlash('mautic.config.config.notice.updated');

                        // We must clear the application cache for the updated values to take effect
                        /** @var \Mautic\CoreBundle\Helper\CacheHelper $cacheHelper */
                        $cacheHelper = $this->factory->getHelper('cache');
                        $cacheHelper->clearCache(false, true);
                    } catch (RuntimeException $exception) {
                        $this->addFlash('mautic.config.config.error.not.updated', array('%exception%' => $exception->getMessage()), 'error');
                    }
                } elseif (!$isWritabale) {
                    $form->addError(new FormError(
                        $this->factory->getTranslator()->trans('mautic.config.notwritable')
                    ));
                }
            }

            // If the form is saved or cancelled, redirect back to the dashboard
            if ($cancelled || $isValid) {
                if (!$cancelled && $this->isFormApplied($form)) {
                    return $this->delegateRedirect($this->generateUrl('mautic_config_action', array('objectAction' => 'edit')));
                } else {
                    return $this->delegateRedirect($this->generateUrl('mautic_dashboard_index'));
                }
            }
        }

        $tmpl = $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index';

        return $this->delegateView(array(
            'viewParameters'  => array(
                'tmpl'        => $tmpl,
                'security'    => $this->factory->getSecurity(),<commit_msg>Fixed namespace issue with a caught exception<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\ConfigBundle\Controller;

use Mautic\CoreBundle\Controller\FormController;
use Mautic\ConfigBundle\Event\ConfigEvent;
use Mautic\ConfigBundle\Event\ConfigBuilderEvent;
use Mautic\ConfigBundle\ConfigEvents;
use Mautic\CoreBundle\Helper\EncryptionHelper;
use Symfony\Component\Form\FormError;

/**
 * Class ConfigController
 */
class ConfigController extends FormController
{

    /**
     * Controller action for editing the application configuration
     *
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function editAction ()
    {
        //admin only allowed
        if (!$this->factory->getUser()->isAdmin()) {
            return $this->accessDenied();
        }

        $event      = new ConfigBuilderEvent($this->factory);
        $dispatcher = $this->get('event_dispatcher');
        $dispatcher->dispatch(ConfigEvents::CONFIG_ON_GENERATE, $event);
        $formConfigs            = $event->getForms();
        $formThemes             = $event->getFormThemes();
        $doNotChange            = $this->factory->getParameter('security.restrictedConfigFields');
        $doNotChangeDisplayMode = $this->factory->getParameter('security.restrictedConfigFields.displayMode', 'remove');

        $this->mergeParamsWithLocal($formConfigs, $doNotChange);

        /* @type \Mautic\ConfigBundle\Model\ConfigModel $model */
        $model = $this->factory->getModel('config');

        // Create the form
        $action = $this->generateUrl('mautic_config_action', array('objectAction' => 'edit'));
        $form   = $model->createForm($formConfigs, $this->get('form.factory'), array(
            'action'                 => $action,
            'doNotChange'            => $doNotChange,
            'doNotChangeDisplayMode' => $doNotChangeDisplayMode
        ));

        /** @var \Mautic\InstallBundle\Configurator\Configurator $configurator */
        $configurator = $this->get('mautic.configurator');
        $isWritabale  = $configurator->isFileWritable();

        // Check for a submitted form and process it
        if ($this->request->getMethod() == 'POST') {
            if (!$cancelled = $this->isFormCancelled($form)) {
                $isValid = false;
                if ($isWritabale && $isValid = $this->isFormValid($form)) {

                    // Bind request to the form
                    $post     = $this->request->request;
                    $formData = $form->getData();

                    // Dispatch pre-save event. Bundles may need to modify some field values like passwords before save
                    $configEvent = new ConfigEvent($formData, $post);
                    $dispatcher->dispatch(ConfigEvents::CONFIG_PRE_SAVE, $configEvent);
                    $formValues = $configEvent->getConfig();

                    // Prevent these from getting overwritten with empty values
                    $unsetIfEmpty = $configEvent->getPreservedFields();

                    // Merge each bundle's updated configuration into the local configuration
                    foreach ($formValues as $object) {
                        $checkThese = array_intersect(array_keys($object), $unsetIfEmpty);
                        foreach ($checkThese as $checkMe) {
                            if (empty($object[$checkMe])) {
                                unset($object[$checkMe]);
                            }
                        }

                        $configurator->mergeParameters($object);
                    }

                    try {
                        // Ensure the config has a secret key
                        $params = $configurator->getParameters();
                        if (empty($params['secret_key'])) {
                            $configurator->mergeParameters(array('secret_key' => EncryptionHelper::generateKey()));
                        }

                        $configurator->write();

                        $this->addFlash('mautic.config.config.notice.updated');

                        // We must clear the application cache for the updated values to take effect
                        /** @var \Mautic\CoreBundle\Helper\CacheHelper $cacheHelper */
                        $cacheHelper = $this->factory->getHelper('cache');
                        $cacheHelper->clearCache(false, true);
                    } catch (\RuntimeException $exception) {
                        $this->addFlash('mautic.config.config.error.not.updated', array('%exception%' => $exception->getMessage()), 'error');
                    }
                } elseif (!$isWritabale) {
                    $form->addError(new FormError(
                        $this->factory->getTranslator()->trans('mautic.config.notwritable')
                    ));
                }
            }

            // If the form is saved or cancelled, redirect back to the dashboard
            if ($cancelled || $isValid) {
                if (!$cancelled && $this->isFormApplied($form)) {
                    return $this->delegateRedirect($this->generateUrl('mautic_config_action', array('objectAction' => 'edit')));
                } else {
                    return $this->delegateRedirect($this->generateUrl('mautic_dashboard_index'));
                }
            }
        }

        $tmpl = $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index';

        return $this->delegateView(array(
            'viewParameters'  => array(
                'tmpl'        => $tmpl,
                'security'    => $this->factory->getSecurity(),
{'fraction_non_alphanumeric': 0.12496805520061334, 'fraction_numerical': 0.0011926058437686344, 'mean_word_length': 1.859230394544569, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 137, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '673995', 'n_tokens_mistral': 3015, 'n_tokens_neox': 2886, 'n_words': 555}