You are here

function captcha_questions_admin_settings in Captcha Questions 7

Implements hook_form().

1 string reference to 'captcha_questions_admin_settings'
captcha_questions_menu in ./captcha_questions.module
Implements hook_menu().

File

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

Code

function captcha_questions_admin_settings($form, &$form_state) {
  $form['description'] = array(
    '#type' => 'markup',
    '#markup' => t("This is just a very simple mechanism to protect from automated bots. To make it as easy for real humans, consider providing the answer with the question. Example '<em>What is Mickeys last name? It's \"Mouse\"</em>'. You could also do simply 'What is 1+1?', but some bots may figure that out."),
  );
  $form['configuration'] = array(
    '#type' => 'fieldset',
    '#title' => t('Logging options'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  // Disables checkbox if dblog module is not enabled.
  $form['configuration']['captcha_questions_watchdog'] = array(
    '#type' => 'checkbox',
    '#title' => t('Log to watchdog'),
    '#description' => t('Check this box to log all failed submissions to watchdog'),
    '#default_value' => variable_get('captcha_questions_watchdog', 0),
    '#disabled' => module_exists('dblog') ? FALSE : TRUE,
  );

  // Disables checkbox if captcha_questions_dblog module is not enabled.
  $form['configuration']['captcha_questions_dblog'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable logging internal database table'),
    '#description' => t('Check this box to log all failed submissions in separate database table'),
    '#default_value' => variable_get('captcha_questions_dblog', 0),
    '#disabled' => module_exists('captcha_questions_dblog') ? FALSE : TRUE,
  );
  $form['settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('CAPTCHA'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $question = variable_get('captcha_questions_question', t("What is Mickeys last name? It's Mouse."));
  $form['settings']['captcha_questions_question'] = array(
    '#type' => 'textfield',
    '#title' => t('Question to ask'),
    '#default_value' => $question,
    '#maxlength' => 256,
  );
  $answers = variable_get('captcha_questions_answers', array(
    'Mouse',
  ));
  $form['settings']['captcha_questions_answers'] = array(
    '#type' => 'textarea',
    '#title' => 'Accepted answer(s)',
    '#default_value' => implode("\n", $answers),
    '#description' => t('Please provide one or more accepted answers, one per line. Answers are case-insensitive'),
    '#format' => NULL,
  );
  $description = variable_get('captcha_questions_description', t('Please answer the question'));
  $form['settings']['captcha_questions_description'] = array(
    '#type' => 'textfield',
    '#title' => t('Description'),
    '#default_value' => $description,
    '#maxlength' => 256,
  );
  $form['forms'] = array(
    '#type' => 'fieldset',
    '#title' => t('Form protection'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#prefix' => '<div id="captcha-questions-form-id-wrapper">',
    '#suffix' => '</div>',
  );

  // Getting form_ids.
  if (!isset($form_state['form_ids'])) {
    $form_state['form_ids'] = captcha_questions_get_form_ids();
  }

  // Adding new custom form_ids.
  if (isset($form_state['values']['add_form_name'])) {
    if ($form_state['values']['add_form_name'] != '') {
      $form_state['form_ids'][] = $form_state['values']['add_form_name'];
    }
  }

  // Getting previously selected form_ids.
  $form_state['selected_form_ids'] = variable_get('captcha_questions_form_ids', array());

  // Create select list with previously selected form_ids as selected.
  $form['forms']['captcha_questions_form_ids'] = array(
    '#type' => 'checkboxes',
    '#options' => drupal_map_assoc($form_state['form_ids']),
    '#default_value' => array_intersect($form_state['selected_form_ids'], $form_state['form_ids']),
    '#title' => t('What forms should be protected?'),
  );

  // Adding node title and link to webform_ids options text.
  if (module_exists('webform')) {
    $webform_forms = captcha_questions_get_webforms();
    foreach ($webform_forms as $webform_id => $webform) {
      $webform_node_link = l($webform['title'], 'node/' . $webform['nid']);
      $webform_form_id_option_text = $webform_id . ' (' . $webform_node_link . ')';
      $form['forms']['captcha_questions_form_ids']['#options'][$webform_id] = $webform_form_id_option_text;
    }
  }
  $form['forms']['add_form_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom form_id'),
    '#default_value' => '',
    '#maxlength' => 256,
  );
  $form['forms']['add_form'] = array(
    '#type' => 'submit',
    '#value' => t('Add custom form_id'),
    '#submit' => array(
      'captcha_questions_add_form',
    ),
    '#ajax' => array(
      'callback' => 'captcha_questions_add_form_callback',
      'wrapper' => 'captcha-questions-form-id-wrapper',
    ),
  );
  $form['#submit'][] = 'captcha_questions_admin_settings_submit';
  return system_settings_form($form);
}