You are here

function ad_nodeapi in Advertisement 5.2

Same name and namespace in other branches
  1. 5 ad.module \ad_nodeapi()
  2. 6.3 ad.module \ad_nodeapi()
  3. 6 ad.module \ad_nodeapi()
  4. 6.2 ad.module \ad_nodeapi()
  5. 7 ad.module \ad_nodeapi()

Drupal _nodeapi hook.

File

./ad.module, line 834
An advertising system for Drupal powered websites.

Code

function ad_nodeapi(&$node, $op, $teaser, $page) {
  global $user;
  switch ($op) {
    case 'load':
      $ad = db_fetch_array(db_query('SELECT * FROM {ads} WHERE aid = %d', $node->nid));
      $adtype = module_invoke('ad_' . $ad['adtype'], 'adapi', 'load', $ad);
      if (is_array($adtype)) {
        return array_merge($ad, $adtype);
      }
      else {
        return $ad;
      }
      break;
    case 'insert':
      if ($node->adtype) {
        if ($node->status != 1 && $node->adstatus == 'active') {
          $node->adstatus = 'expired';
        }
        $activated = $node->adstatus == 'active' ? time() : 0;
        db_query("INSERT INTO {ads} (aid, uid, adstatus, adtype, redirect, autoactivate, autoexpire, activated, maxviews, maxclicks) VALUES(%d, %d, '%s', '%s', '%s', %d, %d, %d, %d, %d)", $node->nid, $node->uid, $node->adstatus, $node->adtype, url("ad/redirect/{$node->nid}", NULL, NULL, TRUE), $node->autoactivate ? strtotime($node->autoactivate) : '', $node->autoexpire ? strtotime($node->autoexpire) : '', $activated, (int) $node->maxviews, (int) $node->maxclicks);
        ad_statistics_increment($node->nid, 'create');
      }
      break;
    case 'update':
      if ($node->adtype) {
        $ad = db_fetch_object(db_query('SELECT * FROM {ads} WHERE aid = %d', $node->nid));

        // Ad must be in approved state to be able to autoactivate it.
        if ($node->adstatus != 'approved' && $node->autoactivate) {
          if ($node->adstatus == 'active') {

            // This ad is already active, no need to autoactivate it.
            $node->autoactivate = 0;
          }
          else {
            drupal_set_message(t('This ad will not be automatically activated at the scheduled time because it is not in the <em>approved</em> state.'), 'error');
          }
        }

        // If this node has been upublished, the ad should no longer be active.
        if ($node->status != 1 && $node->adstatus == 'active') {
          $node->adstatus = 'expired';
        }

        // Check if ad is being manually activated.
        if ($ad->adstatus != 'active' && $node->adstatus == 'active') {
          $activated = time();
        }
        else {
          if ($ad->status != 'expired' && $node->adstatus == 'expired') {

            // Ad has been manually expired.
            $expired = time();
          }
          else {
            $activated = $ad->activated;
            $expired = $ad->expired;
          }
        }

        // Ad status has changed, record the event.
        if ($ad->adstatus != $node->adstatus) {
          ad_statistics_increment($node->nid, $node->adstatus);
        }

        // Update ads table with new information.
        db_query("UPDATE {ads} SET uid = %d, adstatus = '%s', adtype = '%s', autoactivate = %d, autoexpire = %d, activated = %d, maxviews = %d, maxclicks = %d, expired = %d WHERE aid = %d", $node->uid, $node->adstatus, $node->adtype, $node->autoactivate ? strtotime($node->autoactivate) : '', $node->autoexpire ? strtotime($node->autoexpire) : '', $activated, (int) $node->maxviews, (int) $node->maxclicks, $expired, $node->nid);

        // Be sure ad owner has at least default ad permissions.
        ad_statistics_increment($node->nid, 'update');
      }
      break;
    case 'delete':
      db_query("DELETE FROM {ads} WHERE aid = %d", $node->nid);
      db_query("DELETE FROM {ad_statistics} WHERE aid = %d", $node->nid);

      // All that's left of the ad is a single timestamp as to when it was
      // deleted.
      ad_statistics_increment($node->nid, 'delete');
      break;
    case 'view':
      if ($node->adtype) {
        $node = node_prepare($node, $teaser);
        $node->content['body'] = array(
          '#value' => $teaser ? $node->teaser : theme('node_ad', $node, $page),
          '#weight' => 1,
        );
      }
      break;
  }

  // Allow ad type module to act on nodeapi events.  The adapi hook provides
  // access to additional variables not available in the nodeapi hook.
  if ($node->adtype) {

    // Don't use module_invoke, as in pre-PHP5 the changes to $node won't be
    // passed back.
    $function = "ad_{$node->adtype}" . '_adapi';
    if (function_exists($function)) {
      $function($op, $node);
    }
  }

  // Allow ad cache module to act on nodeapi events.
  $cache = variable_get('ad_cache', 'none');
  if ($cache != 'none') {
    $function = "ad_cache_{$cache}" . '_adcacheapi';
    if (function_exists($function)) {
      $function($op, $node);
    }
  }

  // Rebuild the cache after all hooks are invoked.
  switch ($op) {
    case 'insert':
    case 'update':
    case 'delete':
      if ($node->adtype) {
        ad_rebuild_cache();
      }
  }
}