Projet

Général

Profil

Paste
Télécharger (4,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / modules / php.rules.inc @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Rules integration for the php module.
6
 *
7
 * @addtogroup rules
8
 *
9
 * @{
10
 */
11

    
12
/**
13
 * Implements hook_rules_file_info() on behalf of the php module.
14
 */
15
function rules_php_file_info() {
16
  return array('modules/php.eval');
17
}
18

    
19
/**
20
 * Implements hook_rules_evaluator_info() on behalf of the php module.
21
 */
22
function rules_php_evaluator_info() {
23
  return array(
24
    'php' => array(
25
      'class' => 'RulesPHPEvaluator',
26
      'type' => array('text', 'uri'),
27
      'weight' => -10,
28
      'module' => 'php',
29
    ),
30
  );
31
}
32

    
33
/**
34
 * Implements hook_rules_data_processor_info() on behalf of the php module.
35
 */
36
function rules_php_data_processor_info() {
37
  return array(
38
    'php' => array(
39
      'class' => 'RulesPHPDataProcessor',
40
      'type' => array('text', 'token', 'decimal', 'integer', 'date', 'duration', 'boolean', 'uri'),
41
      'weight' => 10,
42
      'module' => 'php',
43
    ),
44
  );
45
}
46

    
47
/**
48
 * Implements hook_rules_action_info() on behalf of the php module.
49
 */
50
function rules_php_action_info() {
51
  return array(
52
    'php_eval' => array(
53
      'label' => t('Execute custom PHP code'),
54
      'group' => t('PHP'),
55
      'parameter' => array(
56
        'code' => array(
57
          'restriction' => 'input',
58
          'type' => 'text',
59
          'label' => t('PHP code'),
60
          'description' => t('Enter PHP code without &lt;?php ?&gt; delimiters.'),
61
        ),
62
      ),
63
      'base' => 'rules_execute_php_eval',
64
      'access callback' => 'rules_php_integration_access',
65
    ),
66
  );
67
}
68

    
69
/**
70
 * Alter the form for improved UX.
71
 */
72
function rules_execute_php_eval_form_alter(&$form, &$form_state) {
73
  // Remove the PHP evaluation help to avoid confusion whether <?php tags should
74
  // be used. But keep the help about available variables.
75
  $form['parameter']['code']['settings']['help']['php']['#type'] = 'container';
76
  $form['parameter']['code']['settings']['help']['php']['top']['#markup'] = t('The following variables are available and may be used by your PHP code:');
77
}
78

    
79
/**
80
 * Process the settings to prepare code execution.
81
 */
82
function rules_execute_php_eval_process(RulesAbstractPlugin $element) {
83
  $element->settings['used_vars'] = RulesPHPEvaluator::getUsedVars('<?' . $element->settings['code'], $element->availableVariables());
84
}
85

    
86
/**
87
 * Specify the php module as dependency.
88
 */
89
function rules_execute_php_eval_dependencies() {
90
  return array('php');
91
}
92

    
93
/**
94
 * PHP integration access callback.
95
 */
96
function rules_php_integration_access() {
97
  return user_access('use PHP for settings');
98
}
99

    
100
/**
101
 * Implements hook_rules_condition_info() on behalf of the PHP module.
102
 */
103
function rules_php_condition_info() {
104
  return array(
105
    'php_eval' => array(
106
      'label' => t('Execute custom PHP code'),
107
      'group' => t('PHP'),
108
      'parameter' => array(
109
        'code' => array(
110
          'restriction' => 'input',
111
          'type' => 'text',
112
          'label' => t('PHP code'),
113
          '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';")),
114
        ),
115
      ),
116
      'base' => 'rules_execute_php_eval',
117
      'access callback' => 'rules_php_integration_access',
118
    ),
119
  );
120
}
121

    
122
/**
123
 * Generates help for the PHP actions, conditions and input evaluator.
124
 */
125
function rules_php_evaluator_help($var_info, $action_help = FALSE) {
126
  $render['top'] = array(
127
    '#prefix' => '<p>',
128
    '#suffix' => '</p>',
129
    '#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:'),
130
  );
131
  $render['vars'] = array(
132
    '#theme' => 'table',
133
    '#header' => array(t('Variable name'), t('Type'), t('Description')),
134
    '#attributes' => array('class' => array('rules-php-help')),
135
  );
136

    
137
  $cache = rules_get_cache();
138
  foreach ($var_info as $name => $info) {
139
    $row   = array();
140
    $row[] = '$' . check_plain($name);
141
    $label = isset($cache['data_info'][$info['type']]['label']) ? $cache['data_info'][$info['type']]['label'] : $info['type'];
142
    $row[] = check_plain(drupal_ucfirst($label));
143
    $row[] = check_plain($info['label']);
144
    $render['vars']['#rows'][] = $row;
145
  }
146

    
147
  if ($action_help) {
148
    $render['updated_help'] = array(
149
      '#prefix' => '<p>',
150
      '#suffix' => '</p>',
151
      '#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>')),
152
    );
153
  }
154
  return $render;
155
}
156

    
157
/**
158
 * @}
159
 */