Projet

Général

Profil

Paste
Télécharger (5,99 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / modules / entity.eval.inc @ 76e2e7c3

1
<?php
2

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

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

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

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

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

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

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

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

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

    
67
    if ($element->settings['property']) {
68
      $wrapper = rules_get_entity_metadata_wrapper_all_properties($element);
69
      if (isset($wrapper->{$element->settings['property']}) && $property = $wrapper->{$element->settings['property']}) {
70
        $element_info['parameter']['value']['type'] = $property->type();
71
        $element_info['parameter']['value']['options list']  = $property->optionsList() ? 'rules_action_entity_query_value_options_list' : FALSE;
72
      }
73
    }
74
  }
75
  $element_info['provides']['entity_fetched']['type'] = 'list<' . $element->settings['type'] . '>';
76
}
77

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

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

    
121
/**
122
 * Action: Save entities.
123
 */
124
function rules_action_entity_save($wrapper, $immediate = FALSE, $settings, $state, $element) {
125
  $state->saveChanges($settings['data:select'], $wrapper, $immediate);
126
}
127

    
128
/**
129
 * Action: Delete entities.
130
 */
131
function rules_action_entity_delete($wrapper, $settings, $state, $element) {
132
  try {
133
    $wrapper->delete();
134
  }
135
  catch (EntityMetadataWrapperException $e) {
136
    throw new RulesEvaluationException($e->getMessage(), array(), $element);
137
  }
138
}
139

    
140
/**
141
 * Condition: Entity is new.
142
 */
143
function rules_condition_entity_is_new($wrapper, $settings, $state, $element) {
144
  return !$wrapper->getIdentifier() || !empty($wrapper->value()->is_new);
145
}
146

    
147
/**
148
 * Condition: Entity has field.
149
 */
150
function rules_condition_entity_has_field($wrapper, $field_name, $settings, $state) {
151
  return isset($wrapper->$field_name) || isset($wrapper->value()->$field_name);
152
}
153

    
154
/**
155
 * Condition: Entity is of type.
156
 */
157
function rules_condition_entity_is_of_type($wrapper, $type) {
158
  return $wrapper->type() == $type;
159
}
160

    
161
/**
162
 * Condition: Entity is of type and bundle.
163
 */
164
function rules_condition_entity_is_of_bundle($wrapper, $type, $bundles) {
165
  return $wrapper->type() == $type && in_array($wrapper->getBundle(), $bundles);
166
}
167

    
168
/**
169
 * Condition: User has access to field.
170
 */
171
function rules_condition_entity_field_access(EntityDrupalWrapper $wrapper, $field_name, $op, $account = NULL) {
172
  $field = field_info_field($field_name);
173
  return !empty($field) && field_access($op, $field, $wrapper->type(), $wrapper->value(), $account = NULL);
174
}