<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
 */

$fields   = $form->getFields();
$formName = \Mautic\CoreBundle\Helper\InputHelper::alphanum($form->getName());
?>

<script type="text/javascript">
var MauticForm_<?php echo $formName; ?> = {
    formId: "mauticform_<?php echo $formName; ?>",
    validateForm: function () {
        var formValid = true;

        function validateOptions(elOptions) {
            var optionsValid = false;

            if (elOptions.length == undefined) {
                elOptions = [ elOptions ];
            }

            for (var i=0; i < elOptions.length; i++) {
                if (elOptions[i].checked) {
                    optionsValid = true;
                    break;
                }
            }

            return optionsValid;
        }

        function validateEmail(email) {
            var atpos = email.indexOf("@");
            var dotpos = email.lastIndexOf(".");
            var valid = (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= email.length) ? false : true;
            return valid;
        }

        function markError(containerId, valid) {
            var elContainer = document.getElementById(containerId);
            var elErrorSpan = elContainer.querySelector('.mauticform-errormsg');
            elErrorSpan.style.display = (valid) ? 'none' : '';
        }

        var elForm = document.getElementById(this.formId);
        <?php foreach ($fields as $f):
        if ($f->isRequired()):
        $name = "mauticform[".$f->getAlias()."]";
        $id   = 'mauticform_' . $f->getAlias();
        $type = $f->getType();
        switch ($type):
        case 'select':
        case 'country':
            $properties = $f->getProperties();
            $multiple   = $properties['multiple'];
            if ($multiple)
                $name .= '[]';
        ?>

        var valid = (elForm.elements["<?php echo $name; ?>"].value != '');
        <?php
        break;
        case 'radiogrp':
        case 'checkboxgrp':
            if ($type == 'checkboxgrp') $name .= '[]';
        ?>

        var elOptions = elForm.elements["<?php echo $name; ?>"];
        var valid = validateOptions(elOptions);
        <?php
        break;
        case 'email':
        ?>

        var valid = validateEmail(elForm.elements["<?php echo $name; ?>"].value);
        <?php
        break;
        default:
        ?>

        var valid = (elForm.elements["<?php echo $name; ?>"].value != '');
        <?php
        break;
        endswitch;
        ?>
        markError('<?php echo $id; ?>', valid);
        if (!valid) formValid = false;
        <?php
        endif;
        endforeach;
        ?>

        if (formValid) {
            document.getElementById('mauticform_<?php echo $formName ?>_return').value = document.URL;
        }
        return formValid;
    },
    checkMessages: function() {
        var query = {};
        location.search.substr(1).split("&").forEach(function(item) {query[item.split("=")[0]] = item.split("=")[1]});
        if (typeof query.mauticError !== 'undefined') {
            var errorContainer = document.getElementById('mauticform_<?php echo $formName; ?>_error');
            errorContainer.innerHTML = decodeURIComponent(query.mauticError);
        } else if (typeof query.mauticMessage !== 'undefined') {
            var messageContainer = document.getElementById('mauticform_<?php echo $formName; ?>_message');
            messageContainer.innerHTML = decodeURIComponent(query.mauticMessage);
        }
    }
}
MauticForm_<?php echo $formName; ?>.checkMessages();
</script><commit_msg>Store form data in localStorage and retrieve if Mautic returns to the form with errors<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
 */

$fields   = $form->getFields();
$formName = \Mautic\CoreBundle\Helper\InputHelper::alphanum($form->getName());
?>

