You are here

function _node_expire_nodeapi in Node expire 6.2

Implementation of hook_nodeapi().

1 call to _node_expire_nodeapi()
node_expire_nodeapi in ./node_expire.module
Implementation of hook_nodeapi().

File

./node_expire.nodeapi.inc, line 11
Node API integration.

Code

function _node_expire_nodeapi(&$ntypes, &$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'load':
      $query = db_query('SELECT expire, expired, lastnotify
        FROM {node_expire} WHERE nid = %d', $node->nid);

      // Use the existing expiration data if present.
      if ($row = db_fetch_object($query)) {
        $node->expire = $row->expire;
        $node->expired = $row->expired;
        $node->lastnotify = $row->lastnotify;
      }
      break;
    case 'prepare':

      // To prevent default value 1969-12-31 check also $ntypes['default'].
      if (!isset($node->expire) && $ntypes['default']) {
        $node->expire = format_date(strtotime($ntypes['default']), 'custom', NODE_EXPIRE_FORMAT);
      }

      // This gives a way to users without edit exipration permission to update nodes with default expiration.
      if (isset($node->expire) && !user_access('edit node expire')) {
        $node->expire = format_date(strtotime($ntypes['default']), 'custom', NODE_EXPIRE_FORMAT);
      }
      break;
    case 'validate':

      // The only restriction we have is that the node can't expire in the past.
      if ($node->expire == '') {
        if (!empty($ntypes['required']) && $ntypes['default']) {
          form_set_error('expire_date', t('You must choose an expiration date.'));
        }
      }
      elseif (!($expire = strtotime($node->expire)) or $expire <= 0) {
        form_set_error('expire_date', t('You have to specify a valid expiration date.'));
      }
      elseif ($expire <= time()) {
        form_set_error('expire_date', t("You can't expire a node in the past!"));
      }
      elseif (!empty($ntypes['max']) and $expire > strtotime($ntypes['max'], $node->created)) {
        form_set_error('expire_date', t('It must expire before %date.', array(
          '%date' => format_date(strtotime($ntypes['max'], $node->created), 'custom', NODE_EXPIRE_FORMAT),
        )));
      }
      break;
    case 'update':
    case 'insert':

      // has the expiration been removed, or does it exist?
      if (isset($node->expire)) {
        db_query('DELETE FROM {node_expire} WHERE nid = %d', $node->nid);

        // should we create a new record?
        if ($node->expire) {
          if (strtotime($node->expire)) {
            $node->expire = strtotime($node->expire);
          }
          $node->expired = FALSE;
          drupal_write_record('node_expire', $node);
        }
      }
      break;
    case 'delete':
      db_query('DELETE FROM {node_expire} WHERE nid = %d', $node->nid);
      break;
  }
}