259d258
<           $old_sid = workflow_node_current_state($node);
290,294c289
<             // 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));
---
>             workflow_schedule_transition($node, $sid, $scheduled, $comment);
476a472,476
>   
>   // clear any references in the scheduled listing
>   // we must do this before the transition post call in order to all 
>   // related actions to schedule a future transition
>   db_query('DELETE FROM {workflow_scheduled_transition} WHERE nid = %d', $node->nid);
481,483d480
<   
<   // clear any references in the scheduled listing
<   db_query('DELETE FROM {workflow_scheduled_transition} WHERE nid = %d', $node->nid);
548a546,763
>  * Schedule a transition (change state of a node).
>  *
>  * @param object $node The node to be modified
>  * @param int $sid The id of the next state
>  * @param date $timestamp The scheduled date of the transition
>  * @param string $comment A comment for the transition
>  * @return bool TRUE if successful, FALSE otherwise
>  */
> function workflow_schedule_transition($node, $sid, $timestamp, $comment = NULL) {
>   $old_sid = workflow_node_current_state($node);
>   
>   // Make sure this is a valid transition.  We don't really care if it is allowed by the
>   // current user as it would have to be setup by an admin.  It may very well not be
>   // allowed by the current user and that's fine.  That being said, the function that
>   // calls this one should ensure proper access prior to calling
>   $tid = workflow_get_transition_id($old_sid, $sid);
>   if (!$tid) {
>     watchdog('workflow', t('Attempt to schedule nonexistent transition (from %old to %new)', array('%old' => $old_sid, '%new' => $sid), WATCHDOG_ERROR));
>       return FALSE;
>   }
> 
>   // get the names of the old and new state for pretty log entry
>   $old_state = workflow_get_state($old_sid);
>   $old_name = $old_state['state'];
>   $new_state = workflow_get_state($sid);
>   $new_name = $new_state['state'];
> 
>   // make the entry
>   $nid = $node->nid;
> 
>   // 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, $timestamp, $comment);
> 
>   watchdog('workflow', t('Scheduled transition from %old to %new for %title on %date', array('%old' => $old_name, '%new' => $new_name, '%title' => $node->title, '%date' => format_date($timestamp))));
> 
>   drupal_set_message("$node->title is scheduled for state change on " . format_date($timestamp));
> }
> 
> /**
>  * Implementation of a Drupal action.
>  * Schedules a state change
>  *
>  */
> function action_workflow_schedule_transition($op, $edit = array(), &$node) {
>   switch($op) {
>     case 'do':
>       $sid = intval($edit['next_state']);
>       $timestamp = time();
>       if ($edit['schedule_type'] == 0)
>       {
>         $timestamp = strtotime($edit['timeoffset'].' '.$edit['offsetunits']);
>       }
>       else
>       {
>         $timestamp = format_date($edit['date'], 'custom', 'Y') . 
>                       format_date($edit['date'], 'custom', 'n') . 
>                       format_date($edit['date'], 'custom', 'j') .
>                       ' ' . $edit['hour'] . 'Z';
>         if ($timestamp = strtotime($timestamp)) {
>           // 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);
>           }
>           $timestamp = $timestamp - $timezone;
>         }
>       }
>       if (!$timestamp)
>       {
>         watchdog('workflow', t('Error generating timestamp for scheduled transition'), WATCHDOG_ERROR);
>       }
>       else
>       {
>         workflow_schedule_transition($node, $sid, $timestamp, $edit['comment']);
>       }
>       break;
> 
>     case 'metadata':
>       return array(
>         'description' => t('Schedule workflow state change'),
>         'type' => t('Workflow'),
>         'batchable' => TRUE,
>         'configurable' => TRUE,
>       );
> 
>     // return an HTML config form for the action
>     case 'form':
>       //if (!isset($edit['next_state'])) $edit['next_state'] = '';
>       if (!isset($edit['date'])) $edit['date'] = time();
>       if (!isset($edit['hour'])) $edit['hour'] = '00:00';
>       //if (!isset($edit['timeoffset'])) $edit['timeoffset'] = 0;
>       if (!isset($edit['offsetunits'])) $edit['offsetunits'] = 'day';
>       if (!isset($edit['schedule_type'])) $edit['schedule_type'] = 0;
>       //if (!isset($edit['comment'])) $edit['comment'] = '';
>       $form = array();
> 
>       $states = array();
> 
>       $workflows = workflow_get_all();
>       foreach ($workflows AS $wid => $wname)
>       {
>         $wstates = workflow_get_states($wid);
>         foreach ($wstates AS $sid => $sname)
>         {
>           if (!workflow_is_system_state($sname))
>           {
>             $states[$sid] = $wname.'::'.$sname;
>           }
>         }
>       }
> 
>       $form['next_state'] = array(
>         '#type' => 'select',
>         '#title' => t('Next State'),
>         '#options' => $states,
>         '#description' => t('Select which state to transition to'),
>         '#default_value' => $edit['next_state'],
>       );
> 
>       $form['schedule_type'] = array (
>         '#type' => 'radios',
>         '#title' => t('Schedule Type'),
>         '#options' => array (
>           t('Offset'),
>           t('Specific Date'),
>         ),
>         '#default_value' => $edit['schedule_type'],
>       );
> 
>       $form['timeoffset'] = array(
>         '#type' => 'textfield',
>         '#title' => t('Offset'),
>         '#description' => t('Please enter the time offset you wish the transition to be scheduled for.  The transition will be scheduled for the given number of seconds from the time in which this action is executed'),
>         '$default_value' => $edit['timeoffset'],
>       );
> 
>       $form['offsetunits'] = array(
>         '#type' => 'select',
>         '#title' => t('Offset Units'),
>         '#options' => array(
>           'sec' => t('Seconds'),
>           'min' => t('Minutes'),
>           'hour' => t('Hours'),
>           'day' => t('Days'),
>           'week' => t('Weeks'),
>           'month' => t('Months')
>         ),
>         '#default_value' => $edit['offsetunits'],
>         '#description' => t('Units for the specified offset.  Actions will only happen at cron runs, so the effectiveness of shorter units will depend on how frequently your cron runs.'),
>       );
> 
>       $form['date'] = array (
>         '#type' => 'date',
>         '#title' => t('Date'),
>         '#default_value' => array('day' => format_date($edit['date'], 'custom', 'j'),
>                               'month' => format_date($edit['date'], 'custom', 'n'),
>                               'year' => format_date($edit['date'], 'custom', 'Y')),
>       );
>   
>       $form['hour'] = array (
>         '#type' => 'textfield',
>         '#description' => t('Please enter a time in 24 hour (eg. HH:MM) format. If no date is included, the default will be midnight on the specified date. The current time is: ') . format_date(time()),
>         '#default_value' => $edit['hour'],
>       );
>     
>       $form['comment'] = array(
>         '#type' => 'textarea',
>         '#title' => t('Comment'),
>         '#description' => t('A comment to put in the workflow log.'),
>         '#default_value' => $edit['comment'],
>       );
> 
>       return $form;
> 
>     // validate the HTML form
>     case 'validate':
>       $errors = array();
> 
>       if ($edit['schedule_type'] == 0)
>       {
>         /* Validate offset was given */
>         if (!is_numeric($edit['timeoffset']))
>         {
>           $errors['timeoffset'] = t('Please provide a numeric value');
>         }
>       }
>       else
>       {
>         /* Validate hours format */
>         if (preg_match('/^\s*$/',$edit['hour']) == 0 &&
>             preg_match('/^\d{2}\:\d{2}$/',$edit['hour'] == 0))
>         {
>           $errors['hour'] = t('Please provide the hour in HH:MM format');
>         }
>       }
> 
>       return count($errors) == 0;
> 
>     // process the HTML form to store configuration
>     case 'submit':
>       $params = array(
>         'next_state' => $edit['next_state'],
>         'schedule_type' => $edit['schedule_type'],
>         'timeoffset' => $edit['timeoffset'],
>         'offsetunits' => $edit['offsetunits'],
>         'date' => $edit['date'],
>         'hour' => $edit['hour'],
>         'comment' => $edit['comment']
>       );
>       return $params;
>   }
> }
> 
> /**
