rules_scheduler.admin.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @file
  4. * Admin forms for scheduling.
  5. */
  6. /**
  7. * Schedule page with a view for the scheduled tasks.
  8. */
  9. function rules_scheduler_schedule_page() {
  10. // Display view for all scheduled tasks.
  11. if (module_exists('views')) {
  12. // We cannot use views_embed_view() here as we need to set the path for the
  13. // component filter form.
  14. $view = views_get_view('rules_scheduler');
  15. $view->override_path = RULES_SCHEDULER_PATH;
  16. $task_list = $view->preview();
  17. }
  18. else {
  19. $task_list = t('To display scheduled tasks you have to install the <a href="https://www.drupal.org/project/views">Views</a> module.');
  20. }
  21. $page['task_view'] = array(
  22. '#markup' => $task_list,
  23. );
  24. $form = drupal_get_form('rules_scheduler_form');
  25. $page['delete'] = array(
  26. '#markup' => drupal_render($form),
  27. );
  28. return $page;
  29. }
  30. /**
  31. * Form for deletion of tasks by component.
  32. */
  33. function rules_scheduler_form($form, &$form_state) {
  34. $result = db_select('rules_scheduler', 'r')
  35. ->fields('r', array('config'))
  36. ->distinct()
  37. ->execute();
  38. $config_options = array_intersect_key(rules_get_components(TRUE), $result->fetchAllAssoc('config'));
  39. // Fieldset for canceling by component name.
  40. $form['delete_by_config'] = array(
  41. '#type' => 'fieldset',
  42. '#title' => t('Delete tasks by component name'),
  43. '#disabled' => empty($config_options),
  44. );
  45. $form['delete_by_config']['config'] = array(
  46. '#title' => t('Component'),
  47. '#type' => 'select',
  48. '#options' => $config_options,
  49. '#description' => t('Select the component for which to delete all scheduled tasks.'),
  50. '#required' => TRUE,
  51. );
  52. $form['delete_by_config']['submit'] = array(
  53. '#type' => 'submit',
  54. '#value' => t('Delete tasks'),
  55. '#submit' => array('rules_scheduler_form_delete_by_config_submit'),
  56. );
  57. return $form;
  58. }
  59. /**
  60. * Submit handler for deleting future scheduled tasks.
  61. */
  62. function rules_scheduler_form_delete_by_config_submit($form, &$form_state) {
  63. $config = rules_config_load($form_state['values']['config']);
  64. rules_action('schedule_delete')->execute($config->name);
  65. drupal_set_message(t('All scheduled tasks associated with %config have been deleted.', array('%config' => $config->label())));
  66. $form_state['redirect'] = RULES_SCHEDULER_PATH;
  67. }
  68. /**
  69. * Confirmation form for deleting single tasks.
  70. */
  71. function rules_scheduler_delete_task($form, &$form_state, $task) {
  72. $form_state['task'] = $task;
  73. $config = rules_config_load($task['config']);
  74. $path['path'] = isset($_GET['destination']) ? $_GET['destination'] : RULES_SCHEDULER_PATH;
  75. $title = t('Are you sure you want to delete the scheduled task %id?', array('%id' => $task['tid']));
  76. if (!empty($task['identifier'])) {
  77. $msg = t('This task with the custom identifier %id executes component %label on %date. The action cannot be undone.', array(
  78. '%label' => $config->label(),
  79. '%id' => $task['identifier'],
  80. '%date' => format_date($task['date']),
  81. ));
  82. }
  83. else {
  84. $msg = t('This task executes component %label and will be executed on %date. The action cannot be undone.', array(
  85. '%label' => $config->label(),
  86. '%date' => format_date($task['date']),
  87. ));
  88. }
  89. return confirm_form($form, $title, $path, $msg, t('Delete'), t('Cancel'));
  90. }
  91. /**
  92. * Submit handler for deleting single tasks.
  93. */
  94. function rules_scheduler_delete_task_submit($form, &$form_state) {
  95. rules_scheduler_task_delete($form_state['task']['tid']);
  96. drupal_set_message(t('Task %tid has been deleted.', array('%tid' => $form_state['task']['tid'])));
  97. $form_state['redirect'] = RULES_SCHEDULER_PATH;
  98. }
  99. /**
  100. * Configuration form to manually schedule a rules component.
  101. */
  102. function rules_scheduler_schedule_form($form, &$form_state, $rules_config, $base_path) {
  103. // Only components can be scheduled.
  104. if (!($rules_config instanceof RulesTriggerableInterface)) {
  105. RulesPluginUI::$basePath = $base_path;
  106. $form_state['component'] = $rules_config->name;
  107. $action = rules_action('schedule', array('component' => $rules_config->name));
  108. $action->form($form, $form_state);
  109. // The component should be fixed, so hide the parameter for it.
  110. $form['parameter']['component']['#access'] = FALSE;
  111. $form['submit'] = array(
  112. '#type' => 'submit',
  113. '#value' => t('Schedule'),
  114. );
  115. $form['#validate'] = array('rules_ui_form_rules_config_validate');
  116. return $form;
  117. }
  118. drupal_not_found();
  119. exit;
  120. }
  121. /**
  122. * Submit callback to execute the scheduling action.
  123. */
  124. function rules_scheduler_schedule_form_submit($form, &$form_state) {
  125. $action = $form_state['rules_element'];
  126. $action->execute();
  127. drupal_set_message(t('Component %label has been scheduled.', array('%label' => rules_config_load($form_state['component'])->label())));
  128. $form_state['redirect'] = RULES_SCHEDULER_PATH;
  129. }