You are here

function node_expire_nodeapi in Node expire 5

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

Prepare and parse the data from our node entry forms.

File

./node_expire.module, line 712
Alerts administrators of possibly outdated materials and optionally unpublishes them.

Code

function node_expire_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'view':
    case 'print':
      $query = db_query('SELECT expire, expiremode FROM {node_expire} WHERE nid = %d', $node->nid);

      // Use the existing expiration data if present.
      if (db_num_rows($query) > 0) {
        $row = db_fetch_object($query);
        $node->expire = $row->expire ? format_date(strtotime($row->expire), 'large') : '';
        $node->expiration_type = $row->expiremode;
      }
      else {
        $node->expire = '';
        $node->expiration_type = 'none';
      }
      break;
    case 'prepare':

      // Is the expiration feature enabled for this node type?
      $curdefaults = variable_get('node-expire-node-visibility', array());
      if (!isset($curdefaults[$node->type]) || !user_access('edit expirations')) {
        $node->expire_disabled = true;
      }
      else {
        $curdefaults = $curdefaults[$node->type];
        $query = db_query('SELECT expire, expiresec, expiremode, isroot FROM {node_expire} WHERE nid = %d', $node->nid);

        // Use the existing expiration data if present.
        if (db_num_rows($query) > 0) {
          $row = db_fetch_object($query);
          $node->expiration_type = $row->expiremode;
          $node->isroot = $row->isroot;
          if ($node->expiration_type == 'none') {
            $node->expire_timefrom = $curdefaults['expire_timefrom'];
            $node->expire = format_date(strtotime($curdefaults['expire_timefrom']), 'custom', 'Y-m-d H:i:s');
          }
          else {
            $node->expire = $row->expire;
            $node->expire_timefrom = $row->expiresec;
          }
        }
        else {

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

            // If, for whatever reason, no parent data is available, use the defaults
            if (db_num_rows($query) == 0) {
              $node->expire = format_date(strtotime($curdefaults['expire_timefrom']), 'custom', 'Y-m-d H:i:s');
              $node->expire_timefrom = $curdefaults['expire_timefrom'];
              $node->isroot = $curdefaults['isroot'];
              $node->expiration_type = $curdefaults['expiration_type'];
              if ($curdefaults['expiration_type'] == 'onupdate') {
                $node->expire = format_date(strtotime($curdefaults['expire_timefrom']), 'custom', 'Y-m-d H:i:s');
              }
              else {
                $node->expire = format_date(strtotime($curdefaults['expire']), 'custom', 'Y-m-d H:i:s');
              }
            }
            else {
              $row = db_fetch_object($query);
              $node->expire = $row->expire;
              $node->expire_timefrom = $row->expiresec;
              $node->expiration_type = $row->expiremode;
              $node->isroot = 0;
            }
          }
          else {
            $node->expire = format_date(strtotime($curdefaults['expire_timefrom']), 'custom', 'Y-m-d H:i:s');
            $node->expire_timefrom = $curdefaults['expire_timefrom'];
            $node->isroot = $curdefaults['isroot'];
            $node->expiration_type = $curdefaults['expiration_type'];
            if ($curdefaults['expiration_type'] == 'onupdate') {
              $node->expire = format_date(strtotime($curdefaults['expire_timefrom']), 'custom', 'Y-m-d H:i:s');
            }
            else {
              $node->expire = format_date(strtotime($curdefaults['expire']), 'custom', 'Y-m-d H:i:s');
            }
          }
        }
      }
      break;
    case 'validate':

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

      // We only want to deal with the database if the expiration feature is available for this node type
      $curdefaults = variable_get('node-expire-node-visibility', array());
      if (isset($curdefaults[$node->type])) {

        // Do we need to update the database?
        $update = 1;

        // We only care about the defaults for this node type
        $curdefaults = $curdefaults[$node->type];

        // Does this user have access to change any expiration settings?
        if (!user_access('edit expirations')) {

          // Does this node already have data for us to work with?
          $query = db_query('SELECT expire, expiresec, expiremode, isroot FROM {node_expire} WHERE nid = %d', $node->nid);
          if (db_num_rows($query) > 0) {
            $row = db_fetch_object($query);

            // Let's keep our current settings
            $node->expire = $row->expire;
            $node->expire_timefrom = $row->expiresec;
            $node->expiration_type = $row->expiremode;
            $node->isroot = $row->expiresec ? 1 : 0;

            // If this node is an "on update" expiration, update the expiration time.
            if ($node->expiration_type == 'onupdate') {
              $node->expire = strtotime($node->expire_timefrom);
            }
            else {
              $update = 0;
            }
          }
          else {
            if ($node->type == 'book' && variable_get('node-expire-book-props', 1) && $node->parent > 0) {
              $query = db_query('SELECT expire, expiresec, expiremode FROM {node_expire} WHERE nid = %d', $node->parent);
              if (db_num_rows($query) > 0) {
                $row = db_fetch_object($query);
                $node->expire = $row->expire;
                $node->expire_timefrom = $row->expiresec;
                $node->expiration_type = $row->expiremode;
                $node->isroot = 0;

                // If the parent node is set to base expiration off last update time, let's mark this node accordingly.
                if ($node->expiration_type == 'onupdate') {
                  $node->expire = strtotime($node->expire_timefrom);
                }
              }
              else {
                $node->expire = strtotime($curdefaults['expire']);
                $node->expire_timefrom = $curdefaults['expire_timefrom'];
                $node->isroot = $curdefaults['isroot'];
                $node->expiration_type = $curdefaults['expiration_type'];
                if ($node->expiration_type == 'onupdate') {
                  $node->expire = strtotime($node->expire_timefrom);
                }

                /**
                 * The user can't change these settings, and it would be set to expire already,
                 * so, let's just not expire the node at all.
                 */
                if ($node->expire <= time()) {
                  $node->expiration_type = 'none';
                }
              }
            }
            else {
              $node->expire = strtotime($curdefaults['expire']);
              $node->expire_timefrom = $curdefaults['expire_timefrom'];
              $node->isroot = $curdefaults['isroot'];
              $node->expiration_type = $curdefaults['expiration_type'];
              if ($node->expiration_type == 'onupdate') {
                $node->expire = strtotime($node->expire_timefrom);
              }

              /**
               * The user can't change these settings, and it would be set to expire already,
               * so, let's just not expire the node at all.
               */
              if ($node->expire <= time()) {
                $node->expiration_type = 'none';
              }
            }
          }

          // Get the information SQL ready - format_date screws up the time zones so we don't want it.
          $node->expire = $node->expire ? date('Y-m-d H:i:s', $node->expire) : '1980-01-01 00:00:00';
        }
        else {

          // If this is a book page, and it's not marked as a root book, override the user's input with the parent's data.
          if ($node->type == 'book' && variable_get('node-expire-book-props', 1) && $node->parent > 0 && $node->isroot == 0) {
            $query = db_query('SELECT expire, expiresec, expiremode FROM {node_expire} WHERE nid = %d', $node->parent);
            if (db_num_rows($query) > 0) {
              $row = db_fetch_object($query);
              $node->expire = $row->expire;
              $node->expire_timefrom = $row->expiresec;
              $node->expiration_type = $row->expiremode;
              $node->isroot = 0;

              // If the parent node is set to base expiration off last update time, let's mark this node accordingly.
              if ($node->expiration_type == 'onupdate') {
                $node->expire = strtotime($node->expire_timefrom);
              }
            }
            else {
              if ($node->expiration_type == 'date') {
                $node->expire = strtotime($node->expire);
                $node->expire_timefrom = '';
              }
              else {
                if ($node->expiration_type == 'onupdate') {
                  $node->expire = strtotime($node->expire_timefrom);
                }
                else {
                  $node->expiration_type = 'none';
                  $node->expire = 0;
                  $node->expire_timefrom = 0;
                }
              }
            }
          }
          else {
            if ($node->expiration_type == 'date') {
              $node->expire = strtotime($node->expire);
              $node->expire_timefrom = '';
            }
            else {
              if ($node->expiration_type == 'onupdate') {
                $node->expire = strtotime($node->expire_timefrom);
              }
              else {
                $node->expiration_type = 'none';
                $node->expire = 0;
                $node->expire_timefrom = 0;
              }
            }
          }

          // Get the information SQL ready - format_date screws up the time zones so we don't want it.
          $node->expire = $node->expire ? date('Y-m-d H:i:s', $node->expire) : '1980-01-01 00:00:00';

          /**
           * 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' && variable_get('node-expire-book-props', 1)) {
            _node_expire_propagate_new($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 ($update) {
          db_query('DELETE FROM {node_expire} WHERE nid = %d', $node->nid);
          db_query("INSERT INTO {node_expire} (nid, expire, expiresec, expiremode, isroot) VALUES (%d, '%s', '%s', '%s', %d)", $node->nid, $node->expire, $node->expire_timefrom, $node->expiration_type, $node->isroot);
        }
      }
      break;
    case 'delete':
      db_query('DELETE FROM {node_expire} WHERE nid = %d', $node->nid);
      break;
  }
}