1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* General data related rules integration.
|
6
|
*
|
7
|
* @addtogroup rules
|
8
|
*
|
9
|
* @{
|
10
|
*/
|
11
|
|
12
|
/**
|
13
|
* Implements hook_rules_category_info() on behalf of the pseudo data module.
|
14
|
*/
|
15
|
function rules_data_category_info() {
|
16
|
return array(
|
17
|
'rules_data' => array(
|
18
|
'label' => t('Data'),
|
19
|
'equals group' => t('Data'),
|
20
|
'weight' => -50,
|
21
|
),
|
22
|
);
|
23
|
}
|
24
|
|
25
|
/**
|
26
|
* Implements hook_rules_file_info() on behalf of the pseudo data module.
|
27
|
*
|
28
|
* @see rules_core_modules()
|
29
|
*/
|
30
|
function rules_data_file_info() {
|
31
|
return array('modules/data.eval');
|
32
|
}
|
33
|
|
34
|
/**
|
35
|
* Implements hook_rules_action_info() on behalf of the pseudo data module.
|
36
|
*
|
37
|
* @see rules_core_modules()
|
38
|
*/
|
39
|
function rules_data_action_info() {
|
40
|
$return['data_set'] = array(
|
41
|
'label' => t('Set a data value'),
|
42
|
'parameter' => array(
|
43
|
'data' => array(
|
44
|
'type' => '*',
|
45
|
'label' => t('Data'),
|
46
|
'description' => t('Specifies the data to be modified using a data selector, e.g. "node:author:name".'),
|
47
|
'restriction' => 'selector',
|
48
|
'wrapped' => TRUE,
|
49
|
'allow null' => TRUE,
|
50
|
),
|
51
|
'value' => array(
|
52
|
'type' => '*',
|
53
|
'label' => t('Value'),
|
54
|
'description' => t('The new value to set for the specified data.'),
|
55
|
'allow null' => TRUE,
|
56
|
'optional' => TRUE,
|
57
|
),
|
58
|
),
|
59
|
'group' => t('Data'),
|
60
|
'base' => 'rules_action_data_set',
|
61
|
);
|
62
|
$return['data_calc'] = array(
|
63
|
'label' => t('Calculate a value'),
|
64
|
'parameter' => array(
|
65
|
'input_1' => array(
|
66
|
'type' => array('decimal', 'date'),
|
67
|
'label' => t('Input value 1'),
|
68
|
'description' => t('The first input value for the calculation.'),
|
69
|
),
|
70
|
'op' => array(
|
71
|
'type' => 'text',
|
72
|
'label' => t('Operator'),
|
73
|
'description' => t('The calculation operator.'),
|
74
|
'options list' => 'rules_action_data_calc_operator_options',
|
75
|
'restriction' => 'input',
|
76
|
'default value' => '+',
|
77
|
),
|
78
|
'input_2' => array(
|
79
|
'type' => 'decimal',
|
80
|
'label' => t('Input value 2'),
|
81
|
'description' => t('The second input value.'),
|
82
|
),
|
83
|
),
|
84
|
'group' => t('Data'),
|
85
|
'base' => 'rules_action_data_calc',
|
86
|
'provides' => array(
|
87
|
'result' => array(
|
88
|
'type' => 'unknown',
|
89
|
'label' => t('Calculation result'),
|
90
|
),
|
91
|
),
|
92
|
);
|
93
|
$return['list_add'] = array(
|
94
|
'label' => t('Add an item to a list'),
|
95
|
'parameter' => array(
|
96
|
'list' => array(
|
97
|
'type' => 'list',
|
98
|
'label' => t('List', array(), array('context' => 'data_types')),
|
99
|
'description' => t('The data list, to which an item is to be added.'),
|
100
|
'restriction' => 'selector',
|
101
|
'allow null' => TRUE,
|
102
|
'save' => TRUE,
|
103
|
),
|
104
|
'item' => array(
|
105
|
'type' => 'unknown',
|
106
|
'label' => t('Item to add'),
|
107
|
),
|
108
|
'unique' => array(
|
109
|
'type' => 'boolean',
|
110
|
'label' => t('Enforce uniqueness'),
|
111
|
'description' => t('Only add the item to the list if it is not yet contained.'),
|
112
|
'optional' => TRUE,
|
113
|
'default value' => FALSE,
|
114
|
),
|
115
|
'pos' => array(
|
116
|
'type' => 'text',
|
117
|
'label' => t('Insert position'),
|
118
|
'optional' => TRUE,
|
119
|
'default value' => 'end',
|
120
|
'options list' => 'rules_action_data_list_add_positions',
|
121
|
),
|
122
|
),
|
123
|
'group' => t('Data'),
|
124
|
'base' => 'rules_action_data_list_add',
|
125
|
'callbacks' => array(
|
126
|
'info_alter' => 'rules_data_list_info_alter',
|
127
|
'form_alter' => 'rules_data_list_form_alter',
|
128
|
),
|
129
|
);
|
130
|
$return['list_remove'] = array(
|
131
|
'label' => t('Remove an item from a list'),
|
132
|
'parameter' => array(
|
133
|
'list' => array(
|
134
|
'type' => 'list',
|
135
|
'label' => t('List', array(), array('context' => 'data_types')),
|
136
|
'description' => t('The data list for which an item is to be removed.'),
|
137
|
'restriction' => 'selector',
|
138
|
'save' => TRUE,
|
139
|
),
|
140
|
'item' => array(
|
141
|
'type' => 'unknown',
|
142
|
'label' => t('Item to remove'),
|
143
|
),
|
144
|
),
|
145
|
'group' => t('Data'),
|
146
|
'base' => 'rules_action_data_list_remove',
|
147
|
'callbacks' => array(
|
148
|
'info_alter' => 'rules_data_list_info_alter',
|
149
|
'form_alter' => 'rules_data_list_form_alter',
|
150
|
),
|
151
|
);
|
152
|
$return['variable_add'] = array(
|
153
|
'label' => t('Add a variable'),
|
154
|
'named parameter' => TRUE,
|
155
|
'parameter' => array(
|
156
|
'type' => array(
|
157
|
'type' => 'text',
|
158
|
'label' => t('Type'),
|
159
|
'options list' => 'rules_data_action_variable_add_options',
|
160
|
'description' => t('Specifies the type of the variable that should be added.'),
|
161
|
'restriction' => 'input',
|
162
|
),
|
163
|
'value' => array(
|
164
|
'type' => 'unknown',
|
165
|
'label' => t('Value'),
|
166
|
'optional' => TRUE,
|
167
|
'description' => t('Optionally, specify the initial value of the variable.'),
|
168
|
),
|
169
|
),
|
170
|
'provides' => array(
|
171
|
'variable_added' => array(
|
172
|
'type' => 'unknown',
|
173
|
'label' => t('Added variable'),
|
174
|
),
|
175
|
),
|
176
|
'group' => t('Data'),
|
177
|
'base' => 'rules_action_variable_add',
|
178
|
'callbacks' => array(
|
179
|
'form_alter' => 'rules_action_type_form_alter',
|
180
|
'validate' => 'rules_action_create_type_validate',
|
181
|
),
|
182
|
);
|
183
|
|
184
|
if (rules_data_action_data_create_options()) {
|
185
|
$return['data_create'] = array(
|
186
|
'label' => t('Create a data structure'),
|
187
|
'named parameter' => TRUE,
|
188
|
'parameter' => array(
|
189
|
'type' => array(
|
190
|
'type' => 'text',
|
191
|
'label' => t('Type'),
|
192
|
'options list' => 'rules_data_action_data_create_options',
|
193
|
'description' => t('Specifies the type of the data structure that should be created.'),
|
194
|
'restriction' => 'input',
|
195
|
),
|
196
|
// Further needed parameters depend on the type.
|
197
|
),
|
198
|
'provides' => array(
|
199
|
'data_created' => array(
|
200
|
'type' => 'unknown',
|
201
|
'label' => t('Created data'),
|
202
|
),
|
203
|
),
|
204
|
'group' => t('Data'),
|
205
|
'base' => 'rules_action_data_create',
|
206
|
'callbacks' => array(
|
207
|
'form_alter' => 'rules_action_type_form_alter',
|
208
|
'validate' => 'rules_action_create_type_validate',
|
209
|
),
|
210
|
);
|
211
|
}
|
212
|
$return['data_convert'] = array(
|
213
|
'label' => t('Convert data type'),
|
214
|
'parameter' => array(
|
215
|
'type' => array(
|
216
|
'type' => 'token',
|
217
|
'label' => t('Target type'),
|
218
|
'description' => t('The data type to convert a value to.'),
|
219
|
'options list' => 'rules_action_data_convert_types_options',
|
220
|
'restriction' => 'input',
|
221
|
),
|
222
|
'value' => array(
|
223
|
'type' => array('decimal', 'integer', 'text', 'token'),
|
224
|
'label' => t('Value to convert'),
|
225
|
'default mode' => 'selector',
|
226
|
),
|
227
|
),
|
228
|
'provides' => array(
|
229
|
'conversion_result' => array(
|
230
|
'type' => 'unknown',
|
231
|
'label' => t('Conversion result'),
|
232
|
),
|
233
|
),
|
234
|
'group' => t('Data'),
|
235
|
'base' => 'rules_action_data_convert',
|
236
|
'named parameter' => TRUE,
|
237
|
'callbacks' => array(
|
238
|
'form_alter' => 'rules_action_type_form_alter',
|
239
|
),
|
240
|
);
|
241
|
return $return;
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* Data conversation action: Options list callback for the target type.
|
246
|
*/
|
247
|
function rules_action_data_convert_types_options(RulesPlugin $element, $param_name) {
|
248
|
return array(
|
249
|
'decimal' => t('Decimal'),
|
250
|
'integer' => t('Integer'),
|
251
|
'text' => t('Text'),
|
252
|
'token' => t('Token'),
|
253
|
);
|
254
|
}
|
255
|
|
256
|
/**
|
257
|
* Data conversation action: Options list callback for rounding behavior.
|
258
|
*/
|
259
|
function rules_action_data_convert_rounding_behavior_options(RulesPlugin $element, $param_name) {
|
260
|
return array(
|
261
|
'down' => t('Always down (9.5 -> 9)'),
|
262
|
'round' => t('Round, half up (9.5 -> 10)'),
|
263
|
'up' => t('Always up (9.5 -> 10)'),
|
264
|
);
|
265
|
}
|
266
|
|
267
|
/**
|
268
|
* Customize access check for data set action.
|
269
|
*/
|
270
|
function rules_action_data_set_access(RulesAbstractPlugin $element) {
|
271
|
if (isset($element->settings['data:select']) && $wrapper = $element->applyDataSelector($element->settings['data:select'])) {
|
272
|
return $wrapper instanceof EntityMetadataWrapper && $wrapper->access('edit');
|
273
|
}
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* Custom validation callback for the data set action.
|
278
|
*/
|
279
|
function rules_action_data_set_validate(RulesAbstractPlugin $element) {
|
280
|
$element->settings += array('data:select' => NULL);
|
281
|
$info = $element->applyDataSelector($element->settings['data:select'])->info();
|
282
|
if (strpos($element->settings['data:select'], ':') !== FALSE && empty($info['setter callback'])) {
|
283
|
throw new RulesIntegrityException(t("The selected data property doesn't support writing."), array($element, 'parameter', 'data'));
|
284
|
}
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* Form alter callback for the data_set action.
|
289
|
*/
|
290
|
function rules_action_data_set_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
|
291
|
if (!empty($options['init']) && !isset($form_state['rules_element_step'])) {
|
292
|
$form['negate']['#access'] = FALSE;
|
293
|
unset($form['parameter']['value']);
|
294
|
unset($form['parameter']['language']);
|
295
|
$form['submit'] = array(
|
296
|
'#type' => 'submit',
|
297
|
'#value' => t('Continue'),
|
298
|
'#limit_validation_errors' => array(array('parameter', 'data')),
|
299
|
'#submit' => array('rules_form_submit_rebuild'),
|
300
|
);
|
301
|
$form_state['rules_element_step'] = 'data_value';
|
302
|
// Clear the parameter mode for the value parameter, so its gets the proper
|
303
|
// default value based upon the type of the the selected data on rebuild.
|
304
|
unset($form_state['parameter_mode']['value']);
|
305
|
}
|
306
|
else {
|
307
|
// Change the data parameter to be not editable.
|
308
|
$form['parameter']['data']['settings']['#access'] = FALSE;
|
309
|
// @todo Improve display.
|
310
|
$form['parameter']['data']['info'] = array(
|
311
|
'#prefix' => '<p>',
|
312
|
'#markup' => t('<strong>Selected data:</strong> %selector', array('%selector' => $element->settings['data:select'])),
|
313
|
'#suffix' => '</p>',
|
314
|
);
|
315
|
}
|
316
|
}
|
317
|
|
318
|
/**
|
319
|
* Form alter callback for the data calculation action.
|
320
|
*/
|
321
|
function rules_action_data_calc_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
|
322
|
|
323
|
$form['reload'] = array(
|
324
|
'#weight' => 5,
|
325
|
'#type' => 'submit',
|
326
|
'#name' => 'reload',
|
327
|
'#value' => t('Reload form'),
|
328
|
'#limit_validation_errors' => array(array('parameter', 'input_1')),
|
329
|
'#submit' => array('rules_form_submit_rebuild'),
|
330
|
'#ajax' => rules_ui_form_default_ajax(),
|
331
|
);
|
332
|
}
|
333
|
|
334
|
/**
|
335
|
* Validate callback for entity create, add variable and data create actions.
|
336
|
*/
|
337
|
function rules_action_create_type_validate($element) {
|
338
|
if (!isset($element->settings['type'])) {
|
339
|
throw new RulesIntegrityException(t('Invalid type specified.'), array($element, 'parameter', 'type'));
|
340
|
}
|
341
|
}
|
342
|
|
343
|
/**
|
344
|
* Form alter callback for the list add and remove actions.
|
345
|
*
|
346
|
* Use multiple steps to configure the action to update the item configuration
|
347
|
* form once we know the data type.
|
348
|
*
|
349
|
* @see rules_data_list_info_alter()
|
350
|
*/
|
351
|
function rules_data_list_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
|
352
|
if (!empty($options['init']) && !isset($form_state['rules_element_step'])) {
|
353
|
unset($form['parameter']['item'], $form['parameter']['pos']);
|
354
|
$form_state['rules_element_step'] = 1;
|
355
|
$form['negate']['#access'] = FALSE;
|
356
|
$form['parameter']['unique']['#access'] = FALSE;
|
357
|
$form['submit'] = array(
|
358
|
'#type' => 'submit',
|
359
|
'#value' => t('Continue'),
|
360
|
'#limit_validation_errors' => array(array('parameter', 'list')),
|
361
|
'#submit' => array('rules_form_submit_rebuild'),
|
362
|
);
|
363
|
}
|
364
|
else {
|
365
|
// Change the list parameter to be not editable any more.
|
366
|
$form['parameter']['list']['settings']['#access'] = FALSE;
|
367
|
$form['parameter']['list']['info'] = array(
|
368
|
'#prefix' => '<p>',
|
369
|
'#markup' => t('<strong>Selected list:</strong> %selector', array('%selector' => $element->settings['list:select'])),
|
370
|
'#suffix' => '</p>',
|
371
|
);
|
372
|
}
|
373
|
}
|
374
|
|
375
|
/**
|
376
|
* Form alter callback for actions relying on the entity type or the data type.
|
377
|
*/
|
378
|
function rules_action_type_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
|
379
|
$first_step = empty($element->settings['type']);
|
380
|
$form['reload'] = array(
|
381
|
'#weight' => 5,
|
382
|
'#type' => 'submit',
|
383
|
'#name' => 'reload',
|
384
|
'#value' => $first_step ? t('Continue') : t('Reload form'),
|
385
|
'#limit_validation_errors' => array(array('parameter', 'type')),
|
386
|
'#submit' => array('rules_action_type_form_submit_rebuild'),
|
387
|
'#ajax' => rules_ui_form_default_ajax(),
|
388
|
);
|
389
|
// Use ajax and trigger as the reload button.
|
390
|
$form['parameter']['type']['settings']['type']['#ajax'] = $form['reload']['#ajax'] + array(
|
391
|
'event' => 'change',
|
392
|
'trigger_as' => array('name' => 'reload'),
|
393
|
);
|
394
|
|
395
|
if ($first_step) {
|
396
|
// In the first step show only the type select.
|
397
|
foreach (element_children($form['parameter']) as $key) {
|
398
|
if ($key != 'type') {
|
399
|
unset($form['parameter'][$key]);
|
400
|
}
|
401
|
}
|
402
|
unset($form['submit']);
|
403
|
unset($form['provides']);
|
404
|
// Disable #ajax for the first step as it has troubles with lazy-loaded JS.
|
405
|
// @todo Re-enable once JS lazy-loading is fixed in core.
|
406
|
unset($form['parameter']['type']['settings']['type']['#ajax']);
|
407
|
unset($form['reload']['#ajax']);
|
408
|
}
|
409
|
else {
|
410
|
// Hide the reload button in case js is enabled and it's not the first step.
|
411
|
$form['reload']['#attributes'] = array('class' => array('rules-hide-js'));
|
412
|
}
|
413
|
}
|
414
|
|
415
|
/**
|
416
|
* FAPI submit callback for reloading the type form for entities or data types.
|
417
|
*/
|
418
|
function rules_action_type_form_submit_rebuild($form, &$form_state) {
|
419
|
rules_form_submit_rebuild($form, $form_state);
|
420
|
// Clear the parameter modes for the parameters, so they get the proper
|
421
|
// default values based upon the data types on rebuild.
|
422
|
$form_state['parameter_mode'] = array();
|
423
|
}
|
424
|
|
425
|
/**
|
426
|
* Options list callback for possible insertion positions.
|
427
|
*/
|
428
|
function rules_action_data_list_add_positions() {
|
429
|
return array(
|
430
|
'end' => t('Append the item to the end.'),
|
431
|
'start' => t('Prepend the item to the front.'),
|
432
|
);
|
433
|
}
|
434
|
|
435
|
/**
|
436
|
* Options list callback for variable add action.
|
437
|
*/
|
438
|
function rules_data_action_variable_add_options() {
|
439
|
return RulesPluginUI::getOptions('data');
|
440
|
}
|
441
|
|
442
|
/**
|
443
|
* Options list callback for the data calculation action.
|
444
|
*/
|
445
|
function rules_action_data_calc_operator_options(RulesPlugin $element, $param_name) {
|
446
|
$options = array(
|
447
|
'+' => '( + )',
|
448
|
'-' => '( - )',
|
449
|
'*' => '( * )',
|
450
|
'/' => '( / )',
|
451
|
'min' => 'min',
|
452
|
'max' => 'max',
|
453
|
);
|
454
|
// Only show +/- in case a date has been selected.
|
455
|
if (($info = $element->getArgumentInfo('input_1')) && $info['type'] == 'date') {
|
456
|
unset($options['*']);
|
457
|
unset($options['/']);
|
458
|
}
|
459
|
return $options;
|
460
|
}
|
461
|
|
462
|
/**
|
463
|
* Options list callback for data create action.
|
464
|
*/
|
465
|
function rules_data_action_data_create_options() {
|
466
|
$cache = rules_get_cache();
|
467
|
$data_info = $cache['data_info'];
|
468
|
$entity_info = entity_get_info();
|
469
|
// Remove entities.
|
470
|
$data_info = array_diff_key($data_info, $entity_info);
|
471
|
$options = array();
|
472
|
foreach ($data_info as $type => $properties) {
|
473
|
if (isset($properties['creation callback'])) {
|
474
|
// Add data types with creation callback only.
|
475
|
$options[$type] = $properties['label'];
|
476
|
}
|
477
|
}
|
478
|
natcasesort($options);
|
479
|
return $options;
|
480
|
}
|
481
|
|
482
|
/**
|
483
|
* Implements hook_rules_condition_info() on behalf of the pseudo data module.
|
484
|
*
|
485
|
* @see rules_core_modules()
|
486
|
*/
|
487
|
function rules_data_condition_info() {
|
488
|
return array(
|
489
|
'data_is' => array(
|
490
|
'label' => t('Data comparison'),
|
491
|
'parameter' => array(
|
492
|
'data' => array(
|
493
|
'type' => '*',
|
494
|
'label' => t('Data to compare'),
|
495
|
'description' => t('The data to be compared, specified by using a data selector, e.g. "node:author:name".'),
|
496
|
'allow null' => TRUE,
|
497
|
),
|
498
|
'op' => array(
|
499
|
'type' => 'text',
|
500
|
'label' => t('Operator'),
|
501
|
'description' => t('The comparison operator.'),
|
502
|
'optional' => TRUE,
|
503
|
'default value' => '==',
|
504
|
'options list' => 'rules_condition_data_is_operator_options',
|
505
|
'restriction' => 'input',
|
506
|
),
|
507
|
'value' => array(
|
508
|
'type' => '*',
|
509
|
'label' => t('Data value'),
|
510
|
'description' => t('The value to compare the data with.'),
|
511
|
'allow null' => TRUE,
|
512
|
),
|
513
|
),
|
514
|
'group' => t('Data'),
|
515
|
'base' => 'rules_condition_data_is',
|
516
|
),
|
517
|
'data_is_empty' => array(
|
518
|
'label' => t('Data value is empty'),
|
519
|
'parameter' => array(
|
520
|
'data' => array(
|
521
|
'type' => '*',
|
522
|
'label' => t('Data to check'),
|
523
|
'description' => t('The data to be checked to be empty, specified by using a data selector, e.g. "node:author:name".'),
|
524
|
'allow null' => TRUE,
|
525
|
'wrapped' => TRUE,
|
526
|
),
|
527
|
),
|
528
|
'group' => t('Data'),
|
529
|
'base' => 'rules_condition_data_is_empty',
|
530
|
),
|
531
|
'list_contains' => array(
|
532
|
'label' => t('List contains item'),
|
533
|
'parameter' => array(
|
534
|
'list' => array(
|
535
|
'type' => 'list',
|
536
|
'label' => t('List', array(), array('context' => 'data_types')),
|
537
|
'restriction' => 'selector',
|
538
|
),
|
539
|
'item' => array(
|
540
|
'type' => 'unknown',
|
541
|
'label' => t('Item'),
|
542
|
'description' => t('The item to check for.'),
|
543
|
),
|
544
|
),
|
545
|
'group' => t('Data'),
|
546
|
'base' => 'rules_condition_data_list_contains',
|
547
|
'callbacks' => array(
|
548
|
'info_alter' => 'rules_data_list_info_alter',
|
549
|
'form_alter' => 'rules_data_list_form_alter',
|
550
|
),
|
551
|
),
|
552
|
'list_count_is' => array(
|
553
|
'label' => t('List count comparison'),
|
554
|
'parameter' => array(
|
555
|
'list' => array(
|
556
|
'type' => 'list',
|
557
|
'label' => t('List to check'),
|
558
|
'description' => t('A multi value data element to have its count compared, specified by using a data selector, eg node:author:roles.'),
|
559
|
),
|
560
|
'op' => array(
|
561
|
'type' => 'text',
|
562
|
'label' => t('Operator'),
|
563
|
'description' => t('The comparison operator.'),
|
564
|
'optional' => TRUE,
|
565
|
'default value' => '==',
|
566
|
'options list' => 'rules_condition_data_list_count_is_operator_options',
|
567
|
'restriction' => 'input',
|
568
|
),
|
569
|
'value' => array(
|
570
|
'type' => 'integer',
|
571
|
'label' => t('Count'),
|
572
|
'description' => t('The count to compare the data count with.'),
|
573
|
),
|
574
|
),
|
575
|
'group' => t('Data'),
|
576
|
'base' => 'rules_condition_data_list_count_is',
|
577
|
),
|
578
|
'text_matches' => array(
|
579
|
'label' => t('Text comparison'),
|
580
|
'parameter' => array(
|
581
|
'text' => array(
|
582
|
'type' => 'text',
|
583
|
'label' => t('Text'),
|
584
|
'restriction' => 'selector',
|
585
|
),
|
586
|
'match' => array(
|
587
|
'type' => 'text',
|
588
|
'label' => t('Matching text'),
|
589
|
),
|
590
|
'operation' => array(
|
591
|
'type' => 'text',
|
592
|
'label' => t('Comparison operation'),
|
593
|
'options list' => 'rules_data_text_comparison_operation_list',
|
594
|
'restriction' => 'input',
|
595
|
'default value' => 'contains',
|
596
|
'optional' => TRUE,
|
597
|
'description' => t('In case the comparison operation @regex is selected, the matching pattern will be interpreted as a <a href="@regex-wikipedia">regular expression</a>. Tip: <a href="@RegExr">RegExr: Online Regular Expression Testing Tool</a> is helpful for learning, writing, and testing Regular Expressions.', array('@regex-wikipedia' => 'http://en.wikipedia.org/wiki/Regular_expression', '@RegExr' => 'http://gskinner.com/RegExr/', '@regex' => t('regular expression'))),
|
598
|
),
|
599
|
),
|
600
|
'group' => t('Data'),
|
601
|
'base' => 'rules_data_text_comparison',
|
602
|
),
|
603
|
);
|
604
|
}
|
605
|
|
606
|
/**
|
607
|
* Asserts the bundle of entities, if it's compared.
|
608
|
*
|
609
|
* If the bundle is compared, add the metadata assertion so other elements
|
610
|
* can make use of properties specific to the bundle.
|
611
|
*/
|
612
|
function rules_condition_data_is_assertions($element) {
|
613
|
// Assert the bundle of entities, if it's compared.
|
614
|
if ($wrapper = $element->applyDataSelector($element->settings['data:select'])) {
|
615
|
$info = $wrapper->info();
|
616
|
if (isset($info['parent']) && $info['parent'] instanceof EntityDrupalWrapper) {
|
617
|
$entity_info = $info['parent']->entityInfo();
|
618
|
if (isset($entity_info['entity keys']['bundle']) && $entity_info['entity keys']['bundle'] == $info['name']) {
|
619
|
// Assert that the entity is of bundle $value.
|
620
|
$value = is_array($element->settings['value']) ? $element->settings['value'] : array($element->settings['value']);
|
621
|
// Chop of the last part of the selector.
|
622
|
$parts = explode(':', $element->settings['data:select'], -1);
|
623
|
return array(implode(':', $parts) => array('bundle' => $value));
|
624
|
}
|
625
|
}
|
626
|
}
|
627
|
}
|
628
|
|
629
|
/**
|
630
|
* Form alter callback for the condition data_is.
|
631
|
*
|
632
|
* Use multiple steps to configure the condition as the needed type of the value
|
633
|
* depends on the selected data.
|
634
|
*/
|
635
|
function rules_condition_data_is_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
|
636
|
if (!empty($options['init']) && !isset($form_state['rules_element_step'])) {
|
637
|
unset($form['parameter']['op'], $form['parameter']['value']);
|
638
|
$form['negate']['#access'] = FALSE;
|
639
|
$form_state['rules_element_step'] = 'data_value';
|
640
|
$form['submit'] = array(
|
641
|
'#type' => 'submit',
|
642
|
'#value' => t('Continue'),
|
643
|
'#limit_validation_errors' => array(array('parameter', 'data'), array('parameter', 'op')),
|
644
|
'#submit' => array('rules_form_submit_rebuild'),
|
645
|
);
|
646
|
// Clear the parameter mode for the value parameter, so its gets the proper
|
647
|
// default value based upon the type of the the selected data on rebuild.
|
648
|
unset($form_state['parameter_mode']['value']);
|
649
|
}
|
650
|
else {
|
651
|
// Change the data parameter to be not editable.
|
652
|
$form['parameter']['data']['settings']['#access'] = FALSE;
|
653
|
// @todo Improve display.
|
654
|
$form['parameter']['data']['info'] = array(
|
655
|
'#prefix' => '<p>',
|
656
|
'#markup' => t('<strong>Selected data:</strong> %selector', array('%selector' => $element->settings['data:select'])),
|
657
|
'#suffix' => '</p>',
|
658
|
);
|
659
|
|
660
|
// Limit the operations to what makes sense for the selected data type.
|
661
|
$info = $element->pluginParameterInfo();
|
662
|
$data_info = $info['value'];
|
663
|
if ($element->settings['op'] == 'IN') {
|
664
|
$data_info['type'] = entity_property_list_extract_type($data_info['type']);
|
665
|
}
|
666
|
|
667
|
if (!RulesData::typesMatch($data_info, array('type' => array('decimal', 'date')))) {
|
668
|
$options =& $form['parameter']['op']['settings']['op']['#options'];
|
669
|
unset($options['<'], $options['>']);
|
670
|
}
|
671
|
// Remove 'contains' if it is not selected, as it is deprecated by the
|
672
|
// text comparison condition.
|
673
|
if ($element->settings['op'] != 'contains') {
|
674
|
unset($form['parameter']['op']['settings']['op']['#options']['contains']);
|
675
|
}
|
676
|
|
677
|
// Auto-refresh the form if the operation is changed, so the input form
|
678
|
// changes in case "is one of" requires a list value.
|
679
|
$form['parameter']['op']['settings']['op']['#ajax'] = rules_ui_form_default_ajax() + array(
|
680
|
'trigger_as' => array('name' => 'reload'),
|
681
|
);
|
682
|
// Provide a reload button for non-JS users.
|
683
|
$form['reload'] = array(
|
684
|
'#type' => 'submit',
|
685
|
'#value' => t('Reload form'),
|
686
|
'#limit_validation_errors' => array(array('parameter', 'data'), array('parameter', 'op')),
|
687
|
'#submit' => array('rules_form_submit_rebuild'),
|
688
|
'#ajax' => rules_ui_form_default_ajax(),
|
689
|
'#weight' => 5,
|
690
|
);
|
691
|
// Hide the reload button in case JS is enabled.
|
692
|
$form['reload']['#attributes'] = array('class' => array('rules-hide-js'));
|
693
|
}
|
694
|
}
|
695
|
|
696
|
/**
|
697
|
* Provides configuration help for the data_is condition.
|
698
|
*/
|
699
|
function rules_condition_data_is_help() {
|
700
|
return array('#markup' => t('Compare two data values of the same type with each other.'));
|
701
|
}
|
702
|
|
703
|
/**
|
704
|
* Options list callback for condition data_is.
|
705
|
*/
|
706
|
function rules_condition_data_is_operator_options() {
|
707
|
return array(
|
708
|
'==' => t('equals'),
|
709
|
'IN' => t('is one of'),
|
710
|
'<' => t('is lower than'),
|
711
|
'>' => t('is greater than'),
|
712
|
// Note: This is deprecated by the text comparison condition.
|
713
|
'contains' => t('contains'),
|
714
|
);
|
715
|
}
|
716
|
|
717
|
/**
|
718
|
* Options list callback for condition text_matches.
|
719
|
*/
|
720
|
function rules_data_text_comparison_operation_list() {
|
721
|
return array(
|
722
|
'contains' => t('contains'),
|
723
|
'starts' => t('starts with'),
|
724
|
'ends' => t('ends with'),
|
725
|
'regex' => t('regular expression'),
|
726
|
);
|
727
|
}
|
728
|
|
729
|
/**
|
730
|
* Returns the options list as specified by the selected property of the first parameter.
|
731
|
*
|
732
|
* @see rules_data_list_info_alter()
|
733
|
* @see rules_action_data_set_info_alter()
|
734
|
* @see rules_condition_data_is_info_alter()
|
735
|
*/
|
736
|
function rules_data_selector_options_list(RulesAbstractPlugin $element) {
|
737
|
$name = rules_array_key($element->pluginParameterInfo());
|
738
|
// If the selected data property has an option list, make use of it.
|
739
|
if (isset($element->settings[$name . ':select']) && $wrapper = $element->applyDataSelector($element->settings[$name . ':select'])) {
|
740
|
return $wrapper->optionsList($element instanceof RulesActionInterface ? 'edit' : 'view');
|
741
|
}
|
742
|
}
|
743
|
|
744
|
/**
|
745
|
* Options list callback for condition list_count_is.
|
746
|
*/
|
747
|
function rules_condition_data_list_count_is_operator_options() {
|
748
|
return array(
|
749
|
'==' => t('equals'),
|
750
|
'<' => t('is lower than'),
|
751
|
'>' => t('is greater than'),
|
752
|
);
|
753
|
}
|
754
|
|
755
|
/**
|
756
|
* @}
|
757
|
*/
|