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