php.rules.inc 4.5 KB

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