php.rules.inc 4.5 KB

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