1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file rules integration for the php module
|
5
|
*
|
6
|
* @addtogroup rules
|
7
|
* @{
|
8
|
*/
|
9
|
|
10
|
/**
|
11
|
* Implements hook_rules_file_info() on behalf of the php module.
|
12
|
*/
|
13
|
function rules_php_file_info() {
|
14
|
return array('modules/php.eval');
|
15
|
}
|
16
|
|
17
|
/**
|
18
|
* Implements hook_rules_evaluator_info() on behalf of the php module.
|
19
|
*/
|
20
|
function rules_php_evaluator_info() {
|
21
|
return array(
|
22
|
'php' => array(
|
23
|
'class' => 'RulesPHPEvaluator',
|
24
|
'type' => array('text', 'uri'),
|
25
|
'weight' => -10,
|
26
|
'module' => 'php',
|
27
|
),
|
28
|
);
|
29
|
}
|
30
|
|
31
|
/**
|
32
|
* Implements hook_rules_data_processor_info() on behalf of the php module.
|
33
|
*/
|
34
|
function rules_php_data_processor_info() {
|
35
|
return array(
|
36
|
'php' => array(
|
37
|
'class' => 'RulesPHPDataProcessor',
|
38
|
'type' => array('text', 'token', 'decimal', 'integer', 'date', 'duration', 'boolean', 'uri'),
|
39
|
'weight' => 10,
|
40
|
'module' => 'php',
|
41
|
),
|
42
|
);
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* Implements hook_rules_action_info() on behalf of the php module.
|
47
|
*/
|
48
|
function rules_php_action_info() {
|
49
|
return array(
|
50
|
'php_eval' => array(
|
51
|
'label' => t('Execute custom PHP code'),
|
52
|
'group' => t('PHP'),
|
53
|
'parameter' => array(
|
54
|
'code' => array(
|
55
|
'restriction' => 'input',
|
56
|
'type' => 'text',
|
57
|
'label' => t('PHP code'),
|
58
|
'description' => t('Enter PHP code without <?php ?> delimiters.'),
|
59
|
),
|
60
|
),
|
61
|
'base' => 'rules_execute_php_eval',
|
62
|
'access callback' => 'rules_php_integration_access',
|
63
|
),
|
64
|
);
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Alter the form for improved UX.
|
69
|
*/
|
70
|
function rules_execute_php_eval_form_alter(&$form, &$form_state) {
|
71
|
// Remove the PHP evaluation help to avoid confusion whether <?php tags should
|
72
|
// be used. But keep the help about available variables.
|
73
|
$form['parameter']['code']['settings']['help']['php']['#type'] = 'container';
|
74
|
$form['parameter']['code']['settings']['help']['php']['top']['#markup'] = t('The following variables are available and may be used by your PHP code:');
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Process the settings to prepare code execution.
|
79
|
*/
|
80
|
function rules_execute_php_eval_process(RulesAbstractPlugin $element) {
|
81
|
$element->settings['used_vars'] = RulesPHPEvaluator::getUsedVars('<?' . $element->settings['code'], $element->availableVariables());
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* Specify the php module as dependency.
|
86
|
*/
|
87
|
function rules_execute_php_eval_dependencies() {
|
88
|
return array('php');
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* PHP integration access callback.
|
93
|
*/
|
94
|
function rules_php_integration_access() {
|
95
|
return user_access('use PHP for settings');
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Implements hook_rules_condition_info() on behalf of the PHP module.
|
100
|
*/
|
101
|
function rules_php_condition_info() {
|
102
|
return array(
|
103
|
'php_eval' => array(
|
104
|
'label' => t('Execute custom PHP code'),
|
105
|
'group' => t('PHP'),
|
106
|
'parameter' => array(
|
107
|
'code' => array(
|
108
|
'restriction' => 'input',
|
109
|
'type' => 'text',
|
110
|
'label' => t('PHP code'),
|
111
|
'description' => t('Enter PHP code without <?php ?> delimiters that returns a boolean value; e.g. <code>@code</code>.', array('@code' => "return arg(0) == 'node';")),
|
112
|
),
|
113
|
),
|
114
|
'base' => 'rules_execute_php_eval',
|
115
|
'access callback' => 'rules_php_integration_access',
|
116
|
),
|
117
|
);
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* Generates help for the PHP actions, conditions and input evaluator.
|
122
|
*/
|
123
|
function rules_php_evaluator_help($var_info, $action_help = FALSE) {
|
124
|
$render['top'] = array(
|
125
|
'#prefix' => '<p>',
|
126
|
'#suffix' => '</p>',
|
127
|
'#markup' => t('PHP code inside of <?php ?> delimiters will be evaluated and replaced by its output. E.g. <? echo 1+1?> will be replaced by 2.')
|
128
|
. ' ' . t('Furthermore you can make use of the following variables:'),
|
129
|
);
|
130
|
$render['vars'] = array(
|
131
|
'#theme' => 'table',
|
132
|
'#header' => array(t('Variable name'), t('Type'), t('Description')),
|
133
|
'#attributes' => array('class' => array('rules-php-help')),
|
134
|
);
|
135
|
|
136
|
$cache = rules_get_cache();
|
137
|
foreach ($var_info as $name => $info) {
|
138
|
$row = array();
|
139
|
$row[] = '$'. check_plain($name);
|
140
|
$label = isset($cache['data_info'][$info['type']]['label']) ? $cache['data_info'][$info['type']]['label'] : $info['type'];
|
141
|
$row[] = check_plain(drupal_ucfirst($label));
|
142
|
$row[] = check_plain($info['label']);
|
143
|
$render['vars']['#rows'][] = $row;
|
144
|
}
|
145
|
|
146
|
if ($action_help) {
|
147
|
$render['updated_help'] = array(
|
148
|
'#prefix' => '<p>',
|
149
|
'#suffix' => '</p>',
|
150
|
'#markup' => t("If you want to change a variable just return an array of new variable values, e.g.: !code", array('!code' => '<pre>return array("node" => $node);</pre>')),
|
151
|
);
|
152
|
}
|
153
|
return $render;
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* @}
|
158
|
*/
|