<script type="text/javascript">
var MauticForm_<?php echo $formName; ?> = {
    formId: "mauticform_<?php echo $formName; ?>",
    validateForm: function () {
        var formValid = true;

        function validateOptions(elOptions) {
            var optionsValid = false;

            if (elOptions.length == undefined) {
                elOptions = [ elOptions ];
            }

            for (var i=0; i < elOptions.length; i++) {
                if (elOptions[i].checked) {
                    optionsValid = true;
                    break;
                }
            }

            return optionsValid;
        }

        function validateEmail(email) {
            var atpos = email.indexOf("@");
            var dotpos = email.lastIndexOf(".");
            var valid = (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= email.length) ? false : true;
            return valid;
        }

        function markError(containerId, valid) {
            var elContainer = document.getElementById(containerId);
            var elErrorSpan = elContainer.querySelector('.mauticform-errormsg');
            elErrorSpan.style.display = (valid) ? 'none' : '';
        }

        var elForm = document.getElementById(this.formId);
        <?php
        foreach ($fields as $f):
            if ($f->isRequired()):
                echo "\n";

                $name = "mauticform[".$f->getAlias()."]";
                $id   = 'mauticform_' . $f->getAlias();
                $type = $f->getType();

                switch ($type):
                    case 'select':
                    case 'country':
                        $properties = $f->getProperties();
                        $multiple   = $properties['multiple'];
                        if ($multiple)
                            $name .= '[]';

                        echo "        var valid = (elForm.elements[\"$name\"].value != '');\n";

                        break;

                    case 'radiogrp':
                    case 'checkboxgrp':
                        if ($type == 'checkboxgrp') $name .= '[]';

                        echo "        var elOptions = elForm.elements[\"$name\"];\n";
                        echo "        var valid = validateOptions(elOptions);\n";

                        break;

                    case 'email':
                        echo "        var valid = validateEmail(elForm.elements[\"$name\"].value);\n";

                        break;

                    default:
                        echo "        var valid = (elForm.elements[\"$name\"].value != '');\n";

                        break;
                endswitch;

                echo "        markError('$id', valid);\n";
                echo "        if (!valid) formValid = false;\n";

            endif;
        endforeach;
        ?>

        if (formValid) {
            document.getElementById('mauticform_<?php echo $formName ?>_return').value = document.URL;
            if (typeof localStorage !== 'undefined') {
                try {
                    var vars = {};
                    for (var i = 0; i < elForm.elements.length; i++) {
                        var e = elForm.elements[i];

                        if (e.type == 'hidden' || e.type == 'button') {
                            continue;
                        }

                        vars[e.id] = {
                            'id': e.id,
                            'name': e.name,
                            'type': e.type,
                            'value': e.value,
                            'checked': (e.type == 'checkbox' || e.type == 'radio') ? e.checked : false
                        }
                    }

                    localStorage.setItem('<?php echo $formName; ?>', JSON.stringify(vars));
                } catch (err) {}
            }
        }

        return formValid;
    },
    checkMessages: function() {
        var query  = {};

        location.search.substr(1).split("&").forEach(function(item) {query[item.split("=")[0]] = item.split("=")[1]});
        if (typeof query.mauticError !== 'undefined') {
            var errorContainer = document.getElementById('mauticform_<?php echo $formName; ?>_error');
            errorContainer.innerHTML = decodeURIComponent(query.mauticError);

            if (typeof localStorage !== 'undefined') {
                try {
                    var storedData = localStorage.getItem('<?php echo $formName; ?>');
                    if (storedData) {
                        var vars = JSON.parse(storedData);
                        console.log(vars);
                        var key;
                        var el;
                        for (key in vars) {
                            el = vars[key];
                            if (el.type == 'checkbox' || el.type == 'radio') {
                                document.getElementById(el.id).checked = el.checked;
                            } else {
                                document.getElementById(el.id).value = el.value;
                            }
                        }
                    }
                } catch (err) {}
            }
        } else {
            if (typeof query.mauticMessage !== 'undefined') {
                var messageContainer = document.getElementById('mauticform_<?php echo $formName; ?>_message');
                messageContainer.innerHTML = decodeURIComponent(query.mauticMessage);
            }

            if (typeof localStorage !== 'undefined') {
                localStorage.removeItem('<?php echo $formName; ?>');
            }
        }
    }
}
MauticForm_<?php echo $formName; ?>.checkMessages();
</script>
{'fraction_non_alphanumeric': 0.1391339620728121, 'fraction_numerical': 0.0029408782070783897, 'mean_word_length': 1.4611929124032943, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 52, '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': '3516348', 'n_tokens_mistral': 2667, 'n_tokens_neox': 2572, 'n_words': 548}