You are here

function node_expire_nodeapi in Node expire 6

Same name and namespace in other branches
  1. 5 node_expire.module \node_expire_nodeapi()
  2. 6.2 node_expire.module \node_expire_nodeapi()

Prepare and parse the data from our node entry forms.

@todo Propegate to child nodes.

File

./node_expire.module, line 200

Code

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

  // Check if the Node Expire feature is enabled for the node type
  $curdefaults = variable_get('node-expire-node-visibility', array());
  if (empty($curdefaults[$node->type])) {
    return;
  }
  switch ($op) {
    case 'load':
      $query = db_query('SELECT expire, expire_timefrom, expiremode, isroot
        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->expiremode = $row->expiremode;
        $node->expire_timefrom = $row->expire_timefrom;
        $node->isroot = $row->isroot;
        $node->node_expire = 'nid';
      }
      break;
    case 'prepare':
      $curdefaults = $curdefaults[$node->type];

      // Use the existing expiration data if present.
      if (empty($node->node_expire)) {

        // If this is a new book page, and inheritance is enabled, lets inherit the data.
        if ($node->type == 'book' and variable_get('node-expire-book-props', 1) and arg(4) > 0 and $query = db_query('SELECT expire, expire_timefrom, expiremode FROM {node_expire} WHERE nid = %d', arg(4))) {

          // If, for whatever reason, no parent data is available, use the defaults
          $row = db_fetch_object($query);
          $node->expire = $row->expire;
          $node->expire_timefrom = $row->expire_timefrom;
          $node->expiremode = $row->expiremode;
          $node->isroot = 0;
        }
        else {
          $node->expire_timefrom = $curdefaults['expire_timefrom'];
          $node->isroot = $curdefaults['isroot'];
          $node->expiremode = $curdefaults['expiremode'];
          if ($curdefaults['expiremode'] == NODE_EXPIRE_ONUPDATE) {
            $node->expire = format_date(strtotime($curdefaults['expire_timefrom']), 'custom', NODE_EXPIRE_FORMAT);
          }
          else {
            $node->expire = format_date(strtotime($curdefaults['expire']), 'custom', NODE_EXPIRE_FORMAT);
          }
        }
      }
      break;
    case 'validate':

      // The only restriction we have is that the node can't expire in the past.
      if ($node->expiremode == NODE_EXPIRE_DATE) {
        if ($node->expire <= 0) {
          form_set_error('expire_date', t('You have to specify a valid date.'));
        }
        elseif (strtotime($node->expire) <= time()) {
          form_set_error('expire_date', t("You can't expire a node in the past!"));
        }
      }
      break;
    case 'update':
    case 'insert':

      // Do we need to save data in the database?
      $save = TRUE;

      // We only care about the defaults for this node type
      $curdefaults = $curdefaults[$node->type];
      if ($node->expiremode == NODE_EXPIRE_DATE) {
        $node->expire = strtotime($node->expire);
        $node->expire_timefrom = '';
      }
      elseif ($node->expiremode == NODE_EXPIRE_ONUPDATE) {
        $node->expire = strtotime($node->expire_timefrom);
      }
      else {
        $node->expiremode = NODE_EXPIRE_NONE;
        $node->expire = 0;
        $node->expire_timefrom = 0;
      }

      // Propagate the settings to the child nodes, only if enabled. Notice
      // this is in the section for people with access to edit these settings.
      // It's a waste of resources to perform the recursion task as nothing
      // will be changed.
      if ($node->type == 'book' and variable_get('node-expire-book-props', 1)) {
        module_load_include('book.inc', 'node_expire');
        _node_expire_propagate_book($node->nid, $node->expire, $node);
      }

      // To keep track of inheritances and other such things, every node records
      // its expiration settings, not just ones set to expire.
      if ($save) {
        drupal_write_record('node_expire', $node, $node->node_expire);
      }
      break;
    case 'delete':
      db_query('DELETE FROM {node_expire} WHERE nid = %d', $node->nid);
      break;
  }
}