You are here

function scheduler_nodeapi in Scheduler 6

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

Implementation of hook_nodeapi().

File

./scheduler.module, line 591

Code

function scheduler_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  // Run $op == load for any user.
  if ($op == 'load') {
    $publishing_enabled = variable_get('scheduler_publish_enable_' . $node->type, 0) == 1;
    $unpublishing_enabled = variable_get('scheduler_unpublish_enable_' . $node->type, 0) == 1;
    if (isset($node->nid) && $node->nid && ($publishing_enabled || $unpublishing_enabled)) {
      $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'];
          $row['published'] = $row['publish_on'] ? date(variable_get('date_format_long', 'l, F j, Y - H:i'), $row['publish_on']) : NULL;
          $row['unpublished'] = $row['unpublish_on'] ? date(variable_get('date_format_long', 'l, F j, Y - H:i'), $row['unpublish_on']) : NULL;
          $node->scheduler = $row;
        }
      }
    }
  }
  elseif ($op == 'view') {
    if (isset($a4) && $a4 && isset($node->unpublish_on) && $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 . '" />');
    }
  }
  elseif (user_access('schedule (un)publishing of nodes')) {
    switch ($op) {
      case 'validate':
      case 'presave':

        // adjust the entered times for timezone consideration.
        // Note, we must check to see if the value is numeric,
        // if it is, assume we have already done the strtotime
        // conversion. This prevents us running strtotime on
        // a value we have already converted. This is needed
        // because DRUPAL6 removed 'submit' and added 'presave'
        // and all this happens at different times.
        $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT);
        if (isset($node->publish_on) && $node->publish_on && !is_numeric($node->publish_on)) {
          $publishtime = _scheduler_strtotime($node->publish_on);
          if ($publishtime === FALSE) {
            form_set_error('publish_on', t("The 'publish on' value does not match the expected format of %time", array(
              '%time' => format_date(time(), 'custom', $date_format),
            )));
          }
          elseif ($publishtime && $publishtime < time()) {
            form_set_error('publish_on', t("The 'publish on' date must be in the future"));
          }
          else {
            $node->publish_on = $publishtime;
          }
        }
        if (isset($node->unpublish_on) && $node->unpublish_on && !is_numeric($node->unpublish_on)) {
          $unpublishtime = _scheduler_strtotime($node->unpublish_on);
          if ($unpublishtime === FALSE) {
            form_set_error('unpublish_on', t("The 'unpublish on' value does not match the expected format of %time", array(
              '%time' => format_date(time(), 'custom', $date_format),
            )));
          }
          elseif ($unpublishtime && $unpublishtime < time()) {
            form_set_error('unpublish_on', t("The 'unpublish on' date must be in the future"));
          }
          else {
            $node->unpublish_on = $unpublishtime;
          }
        }
        if (isset($publishtime) && isset($unpublishtime) && $unpublishtime < $publishtime) {
          form_set_error('unpublish_on', t("The 'unpublish on' date must be later than the 'publish on' date."));
        }

        // 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
        if (isset($node->publish_on) && $node->publish_on != '' && is_numeric($node->publish_on) && $node->publish_on > time()) {
          $node->status = 0;
          drupal_set_message(t('This post is unpublished and will be published @publish_time.', array(
            '@publish_time' => format_date($node->publish_on, 'custom', $date_format),
          )), 'status', FALSE);
        }
        break;
      case 'insert':

        // only insert into database if we need to (un)publish this node at some date
        if (isset($node->nid) && $node->nid && (isset($node->publish_on) && $node->publish_on != NULL) || isset($node->unpublish_on) && $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));
          $publish_on = isset($node->publish_on) ? $node->publish_on : NULL;
          $unpublish_on = isset($node->unpublish_on) ? $node->unpublish_on : NULL;

          // 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->status == 0 && $publish_on || $unpublish_on) {
              db_query('UPDATE {scheduler} SET publish_on = %d, unpublish_on = %d WHERE nid = %d', $publish_on, $unpublish_on, $node->nid);
            }
            else {
              db_query('DELETE FROM {scheduler} WHERE nid = %d', $node->nid);
            }
          }
          elseif ($publish_on || $unpublish_on) {
            db_query('INSERT INTO {scheduler} (nid, publish_on, unpublish_on) VALUES (%d, %d, %d)', $node->nid, $publish_on, $unpublish_on);
          }
        }
        break;
      case 'delete':
        if (isset($node->nid) && $node->nid) {
          db_query('DELETE FROM {scheduler} WHERE nid = %d', $node->nid);
        }
        break;
    }
  }
}