webform_validation.module 15.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
<?php

/**
 * @file
 * Add validation rules to webforms
 */

include_once('webform_validation.validators.inc');
include_once('webform_validation.rules.inc');

/**
 * Implements hook_menu().
 */
function webform_validation_menu() {
  $items = array();

  $items['node/%webform_menu/webform/validation'] = array(
    'title' => 'Form validation',
    'page callback' => 'webform_validation_manage',
    'page arguments' => array(1),
    'access callback' => 'node_access',
    'access arguments' => array('update', 1),
    'file' => 'webform_validation.admin.inc',
    'weight' => 3,
    'type' => MENU_LOCAL_TASK,
  );

  $items['node/%webform_menu/webform/validation/add/%'] = array(
    'title' => 'Add validation',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('webform_validation_manage_rule', 1, 'add', 5),
    'access callback' => 'node_access',
    'access arguments' => array('update', 1),
    'file' => 'webform_validation.admin.inc',
    'type' => MENU_CALLBACK,
  );

  $items['node/%webform_menu/webform/validation/edit/%/%webform_validation_rule'] = array(
    'title' => 'Edit rule',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('webform_validation_manage_rule', 1, 'edit', 5, 6),
    'access callback' => 'node_access',
    'access arguments' => array('update', 1),
    'file' => 'webform_validation.admin.inc',
    'type' => MENU_CALLBACK,
  );

  $items['node/%webform_menu/webform/validation/delete/%webform_validation_rule'] = array(
    'title' => 'Delete rule',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('webform_validation_delete_rule', 5),
    'access callback' => 'node_access',
    'access arguments' => array('update', 1),
    'file' => 'webform_validation.admin.inc',
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Loads validation rule from menu parameter
 */
function webform_validation_rule_load($ruleid) {
  return webform_validation_get_rule($ruleid);
}


/**
 * Implements hook_theme().
 */
function webform_validation_theme() {
  return array(
    'webform_validation_manage_add_rule' => array(
      'variables' => array(
        'nid' => NULL,
      ),
    ),
    'webform_validation_manage_overview_form' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Implements hook_form_alter().
 */
function webform_validation_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'webform_client_form_') !== FALSE) {
    $form['#validate'][] = 'webform_validation_validate';
  }
}

/**
 * Implements hook_i18n_string_info().
 */
function webform_validation_i18n_string_info() {
  $groups = array();
  $groups['webform_validation'] = array(
    'title' => t('Webform Validation'),
    'description' => t('Translatable strings for webform validation translation'),
    'format' => FALSE, // This group doesn't have strings with format
    'list' => FALSE, // This group cannot list all strings
    'refresh callback' => 'webform_validation_i18n_string_refresh',
  );
  return $groups;
}

/**
 * Webform validation handler to validate against the given rules
 */
function webform_validation_validate($form, &$form_state) {
  $page_count = 1;
  $nid = $form_state['values']['details']['nid'];
  $node = node_load($nid);
  $values = isset($form_state['values']['submitted']) ? $form_state['values']['submitted'] : NULL;
  $flat_values = _webform_client_form_submit_flatten($node, $values);
  $rules = webform_validation_get_node_rules($nid);
  $sid = empty($form_state['values']['details']['sid']) ? 0 : $form_state['values']['details']['sid'];

  // Get number of pages for this webform
  if (isset($form_state['webform']['page_count'])) {
    $page_count = $form_state['webform']['page_count'];
  }
  elseif (isset($form_state['storage']['page_count'])) {
    $page_count = $form_state['storage']['page_count'];
  }

  // Filter out rules that don't apply to this step in the multistep form
  if ($values && $page_count && $page_count > 1) {
    $current_page_components = webform_validation_get_field_keys($form_state['values']['submitted'], $node);
    if ($rules) {
       // filter out rules that don't belong in the current step
      foreach ($rules as $ruleid => $rule) {
        // get all the component formkeys for this specific validation rule
        $rule_formkeys = webform_validation_rule_get_formkeys($rule);
        $rule_applies_to_current_page = FALSE;
        if (!empty($rule_formkeys)) {
          foreach ($rule_formkeys as $formkey) {
            if (in_array($formkey, $current_page_components)) {
              // this rule applies to the current page,
              // because one of the rule components is on the page
              $rule_applies_to_current_page = TRUE;
            }
          }
        }

        if (!$rule_applies_to_current_page) {
          unset($rules[$ruleid]);
        }
      }
    }
  }

  if ($rules) {
    $component_definitions = webform_validation_prefix_keys($node->webform['components']);
    // Remove hidden components.
    foreach ($component_definitions as $key => $component) {
      if (isset($flat_values[$key]) && _webform_client_form_rule_check($node, $component, 0, $form_state['values']['submitted']) !== WEBFORM_CONDITIONAL_INCLUDE) {
        unset($flat_values[$key]);
      }
    }
    foreach ($rules as $rule) {
      // create a list of components that need validation against this rule (component id => user submitted value)
      $items = array();
      foreach ($rule['components'] as $cid => $component) {
        if (array_key_exists($cid, $flat_values)) {
          $items[$cid] = $flat_values[$cid];
        }
      }
      // prefix array keys to avoid reindexing by the module_invoke_all function call
      $items = webform_validation_prefix_keys($items);
      $rule['sid'] = $sid;
      // have the submitted values validated
      $errors = module_invoke_all("webform_validation_validate", $rule['validator'], $items, $component_definitions, $rule);
      if ($errors) {
        $errors = webform_validation_unprefix_keys($errors);
        $components = webform_validation_unprefix_keys($component_definitions);
        foreach ($errors as $item_key => $error) {
          // build the proper form element error key, taking into account hierarchy
          $error_key = 'submitted][' . webform_validation_parent_tree($item_key, $components) . $components[$item_key]['form_key'];
          if (is_array($error)) {
            foreach ($error as $sub_item_key => $sub_error) {
              form_set_error($error_key . '][' . $sub_item_key, $sub_error);
            }
          }
          else {
            // @ignore security_form_set_error. filter_xss() is run in _webform_validation_i18n_error_message().
            form_set_error($error_key, $error);
          }
        }
      }
    }
  }
}

/**
 * Recursive helper function to get all field keys (including fields in fieldsets)
 */
function webform_validation_get_field_keys($submitted, $node) {
  static $fields = array();
  foreach (element_children($submitted) as $child) {
    if (is_array($submitted[$child]) && element_children($submitted[$child])) {
      // only keep searching recursively if it's a fieldset
      $group_components = _webform_validation_get_group_types();
      if (in_array(_webform_validation_get_component_type($node, $child), $group_components)) {
        webform_validation_get_field_keys($submitted[$child], $node);
      }
      else {
        $fields[$child] = $child;
      }

    }
    else {
      $fields[$child] = $child;
    }
  }
  return $fields;
}

/**
 * Recursively add the parents for the element, to be used as first argument to form_set_error
 */
function webform_validation_parent_tree($cid, $components) {
  $output = '';
  if ($pid = $components[$cid]['pid']) {
    $output .= webform_validation_parent_tree($pid, $components);
    $output .= $components[$pid]['form_key'] . '][';
  }
  return $output;
}

/**
 * Get an array of formkeys for all components that have been assigned to a rule
 */
function webform_validation_rule_get_formkeys($rule) {
  $formkeys = array();
  if (isset($rule['components'])) {
    foreach ($rule['components'] as $cid => $component) {
      $formkeys[] = $component['form_key'];
    }
  }
  return $formkeys;
}

/**
 * Prefix numeric array keys to avoid them being reindexed by module_invoke_all
 */
function webform_validation_prefix_keys($arr) {
  $ret = array();
  foreach ($arr as $k => $v) {
    $ret['item_' . $k] = $v;
  }
  return $ret;
}

/**
 * Undo prefixing numeric array keys to avoid them being reindexed by module_invoke_all
 */
function webform_validation_unprefix_keys($arr) {
  $ret = array();
  foreach ($arr as $k => $v) {
    $new_key = str_replace('item_', '', $k);
    $ret[$new_key] = $v;
  }
  return $ret;
}
/**
 * Theme the 'add rule' list
 */
function theme_webform_validation_manage_add_rule($variables) {
  $nid = $variables['nid'];
  $output = '';
  $validators = webform_validation_get_validators();

  if ($validators) {
    $results = db_query('SELECT DISTINCT type FROM {webform_component} WHERE nid = :nid', array('nid' => $nid));
    $types = array();
    while ($item = $results->fetch()) {
      $types[] = $item->type;
    }

    $output = '<h3>' . t('Add a validation rule') . '</h3>';
    $output .= '<dl>';
    foreach ($validators as $validator_key => $validator_info) {
      $url = 'node/' . $nid . '/webform/validation/add/' . $validator_key;
      if (array_intersect($types, $validator_info['component_types'])) {
        $title = l($validator_info['name'], $url, array('query' => drupal_get_destination()));
        $component_list_postfix = '';
      }
      else {
        $title = $validator_info['name'];
        $component_list_postfix = '; ' . t('none present in this form');
      }
      $item = '<dt>' . $title . '</dt>';
      $item .= '<dd>';
      $item .= $validator_info['description'];
      $item .= ' ' . t('Works with: @component_types.', array('@component_types' => implode(', ', $validator_info['component_types']) . $component_list_postfix)) . '</dd>';
      $output .= $item;
    }
    $output .= '</dl>';
  }
  return $output;
}

/**
 * Implements hook_webform_validation().
 */
function webform_validation_webform_validation($type, $op, $data) {
  if ($type == 'rule' && in_array($op, array('add', 'edit'))) {
    if (module_exists('i18n_string') && isset($data['error_message'])) {
      i18n_string_update('webform_validation:error_message:' . $data['ruleid'] . ':message', $data['error_message']);
    }
  }
}

/**
 * Implements hook_node_insert().
 */
function webform_validation_node_insert($node) {
  if (module_exists('clone') && in_array($node->type, webform_variable_get('webform_node_types'))) {
    webform_validation_node_clone($node);
  }
}

/**
 * Implements hook_node_delete().
 */
function webform_validation_node_delete($node) {
  $rules = webform_validation_get_node_rules($node->nid);
  if ($rules) {
    foreach (array_keys($rules) as $ruleid) {
      webform_dynamic_delete_rule($ruleid);
    }
  }
}

/**
 * Adds support for node_clone module
 */
function webform_validation_node_clone($node) {
  if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
    return;
  }
  if (isset($node->clone_from_original_nid)) {
    $original_nid = $node->clone_from_original_nid;
    // Get existing rules for original node
    $rules = webform_validation_get_node_rules($original_nid);
    if ($rules) {
      foreach ($rules as $orig_ruleid => $rule) {
        unset($rule['ruleid']);
        $rule['action'] = 'add';
        $rule['nid'] = $node->nid; // attach existing rules to new node
        $rule['rule_components'] = $rule['components'];
        webform_validation_rule_save($rule);
      }
    }
  }
}

