Projet

Général

Profil

Paste
Télécharger (4,34 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / commerce.rules.inc @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Rules integration for Drupal Commerce.
6
 *
7
 * @addtogroup rules
8
 * @{
9
 */
10

    
11
/**
12
 * Implements hook_rules_condition_info_alter().
13
 * @todo remove "entity_exists" condition when http://drupal.org/node/1031530
14
 * is fixed.
15
 */
16
function commerce_rules_condition_info_alter(&$conditions) {
17
  // If the Rules module hasn't already provided "entity_exists", we add it here.
18
  if (!isset($conditions['entity_exists'])) {
19
    module_load_include('inc', 'rules', 'modules/data.rules');
20

    
21
    $conditions['entity_exists'] = array(
22
      'label' => t('Entity exists by property'),
23
      'parameter' => array(
24
        'type' => array(
25
          'type' => 'text',
26
          'label' => t('Entity type'),
27
          'options list' => 'commerce_entity_type_options_list',
28
          'description' => t('Specifies the type of the entity that should be fetched.'),
29
          'restriction' => 'input',
30
         ),
31
        'property' => array(
32
          'type' => 'text',
33
          'label' => t('Property'),
34
          'description' => t('The property by which the entity is to be selected.'),
35
          'restriction' => 'input',
36
         ),
37
        'value' => array(
38
          'type' => 'text',
39
          'label' => t('Value'),
40
          'description' => t('The property value of the entity to be fetched.'),
41
        ),
42
      ),
43
      'group' => t('Entities'),
44
      'base' => 'commerce_condition_entity_exists',
45
      'module' => 'commerce',
46
      'callbacks' => array(
47
        'form_alter' => 'rules_action_type_form_alter',
48
      ),
49
    );
50
  }
51
}
52

    
53
/**
54
 * Returns options containing entity types having the given key set in the info.
55
 */
56
function commerce_entity_type_options_list() {
57
  $types = array();
58

    
59
  foreach (entity_get_info() as $type => $entity_info) {
60
    $types[$type] = $entity_info['label'];
61
  }
62

    
63
  return $types;
64
}
65

    
66
/**
67
 * Condition callback: checks to see if an entity exists with the matching
68
 *   property value.
69
 */
70
function commerce_condition_entity_exists($type, $property, $value) {
71
  $result = entity_property_query($type, $property, $value, 1);
72
  return !empty($result);
73
}
74

    
75
/**
76
 * Info alteration callback for the entity query action.
77
 */
78
function commerce_condition_entity_exists_info_alter(&$element_info, RulesAbstractPlugin $element) {
79
  $element->settings += array('type' => NULL, 'property' => NULL);
80
  if ($element->settings['type']) {
81
    $element_info['parameter']['property']['options list'] = 'commerce_condition_entity_exists_property_options_list';
82

    
83
    if ($element->settings['property']) {
84
      $wrapper = entity_metadata_wrapper($element->settings['type']);
85
      if (isset($wrapper->{$element->settings['property']}) && $property = $wrapper->{$element->settings['property']}) {
86
        $element_info['parameter']['value']['type'] = $property->type();
87
        $element_info['parameter']['value']['options list']  = $property->optionsList() ? 'commerce_condition_entity_exists_value_options_list' : FALSE;
88
      }
89
    }
90
  }
91
}
92

    
93
/**
94
 * Returns the options list for choosing a property of an entity type.
95
 */
96
function commerce_condition_entity_exists_property_options_list(RulesAbstractPlugin $element) {
97
  $element->settings += array('type' => NULL);
98
  if ($element->settings['type']) {
99
    $properties = entity_get_all_property_info($element->settings['type']);
100
    return rules_extract_property($properties, 'label');
101
  }
102
}
103

    
104
/**
105
 * Returns the options list specified for the chosen property.
106
 */
107
function commerce_condition_entity_exists_value_options_list(RulesAbstractPlugin $element) {
108
  // Get the possible values for the selected property.
109
  $element->settings += array('type' => NULL, 'property' => NULL);
110
  if ($element->settings['type'] && $element->settings['property']) {
111
    $wrapper = entity_metadata_wrapper($element->settings['type']);
112

    
113
    if (isset($wrapper->{$element->settings['property']}) && $property = $wrapper->{$element->settings['property']}) {
114
      return $property->optionsList('view');
115
    }
116
  }
117
}
118

    
119
/**
120
 * Custom validate callback for data query action.
121
 */
122
function commerce_condition_entity_exists_validate($element) {
123
  if (!isset($element->settings['type'])) {
124
    throw new RulesEvaluationException('Invalid type specified.', array(), array($element, 'parameter', 'type'));
125
  }
126
  if (!isset($element->settings['property'])) {
127
    throw new RulesEvaluationException('Invalid property specified.', array(), array($element, 'parameter', 'property'));
128
  }
129
}
130

    
131
/**
132
 * @}
133
 */