You are here

function captcha_questions_admin_settings_submit in Captcha Questions 7

Implements hook_submit().

1 string reference to 'captcha_questions_admin_settings_submit'
captcha_questions_admin_settings in ./captcha_questions.admin.inc
Implements hook_form().

File

./captcha_questions.admin.inc, line 157
Functionality and helper functions for Captcha questions administration.

Code

function captcha_questions_admin_settings_submit($form, &$form_state) {

  // If question, answer and description are all empty, delete the variables.
  // The last variable, captcha_questions_form_ids are removed on uninstall.
  if (empty($form_state['values']['captcha_questions_question']) && empty($form_state['values']['captcha_questions_answer'])) {
    variable_del('captcha_questions_question');
    variable_del('captcha_questions_answer');
    variable_del('captcha_questions_description');
  }
  if (empty($form_state['values']['captcha_questions_question']) && empty($form_state['values']['captcha_questions_answers'])) {
    variable_del('captcha_questions_question');
    variable_del('captcha_questions_answers');
    variable_del('captcha_questions_description');
  }

  // Split answers into arrays and set variable manually.
  $answers = explode("\n", $form_state['values']['captcha_questions_answers']);
  $answers = array_map('trim', $answers);
  asort($answers);
  variable_set('captcha_questions_answers', $answers);
  unset($form_state['values']['captcha_questions_answers']);
  $count_protected_form_ids = 0;

  // Counting number of selected form_ids, removing unselected form_ids.
  foreach ($form_state['values']['captcha_questions_form_ids'] as $form_id => $value) {
    if ($form_id === $value) {
      $count_protected_form_ids++;
    }
    else {
      unset($form_state['values']['captcha_questions_form_ids'][$form_id]);
    }
  }
  if ($count_protected_form_ids == 0) {
    drupal_set_message(t('No forms selected'));
  }
  else {
    $message = format_plural($count_protected_form_ids, '1 form protected', '@count forms protected');
    drupal_set_message($message);
  }
}