<?php
/**
 * @file
 * Hooks and helper functions for captcha_webform.
 */

/**
 * Implements hook_entity_insert().
 */
function captcha_webform_entity_insert($entity, $type) {
  if (variable_get('captcha_webform_enabled_by_default', 0)) {
    _captcha_webform__set_webform_captcha('default', $entity, $type);
  }
}

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

/**
 * Helper function to set (or unset) a captcha whenever any webform is changed.
 */
function _captcha_webform__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 hook_menu().
 */
function captcha_webform_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('captcha_webform_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 captcha_webform_admin_settings() {
  $form = array();

  $form['captcha_webform_enabled_by_default'] = array(
    '#type' => 'checkbox',
    '#default_value' => variable_get('captcha_webform_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['captcha_webform_allow_user_config'] = array(
    '#type' => 'checkbox',
    '#default_value' => variable_get('captcha_webform_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 captcha_webform_form_alter(&$form, &$form_state) {
  if ($form['#form_id'] == 'webform_configure_form' && variable_get('captcha_webform_allow_user_config', 0)) {
    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'] = t('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'] = t('Enable Captcha');
    $form['webform-captcha']['user-config']['#default_value'] = $webform_captcha_state;

    $form['#submit'][] = 'captcha_webform_submit_handler';
  }
}

/**
 * User input determines whether the captcha is enabled.
 */
function captcha_webform_submit_handler($form, &$form_state) {
  $entity = $form['#node'];
  if ($form_state['input']['user-config']) {
    _captcha_webform__set_webform_captcha('default', $entity, 'node');
  }
  else {
    _captcha_webform__set_webform_captcha('none', $entity, 'node');
  }
}
