Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Admin forms for scheduling.
6
 */
7

    
8
/**
9
 * Schedule page with a view for the scheduled tasks.
10
 */
11
function rules_scheduler_schedule_page() {
12
  // Display view for all scheduled tasks.
13
  if (module_exists('views')) {
14
    // We cannot use views_embed_view() here as we need to set the path for the
15
    // component filter form.
16
    $view = views_get_view('rules_scheduler');
17
    $view->override_path = RULES_SCHEDULER_PATH;
18
    $task_list = $view->preview();
19
  }
20
  else {
21
    $task_list = t('To display scheduled tasks you have to install the <a href="https://www.drupal.org/project/views">Views</a> module.');
22
  }
23
  $page['task_view'] = array(
24
    '#markup' => $task_list,
25
  );
26
  $form = drupal_get_form('rules_scheduler_form');
27
  $page['delete'] = array(
28
    '#markup' => drupal_render($form),
29
  );
30
  return $page;
31
}
32

    
33
/**
34
 * Form for deletion of tasks by component.
35
 */
36
function rules_scheduler_form($form, &$form_state) {
37
  $result = db_select('rules_scheduler', 'r')
38
    ->fields('r', array('config'))
39
    ->distinct()
40
    ->execute();
41
  $config_options = array_intersect_key(rules_get_components(TRUE), $result->fetchAllAssoc('config'));
42

    
43
  // Fieldset for canceling by component name.
44
  $form['delete_by_config'] = array(
45
    '#type' => 'fieldset',
46
    '#title' => t('Delete tasks by component name'),
47
    '#disabled' => empty($config_options),
48
  );
49
  $form['delete_by_config']['config'] = array(
50
    '#title' => t('Component'),
51
    '#type' => 'select',
52
    '#options' => $config_options,
53
    '#description' => t('Select the component for which to delete all scheduled tasks.'),
54
    '#required' => TRUE,
55
  );
56
  $form['delete_by_config']['submit'] = array(
57
    '#type' => 'submit',
58
    '#value' => t('Delete tasks'),
59
    '#submit' => array('rules_scheduler_form_delete_by_config_submit'),
60
  );
61
  return $form;
62
}
63

    
64
/**
65
 * Submit handler for deleting future scheduled tasks.
66
 */
67
function rules_scheduler_form_delete_by_config_submit($form, &$form_state) {
68
  $config = rules_config_load($form_state['values']['config']);
69
  rules_action('schedule_delete')->execute($config->name);
70
  drupal_set_message(t('All scheduled tasks associated with %config have been deleted.', array('%config' => $config->label())));
71
  $form_state['redirect'] = RULES_SCHEDULER_PATH;
72
}
73

    
74
/**
75
 * Confirmation form for deleting single tasks.
76
 */
77
function rules_scheduler_delete_task($form, &$form_state, $task) {
78
  $form_state['task'] = $task;
79
  $config = rules_config_load($task['config']);
80
  $path['path'] = isset($_GET['destination']) ? $_GET['destination'] : RULES_SCHEDULER_PATH;
81

    
82
  $title = t('Are you sure you want to delete the scheduled task %id?', array('%id' => $task['tid']));
83
  if (!empty($task['identifier'])) {
84
    $msg = t('This task with the custom identifier %id executes component %label on %date. The action cannot be undone.', array(
85
      '%label' => $config->label(),
86
      '%id' => $task['identifier'],
87
      '%date' => format_date($task['date']),
88
    ));
89
  }
90
  else {
91
    $msg = t('This task executes component %label and will be executed on %date. The action cannot be undone.', array(
92
      '%label' => $config->label(),
93
      '%date' => format_date($task['date']),
94
    ));
95
  }
96
  return confirm_form($form, $title, $path, $msg, t('Delete'), t('Cancel'));
97
}
98

    
99
/**
100
 * Submit handler for deleting single tasks.
101
 */
102
function rules_scheduler_delete_task_submit($form, &$form_state) {
103
  rules_scheduler_task_delete($form_state['task']['tid']);
104
  drupal_set_message(t('Task %tid has been deleted.', array('%tid' => $form_state['task']['tid'])));
105
  $form_state['redirect'] = RULES_SCHEDULER_PATH;
106
}
107

    
108
/**
109
 * Configuration form to manually schedule a rules component.
110
 */
111
function rules_scheduler_schedule_form($form, &$form_state, $rules_config, $base_path) {
112
  // Only components can be scheduled.
113
  if (!($rules_config instanceof RulesTriggerableInterface)) {
114
    RulesPluginUI::$basePath = $base_path;
115
    $form_state['component'] = $rules_config->name;
116
    $action = rules_action('schedule', array('component' => $rules_config->name));
117
    $action->form($form, $form_state);
118
    // The component should be fixed, so hide the parameter for it.
119
    $form['parameter']['component']['#access'] = FALSE;
120
    $form['submit'] = array(
121
      '#type' => 'submit',
122
      '#value' => t('Schedule'),
123
    );
124
    $form['#validate'] = array('rules_ui_form_rules_config_validate');
125
    return $form;
126
  }
127
  drupal_not_found();
128
  exit;
129
}
130

    
131
/**
132
 * Submit callback to execute the scheduling action.
133
 */
134
function rules_scheduler_schedule_form_submit($form, &$form_state) {
135
  $action = $form_state['rules_element'];
136
  $action->execute();
137
  drupal_set_message(t('Component %label has been scheduled.', array('%label' => rules_config_load($form_state['component'])->label())));
138
  $form_state['redirect'] = RULES_SCHEDULER_PATH;
139
}