You are here

function classified_node_presave in Classified Ads 6.3

Same name and namespace in other branches
  1. 7.3 classified.module \classified_node_presave()

Implements the D7 hook_node_presave().

Auto-assign expiration date before saving, for both update and insert.

This used to trigger modr8 in versions <= Drupal 6. However modr8 is not ported to Drupal 7 and the node.moderate column no longer exists in core. Use an actual workflow solution instead.

1 call to classified_node_presave()
classified_nodeapi in ./classified.module
Implements hook_nodeapi().

File

./classified.module, line 1222
A pure D6 classified ads module inspired by the ed_classified module.

Code

function classified_node_presave($node) {
  if ($node->type != 'classified') {
    return;
  }
  if (!isset($node->expire_mode)) {
    $node->expire_mode = 'reset';
  }
  $expires_before = isset($node->expires) ? $node->expires : PHP_INT_MAX;

  // $_SERVER['REQUEST_TIME'] may be different from time() and node_save() uses time()
  $now = time();
  switch ($node->expire_mode) {
    case 'force':
      $node->expires = $node->expire_date_ts;
      break;
    case 'node':

      // Do nothing: keep unchanged.
      break;
    case 'reset':
    default:
      $lifetimes = _classified_get('lifetimes');
      $vid = _classified_get('vid');
      $category = isset($node->taxonomy[$vid]) ? $node->taxonomy[$vid] : NULL;
      $node->expires = empty($category) || empty($lifetimes[$category]) ? $now + reset($lifetimes) * 24 * 60 * 60 : $now + $lifetimes[$category] * 24 * 60 * 60;
      break;
  }

  // Republish ads if their expiration date is in the future.
  if ($expires_before < $now && !$node->status && $node->expires > $now) {
    $node->status = 1;
  }
  unset($node->expire_date);
  unset($node->expire_date_ts);
  unset($node->expire_mode);

  // If modr8 is enabled, editing ads should sends them back to moderation
  if (_classified_get('edit-modr8') && module_exists('modr8')) {

    // @link http://drupal.org/node/310723#comment-3021656 @endlink
    // A node in moderation should stay there.
    // A node not in moderation should move to moderation if the user who has
    // just edited does not have the right permission.
    // We handle this change in presave so on return to the calling
    // node_save() the saving is done for us.
    if (!$node->moderate && !user_access('bypass moderation queue')) {
      $node_options = variable_get('node_options_' . $node->type, array(
        'status',
        'promote',
      ));
      $node->moderate = in_array('moderate', $node_options);
    }
  }
}