rules_scheduler.rules.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @file
  4. * Rules integration for the rules scheduler module.
  5. *
  6. * @addtogroup rules
  7. * @{
  8. */
  9. /**
  10. * Implements hook_rules_action_info().
  11. */
  12. function rules_scheduler_rules_action_info() {
  13. $items['schedule'] = array(
  14. 'label' => t('Schedule component evaluation'),
  15. 'group' => t('Rules scheduler'),
  16. 'base' => 'rules_scheduler_action_schedule',
  17. 'named parameter' => TRUE,
  18. 'parameter' => array(
  19. 'component' => array(
  20. 'type' => 'text',
  21. 'label' => t('Component'),
  22. 'options list' => 'rules_scheduler_component_options_list',
  23. 'restriction' => 'input',
  24. 'description' => 'Select the component to schedule. Only components containing actions are available – no condition sets.',
  25. ),
  26. 'date' => array(
  27. 'type' => 'date',
  28. 'label' => t('Scheduled evaluation date'),
  29. ),
  30. 'identifier' => array(
  31. 'type' => 'text',
  32. 'label' => t('Identifier'),
  33. 'description' => t('A string used for identifying this task. Any existing tasks for this component with the same identifier will be replaced.'),
  34. 'optional' => TRUE,
  35. ),
  36. // Further needed parameter by the component are added during processing.
  37. ),
  38. );
  39. // Add action to delete scheduled tasks.
  40. $items['schedule_delete'] = array(
  41. 'label' => t('Delete scheduled tasks'),
  42. 'group' => t('Rules scheduler'),
  43. 'base' => 'rules_scheduler_action_delete',
  44. 'parameter' => array(
  45. 'component' => array(
  46. 'type' => 'text',
  47. 'label' => t('Component'),
  48. 'options list' => 'rules_scheduler_component_options_list',
  49. 'description' => t('The component for which scheduled tasks will be deleted.'),
  50. 'optional' => TRUE,
  51. ),
  52. 'task' => array(
  53. 'type' => 'text',
  54. 'label' => t('Task identifier'),
  55. 'description' => t('All tasks that are annotated with the given identifier will be deleted.'),
  56. 'optional' => TRUE,
  57. ),
  58. ),
  59. );
  60. return $items;
  61. }
  62. /**
  63. * Options list callback returning a list of action components.
  64. */
  65. function rules_scheduler_component_options_list() {
  66. return rules_get_components(TRUE, 'action');
  67. }
  68. /**
  69. * Base action implementation for scheduling components.
  70. */
  71. function rules_scheduler_action_schedule($args, $element) {
  72. $state = $args['state'];
  73. if ($component = rules_get_cache('comp_' . $args['component'])) {
  74. // Manually create a new evaluation state for scheduling the evaluation.
  75. $new_state = new RulesState();
  76. // Register all parameters as variables.
  77. foreach ($element->pluginParameterInfo() as $name => $info) {
  78. if (strpos($name, 'param_') === 0) {
  79. // Remove the parameter name prefix 'param_'.
  80. $var_name = substr($name, 6);
  81. $new_state->addVariable($var_name, $state->currentArguments[$name], $info);
  82. }
  83. }
  84. rules_scheduler_schedule_task(array(
  85. 'date' => $args['date'],
  86. 'config' => $args['component'],
  87. 'state' => $new_state,
  88. 'identifier' => $args['identifier'],
  89. ));
  90. }
  91. else {
  92. throw new RulesEvaluationException('Unable to get the component %name', array('%name' => $args['component']), $element, RulesLog::ERROR);
  93. }
  94. }
  95. /**
  96. * Info alteration callback for the schedule action.
  97. */
  98. function rules_scheduler_action_schedule_info_alter(&$element_info, RulesPlugin $element) {
  99. if (isset($element->settings['component'])) {
  100. // If run during a cache rebuild the cache might not be instantiated yet,
  101. // so fail back to loading the component from database.
  102. if (($component = rules_get_cache('comp_' . $element->settings['component'])) || $component = rules_config_load($element->settings['component'])) {
  103. // Add in the needed parameters.
  104. foreach ($component->parameterInfo() as $name => $info) {
  105. $element_info['parameter']['param_' . $name] = $info;
  106. }
  107. }
  108. }
  109. }
  110. /**
  111. * Validate callback for the schedule action to make sure the component exists and is not dirty.
  112. *
  113. * @see rules_element_invoke_component_validate()
  114. */
  115. function rules_scheduler_action_schedule_validate(RulesPlugin $element) {
  116. $info = $element->info();
  117. $component = rules_config_load($element->settings['component']);
  118. if (!$component) {
  119. throw new RulesIntegrityException(t('The component %config does not exist.', array('%config' => $element->settings['component'])), $element);
  120. }
  121. // Check if the component is marked as dirty.
  122. rules_config_update_dirty_flag($component);
  123. if (!empty($component->dirty)) {
  124. throw new RulesIntegrityException(t('The utilized component %config fails the integrity check.', array('%config' => $element->settings['component'])), $element);
  125. }
  126. }
  127. /**
  128. * Help for the schedule action.
  129. */
  130. function rules_scheduler_action_schedule_help() {
  131. 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.",
  132. array('!status' => l('Status report', 'admin/reports/status'),
  133. '@url' => rules_external_help('scheduler')));
  134. }
  135. /**
  136. * Form alter callback for the schedule action.
  137. */
  138. function rules_scheduler_action_schedule_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
  139. $first_step = empty($element->settings['component']);
  140. $form['reload'] = array(
  141. '#weight' => 5,
  142. '#type' => 'submit',
  143. '#name' => 'reload',
  144. '#value' => $first_step ? t('Continue') : t('Reload form'),
  145. '#limit_validation_errors' => array(array('parameter', 'component')),
  146. '#submit' => array('rules_action_type_form_submit_rebuild'),
  147. '#ajax' => rules_ui_form_default_ajax(),
  148. );
  149. // Use ajax and trigger as the reload button.
  150. $form['parameter']['component']['settings']['type']['#ajax'] = $form['reload']['#ajax'] + array(
  151. 'event' => 'change',
  152. 'trigger_as' => array('name' => 'reload'),
  153. );
  154. if ($first_step) {
  155. // In the first step show only the component select.
  156. foreach (element_children($form['parameter']) as $key) {
  157. if ($key != 'component') {
  158. unset($form['parameter'][$key]);
  159. }
  160. }
  161. unset($form['submit']);
  162. unset($form['provides']);
  163. }
  164. else {
  165. // Hide the reload button in case js is enabled and it's not the first step.
  166. $form['reload']['#attributes'] = array('class' => array('rules-hide-js'));
  167. }
  168. }
  169. /**
  170. * Action: Delete scheduled tasks.
  171. */
  172. function rules_scheduler_action_delete($component_name = NULL, $task_identifier = NULL) {
  173. $query = db_delete('rules_scheduler');
  174. if (!empty($component_name)) {
  175. $query->condition('config', $component_name);
  176. }
  177. if (!empty($task_identifier)) {
  178. $query->condition('identifier', $task_identifier);
  179. }
  180. $query->execute();
  181. }
  182. /**
  183. * Cancel scheduled task action validation callback.
  184. */
  185. function rules_scheduler_action_delete_validate($element) {
  186. if (empty($element->settings['task']) && empty($element->settings['task:select']) &&
  187. empty($element->settings['component']) && empty($element->settings['component:select'])) {
  188. throw new RulesIntegrityException(t('You have to specify at least either a component or a task identifier.'), $element);
  189. }
  190. }
  191. /**
  192. * Help for the cancel action.
  193. */
  194. function rules_scheduler_action_delete_help() {
  195. 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.');
  196. }
  197. /**
  198. * @}
  199. */