--- Desktop/workflow/workflow.module	2006-07-27 20:02:32.000000000 -0500
+++ /var/www/localhost/htdocs/drupal/modules/workflow/workflow.module	2006-08-25 15:21:46.000000000 -0500
@@ -349,11 +349,19 @@
 
 /**
  * Implementation of a Drupal action.
- * Changes the workflow state of a node to the next state of the workflow.
+ * Changes the workflow state of a node to the next state of the workflow or change children.
  *
  */
 function action_workflow_execute_transition($op, $edit = array(), &$node) {
   switch($op) {
+    case 'metadata':
+      return array(
+        'description' => t('Change workflow state of a node to next state'),
+        'type' => t('Workflow'),
+        'batchable' => TRUE,
+        'configurable' => TRUE,
+      );
+  
     case 'do':
       // if this action is being fired because it's attached to a workflow transition
       // then the node's new state (now it's current state) should be in $node->workflow;
@@ -363,49 +371,137 @@
         return;
       }
       $current_state = isset($node->workflow) ? $node->workflow : $node->_workflow;
-
-      // get the node's new state
-      //$new_state = $edit['target_state']; // change to specific state not yet implemented
-      $new_state = '';
-      if ($new_state == '') {
-        $choices = workflow_field_choices($node);
-        foreach ($choices as $sid => $name) {
-          if (isset($flag)) {
-            $new_state = $sid;
-            $new_state_name = $name;
-            break;
+      
+      if(isset($edit['wf_child_state_'.$node->type]) && $edit['wf_child_state_'.$node->type] > 0) {
+        watchdog('workflow', t('Changing workflow state of node %nid child nodes.', array('%nid' => $node->nid)));
+        // query based on node type
+        if($node->type == 'worklog-invoice') {
+          $result = db_query(db_rewrite_sql('SELECT n.* FROM {node} n LEFT JOIN {worklog} w ON n.nid = w.nid LEFT JOIN {worklog_invoice_map} wim ON wim.worklog_nid = n.nid WHERE n.type=\'worklog\' AND n.status = 1 AND wim.invoice_nid = %d'), $node->nid);
+        } else if($node->type == 'book') {
+          $result = db_query(db_rewrite_sql('SELECT n.* FROM {node} n LEFT JOIN {book} b ON n.nid = b.nid WHERE n.type=\'book\' AND n.status = 1 AND b.parent = %d'), $node->nid);
+        } else {
+          watchdog('workflow', t('Failed changing workflow state of children of node id %id to %state because node type %type query missing', array('%id' => intval($child_node->nid), '%state' => check_plain($new_state_name), '%type' => $node->type)));
+          return;
+        }
+        while ($child_node = db_fetch_object($result)) {
+          $child_node->workflow = workflow_node_current_state($child_node);
+          
+          if(isset($edit['wf_change_'.$child_node->type.'_state']) && $edit['wf_change_'.$child_node->type.'_state'] > 0) {
+            $new_state = $edit['wf_default_state_'.$child_node->type];
+          } else {
+            $new_state = '';
           }
-          if ($sid == $current_state) {
-            $flag = TRUE;
+          if ($new_state == '') {
+            $choices = workflow_field_choices($child_node);
+            foreach ($choices as $sid => $name) {
+              if (isset($flag)) {
+                $new_state = $sid;
+                $new_state_name = $name;
+                break;
+              }
+              if ($sid == $current_state) {
+                $flag = TRUE;
+              }
+            }
           }
+          // fire the transition
+          watchdog('action', t('Changing workflow state of node id %id to %state', array('%id' => intval($child_node->nid), '%state' => check_plain($new_state_name))));
+          workflow_execute_transition($child_node, $new_state);
+          watchdog('action', t('Changed workflow state of node id %id to %state', array('%id' => intval($child_node->nid), '%state' => check_plain($new_state_name))));
         }
+      } else {
+        if(isset($edit['wf_change_'.$node->type.'_state']) && $edit['wf_change_'.$node->type.'_state'] > 0) {
+          $new_state = $edit['wf_default_state_'.$node->type];
+        } else {
+          $new_state = '';
+        }
+        if ($new_state == '') {
+          $choices = workflow_field_choices($node);
+          foreach ($choices as $sid => $name) {
+            if (isset($flag)) {
+              $new_state = $sid;
+              $new_state_name = $name;
+              break;
+            }
+            if ($sid == $current_state) {
+              $flag = TRUE;
+            }
+          }
+        }
+        // fire the transition
+        watchdog('action', t('Changing workflow state of node id %id to %state', array('%id' => intval($node->nid), '%state' => check_plain($new_state_name))));
+        workflow_execute_transition($node, $new_state);
+        watchdog('action', t('Changed workflow state of node id %id to %state', array('%id' => intval($node->nid), '%state' => check_plain($new_state_name))));
       }
-
-      // fire the transition
-      watchdog('action', t('Changing workflow state of node id %id to %state', array('%id' => intval($node->nid), '%state' => check_plain($new_state_name))));
-      workflow_execute_transition($node, $new_state);
-      watchdog('action', t('Changed workflow state of node id %id to %state', array('%id' => intval($node->nid), '%state' => check_plain($new_state_name))));
       break;
 
-    case 'metadata':
-      return array(
-        'description' => t('Change workflow state of a node to next state'),
-        'type' => t('Workflow'),
-        'batchable' => TRUE,
-        'configurable' => FALSE,
-      );
-
     // return an HTML config form for the action
     case 'form':
-      return '';
+      $form = array();
+      // add form components
+      $result1 = db_query(db_rewrite_sql('SELECT wtm.* FROM {workflow_type_map} wtm WHERE wtm.wid > 0'));
+      while ($workflow_type = db_fetch_object($result1)) {
+        if (!isset($edit['wf_change_'.$workflow_type->type.'_state'])) $edit['wf_change_'.$workflow_type->type.'_state'] = 0;
+        if (!isset($edit['wf_default_state_'.$workflow_type->type])) $edit['wf_default_state_'.$workflow_type->type] = 0;
+        
+        $options = array(0 => t('No'), 1 => t('Yes'));
+        $form['wf_change_'.$workflow_type->type.'_state'] = array(
+          '#type' => 'select',
+          '#title' => t($workflow_type->type.' default?'),
+          '#default_value' => $edit['wf_change_'.$workflow_type->type.'_state'],
+          '#options' => $options,
+          '#description' => t('Allow default action state for'.$workflow_type->type.'.'),
+        );
+        $result2 = db_query(db_rewrite_sql('SELECT ws.sid,ws.state FROM {workflow_states} ws WHERE ws.wid = %s'),$workflow_type->wid);
+        $options = array(0 => t('Choose One'));
+        while ($workflow_state = db_fetch_object($result2)) {
+          $options[$workflow_state->sid] = t($workflow_state->state);
+        }
+        $form['wf_default_state_'.$workflow_type->type] = array(
+          '#type' => 'select',
+          '#title' => t($workflow_type->type.'?'),
+          '#default_value' => $edit['wf_default_state_'.$workflow_type->type],
+          '#options' => $options,
+          '#description' => t('Which state to switch to '.$workflow_type->type.'.'),
+        );
+        if(module_exist('worklog')) {
+          $options = array(0 => t('No'), 1 => t('Yes'));
+          $form['wf_child_state_'.$workflow_type->type] = array(
+            '#type' => 'select',
+            '#title' => t($workflow_type->type.' change child state too?'),
+            '#default_value' => $edit['wf_child_state_'.$workflow_type->type],
+            '#options' => $options,
+            '#description' => t('Switch children states '.$workflow_type->type.' as well(module worklog must be activated).'),
+          );
+        }
+      }
+      return $form;
 
     // validate the HTML form
     case 'validate':
-      return TRUE;
+      $errors = array();
+      $result1 = db_query(db_rewrite_sql('SELECT wtm.* FROM {workflow_type_map} wtm WHERE wtm.wid > 0'));
+      while ($workflow_type = db_fetch_object($result1)) {
+        if($edit['wf_change_'.$workflow_type->type.'_state'] == 1 && $edit['wf_default_state_'.$workflow_type->type] == 0) {
+          $errors['wf_default_state_'.$workflow_type->type] = t('Please choose a default state or turn off default action state.');
+        }
+      }
+      foreach ($errors as $name => $message) {
+        form_set_error($name, $message);
+      }
+
+      return count($errors) == 0;
 
     // process the HTML form to store configuration
     case 'submit':
-      return '';
+      $params = array();
+      $result = db_query(db_rewrite_sql('SELECT wtm.* FROM {workflow_type_map} wtm WHERE wtm.wid > 0'));
+      while ($workflow_type = db_fetch_object($result)) {
+        $params['wf_change_'.$workflow_type->type.'_state'] = $edit['wf_change_'.$workflow_type->type.'_state'];
+        $params['wf_default_state_'.$workflow_type->type] = $edit['wf_default_state_'.$workflow_type->type];
+        $params['wf_child_state_'.$workflow_type->type] = $edit['wf_child_state_'.$workflow_type->type];
+      }
+      return $params;
   }
 }
 
@@ -1595,4 +1691,4 @@
       $state = db_fetch_object(db_query("SELECT state FROM {workflow_states} WHERE sid=%d", $query));
       return $state->state;
   }
-}
\ No newline at end of file
+}
