Skip to content

Instantly share code, notes, and snippets.

@freshjones
Created November 2, 2015 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freshjones/8e2e15d1fede9afd0d70 to your computer and use it in GitHub Desktop.
Save freshjones/8e2e15d1fede9afd0d70 to your computer and use it in GitHub Desktop.
Patch for workbench scheduler 7.x-1.7-dev to check for transition state before scheulding
diff --git a/workbench_scheduler.module b/workbench_scheduler.module
index 50a1e19..65f18ba 100644
--- a/workbench_scheduler.module
+++ b/workbench_scheduler.module
@@ -1146,6 +1146,32 @@ function workbench_scheduler_schedules_exist() {
}
/**
+ * Check for the existence of a transition from one state to another.
+ *
+ * @param string $from_state
+ * The "from" transition state.
+ * @param string $to_state
+ * The "to" transition state.
+ *
+ * @return boolean
+ * TRUE if a match is found. Otherwise FALSE.
+ */
+function workbench_scheduler_transition_exists($from_state, $to_state) {
+ // Get transitions.
+ $transitions = workbench_moderation_transitions();
+
+ // Check transitions for match.
+ foreach ($transitions as $transition) {
+ if ($transition->from_name === $from_state && $transition->to_name === $to_state) {
+ return TRUE;
+ }
+ }
+
+ // Default return.
+ return FALSE;
+}
+
+/**
* Run schedules for start times / states.
*
* @param int $timestamp
@@ -1235,31 +1261,35 @@ function workbench_scheduler_process_start_dates($timestamp) {
// Load and process the node.
if ($do_process && $node = node_load($node_schedule->nid, $node_schedule->vid)) {
- // If moderation state is 'unpublished' follow a different workflow.
- if ($node_schedule->start_state == 'unpublished' && $node->status) {
- workbench_scheduler_moderate_unpublish($node);
- }
- // Moderate the node using workbench_moderation.
- else {
- workbench_moderation_moderate($node, $node_schedule->start_state);
- }
- // If not the 'published' state, set to 'unpublished'.
- if ($node_schedule->start_state != 'published') {
- $node->status = 0;
- // Update the node.
- node_save($node);
- }
- // Does this schedule have an end date as well?
- if ($node_schedule->end_date) {
- }
- // Do not mark as completed, since need to wait for end date.
- else {
- // No end state so mark schedule as complete.
- workbench_scheduler_node_set_complete($node_schedule->nid, $node_schedule->vid, $node_schedule->sid);
+ // Make sure the transition exists.
+ if (workbench_scheduler_transition_exists($node->workbench_moderation['current']->state, $node_schedule->start_state)) {
+
+ // If moderation state is 'unpublished' follow a different workflow.
+ if ($node_schedule->start_state == 'unpublished' && $node->status) {
+ workbench_scheduler_moderate_unpublish($node);
+ }
+ // Moderate the node using workbench_moderation.
+ else {
+ workbench_moderation_moderate($node, $node_schedule->start_state);
+ }
+ // If not the 'published' state, set to 'unpublished'.
+ if ($node_schedule->start_state != 'published') {
+ $node->status = 0;
+ // Update the node.
+ node_save($node);
+ }
+ // Does this schedule have an end date as well?
+ if ($node_schedule->end_date) {
+ }
+ // Do not mark as completed, since need to wait for end date.
+ else {
+ // No end state so mark schedule as complete.
+ workbench_scheduler_node_set_complete($node_schedule->nid, $node_schedule->vid, $node_schedule->sid);
+ }
+ // Update count of run schedules.
+ $count++;
+ module_invoke_all('workbench_scheduler_post_process_start_dates', $node_schedule);
}
- // Update count of run schedules.
- $count++;
- module_invoke_all('workbench_scheduler_post_process_start_dates', $node_schedule);
}
}
}
@@ -1349,26 +1379,29 @@ function workbench_scheduler_process_end_dates($timestamp) {
// Need to load the node.
if ($do_process && $node = node_load($node_schedule->nid, $node_schedule->vid)) {
- // Only moderate if state is not 'unpublish'.
- // If moderation state is 'unpublished' follow a different workflow.
- if ($node_schedule->end_state == 'unpublished' && $node->status) {
- workbench_scheduler_moderate_unpublish($node);
- }
- // Moderate the node using workbench_moderation.
- else {
- workbench_moderation_moderate($node, $node_schedule->end_state);
- }
- // If not the 'published' state, set to 'unpublished.
- if ($node_schedule->end_state != 'published') {
- $node->status = 0;
- node_save($node);
- }
+ // Make sure the transition exists.
+ if (workbench_scheduler_transition_exists($node->workbench_moderation['current']->state, $node_schedule->end_state)) {
+ // Only moderate if state is not 'unpublish'.
+ // If moderation state is 'unpublished' follow a different workflow.
+ if ($node_schedule->end_state == 'unpublished' && $node->status) {
+ workbench_scheduler_moderate_unpublish($node);
+ }
+ // Moderate the node using workbench_moderation.
+ else {
+ workbench_moderation_moderate($node, $node_schedule->end_state);
+ }
+ // If not the 'published' state, set to 'unpublished.
+ if ($node_schedule->end_state != 'published') {
+ $node->status = 0;
+ node_save($node);
+ }
- // Mark schedule as completed.
- workbench_scheduler_node_set_complete($node_schedule->nid, $node_schedule->vid, $node_schedule->sid);
- // Update count of run schedules.
- $count++;
- module_invoke_all('workbench_scheduler_post_process_end_dates', $node_schedule);
+ // Mark schedule as completed.
+ workbench_scheduler_node_set_complete($node_schedule->nid, $node_schedule->vid, $node_schedule->sid);
+ // Update count of run schedules.
+ $count++;
+ module_invoke_all('workbench_scheduler_post_process_end_dates', $node_schedule);
+ }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment