rules.processor.inc 12 KB

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