You are here

function scheduler_nodeapi in Scheduler 5

Same name and namespace in other branches
  1. 6 scheduler.module \scheduler_nodeapi()

Implementation of hook_nodeapi().

File

./scheduler.module, line 341

Code

function scheduler_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {

  // Run $op == load for any user.
  if ($op == 'load') {
    if (isset($node->nid) && $node->nid && variable_get('scheduler_' . $node->type, 0) == 1) {
      $result = db_query('SELECT * FROM {scheduler} WHERE nid = %d', $node->nid);
      if ($result) {
        $row = db_fetch_array($result);
        if (isset($row['nid'])) {
          unset($row['nid']);
          $node->publish_on = $row['publish_on'];
          $node->unpublish_on = $row['unpublish_on'];
          $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT);
          $row['published'] = $row['publish_on'] ? format_date($row['publish_on'], 'custom', $date_format) : NULL;
          $row['unpublished'] = $row['unpublish_on'] ? format_date($row['unpublish_on'], 'custom', $date_format) : NULL;
          $node->scheduler = $row;
        }
      }
    }
  }
  elseif (user_access('schedule (un)publishing of nodes')) {
    switch ($op) {
      case 'view':
        if ($page && $node->unpublish_on) {
          $unavailable_after = date("d-M-Y H:i:s T", $node->unpublish_on);
          drupal_set_html_head('<meta name="googlebot" content="unavailable_after: ' . $unavailable_after . '">');
        }
        break;
      case 'submit':

        //adjust the entered times for timezone consideration
        $node->publish_on = _scheduler_strtotime($node->publish_on);
        $node->unpublish_on = _scheduler_strtotime($node->unpublish_on);

        // right before we save the node, we need to check if a "publish on" value has been set
        // if it has been set, we want to make sure the node is unpublished
        // since it will be published at a later date (but only if the value is in the future.
        if ($node->publish_on != '' && is_numeric($node->publish_on) && $node->publish_on > time()) {
          $node->status = 0;
        }
        break;
      case 'insert':

        //only insert into database if we need to (un)publish this node at some date
        if (isset($node->nid) && $node->nid && $node->publish_on != NULL || $node->unpublish_on != NULL) {
          db_query('INSERT INTO {scheduler} (nid, publish_on, unpublish_on) VALUES (%d, %d, %d)', $node->nid, $node->publish_on, $node->unpublish_on);
        }
        break;
      case 'update':
        if (isset($node->nid) && $node->nid) {
          $exists = db_result(db_query('SELECT nid FROM {scheduler} WHERE nid = %d', $node->nid));

          // if this node has already been scheduled, update its record
          if ($exists) {

            // only update database if we need to (un)publish this node at some date
            // otherwise the user probably cleared out the (un)publish dates so we should remove the record
            if ($node->publish_on != NULL || $node->unpublish_on != NULL) {
              db_query('UPDATE {scheduler} SET publish_on = %d, unpublish_on = %d WHERE nid = %d', $node->publish_on, $node->unpublish_on, $node->nid);
            }
            else {
              db_query('DELETE FROM {scheduler} WHERE nid = %d', $node->nid);
            }
          }
          else {
            if ($node->publish_on != NULL || $node->unpublish_on != NULL) {
              db_query('INSERT INTO {scheduler} (nid, publish_on, unpublish_on) VALUES (%d, %d, %d)', $node->nid, $node->publish_on, $node->unpublish_on);
            }
          }
        }
        break;
      case 'delete':
        if (isset($node->nid) && $node->nid) {
          db_query('DELETE FROM {scheduler} WHERE nid = %d', $node->nid);
        }
        break;
    }
  }
}