? workflow-370111.patch
Index: workflow.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/workflow/workflow.module,v
retrieving revision 1.83.2.1
diff -u -p -r1.83.2.1 workflow.module
--- workflow.module	5 Jun 2009 22:05:30 -0000	1.83.2.1
+++ workflow.module	4 Aug 2009 14:42:35 -0000
@@ -548,6 +548,11 @@ function workflow_execute_transition($no
 
   // Clear any references in the scheduled listing.
   db_query('DELETE FROM {workflow_scheduled_transition} WHERE nid = %d', $node->nid);
+  
+  // Rules integration
+  if (module_exists('rules')) {
+    rules_invoke_event('workflow_state_changed', $node, $old_sid, $sid);
+  }
 }
 
 /**
@@ -1092,16 +1097,9 @@ function workflow_get_states($wid = NULL
  *   A keyed array with all attributes of the state.
  */
 function workflow_get_state($sid) {
-  $state = array();
-  $result = db_query('SELECT wid, state, weight, sysid, status FROM {workflow_states} WHERE sid = %d', $sid);
+  $result = db_query('SELECT * FROM {workflow_states} WHERE sid = %d', $sid);
   // State IDs are unique, so there should be only one row.
-  $data = db_fetch_object($result);
-  $state['wid'] = $data->wid;
-  $state['state'] = $data->state;
-  $state['weight'] = $data->weight;
-  $state['sysid'] = $data->sysid;
-  $state['status'] = $data->status;
-  return $state;
+  return db_fetch_array($result);
 }
 
 /**
@@ -1500,6 +1498,7 @@ function workflow_token_values($type, $o
  */
 function workflow_token_list($type = 'all') {
 
+  $tokens = array();
   if ($type == 'workflow' || $type == 'node' || $type == 'all') {
     $tokens['workflow']['workflow-name']                          =  'Name of workflow appied to this node';
     $tokens['workflow']['workflow-current-state-name']            =  'Current state of content';
Index: workflow.rules.inc
===================================================================
RCS file: workflow.rules.inc
diff -N workflow.rules.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ workflow.rules.inc	4 Aug 2009 14:42:35 -0000
@@ -0,0 +1,83 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Rules integration for the Workflow module
+ */
+
+/**
+ * Implementation of hook_rules_event_info().
+ */
+function workflow_rules_event_info() {
+  $events = array(
+    'workflow_state_changed' => array(
+      'label' => t('Workflow state has changed'),
+      'module' => 'Workflow',
+      'arguments' =>  workflow_events_workflow_arguments(),
+    ),
+  );
+  return $events;
+}
+
+/**
+ * Returns arguments for a workflow event.
+ */
+function workflow_events_workflow_arguments() {
+  return array(
+    'node' => array('type' => 'node', 'label' => t('Updated content')),
+    'old_state' => array('type' => 'workflow_state', 'label' => t('Old workflow state')),
+    'new_state' => array('type' => 'workflow_state', 'label' => t('New workflow state')),
+    'author' => array('type' => 'user', 'label' => t('Content author'), 'handler' => 'rules_events_argument_node_author'),
+  ) + rules_events_global_user_argument();
+}
+
+/**
+ * Implementation of hook_condition_info().
+ */
+function workflow_rules_condition_info() {
+  return array(
+    'workflow_check_transition' => array(
+      'label' => t('Check workflow transition'),
+      'arguments' => array(
+        'old_state' => array('type' => 'workflow_state', 'label' => t('Old workflow state')),
+        'new_state' => array('type' => 'workflow_state', 'label' => t('New workflow state')),
+      ),
+      'help' => t('Evaluates to TRUE, if the workflow being updated is moved from state A to state B'),
+      'module' => 'Workflow',
+    ),
+  );
+}
+
+/**
+ * Condition implementation: check state transition. 
+ */
+function workflow_check_transition($old_state, $new_state, $settings) {
+  if ($settings['from_state'] === 'ANY') {
+    if ($settings['to_state'] === 'ANY') {
+      return TRUE;
+    }
+    return $new_state === $settings['to_state'];
+  }
+  if ($settings['to_state'] === 'ANY') {
+    return $old_state === $settings['from_state'];
+  }
+  return $old_state === $settings['from_state'] && $new_state === $settings['to_state'];
+}
+
+/**
+ * Implementation of hook_rules_data_type_info().
+ */
+function workflow_rules_data_type_info() {
+  return array(
+    'workflow_state' => array(
+      'label' => t('Workflow state'),
+      'class' => 'rules_data_type',
+      'savable' => FALSE,
+      'identifiable' => FALSE,
+      'uses_input_form' => FALSE,
+      'module' => 'Workflow',
+    ),
+  );
+}
+
Index: workflow.rules_forms.inc
===================================================================
RCS file: workflow.rules_forms.inc
diff -N workflow.rules_forms.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ workflow.rules_forms.inc	4 Aug 2009 14:42:35 -0000
@@ -0,0 +1,56 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Contains configuration forms for workflow rules integration.
+ */
+
+/**
+ * Configuration form for check transition condition.
+ */
+function workflow_check_transition_form($settings, &$form) {
+  $options = array();
+  $options['ANY'] = t('Any state');
+  foreach(workflow_get_all() as $wid => $workflow) {
+    $options[$workflow] = array();
+    foreach (workflow_get_states($wid) as $sid => $state) {
+      $options[$workflow][$sid] = $state;
+    }
+  }
+  $form['settings']['from_state'] = array(
+    '#type' => 'select',
+    '#title' => t('From State'),
+    '#options' => $options,
+    '#default_value' => isset($settings['from_state']) ? $settings['from_state'] : array(),
+    '#required' => TRUE,
+  );
+  $form['settings']['to_state'] = array(
+    '#type' => 'select',
+    '#title' => t('To State'),
+    '#options' => $options,
+    '#default_value' => isset($settings['to_state']) ? $settings['to_state'] : array(),
+    '#required' => TRUE,
+  );
+}
+
+/**
+ * Label callback for check transition condition.
+ */
+function workflow_check_transition_label($settings, $argument_labels) {
+  if ($settings['from_state'] === 'ANY') {
+    $from = t('Any state');
+  }
+  else {
+    $from = workflow_get_state($settings['from_state']);
+    $from = $from['state'];
+  }
+  if ($settings['to_state'] === 'ANY') {
+    $to = t('Any state');
+  }
+  else {
+    $to = workflow_get_state($settings['to_state']);
+    $to = $to['state'];
+  }
+  return t('Check workflow transition from @from to @to', array('@from' => $from, '@to' => $to));
+}
\ No newline at end of file
