You are here

function ad_nodeapi in Advertisement 6

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

Implementation of hook_nodeapi().

File

./ad.module, line 801
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));
      $merge = array_merge((array) $node, (array) $ad);
      $adtype = module_invoke('ad_' . $ad['adtype'], 'adapi', 'load', $merge);
      if (is_array($adtype)) {
        return array_merge($ad, $adtype);
      }
      else {
        return $ad;
      }
      break;
    case 'insert':
      if (isset($node->adtype)) {
        if ($node->status != 1 && $node->adstatus == 'active') {
          $node->adstatus = 'unpublished';
        }
        $activated = $node->adstatus == 'active' ? time() : 0;
        if (!isset($node->autoactive)) {
          $node->autoactivate = 0;
        }
        if (!isset($node->maxviews)) {
          $node->maxviews = 0;
        }
        if (!isset($node->maxclicks)) {
          $node->maxclicks = 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, array(
          'absolute' => TRUE,
        )), $node->autoactivate ? strtotime($node->autoactivate) : '', $node->autoexpire ? strtotime($node->autoexpire) : '', $activated, $node->maxviews, $node->maxclicks);
        ad_statistics_increment($node->nid, 'create');
      }
      break;
    case 'update':
      if (isset($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 = 'unpublished';
        }
        else {
          if ($node->status == 1 && $node->adstatus == 'unpublished') {
            $node->adstatus = 'active';

            // Special "publish" event, may as well track it even though we'll
            // next also record an "active" event.
            ad_statistics_increment($node->nid, 'publish');
          }
        }

        // Check if ad is being manually activated.
        if ($ad->adstatus != 'active' && $node->adstatus == 'active') {
          $activated = time();
        }
        else {
          if ($ad->adstatus != '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', redirect = '%s', autoactivate = %d, autoexpire = %d, activated = %d, maxviews = %d, maxclicks = %d, expired = %d WHERE aid = %d", $node->uid, $node->adstatus, $node->adtype, url('ad/redirect/' . $node->nid, array(
          'absolute' => TRUE,
        )), isset($node->autoactivate) && $node->autoactivate > 0 ? strtotime($node->autoactivate) : '', isset($node->autoexpire) && $node->autoexpire > 0 ? strtotime($node->autoexpire) : '', $activated, isset($node->maxviews) ? (int) $node->maxviews : 0, isset($node->maxclicks) ? (int) $node->maxclicks : 0, isset($expired) ? $expired : 0, $node->nid);
        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 (isset($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 (isset($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);
    }
  }
}