<commit_before><?php
/**
 * c2cActions must be between action in module and the base class sfActions
 * it provides new shortcuts
 *
 * @author     <NAME> <<EMAIL>>
 * @version    SVN: $Id: $
 */

abstract class c2cActions extends sfActions
{
    protected function setMessage($name, $message, $vars = NULL, $persist = true)
    {
        c2cTools::log('{' . $name . '} ' . $message);
        $this->setFlash($name, $this->__($message, $vars), $persist);
    }

    protected function setNotice($message, $vars = NULL, $persist = true)
    {
        $this->setMessage('notice', $message, $vars, $persist);
    }

    protected function setWarning($message, $vars = NULL, $persist = true)
    {
        $this->setMessage('warning', $message, $vars, $persist);
    }

    protected function setError($message, $vars = NULL, $persist = true)
    {
        $this->setMessage('error', $message, $vars, $persist);
    }

    protected function setMessageAndRedirect($name, $message, $url, $vars = NULL, $js = NULL, $status_code = 302)
    {
        if ($this->isAjaxCall())
        {
            c2cTools::log('ajax messageAndRedirect | ' . $message);
            $error_remove = "";
            
            if ($name == 'error')
            {
                $this->getResponse()->setStatusCode(404);
            }
            else
            {
                // auto remove error classes on fields
                sfLoader::loadHelpers(array('Javascript', 'Tag'));
                $field_error = sfConfig::get('app_form_field_error');
                $error_remove = javascript_tag("$('.$field_error').removeClass('$field_error');$('.form_error').hide()");
            }

            return $this->renderText($error_remove . $js . $this->__($message, $vars));
        }
        else
        {
            $this->setMessage($name, $message, $vars);
            $this->redirect(empty($url) ? '@homepage' : $url, $status_code);
        }
    }

    protected function handleErrorForAjax()
    {
        $errors = $this->getRequest()->getErrors();
        $field_error = sfConfig::get('app_form_field_error');
        $js_errors = "'#" .implode(", #", array_keys($errors)) . "'";
        $arrow = sfConfig::get('sf_validation_error_prefix', '');

        $this->getResponse()->setStatusCode(404);

        sfLoader::loadHelpers(array('Javascript', 'Tag'));
        // add error class on fields via js
        $js  = "$('.$field_error').removeClass('$field_error');";
        $js .= "$('.form_error').hide();";// remove all errors
        $js .= "$($js_errors).addClass('$field_error');"; // display new ones (if any);

        // global form error
        $toReturn = $this->__('Oups!') . '<ul>';
        foreach($errors as $name => $error)
        {
            $js .= "$('#error_for_$name').html('" . $arrow . $this->__($error) . $arrow . "').show();";
            $toReturn .= '<li>' . $this->__($error) . '</li>';
        }
        $toReturn .= '</ul>';

        return $this->renderText(javascript_tag($js).$toReturn);
    }

    // generalist handle error (no need to set handle error on every form error)
    // special handleError can be set when initiating datas is needed
    public function handleError()
    {
        if($this->isAjaxCall())<commit_msg>correctly escape strings passed to javascript<commit_after><?php
/**
 * c2cActions must be between action in module and the base class sfActions
 * it provides new shortcuts
 *
 * @author     <NAME> <<EMAIL>>
 * @version    SVN: $Id: $
 */

abstract class c2cActions extends sfActions
{
    protected function setMessage($name, $message, $vars = NULL, $persist = true)
    {
        c2cTools::log('{' . $name . '} ' . $message);
        $this->setFlash($name, $this->__($message, $vars), $persist);
    }

    protected function setNotice($message, $vars = NULL, $persist = true)
    {
        $this->setMessage('notice', $message, $vars, $persist);
    }

    protected function setWarning($message, $vars = NULL, $persist = true)
    {
        $this->setMessage('warning', $message, $vars, $persist);
    }

    protected function setError($message, $vars = NULL, $persist = true)
    {
        $this->setMessage('error', $message, $vars, $persist);
    }

    protected function setMessageAndRedirect($name, $message, $url, $vars = NULL, $js = NULL, $status_code = 302)
    {
        if ($this->isAjaxCall())
        {
            c2cTools::log('ajax messageAndRedirect | ' . $message);
            $error_remove = "";
            
            if ($name == 'error')
            {
                $this->getResponse()->setStatusCode(404);
            }
            else
            {
                // auto remove error classes on fields
                sfLoader::loadHelpers(array('Javascript', 'Tag'));
                $field_error = sfConfig::get('app_form_field_error');
                $error_remove = javascript_tag("$('.$field_error').removeClass('$field_error');$('.form_error').hide()");
            }

            return $this->renderText($error_remove . $js . $this->__($message, $vars));
        }
        else
        {
            $this->setMessage($name, $message, $vars);
            $this->redirect(empty($url) ? '@homepage' : $url, $status_code);
        }
    }

    protected function handleErrorForAjax()
    {
        $errors = $this->getRequest()->getErrors();
        $field_error = sfConfig::get('app_form_field_error');
        $js_errors = "'#" .implode(", #", array_keys($errors)) . "'";
        $arrow = sfConfig::get('sf_validation_error_prefix', '');

        $this->getResponse()->setStatusCode(404);

        sfLoader::loadHelpers(array('Javascript', 'Tag'));
        // add error class on fields via js
        $js  = "$('.$field_error').removeClass('$field_error');";
        $js .= "$('.form_error').hide();";// remove all errors
        $js .= "$($js_errors).addClass('$field_error');"; // display new ones (if any);

        // global form error
        $toReturn = $this->__('Oups!') . '<ul>';
        foreach($errors as $name => $error)
        {
            $js .= "$('#error_for_$name').html(" . json_encode($arrow . $this->__($error) . $arrow) . ").show();";
            $toReturn .= '<li>' . $this->__($error) . '</li>';
        }
        $toReturn .= '</ul>';

        return $this->renderText(javascript_tag($js).$toReturn);
    }

    // generalist handle error (no need to set handle error on every form error)
    // special handleError can be set when initiating datas is needed
    public function handleError()
    {
        if($this->isAjaxCall())
{'fraction_non_alphanumeric': 0.17552948346792624, 'fraction_numerical': 0.003961602925491391, 'mean_word_length': 2.3904958677685952, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 61, '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': '5380363', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1923, 'n_words': 424}