Skip to content

Instantly share code, notes, and snippets.

@johnrobertwilson
Created May 17, 2012 20:40
Show Gist options
  • Save johnrobertwilson/2721472 to your computer and use it in GitHub Desktop.
Save johnrobertwilson/2721472 to your computer and use it in GitHub Desktop.
wf
<?php
/**
* @file
* EPA implementation of State Flow, an extension of the State Machine class
*/
class EPAWorkflow extends StateFlow {
public function init() {
// Initialize states
$this->create_state('draft', array('label' => t('Draft')));
$this->create_state('draft_review', array('label' => t('Draft, needs review')));
$this->create_state('draft_approved', array('label' => t('Draft, approved')));
$this->create_state('published', array(
'label' => t('Published'),
'on_enter' => array($this, 'on_enter_published'),
));
$this->create_state('published_review', array('label' => t('Published, needs review')));
$this->create_state('published_expire', array('label' => t('Draft, scheduled for expiration')));
$this->create_state('unpublished', array(
'on_enter' => array($this, 'on_enter_unpublished'),
'label' => t('Unpublished'),
));
// Initialize events
$this->create_event('send_for_review', array(
'label' => t('Send for review'),
'origin' => 'draft',
'target' => 'draft_review',
));
$this->create_event('approve', array(
'label' => t('Approve for publishing'),
'origin' => 'draft_review',
'target' => 'draft_approved',
'guard' => 'epa_workflow_guard_approver',
));
$this->create_event('return_to_author', array(
'label' => t('Return to author'),
'origin' => 'draft_review',
'target' => 'draft',
'guard' => 'epa_workflow_guard_approver',
));
$this->create_event('publish', array(
'label' => t('Publish'),
'origin' => array('draft_approved', 'published_review', 'published_expire'),
'target' => 'published',
'guard' => 'epa_workflow_guard_publisher',
));
$this->create_event('archive', array(
'label' => t('Archive'),
'origin' => array('draft_approved', 'published'),
'target' => 'unpublished',
'guard' => 'epa_workflow_guard_publisher',
));
$this->create_event('expire', array(
'label' => t('Expire'),
'origin' => 'published_expire',
'target' => 'unpublished',
'guard' => 'epa_workflow_guard_publisher',
));
$this->create_event('revert', array(
'label' => t('Revert'),
'origin' => 'unpublished',
'target' => 'published',
'guard' => 'epa_workflow_guard_publisher',
));
$this->create_event('make_updates', array(
'label' => t('Make updates'),
'origin' => 'unpublished',
'target' => 'draft',
'guard' => 'epa_workflow_guard_publisher',
));
$this->create_event('queue_for_review', array(
'label' => t('Queue for review'),
'origin' => 'published',
'target' => 'published_review',
'guard' => 'epa_workflow_guard_publisher',
));
$this->create_event('archive', array(
'label' => t('Archive'),
'origin' => array('published', 'draft_approved'),
'target' => 'unpublished',
'guard' => 'epa_workflow_guard_publisher',
));
$this->create_event('alert_stakeholders', array(
'label' => t('Alert stakeholders'),
'origin ' => 'published_review',
'target' => 'published_expire',
'guard' => 'epa_workflow_guard_publisher',
));
$this->create_event('publish_immediately', array(
'label' => t('Publish Immediately'),
'origin' => array('draft', 'draft_review'),
'target' => 'published',
'guard' => 'epa_workflow_guard_publisher'
));
}
public function on_enter_unpublished() {
$node = $this->get_object();
$node->status = 0;
node_save($node);
}
public function on_enter_published() {
$node = $this->get_object();
$node->status = 1;
node_save($node);
}
public function on_event_fail($event) {
if ($event) {
$key = array_search($event, $this->events);
drupal_set_message(t('Could not transition node using %event event.', array('%event' => $key)), 'error');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment