Projet

Général

Profil

Paste
Télécharger (7,53 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / rules_scheduler / rules_scheduler.rules.inc @ 76e2e7c3

1
<?php
2

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

    
11
/**
12
 * Implements hook_rules_action_info().
13
 */
14
function rules_scheduler_rules_action_info() {
15
  $items['schedule'] = array(
16
    'label' => t('Schedule component evaluation'),
17
    'group' => t('Rules scheduler'),
18
    'base' => 'rules_scheduler_action_schedule',
19
    'named parameter' => TRUE,
20
    'parameter' => array(
21
      'component' => array(
22
        'type' => 'text',
23
        'label' => t('Component'),
24
        'options list' => 'rules_scheduler_component_options_list',
25
        'restriction' => 'input',
26
        'description' => 'Select the component to schedule. Only components containing actions are available – no condition sets.',
27
      ),
28
      'date' => array(
29
        'type' => 'date',
30
        'label' => t('Scheduled evaluation date'),
31
      ),
32
      'identifier' => array(
33
        'type' => 'text',
34
        'label' => t('Identifier'),
35
        'description' => t('A string used for identifying this task. Any existing tasks for this component with the same identifier will be replaced.'),
36
        'optional' => TRUE,
37
      ),
38
      // Further needed parameter by the component are added during processing.
39
    ),
40
  );
41
  // Add action to delete scheduled tasks.
42
  $items['schedule_delete'] = array(
43
    'label' => t('Delete scheduled tasks'),
44
    'group' => t('Rules scheduler'),
45
    'base' => 'rules_scheduler_action_delete',
46
    'parameter' => array(
47
      'component' => array(
48
        'type' => 'text',
49
        'label' => t('Component'),
50
        'options list' => 'rules_scheduler_component_options_list',
51
        'description' => t('The component for which scheduled tasks will be deleted.'),
52
        'optional' => TRUE,
53
      ),
54
      'task' => array(
55
        'type' => 'text',
56
        'label' => t('Task identifier'),
57
        'description' => t('All tasks that are annotated with the given identifier will be deleted.'),
58
        'optional' => TRUE,
59
      ),
60
    ),
61
  );
62
  return $items;
63
}
64

    
65
/**
66
 * Options list callback returning a list of action components.
67
 */
68
function rules_scheduler_component_options_list() {
69
  return rules_get_components(TRUE, 'action');
70
}
71

    
72
/**
73
 * Base action implementation for scheduling components.
74
 */
75
function rules_scheduler_action_schedule($args, $element) {
76
  $state = $args['state'];
77
  if ($component = rules_get_cache('comp_' . $args['component'])) {
78
    // Manually create a new evaluation state for scheduling the evaluation.
79
    $new_state = new RulesState();
80

    
81
    // Register all parameters as variables.
82
    foreach ($element->pluginParameterInfo() as $name => $info) {
83
      if (strpos($name, 'param_') === 0) {
84
        // Remove the parameter name prefix 'param_'.
85
        $var_name = substr($name, 6);
86
        $new_state->addVariable($var_name, $state->currentArguments[$name], $info);
87
      }
88
    }
89
    rules_scheduler_schedule_task(array(
90
      'date' => $args['date'],
91
      'config' => $args['component'],
92
      'data' => $new_state,
93
      'identifier' => $args['identifier'],
94
    ));
95
  }
96
  else {
97
    throw new RulesEvaluationException('Unable to get the component %name', array('%name' => $args['component']), $element, RulesLog::ERROR);
98
  }
99
}
100

    
101
/**
102
 * Info alteration callback for the schedule action.
103
 */
104
function rules_scheduler_action_schedule_info_alter(&$element_info, RulesPlugin $element) {
105
  if (isset($element->settings['component'])) {
106
    // If run during a cache rebuild the cache might not be instantiated yet,
107
    // so fail back to loading the component from database.
108
    if (($component = rules_get_cache('comp_' . $element->settings['component'])) || $component = rules_config_load($element->settings['component'])) {
109
      // Add in the needed parameters.
110
      foreach ($component->parameterInfo() as $name => $info) {
111
        $element_info['parameter']['param_' . $name] = $info;
112
      }
113
    }
114
  }
115
}
116

    
117
/**
118
 * Validate callback for the schedule action to make sure the component exists and is not dirty.
119
 *
120
 * @see rules_element_invoke_component_validate()
121
 */
122
function rules_scheduler_action_schedule_validate(RulesPlugin $element) {
123
  $info = $element->info();
124
  $component = rules_config_load($element->settings['component']);
125
  if (!$component) {
126
    throw new RulesIntegrityException(t('The component %config does not exist.', array('%config' => $element->settings['component'])), $element);
127
  }
128
  // Check if the component is marked as dirty.
129
  rules_config_update_dirty_flag($component);
130
  if (!empty($component->dirty)) {
131
    throw new RulesIntegrityException(t('The utilized component %config fails the integrity check.', array('%config' => $element->settings['component'])), $element);
132
  }
133
}
134

    
135
/**
136
 * Help for the schedule action.
137
 */
138
function rules_scheduler_action_schedule_help() {
139
  return t("Note that component evaluation is triggered by <em>cron</em> – make sure cron is configured correctly by checking your site's !status. The scheduling time accuracy depends on your configured cron interval. See <a href='@url'>the online documentation</a> for more information on how to schedule evaluation of components.",
140
    array('!status' => l('Status report', 'admin/reports/status'),
141
          '@url' => rules_external_help('scheduler')));
142
}
143

    
144
/**
145
 * Form alter callback for the schedule action.
146
 */
147
function rules_scheduler_action_schedule_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
148
  $first_step = empty($element->settings['component']);
149
  $form['reload'] = array(
150
    '#weight' => 5,
151
    '#type' => 'submit',
152
    '#name' => 'reload',
153
    '#value' => $first_step ? t('Continue') : t('Reload form'),
154
    '#limit_validation_errors' => array(array('parameter', 'component')),
155
    '#submit' => array('rules_action_type_form_submit_rebuild'),
156
    '#ajax' => rules_ui_form_default_ajax(),
157
  );
158
  // Use ajax and trigger as the reload button.
159
  $form['parameter']['component']['settings']['type']['#ajax'] = $form['reload']['#ajax'] + array(
160
    'event' => 'change',
161
    'trigger_as' => array('name' => 'reload'),
162
  );
163

    
164
  if ($first_step) {
165
    // In the first step show only the component select.
166
    foreach (element_children($form['parameter']) as $key) {
167
      if ($key != 'component') {
168
        unset($form['parameter'][$key]);
169
      }
170
    }
171
    unset($form['submit']);
172
    unset($form['provides']);
173
  }
174
  else {
175
    // Hide the reload button in case js is enabled and it's not the first step.
176
    $form['reload']['#attributes'] = array('class' => array('rules-hide-js'));
177
  }
178
}
179

    
180
/**
181
 * Action: Delete scheduled tasks.
182
 */
183
function rules_scheduler_action_delete($component_name = NULL, $task_identifier = NULL) {
184
  $query = db_delete('rules_scheduler');
185
  if (!empty($component_name)) {
186
    $query->condition('config', $component_name);
187
  }
188
  if (!empty($task_identifier)) {
189
    $query->condition('identifier', $task_identifier);
190
  }
191
  $query->execute();
192
}
193

    
194
/**
195
 * Cancel scheduled task action validation callback.
196
 */
197
function rules_scheduler_action_delete_validate($element) {
198
  if (empty($element->settings['task']) && empty($element->settings['task:select']) &&
199
      empty($element->settings['component']) && empty($element->settings['component:select'])) {
200

    
201
    throw new RulesIntegrityException(t('You have to specify at least either a component or a task identifier.'), $element);
202
  }
203
}
204

    
205
/**
206
 * Help for the cancel action.
207
 */
208
function rules_scheduler_action_delete_help() {
209
  return t('This action allows you to delete scheduled tasks that are waiting for future execution.') .' '. t('They can be addressed by an identifier or by the component name, whereas if both are specified only tasks fulfilling both requirements will be deleted.');
210
}
211

    
212
/**
213
 * @}
214
 */