/**
 * Save a validation rule. Data comes from the admin form or nodeapi function in
 * case of node clone.
 *
 * @param array $values
 *   An associative array containing:
 *   - action: "add" or "edit".
 *   - ruleid: ID of the rule to edit. Do not set for "add".
 *   - nid: Node ID of the Webform.
 *   - validator: Machine name of the validator used by this validation rule.
 *   - rulename: Human-readable name for this validation rule.
 *   - rule_components: An array in which the keys and the values are the cid's
 *     of the Webform components that this rule applies to.
 *
 * @return int
 *   The $ruleid of the rule added or edited.
 */
function webform_validation_rule_save($values) {
  if ($values['action'] === 'add') {
    $primary_keys = array();
  }
  elseif ($values['action'] === 'edit') {
    $primary_keys = array('ruleid');
  }
  else {
    return FALSE;
  }

  drupal_write_record('webform_validation_rule', $values, $primary_keys);

  // Delete existing component records for this ruleid.
  if ($values['action'] === 'edit') {
    db_delete('webform_validation_rule_components')
      ->condition('ruleid', $values['ruleid'])
      ->execute();
  }

  $components = array_filter($values['rule_components']);
  if ($values['ruleid'] && $components) {
    webform_validation_save_rule_components($values['ruleid'], $components);
    module_invoke_all('webform_validation', 'rule', $values['action'], $values);
  }

  return $values['ruleid'];
}

