You are here

public function Botcha::formValidate in BOTCHA Spam Prevention 7.3

Same name and namespace in other branches
  1. 6.3 controller/application/botcha.application.controller.inc \Botcha::formValidate()

Overrides IBotcha::formValidate

File

controller/application/botcha.application.controller.inc, line 360
Contains Botcha class.

Class

Botcha
Just a middleman for achieving purposes such as:

Code

public function formValidate($form, &$form_state) {

  // FIXME: where does this empty value come from ? happens with comments
  unset($form_state['values']['']);

  // Get an instance of BOTCHA form controller.
  $form_controller = $this
    ->getController(Botcha::CONTROLLER_TYPE_FORM);
  $botcha_form = $form_controller
    ->getForm($form['form_id']['#value'], FALSE);

  // Check if it is allowed to protect.
  if ($botcha_form
    ->isEnabled()) {

    // Fetch a recipe book and handle spam protection.
    $recipebook_controller = $this
      ->getController(Botcha::CONTROLLER_TYPE_RECIPEBOOK);
    $rbid = $botcha_form
      ->getRecipebook();
    $recipebook = $recipebook_controller
      ->getRecipebook($rbid, FALSE);
    if ($recipebook
      ->isApplicable($form, $form_state)) {

      // Array to store results of spam checks.
      $is_spam = array();
      $recipe_controller = $this
        ->getController(Botcha::CONTROLLER_TYPE_RECIPE);
      foreach ($recipebook
        ->getRecipes() as $recipe_id) {
        $recipe = $recipe_controller
          ->getRecipe($recipe_id, FALSE);
        $is_spam[$recipe_id] = $recipe
          ->isSpam($form, $form_state);
        $recipe
          ->handle($is_spam[$recipe_id] ? 'spam' : 'success', $form, $form_state);
      }

      // To block or not to block we decide using recipe book logic.
      $recipebook
        ->handle($recipebook
        ->isSpam($form, $form_state, $is_spam) ? 'spam' : 'success', $form, $form_state, $is_spam);
    }
  }
}