Projet

Général

Profil

Paste
Télécharger (6,45 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / modules / entity.eval.inc @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains rules integration for entities needed during evaluation.
6
 *
7
 * @addtogroup rules
8
 *
9
 * @{
10
 */
11

    
12
/**
13
 * Action: Fetch data.
14
 */
15
function rules_action_entity_fetch($type, $id, $revision) {
16
  $info = entity_get_info($type);
17

    
18
  // Support the revision parameter, if applicable.
19
  if (!empty($info['entity keys']['revision']) && isset($revision)) {
20
    $conditions = array($info['entity keys']['revision'] => $revision);
21
  }
22

    
23
  $return = entity_load($type, array($id), isset($conditions) ? $conditions : array());
24
  $entity = reset($return);
25
  if (!$entity) {
26
    throw new RulesEvaluationException('Unable to load @entity with id "@id"', array('@id' => $id, '@entity' => $type));
27
  }
28
  return array('entity_fetched' => $entity);
29
}
30

    
31
/**
32
 * Info alteration callback for the entity fetch action.
33
 */
34
function rules_action_entity_fetch_info_alter(&$element_info, RulesAbstractPlugin $element) {
35
  $element->settings += array('type' => NULL);
36
  $info = entity_get_info($element->settings['type']);
37

    
38
  // Fix the type of the identifier.
39
  $element_info['parameter']['id']['type'] = isset($info['entity keys']['name']) ? 'text' : 'integer';
40

    
41
  // Add an optional revision parameter, if supported.
42
  if (!empty($info['entity keys']['revision'])) {
43
    $element_info['parameter']['revision_id'] = array(
44
      'type' => 'integer',
45
      'label' => t('Revision identifier'),
46
      'optional' => TRUE,
47
    );
48
  }
49
  $element_info['provides']['entity_fetched']['type'] = $element->settings['type'];
50
}
51

    
52
/**
53
 * Action: Query entities.
54
 */
55
function rules_action_entity_query($type, $property, $value, $limit) {
56
  $return = entity_property_query($type, $property, $value, $limit);
57
  return array('entity_fetched' => array_values($return));
58
}
59

    
60
/**
61
 * Info alteration callback for the entity query action.
62
 */
63
function rules_action_entity_query_info_alter(&$element_info, RulesAbstractPlugin $element) {
64
  $element->settings += array('type' => NULL, 'property' => NULL);
65
  if ($element->settings['type']) {
66
    $element_info['parameter']['property']['options list'] = 'rules_action_entity_query_property_options_list';
67

    
68
    if ($element->settings['property']) {
69
      $wrapper = rules_get_entity_metadata_wrapper_all_properties($element);
70
      if (isset($wrapper->{$element->settings['property']}) && $property = $wrapper->{$element->settings['property']}) {
71
        $property_type = $property->type();
72
        // If the cardinality of the property > 1, i.e. of type 'list<{type}>',
73
        // we will also accept a parameter of type {type}.
74
        if (substr($property_type, 0, strlen('list<')) === 'list<' && substr($property_type, -strlen('>')) === '>') {
75
          $property_type = array($property_type, substr($property_type, strlen('list<'), strlen($property_type) - strlen('list<>')));
76
        }
77
        $element_info['parameter']['value']['type'] = $property_type;
78
        $element_info['parameter']['value']['options list'] = $property->optionsList() ? 'rules_action_entity_query_value_options_list' : FALSE;
79
      }
80
    }
81
  }
82
  $element_info['provides']['entity_fetched']['type'] = 'list<' . $element->settings['type'] . '>';
83
}
84

    
85
/**
86
 * Action: Create entities.
87
 */
88
function rules_action_entity_create($args, $element) {
89
  $values = array();
90
  foreach ($element->pluginParameterInfo() as $name => $info) {
91
    if ($name != 'type') {
92
      // Remove the parameter name prefix 'param_'.
93
      $values[substr($name, 6)] = $args[$name];
94
    }
95
  }
96
  try {
97
    $data = entity_property_values_create_entity($args['type'], $values);
98
    return array('entity_created' => $data);
99
  }
100
  catch (EntityMetadataWrapperException $e) {
101
    throw new RulesEvaluationException('Unable to create entity @type": ' . $e->getMessage(), array('@type' => $args['type']), $element);
102
  }
103
}
104

    
105
/**
106
 * Info alteration callback for the entity create action.
107
 */
108
function rules_action_entity_create_info_alter(&$element_info, RulesAbstractPlugin $element) {
109
  if (!empty($element->settings['type']) && entity_get_info($element->settings['type'])) {
110
    $wrapper = entity_metadata_wrapper($element->settings['type']);
111
    // Add the data type's needed parameter for loading to the parameter info.
112
    foreach ($wrapper as $name => $child) {
113
      $info = $child->info();
114
      if (!empty($info['required'])) {
115
        $info += array('type' => 'text');
116
        // Prefix parameter names to avoid name clashes
117
        // with existing parameters.
118
        $element_info['parameter']['param_' . $name] = array_intersect_key($info, array_flip(array('type', 'label', 'description')));
119
        $element_info['parameter']['param_' . $name]['options list'] = $child->optionsList() ? 'rules_action_entity_parameter_options_list' : FALSE;
120
      }
121
    }
122
    $element_info['provides']['entity_created']['type'] = $element->settings['type'];
123
    if (($bundleKey = $wrapper->entityKey('bundle')) && isset($element->settings['param_' . $bundleKey])) {
124
      $element_info['provides']['entity_created']['bundle'] = $element->settings['param_' . $bundleKey];
125
    }
126
  }
127
}
128

    
129
/**
130
 * Action: Save entities.
131
 */
132
function rules_action_entity_save($wrapper, $immediate = FALSE, $settings, $state, $element) {
133
  $state->saveChanges($settings['data:select'], $wrapper, $immediate);
134
}
135

    
136
/**
137
 * Action: Delete entities.
138
 */
139
function rules_action_entity_delete($wrapper, $settings, $state, $element) {
140
  try {
141
    $wrapper->delete();
142
  }
143
  catch (EntityMetadataWrapperException $e) {
144
    throw new RulesEvaluationException($e->getMessage(), array(), $element);
145
  }
146
}
147

    
148
/**
149
 * Condition: Entity is new.
150
 */
151
function rules_condition_entity_is_new($wrapper, $settings, $state, $element) {
152
  return !$wrapper->getIdentifier() || !empty($wrapper->value()->is_new);
153
}
154

    
155
/**
156
 * Condition: Entity has field.
157
 */
158
function rules_condition_entity_has_field($wrapper, $field_name, $settings, $state) {
159
  return isset($wrapper->$field_name) || isset($wrapper->value()->$field_name);
160
}
161

    
162
/**
163
 * Condition: Entity is of type.
164
 */
165
function rules_condition_entity_is_of_type($wrapper, $type) {
166
  return $wrapper->type() == $type;
167
}
168

    
169
/**
170
 * Condition: Entity is of type and bundle.
171
 */
172
function rules_condition_entity_is_of_bundle($wrapper, $type, $bundles) {
173
  return $wrapper->type() == $type && in_array($wrapper->getBundle(), $bundles);
174
}
175

    
176
/**
177
 * Condition: User has access to field.
178
 */
179
function rules_condition_entity_field_access(EntityDrupalWrapper $wrapper, $field_name, $op, $account = NULL) {
180
  $field = field_info_field($field_name);
181
  return !empty($field) && field_access($op, $field, $wrapper->type(), $wrapper->value(), $account = NULL);
182
}
183

    
184
/**
185
 * @}
186
 */