1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file Contains classes for data processing.
|
5 |
|
|
*
|
6 |
|
|
* Data processors can be used to process element arguments on evaluation time,
|
7 |
|
|
* e.g. to apply input evaluators or to apply simple calculations to number
|
8 |
|
|
* arguments.
|
9 |
|
|
*/
|
10 |
|
|
|
11 |
|
|
/**
|
12 |
|
|
* Common base class for Rules data processors.
|
13 |
|
|
*/
|
14 |
|
|
abstract class RulesDataProcessor {
|
15 |
|
|
|
16 |
|
|
/**
|
17 |
|
|
* The processors' setting value.
|
18 |
|
|
*/
|
19 |
|
|
protected $setting = NULL;
|
20 |
|
|
|
21 |
|
|
/**
|
22 |
|
|
* Allows chaining processors. If set, the next processor to invoke.
|
23 |
|
|
*/
|
24 |
|
|
protected $processor = NULL;
|
25 |
|
|
|
26 |
|
|
/**
|
27 |
|
|
* Constructor.
|
28 |
|
|
*/
|
29 |
|
|
protected function __construct($setting, $param_info, $var_info = array(), $processor = NULL) {
|
30 |
|
|
$this->setting = $setting;
|
31 |
|
|
$this->processor = $processor;
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
/**
|
35 |
|
|
* Return $this or skip this processor by returning the next processor.
|
36 |
|
|
*/
|
37 |
|
|
protected function getPreparedValue() {
|
38 |
|
|
return isset($this->setting) && array_filter($this->setting) ? $this : $this->processor;
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
/**
|
42 |
|
|
* Returns whether the current user has permission to edit this chain of data
|
43 |
|
|
* processors.
|
44 |
|
|
*/
|
45 |
|
|
public function editAccess() {
|
46 |
|
|
return $this->access() && (!isset($this->processor) || $this->processor->editAccess());
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
/**
|
51 |
|
|
* Prepares the processor for parameters.
|
52 |
|
|
*
|
53 |
|
|
* It turns the settings into a suiting processor object, which gets invoked
|
54 |
|
|
* on evaluation time.
|
55 |
|
|
*
|
56 |
|
|
* @param $setting
|
57 |
|
|
* The processor settings which are to be prepared.
|
58 |
|
|
* @param $param_info
|
59 |
|
|
* The info about the parameter to prepare the processor for.
|
60 |
|
|
* @param $var_info
|
61 |
|
|
* An array of info about the available variables.
|
62 |
|
|
*/
|
63 |
|
|
public static function prepareSetting(&$setting, $param_info, $var_info = array()) {
|
64 |
|
|
$processor = NULL;
|
65 |
|
|
foreach (self::processors($param_info, FALSE) as $name => $info) {
|
66 |
|
|
if (!empty($setting[$name])) {
|
67 |
|
|
$object = new $info['class']($setting[$name], $param_info, $var_info, $processor);
|
68 |
|
|
$processor = $object->getPreparedValue();
|
69 |
|
|
}
|
70 |
|
|
}
|
71 |
|
|
$setting = $processor;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
/**
|
75 |
|
|
* Attaches the form of applicable data processors.
|
76 |
|
|
*/
|
77 |
|
|
public static function attachForm(&$form, $settings, $param_info, $var_info, $access_check = TRUE) {
|
78 |
|
|
// If $settings is already prepared get the settings from the processors.
|
79 |
|
|
if ($settings instanceof RulesDataProcessor) {
|
80 |
|
|
$settings = $settings->getChainSettings();
|
81 |
|
|
}
|
82 |
|
|
foreach (self::processors($param_info, $access_check) as $name => $info) {
|
83 |
|
|
$settings += array($name => array());
|
84 |
|
|
$form[$name] = call_user_func(array($info['class'], 'form'), $settings[$name], $var_info);
|
85 |
|
|
$form[$name]['#weight'] = $info['weight'];
|
86 |
|
|
}
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
/**
|
90 |
|
|
* Returns defined data processors applicable for the given parameter.
|
91 |
|
|
* Optionally also access to the processors is checked.
|
92 |
|
|
*
|
93 |
|
|
* @param $param_info
|
94 |
|
|
* If given, only processors valid for this parameter are returned.
|
95 |
|
|
*/
|
96 |
|
|
public static function processors($param_info = NULL, $access_check = TRUE, $hook = 'data_processor_info') {
|
97 |
|
|
static $items = array();
|
98 |
|
|
|
99 |
|
|
if (!isset($items[$hook]['all'])) {
|
100 |
|
|
$items[$hook]['all'] = rules_fetch_data($hook);
|
101 |
|
|
uasort($items[$hook]['all'], array(__CLASS__, '_item_sort'));
|
102 |
|
|
}
|
103 |
|
|
// Data processing isn't supported for multiple types.
|
104 |
|
|
if (isset($param_info) && is_array($param_info['type'])) {
|
105 |
|
|
return array();
|
106 |
|
|
}
|
107 |
|
|
// Filter the items by type.
|
108 |
|
|
if (isset($param_info['type']) && !isset($items[$hook][$param_info['type']])) {
|
109 |
|
|
$items[$hook][$param_info['type']] = array();
|
110 |
|
|
foreach ($items[$hook]['all'] as $name => $info) {
|
111 |
|
|
// Check whether the parameter type matches the supported types.
|
112 |
|
|
$info += array('type' => 'text');
|
113 |
|
|
if (RulesData::typesMatch($param_info, $info, FALSE)) {
|
114 |
|
|
$items[$hook][$param_info['type']][$name] = $info;
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
// Apply the access check.
|
119 |
|
|
$return = isset($param_info['type']) ? $items[$hook][$param_info['type']] : $items[$hook]['all'];
|
120 |
|
|
if ($access_check) {
|
121 |
|
|
foreach ($return as $base => $info) {
|
122 |
|
|
if (!call_user_func(array($info['class'], 'access'))) {
|
123 |
|
|
unset($return[$base]);
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
return $return;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
public static function _item_sort($a, $b) {
|
131 |
|
|
return $a['weight'] < $b['weight'] ? -1 : ($a['weight'] > $b['weight'] ? 1 : 0);
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
/**
|
135 |
|
|
* Gets the settings array for this and all contained chained processors.
|
136 |
|
|
*/
|
137 |
|
|
public function getChainSettings() {
|
138 |
|
|
foreach ($this->unchain() as $name => $processor) {
|
139 |
|
|
$settings[$name] = $processor->getSetting();
|
140 |
|
|
}
|
141 |
|
|
return isset($settings) ? $settings : array();
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
/**
|
145 |
|
|
* Returns an array of modules which we depend on.
|
146 |
|
|
*/
|
147 |
|
|
public function dependencies() {
|
148 |
|
|
$used_processor_info = array_intersect_key($this->processors(), $this->unchain());
|
149 |
|
|
$modules = array();
|
150 |
|
|
foreach ($used_processor_info as $name => $info) {
|
151 |
|
|
$modules[] = $info['module'];
|
152 |
|
|
}
|
153 |
|
|
return array_filter($modules);
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
/**
|
157 |
|
|
* @return
|
158 |
|
|
* An array of processors keyed by processor name.
|
159 |
|
|
*/
|
160 |
|
|
protected function unchain() {
|
161 |
|
|
$processor = $this;
|
162 |
|
|
while ($processor instanceof RulesDataProcessor) {
|
163 |
|
|
$processors[get_class($processor)] = $processor;
|
164 |
|
|
$processor = $processor->processor;
|
165 |
|
|
}
|
166 |
|
|
// Note: Don't use the static context to call processors() here as we need a
|
167 |
|
|
// late binding to invoke the input evaluators version, if needed.
|
168 |
|
|
$return = array();
|
169 |
|
|
foreach ($this->processors() as $name => $info) {
|
170 |
|
|
if (isset($processors[$info['class']])) {
|
171 |
|
|
$return[$name] = $processors[$info['class']];
|
172 |
|
|
}
|
173 |
|
|
}
|
174 |
|
|
return $return;
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
/**
|
178 |
|
|
* Gets the settings of this processor.
|
179 |
|
|
*/
|
180 |
|
|
public function getSetting() {
|
181 |
|
|
return $this->setting;
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
/**
|
185 |
|
|
* Processes the value. If $this->processor is set, invoke this processor
|
186 |
|
|
* first so chaining multiple processors is working.
|
187 |
|
|
*
|
188 |
|
|
* @param $value
|
189 |
|
|
* The value to process.
|
190 |
|
|
* @param $info
|
191 |
|
|
* Info about the parameter for which we process the value.
|
192 |
|
|
* @param $state RulesState
|
193 |
|
|
* The rules evaluation state.
|
194 |
|
|
* @param $element RulesPlugin
|
195 |
|
|
* The element for which we process the value.
|
196 |
|
|
* @return
|
197 |
|
|
* The processed value.
|
198 |
|
|
*/
|
199 |
|
|
abstract public function process($value, $info, RulesState $state, RulesPlugin $element);
|
200 |
|
|
|
201 |
|
|
/**
|
202 |
|
|
* Return whether the current user has permission to use the processor.
|
203 |
|
|
*/
|
204 |
|
|
public static function access() {
|
205 |
|
|
return TRUE;
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
/**
|
209 |
|
|
* Defines the processor form element.
|
210 |
|
|
*
|
211 |
|
|
* @param $settings
|
212 |
|
|
* The settings of the processor.
|
213 |
|
|
* @param $var_info
|
214 |
|
|
* An array of info about the available variables.
|
215 |
|
|
*
|
216 |
|
|
* @return
|
217 |
|
|
* A form element structure.
|
218 |
|
|
*/
|
219 |
|
|
protected static function form($settings, $var_info) {
|
220 |
|
|
return array();
|
221 |
|
|
}
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
|
225 |
|
|
/**
|
226 |
|
|
* A base processor for use as input evaluators. Input evaluators are not listed
|
227 |
|
|
* in hook_rules_data_processor_info(). Instead they use
|
228 |
|
|
* hook_rules_evaluator_info() and get attached to input forms.
|
229 |
|
|
*/
|
230 |
|
|
abstract class RulesDataInputEvaluator extends RulesDataProcessor {
|
231 |
|
|
|
232 |
|
|
/**
|
233 |
|
|
* Overridden to invoke prepare().
|
234 |
|
|
*/
|
235 |
|
|
protected function __construct($setting, $param_info, $var_info = array(), $processor = NULL) {
|
236 |
|
|
$this->setting = TRUE;
|
237 |
|
|
$this->processor = $processor;
|
238 |
|
|
$this->prepare($setting, $var_info, $param_info);
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
/**
|
242 |
|
|
* Overridden to generate evaluator $options and invoke evaluate().
|
243 |
|
|
*/
|
244 |
|
|
public function process($value, $info, RulesState $state, RulesPlugin $element, $options = NULL) {
|
245 |
|
|
$options = isset($options) ? $options : $this->getEvaluatorOptions($info, $state, $element);
|
246 |
|
|
$value = isset($this->processor) ? $this->processor->process($value, $info, $state, $element, $options) : $value;
|
247 |
|
|
return $this->evaluate($value, $options, $state);
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
/**
|
251 |
|
|
* Generates the evaluator $options.
|
252 |
|
|
*/
|
253 |
|
|
protected function getEvaluatorOptions($info, $state, $element) {
|
254 |
|
|
$cache = rules_get_cache();
|
255 |
|
|
$languages = language_list();
|
256 |
|
|
$info += array(
|
257 |
|
|
'cleaning callback' => isset($cache['data info'][$info['type']]['cleaning callback']) ? $cache['data info'][$info['type']]['cleaning callback'] : FALSE,
|
258 |
|
|
'sanitize' => FALSE,
|
259 |
|
|
);
|
260 |
|
|
$options = array_filter(array(
|
261 |
|
|
'language' => $info['#langcode'] != LANGUAGE_NONE && isset($languages[$info['#langcode']]) ? $languages[$info['#langcode']] : NULL,
|
262 |
|
|
'callback' => $info['cleaning callback'],
|
263 |
|
|
'sanitize' => $info['sanitize'],
|
264 |
|
|
));
|
265 |
|
|
return $options;
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
/**
|
269 |
|
|
* Overriden to prepare input evaluator processors. The setting is expected
|
270 |
|
|
* to be the input value to be evaluated later on and is replaced by the
|
271 |
|
|
* suiting processor.
|
272 |
|
|
*/
|
273 |
|
|
public static function prepareSetting(&$setting, $param_info, $var_info = array()) {
|
274 |
|
|
$processor = NULL;
|
275 |
|
|
foreach (self::evaluators($param_info, FALSE) as $name => $info) {
|
276 |
|
|
$object = new $info['class']($setting, $param_info, $var_info, $processor);
|
277 |
|
|
$processor = $object->getPreparedValue();
|
278 |
|
|
}
|
279 |
|
|
$setting = $processor;
|
280 |
|
|
}
|
281 |
|
|
|
282 |
|
|
protected function getPreparedValue() {
|
283 |
|
|
return isset($this->setting) ? $this : $this->processor;
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
/**
|
287 |
|
|
* Overriden to just attach the help() of evaluators.
|
288 |
|
|
*/
|
289 |
|
|
public static function attachForm(&$form, $settings, $param_info, $var_info, $access_check = TRUE) {
|
290 |
|
|
foreach (self::evaluators($param_info, $access_check) as $name => $info) {
|
291 |
|
|
$form['help'][$name] = call_user_func(array($info['class'], 'help'), $var_info, $param_info);
|
292 |
|
|
$form['help'][$name]['#weight'] = $info['weight'];
|
293 |
|
|
}
|
294 |
|
|
}
|
295 |
|
|
|
296 |
|
|
/**
|
297 |
|
|
* Returns all input evaluators that can be applied to the parameters needed
|
298 |
|
|
* type.
|
299 |
|
|
*/
|
300 |
|
|
public static function evaluators($param_info = NULL, $access_check = TRUE) {
|
301 |
|
|
return parent::processors($param_info, $access_check, 'evaluator_info');
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
/**
|
305 |
|
|
* Overridden to default to our hook, thus being equivalent to
|
306 |
|
|
* self::evaluators().
|
307 |
|
|
*/
|
308 |
|
|
public static function processors($param_info = NULL, $access_check = TRUE, $hook = 'evaluator_info') {
|
309 |
|
|
return parent::processors($param_info, $access_check, $hook);
|
310 |
|
|
}
|
311 |
|
|
|
312 |
|
|
/**
|
313 |
|
|
* Prepares the evalution, e.g. to determine whether the input evaluator has
|
314 |
|
|
* been used. If this evaluator should be skipped just unset $this->setting.
|
315 |
|
|
*
|
316 |
|
|
* @param $text
|
317 |
|
|
* The text to evaluate later on.
|
318 |
|
|
* @param $variables
|
319 |
|
|
* An array of info about available variables.
|
320 |
|
|
* @param $param_info
|
321 |
|
|
* (optional) An array of information about the handled parameter value.
|
322 |
|
|
* For backward compatibility, this parameter is not required.
|
323 |
|
|
*/
|
324 |
|
|
abstract public function prepare($text, $variables);
|
325 |
|
|
|
326 |
|
|
/**
|
327 |
|
|
* Apply the input evaluator.
|
328 |
|
|
*
|
329 |
|
|
* @param $text
|
330 |
|
|
* The text to evaluate.
|
331 |
|
|
* @param $options
|
332 |
|
|
* A keyed array of settings and flags to control the processing.
|
333 |
|
|
* Supported options are:
|
334 |
|
|
* - language: A language object to be used when processing.
|
335 |
|
|
* - callback: A callback function that will be used to post-process
|
336 |
|
|
* replacements that might be incorporated, so they can be cleaned in a
|
337 |
|
|
* certain way.
|
338 |
|
|
* - sanitize: A boolean flag indicating whether incorporated replacements
|
339 |
|
|
* should be sanitized.
|
340 |
|
|
* @param RulesState
|
341 |
|
|
* The rules evaluation state.
|
342 |
|
|
*
|
343 |
|
|
* @return
|
344 |
|
|
* The evaluated text.
|
345 |
|
|
*/
|
346 |
|
|
abstract public function evaluate($text, $options, RulesState $state);
|
347 |
|
|
|
348 |
|
|
/**
|
349 |
|
|
* Provide some usage help for the evaluator.
|
350 |
|
|
*
|
351 |
|
|
* @param $variables
|
352 |
|
|
* An array of info about available variables.
|
353 |
|
|
* @param $param_info
|
354 |
|
|
* (optional) An array of information about the handled parameter value.
|
355 |
|
|
* For backward compatibility, this parameter is not required.
|
356 |
|
|
*
|
357 |
|
|
* @return
|
358 |
|
|
* A renderable array.
|
359 |
|
|
*/
|
360 |
|
|
public static function help($variables) {
|
361 |
|
|
return array();
|
362 |
|
|
}
|
363 |
|
|
|
364 |
|
|
} |