You are here

function workflow_nodeapi in Workflow 5

Same name and namespace in other branches
  1. 5.2 workflow.module \workflow_nodeapi()
  2. 6.2 workflow.module \workflow_nodeapi()
  3. 6 workflow.module \workflow_nodeapi()

Implementation of hook_nodeapi(). Summary of nodeapi ops we can see (Drupal 4.7):

preview submit submit preview (from (from (from (from add add) add) view edit edit) edit) -------- -------- -------- ----- ------- -------- -------- load load load load prepare prepare prepare prepare prepare prepare validate validate validate validate view view view submit submit insert update

File

./workflow.module, line 218

Code

function workflow_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'load':
      $node->_workflow = workflow_node_current_state($node);

      // scheduling information
      $res = db_query('SELECT * FROM {workflow_scheduled_transition} WHERE nid = %d', $node->nid);
      if ($row = db_fetch_object($res)) {
        $node->_workflow_scheduled_sid = $row->sid;
        $node->_workflow_scheduled_timestamp = $row->scheduled;
        $node->_workflow_scheduled_comment = $row->comment;
      }
      break;
    case 'insert':
    case 'update':

      // 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(t("@node_title is scheduled for state change on !scheduled_date", array(
              "@node_title" => $node->title,
              "!scheduled_date" => format_date($scheduled),
            )));
          }
        }
      }
      break;
    case 'delete':
      db_query("DELETE FROM {workflow_node} WHERE nid = %d", $node->nid);
      break;
  }
}