You are here

function classified_node_presave in Classified Ads 7.3

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

Implements 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.

File

./classified.module, line 1354
A pure D7 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;
  $now = REQUEST_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');
      $category = isset($node->classified_category[LANGUAGE_NONE][0]['tid']) ? $node->classified_category[LANGUAGE_NONE][0]['tid'] : NULL;
      $days = empty($category) || empty($lifetimes[$category]) ? reset($lifetimes) : $lifetimes[$category];
      $node->expires = empty($category) || empty($lifetimes[$category]) ? $now + $days * 24 * 60 * 60 : $now + $days * 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);
}