View source
<?php
include_once 'webform_validation.validators.inc';
include_once 'webform_validation.rules.inc';
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;
}
function webform_validation_rule_load($ruleid) {
$result = db_query("SELECT ruleid, rulename, nid, validator, data, error_message, negate, weight FROM {webform_validation_rule} WHERE ruleid = :ruleid", array(
':ruleid' => $ruleid,
), array(
'fetch' => PDO::FETCH_ASSOC,
));
$rule = $result
->fetchAssoc();
if (!$rule) {
return FALSE;
}
$rule['components'] = webform_validation_get_rule_components($ruleid, $rule['nid']);
$rule['negate'] = (bool) $rule['negate'];
return $rule;
}
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',
),
);
}
function webform_validation_form_webform_client_form_alter(&$form, &$form_state, $form_id) {
$form['#validate'][] = 'webform_validation_validate';
if (module_exists('maxlength')) {
$nid = substr($form_id, strlen('webform_client_form') + 1);
$rules = webform_validation_get_node_rules($nid);
foreach ($rules as $ruleid => $rule) {
if ($rule['validator'] == 'max_length') {
$length_limit = $rule['data'];
$components = $rule['components'];
foreach ($components as $cid => $component) {
$form_element =& _webform_validation_get_webform_element($component, $form);
$form_element['#pre_render'][] = 'maxlength_pre_render';
$form_element['#maxlength'] = $length_limit;
$form_element['#maxlength_js'] = TRUE;
}
}
}
}
}
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,
'list' => FALSE,
'refresh callback' => 'webform_validation_i18n_string_refresh',
);
return $groups;
}
function webform_validation_validate($form, &$form_state) {
$static_error_messages =& drupal_static(__FUNCTION__, array());
$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'];
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'];
}
if ($values && $page_count && $page_count > 1) {
$validators = webform_validation_get_validators();
foreach ($rules as $ruleid => $rule) {
if (!array_intersect_key($flat_values, $rule['components'])) {
unset($rules[$ruleid]);
}
elseif (isset($validators[$rule['validator']]['min_components']) && $validators[$rule['validator']]['min_components'] > 1) {
foreach (array_keys($rule['components']) as $cid) {
if ($node->webform['components'][$cid]['page_num'] > $form_state['webform']['page_num']) {
unset($rules[$ruleid]);
break;
}
}
}
}
}
if ($rules) {
if (defined('WebformConditionals::componentShown')) {
$sorter = webform_get_conditional_sorter($node);
if (!$sorter
->isExecuted()) {
$sorter
->executeConditionals(array(), 0);
}
foreach ($node->webform['components'] as $key => $component) {
if ($sorter
->componentVisibility($component['cid'], $component['page_num']) !== WebformConditionals::componentShown) {
unset($flat_values[$key]);
}
}
}
else {
if (!defined('WEBFORM_CONDITIONAL_INCLUDE')) {
define('WEBFORM_CONDITIONAL_INCLUDE', 1);
}
foreach ($node->webform['components'] as $key => $component) {
if (isset($flat_values[$key]) && (int) _webform_client_form_rule_check($node, $component, 0, $form_state['values']['submitted']) !== WEBFORM_CONDITIONAL_INCLUDE) {
unset($flat_values[$key]);
}
}
}
foreach ($rules as $rule) {
$items = array();
foreach ($rule['components'] as $cid => $component) {
if (array_key_exists($cid, $flat_values)) {
$items[$cid] = $flat_values[$cid];
}
}
$rule['sid'] = $sid;
$components = webform_validation_prefix_keys($node->webform['components']);
if (module_exists('webform_localization')) {
module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
foreach ($components as &$component) {
$dummy_element = array(
'#title' => '',
);
_webform_localization_translate_component($dummy_element, $component);
if (isset($dummy_element['#title']) && (string) $dummy_element['#title']) {
$component['name'] = $dummy_element['#title'];
}
}
}
$errors = module_invoke_all("webform_validation_validate", $rule['validator'], webform_validation_prefix_keys($items), $components, $rule);
if ($errors) {
$errors = webform_validation_unprefix_keys($errors);
$context = array(
'validator_name' => $rule['validator'],
'items' => $items,
'components' => $node->webform['components'],
'rule' => $rule,
);
drupal_alter('webform_validation_validate', $errors, $context);
foreach ($errors as $item_key => $error) {
if (in_array($error, $static_error_messages, TRUE)) {
continue;
}
$static_error_messages[] = $error;
$error_key = 'submitted][' . webform_validation_parent_tree($item_key, $node->webform['components']) . $node->webform['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 {
form_set_error($error_key, $error);
}
}
}
}
}
}
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])) {
$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;
}
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;
}
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;
}
function webform_validation_prefix_keys($arr) {
$ret = array();
foreach ($arr as $k => $v) {
$ret['item_' . $k] = $v;
}
return $ret;
}
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;
}
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) {
$validator_types = webform_validation_valid_component_types($validator_key);
$title = $validator_info['name'];
if (array_intersect($types, $validator_types)) {
$url = 'node/' . $nid . '/webform/validation/add/' . $validator_key;
$title = l($title, $url, array(
'query' => drupal_get_destination(),
));
$component_list_postfix = '';
}
else {
$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_types) . $component_list_postfix,
)) . '</dd>';
$output .= $item;
}
$output .= '</dl>';
}
return $output;
}
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']);
}
}
}
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);
}
}
function webform_validation_node_delete($node) {
$rules = webform_validation_get_node_rules($node->nid);
if ($rules) {
$transaction = db_transaction();
foreach (array_keys($rules) as $ruleid) {
webform_dynamic_delete_rule($ruleid);
}
}
}
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;
$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;
$rule['rule_components'] = $rule['components'];
webform_validation_rule_save($rule);
}
}
}
}
function webform_validation_rule_save(array $values) {
if ($values['action'] === 'add') {
$primary_keys = array();
}
elseif ($values['action'] === 'edit') {
$primary_keys = array(
'ruleid',
);
}
else {
return FALSE;
}
$transaction = db_transaction();
drupal_write_record('webform_validation_rule', $values, $primary_keys);
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'];
}
function webform_validation_save_rule_components($ruleid, array $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;
}
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;
}
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;
}
function webform_validation_webform_validator_alter(&$validators) {
if (module_exists('select_or_other')) {
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']);
}
}
}
}
function webform_validation_uuid_node_features_export_alter(&$data, $node, $module) {
$nid = entity_get_id_by_uuid('node', array(
$node->uuid,
));
$nid = reset($nid);
if (webform_validation_get_node_rules($nid)) {
$data['dependencies']['webform_validation'] = 'webform_validation';
}
}
function webform_validation_uuid_node_features_export_render_alter(&$export, $node, $module) {
if (!empty($node->webform)) {
$rules = webform_validation_get_node_rules_assoc($node->nid);
foreach ($rules as &$rule) {
unset($rule['nid']);
unset($rule['ruleid']);
}
$export->webform['validation'] = $rules;
}
}
function webform_validation_entity_uuid_save($node, $entity_type) {
if ($entity_type == 'node') {
if (isset($node->webform['validation'])) {
$rules = $node->webform['validation'];
$orig_rules = webform_validation_get_node_rules_assoc($node->nid);
$transaction = db_transaction();
$delete = array_diff_key($orig_rules, $rules);
foreach ($delete as $rule) {
webform_dynamic_delete_rule($rule['ruleid']);
}
$new = array_diff_key($rules, $orig_rules);
foreach ($new as $rule) {
$rule['action'] = 'add';
$rule['nid'] = $node->nid;
$rule['rule_components'] = $rule['components'];
webform_validation_rule_save($rule);
}
$existing = array_diff_key($rules, $new + $delete);
foreach ($existing as $name => $rule) {
$orig_rule = $orig_rules[$name];
$rule['nid'] = $orig_rule['nid'];
$rule['ruleid'] = $orig_rule['ruleid'];
if ($rule != $orig_rule) {
$rule['action'] = 'edit';
$rule['rule_components'] = $rule['components'];
webform_validation_rule_save($rule);
}
}
}
}
}
function &_webform_validation_get_webform_element(array $component, array &$form) {
$component_ancestors = array(
$component['form_key'],
);
$pid = $component['pid'];
while ($pid) {
$parent = $form['#node']->webform['components'][$pid];
array_unshift($component_ancestors, $parent['form_key']);
$pid = $parent['pid'];
}
$webform_element =& $form['submitted'];
foreach ($component_ancestors as $ancestor) {
$webform_element =& $webform_element[$ancestor];
}
return $webform_element;
}