/**
 * Save components attached to a specific rule.
 *
 * @param int $ruleid
 *   The ruleid of the rule being saved.
 * @param array $components
 *   An array in which the keys are the cid's of the components attached to the rule.
 * @return array
 *   An array of the return statuses for each query keyed by cid.
 */
function webform_validation_save_rule_components($ruleid, $components) {
  $return_status = array();
  foreach ($components as $cid => $component) {
    $return_status[$cid] = db_merge('webform_validation_rule_components')
      ->key(array(
        'ruleid' => $ruleid,
        'cid' => $cid,
      ))
      ->fields(array(
        'ruleid' => $ruleid,
        'cid' => $cid,
      ))
      ->execute();
  }
  return $return_status;
}

/**
 * Given a webform node, get the component type based on a given component key
 */
function _webform_validation_get_component_type($node, $component_key) {
  if ($node->webform['components']) {
    foreach ($node->webform['components'] as $component) {
      if ($component['form_key'] == $component_key) {
        return $component['type'];
      }
    }
  }
  return FALSE;
}

/**
 * Get all webform components that are defined as a group
 */
function _webform_validation_get_group_types() {
  $types = array();
  foreach (webform_components() as $name => $component) {
    if (isset($component['features']['group']) && $component['features']['group']) {
      $types[] = $name;
    }
  }
  return $types;
}

/**
 * Implements hook_webform_validator_alter().
 */
function webform_validation_webform_validator_alter(&$validators) {
  // Add support for the Select (or Other) module
  if (module_exists('select_or_other')) {
    // if this module exists, all select components can now except user input.
    // Thus we provide those components the same rules as a textfield
    if ($validators) {
      foreach ($validators as $validator_name => $validator_info) {
        if (in_array('textfield', $validator_info['component_types'])) {
          $validators[$validator_name]['component_types'][] = 'select';
        }
        $validators[$validator_name]['component_types'] = array_unique($validators[$validator_name]['component_types']);
      }
    }
  }
}