<commit_before><?php

namespace Frontend\Modules\FormBuilder\Widgets;

use Frontend\Core\Engine\Base\Widget as FrontendBaseWidget;
use Frontend\Core\Engine\Form as FrontendForm;
use Frontend\Core\Engine\Language as FL;
use Frontend\Core\Engine\Model as FrontendModel;
use Frontend\Core\Engine\Template as FrontendTemplate;
use Frontend\Modules\FormBuilder\Engine\Model as FrontendFormBuilderModel;
use Frontend\Modules\FormBuilder\FormBuilderEvents;
use Frontend\Modules\FormBuilder\Event\FormBuilderSubmittedEvent;

/**
 * This is the form widget.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Form extends FrontendBaseWidget
{
    /**
     * Fields in HTML form.
     *
     * @var    array
     */
    private $fieldsHTML;

    /**
     * The form.
     *
     * @var FrontendForm
     */
    private $frm;

    /**
     * The form item.
     *
     * @var    array
     */
    private $item;

    /**
     * Create form action and strip the identifier parameter.
     *
     * We use this function to create the action for the form.
     * This action cannot contain an identifier since these are used for
     * statistics and failed form submits cannot be tracked.
     *
     * @return string
     */
    private function createAction()
    {
        // pages
        $action = implode('/', $this->URL->getPages());

        // init parameters
        $parameters = $this->URL->getParameters();
        $moduleParameters = array();
        $getParameters = array();

        // sort by key (important for action order)
        ksort($parameters);

        // loop and filter parameters
        foreach ($parameters as $key => $value) {
            // skip identifier
            if ($key === 'identifier') {
                continue;
            }

            // normal parameter
            if (\SpoonFilter::isInteger($key)) {
                $moduleParameters[] = $value;
            } else {
                // get parameter
                $getParameters[$key] = $value;
            }
        }

        // single language
        if ($this->getContainer()->getParameter('site.multilanguage')) {
            $action = FRONTEND_LANGUAGE . '/' . $action;
        }

        // add to action
        if (count($moduleParameters) > 0) {
            $action .= '/' . implode('/', $moduleParameters);
        }
        if (count($getParameters) > 0) {
            $action .= '?' . http_build_query($getParameters);
        }

        // remove trailing slash
        $action = rtrim($action, '/');

        // cough up action
        return '/' . $action;
    }

    /**
     * Execute the extra.
     */
    public function execute()
    {
        parent::execute();

        $this->loadTemplate();
        $this->loadData();

        // success message
        if (
            $this->URL->hasParameter('identifier')
            && $this->URL->getParameter('identifier') == $this->item['identifier']
        ) {
            $this->parseSuccessMessage();
        } else {
            // create/handle form
            $this->loadForm();
            $this->validateForm();
            $this->parse();
        }

        return $this->tpl->getContent(
            FRONTEND_MODULES_PATH . '/' . $this->getModule() . '/Layout/Widgets/' . $this->getAction() . '.tpl'
        );
    }

    /**
     * Load the data.
     */
    private function loadData()
    {
        // fetch the item
        $this->item = FrontendFormBuilderModel::get((int) $this->data['id']);
    }

    /**
     * Load the form.
     */
    private function loadForm()
    {
        // create form
        $this->frm = new FrontendForm('form' . $this->item['id']);

        // exists and has fields
        if (!empty($this->item) && !empty($this->item['fields'])) {
            // loop fields
            foreach ($this->item['fields'] as $field) {
                // init
                $item['name'] = 'field' . $field['id'];
                $item['type'] = $field['type'];
                $item['label'] = (isset($field['settings']['label'])) ? $field['settings']['label'] : '';
                $item['placeholder'] = (isset($field['settings']['placeholder']) ? $field['settings']['placeholder'] : null);
	            $item['classname'] = (isset($field['settings']['classname']) ? $field['settings']['classname'] : null);
                $item['required'] = isset($field['validations']['required']);
                $item['html'] = '';

                // form values
                $values = (isset($field['settings']['values']) ? $field['settings']['values'] : null);
                $defaultValues = (isset($field['settings']['default_values']) ?
                    $field['settings']['default_values'] :
                    null
                );

                if ($field['type'] == 'dropdown') {
                    // values and labels are the same
                    $values = array_combine($values, $values);

                    // get index of selected item
                    $defaultIndex = array_search($defaultValues, $values, true);
                    if ($defaultIndex === false) {
                        $defaultIndex = null;<commit_msg>Fix tab to space<commit_after><?php

namespace Frontend\Modules\FormBuilder\Widgets;

use Frontend\Core\Engine\Base\Widget as FrontendBaseWidget;
use Frontend\Core\Engine\Form as FrontendForm;
use Frontend\Core\Engine\Language as FL;
use Frontend\Core\Engine\Model as FrontendModel;
use Frontend\Core\Engine\Template as FrontendTemplate;
use Frontend\Modules\FormBuilder\Engine\Model as FrontendFormBuilderModel;
use Frontend\Modules\FormBuilder\FormBuilderEvents;
use Frontend\Modules\FormBuilder\Event\FormBuilderSubmittedEvent;

/**
 * This is the form widget.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Form extends FrontendBaseWidget
{
    /**
     * Fields in HTML form.
     *
     * @var    array
     */
    private $fieldsHTML;

    /**
     * The form.
     *
     * @var FrontendForm
     */
    private $frm;

    /**
     * The form item.
     *
     * @var    array
     */
    private $item;

    /**
     * Create form action and strip the identifier parameter.
     *
     * We use this function to create the action for the form.
     * This action cannot contain an identifier since these are used for
     * statistics and failed form submits cannot be tracked.
     *
     * @return string
     */
    private function createAction()
    {
        // pages
        $action = implode('/', $this->URL->getPages());

        // init parameters
        $parameters = $this->URL->getParameters();
        $moduleParameters = array();
        $getParameters = array();

        // sort by key (important for action order)
        ksort($parameters);

        // loop and filter parameters
        foreach ($parameters as $key => $value) {
            // skip identifier
            if ($key === 'identifier') {
                continue;
            }

            // normal parameter
            if (\SpoonFilter::isInteger($key)) {
                $moduleParameters[] = $value;
            } else {
                // get parameter
                $getParameters[$key] = $value;
            }
        }

        // single language
        if ($this->getContainer()->getParameter('site.multilanguage')) {
            $action = FRONTEND_LANGUAGE . '/' . $action;
        }

        // add to action
        if (count($moduleParameters) > 0) {
            $action .= '/' . implode('/', $moduleParameters);
        }
        if (count($getParameters) > 0) {
            $action .= '?' . http_build_query($getParameters);
        }

        // remove trailing slash
        $action = rtrim($action, '/');

        // cough up action
        return '/' . $action;
    }

    /**
     * Execute the extra.
     */
    public function execute()
    {
        parent::execute();

        $this->loadTemplate();
        $this->loadData();

        // success message
        if (
            $this->URL->hasParameter('identifier')
            && $this->URL->getParameter('identifier') == $this->item['identifier']
        ) {
            $this->parseSuccessMessage();
        } else {
            // create/handle form
            $this->loadForm();
            $this->validateForm();
            $this->parse();
        }

        return $this->tpl->getContent(
            FRONTEND_MODULES_PATH . '/' . $this->getModule() . '/Layout/Widgets/' . $this->getAction() . '.tpl'
        );
    }

    /**
     * Load the data.
     */
    private function loadData()
    {
        // fetch the item
        $this->item = FrontendFormBuilderModel::get((int) $this->data['id']);
    }

    /**
     * Load the form.
     */
    private function loadForm()
    {
        // create form
        $this->frm = new FrontendForm('form' . $this->item['id']);

        // exists and has fields
        if (!empty($this->item) && !empty($this->item['fields'])) {
            // loop fields
            foreach ($this->item['fields'] as $field) {
                // init
                $item['name'] = 'field' . $field['id'];
                $item['type'] = $field['type'];
                $item['label'] = (isset($field['settings']['label'])) ? $field['settings']['label'] : '';
                $item['placeholder'] = (isset($field['settings']['placeholder']) ? $field['settings']['placeholder'] : null);
                $item['classname'] = (isset($field['settings']['classname']) ? $field['settings']['classname'] : null);
                $item['required'] = isset($field['validations']['required']);
                $item['html'] = '';

                // form values
                $values = (isset($field['settings']['values']) ? $field['settings']['values'] : null);
                $defaultValues = (isset($field['settings']['default_values']) ?
                    $field['settings']['default_values'] :
                    null
                );

                if ($field['type'] == 'dropdown') {
                    // values and labels are the same
                    $values = array_combine($values, $values);

                    // get index of selected item
                    $defaultIndex = array_search($defaultValues, $values, true);
                    if ($defaultIndex === false) {
                        $defaultIndex = null;
{'fraction_non_alphanumeric': 0.15329871129063283, 'fraction_numerical': 0.00038468936333910367, 'mean_word_length': 1.793177544990599, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 83, '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': '4115481', 'n_tokens_mistral': 2804, 'n_tokens_neox': 2716, 'n_words': 630}