-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.rules.inc
More file actions
344 lines (323 loc) · 12 KB
/
pool.rules.inc
File metadata and controls
344 lines (323 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* @file
* Rules integration for the pool module.
*/
/**
* Implements hook_rules_condition_info().
*/
function pool_rules_condition_info() {
return array(
'pool_throwedin' => array(
'label' => t('An entity is inside a certain pool'),
'parameter' => array(
'pool' => array(
'type' => 'text',
'label' => t('Pool name or alias'),
),
'throwedin' => array(
'type' => 'entity',
'label' => t('Throwed in entity'),
'description' => t('Specifies the throwed in entity.'),
'wrapped' => TRUE,
'optional' => TRUE,
'default value' => NULL,
),
'throwedin_type' => array(
'type' => 'token',
'label' => t('Throwed in type'),
'description' => t('Specifies the type of the throwed in entity.'),
'options list' => 'pool_rules_entitytype_options',
'restriction' => 'input',
'optional' => TRUE,
'default value' => 'entity',
),
'throwedin_revisioning' => array(
'type' => 'boolean',
'label' => t('Respect revisioning of throwed in entity'),
'optional' => TRUE,
'default_value' => TRUE,
'restriction' => 'input',
),
'throwedby' => array(
'type' => 'entity',
'label' => t('Throwed by entity'),
'description' => t('Specifies the throwed by entity.'),
'wrapped' => TRUE,
'optional' => TRUE,
'default value' => NULL,
),
'throwedby_type' => array(
'type' => 'token',
'label' => t('Throwed by type'),
'description' => t('Specifies the type of the throwed by entity.'),
'options list' => 'pool_rules_entitytype_options',
'restriction' => 'input',
'optional' => TRUE,
'default value' => 'entity',
),
'throwedby_revisioning' => array(
'type' => 'boolean',
'label' => t('Respect revisioning of throwed by entity'),
'optional' => TRUE,
'default_value' => FALSE,
'restriction' => 'input',
),
),
'group' => t('Entity Pools'),
'base' => 'pool_rules_throwedin_condition',
),
);
}
/**
* Implements hook_rules_action_info().
*/
function pool_rules_action_info() {
return array(
'pool_throwin' => array(
'label' => t('Throw entity into pool'),
'parameter' => array(
'pool' => array(
'type' => 'text',
'label' => t('Pool name or alias'),
),
'throwedin' => array(
'type' => 'entity',
'label' => t('Entity to throw in'),
'description' => t('Specifies the entity being throwed.'),
'wrapped' => TRUE,
),
'throwedin_revisioning' => array(
'type' => 'boolean',
'label' => t('Respect revisioning of entity being throwed.'),
'optional' => TRUE,
'default_value' => FALSE,
'restriction' => 'input',
),
'throwedby' => array(
'type' => 'entity',
'label' => t('Thrower'),
'description' => t('Optionally specifies an entity as thrower.'),
'wrapped' => TRUE,
),
'throwedby_revisioning' => array(
'type' => 'boolean',
'label' => t('Respect revisioning of thrower'),
'optional' => TRUE,
'default_value' => FALSE,
'restriction' => 'input',
),
),
'group' => t('Entity Pools'),
'base' => 'pool_rules_throwin_action',
),
'pool_pullout' => array(
'label' => t('Pull out entity from pool'),
'parameter' => array(
'pool' => array(
'type' => 'text',
'label' => t('Pool name or alias'),
),
'throwedin' => array(
'type' => 'entity',
'label' => t('Throwed in entity'),
'description' => t('Specifies the throwed in entity.'),
'wrapped' => TRUE,
'optional' => TRUE,
'default value' => NULL,
),
'throwedin_type' => array(
'type' => 'token',
'label' => t('Throwed in type'),
'description' => t('Specifies the type of the throwed in entity.'),
'options list' => 'pool_rules_entitytype_options',
'restriction' => 'input',
'optional' => TRUE,
'default value' => 'entity',
),
'throwedin_revisioning' => array(
'type' => 'boolean',
'label' => t('Respect revisioning of throwed in entity'),
'optional' => TRUE,
'default_value' => TRUE,
'restriction' => 'input',
),
'throwedby' => array(
'type' => 'entity',
'label' => t('Throwed by entity'),
'description' => t('Specifies the throwed by entity.'),
'wrapped' => TRUE,
'optional' => TRUE,
'default value' => NULL,
),
'throwedby_type' => array(
'type' => 'token',
'label' => t('Throwed by type'),
'description' => t('Specifies the type of the throwed by entity.'),
'options list' => 'pool_rules_entitytype_options',
'restriction' => 'input',
'optional' => TRUE,
'default value' => 'entity',
),
'throwedby_revisioning' => array(
'type' => 'boolean',
'label' => t('Respect revisioning of throwed by entity'),
'optional' => TRUE,
'default_value' => FALSE,
'restriction' => 'input',
),
'throwback' => array(
'type' => 'text',
'label' => t('Throwback behavior'),
'description' => t('Whether the pulled out entity shall be throwed back into the pool or not.'),
'optional' => TRUE,
'default value' => 'throwback',
'options list' => 'pool_rules_throwback_options',
),
),
'provides' => array(
'throwedin' => array(
'type' => 'unknown',
'label' => t('Throwed in entity'),
),
'throwedby' => array(
'type' => 'unknown',
'label' => t('Throwed by entity'),
),
),
'group' => t('Entity Pools'),
'base' => 'pool_rules_pullout_action',
),
);
}
/**
* Rules condition to check whether an entity is inside a certain pool.
*/
function pool_rules_throwedin_condition($pool, $throwedin_wrapped, $throwedin_type, $throwedin_revisioning, $throwedby_wrapped, $throwedby_type, $throwedby_revisioning) {
$item = _pool_rules_pullout($pool, $throwedin_wrapped, $throwedin_type, $throwedin_revisioning, $throwedby_wrapped, $throwedby_type, $throwedby_revisioning);
return (bool) $item;
}
/**
* Rules action to let an entity throw another entity into a certain pool.
*/
function pool_rules_throwin_action($pool, $throwedin_wrapped, $throwedin_revisioning, $throwedby_wrapped, $throwedby_revisioning) {
$throwedin = $throwedin_wrapped->value();
$throwedin_type = $throwedin_wrapped->type();
$throwedby = $throwedby_wrapped->value();
$throwedby_type = $throwedby_wrapped->type();
$item = pool_item_create($throwedin, $throwedin_type, $throwedby, $throwedby_type);
if (!$throwedin_revisioning) {
$item->throwedin_vid = NULL;
}
if (!$throwedby_revisioning) {
$item->throwedby_vid = NULL;
}
pool_throwin($pool, $item);
}
/**
* Rules action to pull out an entity from a certain pool.
*/
function pool_rules_pullout_action($pool, $throwedin_wrapped, $throwedin_type, $throwedin_revisioning, $throwedby_wrapped, $throwedby_type, $throwedby_revisioning, $throwback, $settings) {
$return = array('throwedin' => NULL, 'throwedby' => NULL);
$item = _pool_rules_pullout($pool, $throwedin_wrapped, $throwedin_type, $throwedin_revisioning, $throwedby_wrapped, $throwedby_type, $throwedby_revisioning);
if ($item) {
$throwedin = entity_load($item->throwedin_type, array($item->throwedin_id));
$throwedby = entity_load($item->throwedby_type, array($item->throwedby_id));
if (!empty($throwedin)) {
$throwedin = reset($throwedin);
$throwedin_type = $item->throwedin_type;
$return['throwedin'] = $throwedin;
// Provide a wrapper instead of the entity
// when type hasn't been specified before.
if (!isset($settings['throwedin_type'])) {
$return['throwedin'] = entity_metadata_wrapper($throwedin_type, $throwedin);
}
}
if (!empty($throwedby)) {
$throwedby = reset($throwedby);
$throwedby_type = $item->throwedby_type;
$return['throwedby'] = $throwedby;
if (!isset($settings['throwedby_type'])) {
$return['throwedby'] = entity_metadata_wrapper($throwedby_type, $throwedby);
}
}
switch ($throwback) {
case 'no_throwback':
pool_item_delete($item->itemid);
break;
case 'no_throwback_all':
// The throwed in entity will be constantly pulled out
// of the pool, no matter who or what else threw it in.
while (TRUE) {
$item = pool_item_create($throwedin, $throwedin_type);
if (!$throwedin_revisioning) {
$item->throwedin_vid = NULL;
}
if (!$throwedby_revisioning) {
$item->throwedby_vid = NULL;
}
$item = pool_pullout($pool, $item);
if ($item) {
pool_item_delete($item->itemid);
}
else {
break;
}
}
break;
}
}
return $return;
}
/**
* Info alteration callback for the pool_pullout action.
*/
function pool_rules_pullout_action_info_alter(&$element_info, RulesAbstractPlugin $element) {
if (isset($element->settings['throwedin_type'])) {
$element_info['provides']['throwedin']['type'] = $element->settings['throwedin_type'];
}
if (isset($element->settings['throwedby_type'])) {
$element_info['provides']['throwedby']['type'] = $element->settings['throwedby_type'];
}
}
/**
* Options list callback for the Rules throwback option.
*/
function pool_rules_throwback_options() {
return array(
'throwback' => t('Throwback'),
'no_throwback' => t("Current thrower won't throw entity back"),
'no_throwback_all' => t('Keep entity outside the pool'),
);
}
/**
* Options list callback for the Rules throwedin_type and throwedby_type option.
*/
function pool_rules_entitytype_options($element, $name = NULL) {
$options = array('entity' => t('-Not specified-'));
$options += rules_entity_action_type_options($element, $name);
return $options;
}
/**
* Helper function to pull out a pool item with Rules-provided parameters.
*/
function _pool_rules_pullout($pool, $throwedin_wrapped, $throwedin_type, $throwedin_revisioning, $throwedby_wrapped, $throwedby_type, $throwedby_revisioning) {
// Get the entity objects from wrappers.
$throwedin = isset($throwedin_wrapped) ? $throwedin_wrapped->value() : NULL;
$throwedby = isset($throwedby_wrapped) ? $throwedby_wrapped->value() : NULL;
// Set the proper type values when entities are defined as arguments.
$throwedin_type = isset($throwedin_wrapped) ? $throwedin_wrapped->type() : $throwedin_type;
$throwedby_type = isset($throwedby_wrapped) ? $throwedby_wrapped->type() : $throwedby_type;
// Fix: When entity type is not specified, the value is set to 'entity' for Rules.
// When entity is not specified in pool module, it should be set to NULL.
$throwedin_type = $throwedin_type != 'entity' ? $throwedin_type : NULL;
$throwedby_type = $throwedby_type != 'entity' ? $throwedby_type : NULL;
$item = pool_item_create($throwedin, $throwedin_type, $throwedby, $throwedby_type);
if (!$throwedin_revisioning) {
$item->throwedin_vid = NULL;
}
if (!$throwedby_revisioning) {
$item->throwedby_vid = NULL;
}
return pool_pullout($pool, $item);
}