You are here

public function BotchaRecipeObscureUrl::getJsValue in BOTCHA Spam Prevention 7.3

Same name and namespace in other branches
  1. 6.2 controller/botcha_recipe.controller.inc \BotchaRecipeObscureUrl::getJsValue()
  2. 6.3 controller/recipe/botcha.recipe.controller.inc \BotchaRecipeObscureUrl::getJsValue()
  3. 7.2 controller/botcha_recipe.controller.inc \BotchaRecipeObscureUrl::getJsValue()

File

controller/recipe/botcha.recipe.controller.inc, line 853
Controller layer of the BotchaRecipe objects.

Class

BotchaRecipeObscureUrl

Code

public function getJsValue() {

  // Just ensure that parameters are set.
  $fields = $this
    ->getProperty($this->settings['fields'], 'getFields');
  $js = $this
    ->getProperty($this->settings['js'], 'getJs');
  $chops_positions = array_keys($js['chops']);
  $field2_name = $fields[2]['name'];

  // @todo Replace this workaround, find a better solution.
  // @see?
  // @todo Abstract it.
  $form_id = str_replace('_', '-', $this
    ->getSetting('form_id'));
  $submit = _botcha_url($this
    ->getSetting('form_action'), array(
    'query' => array(
      $field2_name => '__replace__',
    ),
  ));

  // Secure_token.
  $submit = preg_replace('/__replace__/', $js['chops'][$chops_positions[0]] . '\'+v+\'' . $js['chops'][$chops_positions[1]], $submit);
  if (strpos($form_id, '-node-form') !== false) {
    $js_form_id = 'node-form';
  }
  elseif (substr($form_id, 0, 7) == 'comment') {
    $js_form_id = 'comment-form';
  }
  else {
    $js_form_id = $form_id;
  }

  // @todo Abstract it.
  return <<<END
(function (\$) {
  Drupal.behaviors.{<span class="php-variable">$js</span>[<span class="php-string">'name'</span>]} = {
    attach: function (context, settings) {
      \$("input.{<span class="php-variable">$fields</span>[<span class="php-constant">1</span>][<span class="php-string">'class'</span>]}").each(function() {
        f=\$(this)[0];
        if (f.value.indexOf("{<span class="php-variable">$js</span>[<span class="php-string">'match'</span>]}")==0){
          v=f.value.substring({<span class="php-variable">$js</span>[<span class="php-string">'pos'</span>]});
          \$("#{<span class="php-variable">$js_form_id</span>}").get(0).setAttribute('action', '{<span class="php-variable">$submit</span>}');
        }
      });
    }
  };
}(jQuery));
END;
}