You are here

webform_captcha.module in Webform Captcha 7

File

webform_captcha.module
View source
<?php

/**
 * Implements hook_entity_insert().
 */
function webform_captcha_entity_insert($entity, $type) {
  if (variable_get('webform_captcha_enabled_by_default', 0)) {
    _webform_captcha__set_webform_captcha('default', $entity, $type);
  }
}

/**
 * Implements hook_entity_delete().
 */
function webform_captcha_entity_delete($entity, $type) {
  _webform_captcha__set_webform_captcha(NULL, $entity, $type);
}

/**
 * Helper function to set (or unset) a captcha whenever any webform is changed.
 */
function _webform_captcha__set_webform_captcha($captcha_type, $entity, $entity_type) {
  if (module_exists('webform') && module_exists('captcha') && !empty($entity->webform)) {
    list($webform_id) = entity_extract_ids($entity_type, $entity);
    module_load_include('inc', 'captcha');
    $form_id = 'webform_client_form_' . $webform_id;
    captcha_set_form_id_setting($form_id, $captcha_type);
  }
}

/**
 * Implements of hook_menu().
 */
function webform_captcha_menu() {
  $items = array();
  $items['admin/config/people/captcha/captcha/webform'] = array(
    'title' => 'Webform settings',
    'description' => 'Settings specific to webform.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'webform_captcha_admin_settings',
    ),
    'access arguments' => array(
      'administer CAPTCHA settings',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 5,
  );
  return $items;
}

/**
 * Page callback for admin/config/people/captcha/captcha/webform
 */
function webform_captcha_admin_settings() {
  $form = array();
  $form['webform_captcha_enabled_by_default'] = array(
    '#type' => 'checkbox',
    '#default_value' => variable_get('webform_captcha_enabled_by_default', 0),
    '#title' => t('Enable the default Captcha for all new Webforms when they are created'),
    '#description' => t('This setting does not impact existing Webforms.'),
  );
  $form['webform_captcha_allow_user_config'] = array(
    '#type' => 'checkbox',
    '#default_value' => variable_get('webform_captcha_allow_user_config', 0),
    '#title' => t('Display option to enable or disable captcha in Webform settings'),
    '#description' => t('Adds option to each Webform form settings that allowing users with permissoin to configure whether captcha is used.  Does not give users any other captcha options.  Will only add the default.'),
  );
  return system_settings_form($form);
}

/**
 * Implements hook_form_alter().
 */
function webform_captcha_form_alter(&$form, &$form_state) {
  if ($form['#form_id'] == 'webform_configure_form' && variable_get('webform_captcha_allow_user_config', 0)) {

    // not sure why module_load_include is needed
    module_load_include('inc', 'captcha');
    $settings = captcha_get_form_id_setting('webform_client_form_' . $form['nid']['#value']);
    if (isset($settings->captcha_type) && $settings->captcha_type != 'none') {
      $webform_captcha_state = 1;
    }
    else {
      $webform_captcha_state = 0;
    }
    $form['webform-captcha']['#type'] = 'fieldset';
    $form['webform-captcha']['#title'] = 'Spam Prevention';
    $form['webform-captcha']['#collapsible'] = TRUE;
    $form['webform-captcha']['#collapsed'] = FALSE;
    $form['webform-captcha']['user-config']['#type'] = 'checkbox';
    $form['webform-captcha']['user-config']['#title'] = 'Require Captcha';
    $form['webform-captcha']['user-config']['#description'] = 'Requires users to confirm they are not a spam bot before submitting the form.  This does not apply to authenticated users.';
    $form['webform-captcha']['user-config']['#default_value'] = $webform_captcha_state;
    $form['#submit'][] = 'webform_captcha_submit_handler';
  }
}
function webform_captcha_submit_handler($form, &$form_state) {
  $entity = $form['#node'];
  if ($form_state['input']['user-config']) {
    _webform_captcha__set_webform_captcha('default', $entity, 'node');
  }
  else {
    _webform_captcha__set_webform_captcha('none', $entity, 'node');
  }
}

Functions

Namesort descending Description
webform_captcha_admin_settings Page callback for admin/config/people/captcha/captcha/webform
webform_captcha_entity_delete Implements hook_entity_delete().
webform_captcha_entity_insert Implements hook_entity_insert().
webform_captcha_form_alter Implements hook_form_alter().
webform_captcha_menu Implements of hook_menu().
webform_captcha_submit_handler
_webform_captcha__set_webform_captcha Helper function to set (or unset) a captcha whenever any webform is changed.