d2 1
a2 1
// $Id: workflow.module,v 1.1.1.1 2007/11/11 23:02:28 cvsroot Exp $
d198 1
a198 2
  workflow_process_update($node, 'update');
  // node_save($node);
d236 62
a297 1
      workflow_process_update($node, $op);
a1949 66

function workflow_process_update(&$node, $op) {

  // stop if no workflow for this node type
  $wid = workflow_get_workflow_for_type($node->type);
  if (!$wid) {
    break;
  }

  // get new state
  $sid = $node->workflow;
  if (!$sid && $op == 'insert') { // if not specified, use first valid
    $choices = workflow_field_choices($node);
    $keys = array_keys($choices);
    $sid = array_shift($keys);
  }

  // make sure new state is a valid choice
  if (array_key_exists($sid, workflow_field_choices($node))) {
    // check to see if this is an immediate change or a scheduled change
    if (!$node->workflow_scheduled) {
      workflow_execute_transition($node, $sid, $node->workflow_comment); // do transition
    }
    else { // schedule the the time to change the state
      $nid = $node->nid;
      $comment = $node->workflow_comment;
      $old_sid = workflow_node_current_state($node);
      
      if ($node->workflow_scheduled_date['day'] < 10) {
         $node->workflow_scheduled_date['day'] = '0' . 
           $node->workflow_scheduled_date['day'];
      }
      
      if ($node->workflow_scheduled_date['month'] < 10) {
        $node->workflow_scheduled_date['month'] = '0' . 
           $node->workflow_scheduled_date['month'];
      }
      
      if (!$node->workflow_scheduled_hour) {
        $node->workflow_scheduled_hour = '00:00';
      }
      
      $scheduled = $node->workflow_scheduled_date['year'] . 
                   $node->workflow_scheduled_date['month'] . 
                   $node->workflow_scheduled_date['day'] .
                   ' ' . $node->workflow_scheduled_hour . 'Z';
      if ($scheduled = strtotime($scheduled)) {
        // adjust for user and site timezone settings
        global $user;
        if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
          $timezone = $user->timezone;
        }
        else {
          $timezone = variable_get('date_default_timezone', 0);
        }
        $scheduled = $scheduled - $timezone;
        
        // clear previous entries and insert
        db_query("DELETE FROM {workflow_scheduled_transition} WHERE nid = $nid");
        db_query('INSERT INTO {workflow_scheduled_transition} VALUES (%d, %d, %d, %d,\'%s\')', $nid, $old_sid, $sid, $scheduled, $comment);
        
        drupal_set_message("$node->title is scheduled for state change on " . format_date($scheduled));
      }
    }
  }
}
