Project

General

Profile

Revision d0ed0aa6

Added by Assos Assos over 3 years ago

Weekly update of contrib modules

View differences:

drupal7/sites/all/modules/webform_validation/webform_validation.module
59 59
}
60 60

  
61 61
/**
62
 * Loads validation rule from menu parameter.
62
 * Load a validation rule.
63
 *
64
 * @param int $ruleid
65
 *   The rule ID.
66
 *
67
 * @return array|false
68
 *   The rule or FALSE if no rule exists.
63 69
 */
64 70
function webform_validation_rule_load($ruleid) {
65
  return webform_validation_get_rule($ruleid);
71
  $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));
72
  $rule = $result->fetchAssoc();
73
  if (!$rule) {
74
    return FALSE;
75
  }
76
  $rule['components'] = webform_validation_get_rule_components($ruleid, $rule['nid']);
77
  $rule['negate'] = (bool) $rule['negate'];
78
  return $rule;
66 79
}
67 80

  
68 81
/**
......
86 99
 */
87 100
function webform_validation_form_webform_client_form_alter(&$form, &$form_state, $form_id) {
88 101
  $form['#validate'][] = 'webform_validation_validate';
102
  if (module_exists('maxlength')) {
103
    $nid = substr($form_id, strlen('webform_client_form') + 1);
104
    $rules = webform_validation_get_node_rules($nid);
105
    foreach ($rules as $ruleid => $rule) {
106
      if ($rule['validator'] == 'max_length') {
107
        $length_limit = $rule['data'];
108
        $components = $rule['components'];
109
        foreach ($components as $cid => $component) {
110
          // Define $form_element as the webform element representing this
111
          // component, even if it's nested in multiple arrays, as webform
112
          // elemens often are (e.g., fieldsets). Assign by reference here,
113
          // since we need to modify the form element itself and don't know
114
          // its array depth or keys by which to access it.
115
          $form_element = &_webform_validation_get_webform_element($component, $form);
116
          // Append to this form element the relevant properties which are
117
          // supported by maxlength module.
118
          $form_element['#pre_render'][] = 'maxlength_pre_render';
119
          $form_element['#maxlength'] = $length_limit;
120
          $form_element['#maxlength_js'] = TRUE;
121
        }
122
      }
123
    }
124
  }
89 125
}
90 126

  
91 127
/**
......
399 435
function webform_validation_node_delete($node) {
400 436
  $rules = webform_validation_get_node_rules($node->nid);
401 437
  if ($rules) {
438
    $transaction = db_transaction();
402 439
    foreach (array_keys($rules) as $ruleid) {
403 440
      webform_dynamic_delete_rule($ruleid);
404 441
    }
......
458 495
    return FALSE;
459 496
  }
460 497

  
498
  $transaction = db_transaction();
499

  
461 500
  drupal_write_record('webform_validation_rule', $values, $primary_keys);
462 501

  
463 502
  // Delete existing component records for this ruleid.
......
577 616
}
578 617

  
579 618
/**
580
 * Implements hook_uuid_entity_uuid_save().
619
 * Implements hook_entity_uuid_save().
581 620
 */
582 621
function webform_validation_entity_uuid_save($node, $entity_type) {
583 622
  if ($entity_type == 'node') {
584 623
    if (isset($node->webform['validation'])) {
585 624
      $rules = $node->webform['validation'];
586 625
      $orig_rules = webform_validation_get_node_rules_assoc($node->nid);
626
      $transaction = db_transaction();
587 627
      // Delete obsolete rules.
588 628
      $delete = array_diff_key($orig_rules, $rules);
589 629
      foreach ($delete as $rule) {
......
611 651
      }
612 652
    }
613 653
  }
654
}
614 655

  
656
/**
657
 * Get a reference to a specific webform element.
658
 *
659
 * (For a given webform_validation rule component, and a given Drupal webform
660
 * form, get a reference to the webform element represented by the rule
661
 * component; return the correct element regardless of how deeply it's nested
662
 * in webform fieldsets or other wrappers.)
663
 *
664
 * @param array $component
665
 *   Webform validation rule component.
666
 * @param array $form
667
 *   Drupal webform form.
668
 *
669
 * @return array
670
 *   Reference to the webform element represented by the rule component.
671
 */
672
function &_webform_validation_get_webform_element(array $component, array &$form) {
673
  // Define an array of ancestors, beginning with the component itself.
674
  $component_ancestors = array($component['form_key']);
675
  // Define the parent-id, starting with the parent-id of the component itself,
676
  // if any.
677
  $pid = $component['pid'];
678
  // Look into $form['#node']->webform['components'][$pid] to get any parent
679
  // of the component, and continue working up the family tree until there is
680
  // no more parent-id.
681
  while ($pid) {
682
    $parent = $form['#node']->webform['components'][$pid];
683
    // Prepend the parent form_key to the array of ancestors. This causes the
684
    // array of ancestors to be ordered from ancestor to descendant.
685
    array_unshift($component_ancestors, $parent['form_key']);
686
    // Note this parent's parent-id, if any.
687
    $pid = $parent['pid'];
688
  }
689
  // $component_ancestors now contains the ordered ancestry. Cycle through it to
690
  // get the correct member of $form['submitted']. Assign by reference so that
691
  // we have a good reference to $webform_element to return.
692
  $webform_element = &$form['submitted'];
693
  foreach ($component_ancestors as $ancestor) {
694
    $webform_element = &$webform_element[$ancestor];
695
  }
696
  return $webform_element;
615 697
}

Also available in: Unified diff