You are here

function ad_nodeapi in Advertisement 7

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 ad.module \ad_nodeapi()
  5. 6.2 ad.module \ad_nodeapi()
6 calls to ad_nodeapi()
ad_node_delete in ./ad.module
Implementation of hook_node_delete().
ad_node_insert in ./ad.module
Implementation of hook_node_insert().
ad_node_load in ./ad.module
Implementation of hook_node_load().
ad_node_update in ./ad.module
Implementation of hook_node_update().
ad_node_validate in ./ad.module
Implementation of hook_node_validate().

... See full list

File

./ad.module, line 1006

Code

function ad_nodeapi(&$node, $op, $teaser = false, $page = false) {
  global $user;
  switch ($op) {
    case 'load':
      if ($node->nid && $node->type == 'ad') {
        $ad = db_query('SELECT * FROM {ads} WHERE aid = :aid', array(
          ':aid' => $node->nid,
        ))
          ->fetchAssoc();
        foreach ($ad as $name => $value) {
          $node->{$name} = $value;
        }
        $merge = (object) array_merge((array) $node, (array) $ad);
        $adtype = module_invoke('ad_' . $ad['adtype'], 'adapi', 'load', $merge);
        foreach ($adtype as $name => $value) {
          $node->{$name} = $value;
        }
      }
      break;
    case 'insert':
      if (isset($node->adtype)) {
        if ($node->status != 1 && $node->adstatus == 'active') {
          $node->adstatus = 'expired';
        }
        $activated = $node->adstatus == 'active' ? time() : 0;
        $node->autoactivate = isset($node->autoactivate) ? trim($node->autoactivate) : 0;
        $node->autoactivate = is_numeric($node->autoactivate) ? $node->autoactivate : strtotime($node->autoactivate);
        $node->autoexpire = isset($node->autoexpire) ? trim($node->autoexpire) : 0;
        $node->autoexpire = is_numeric($node->autoexpire) ? $node->autoexpire : strtotime($node->autoexpire);
        if (!isset($node->maxviews)) {
          $node->maxviews = 0;
        }
        if (!isset($node->maxclicks)) {
          $node->maxclicks = 0;
        }
        db_insert('ads')
          ->fields(array(
          'aid' => $node->nid,
          'uid' => $node->uid,
          'adstatus' => $node->adstatus,
          'adtype' => $node->adtype,
          'redirect' => url('ad/redirect/' . $node->nid, array(
            'absolute' => TRUE,
          )),
          'autoactivate' => $node->autoactivate ? strtotime($node->autoactivate) : 0,
          'autoexpire' => $node->autoexpire ? strtotime($node->autoexpire) : 0,
          'activated' => $activated,
          'maxviews' => $node->maxviews,
          'maxclicks' => $node->maxclicks,
        ))
          ->execute();
        ad_statistics_increment($node->nid, 'create');
      }
      break;
    case 'update':
      if (isset($node->adtype)) {
        $ad = db_query('SELECT * FROM {ads} WHERE aid = :aid', array(
          ':aid' => $node->nid,
        ))
          ->fetch();
        $node->autoactivate = isset($node->autoactivate) ? trim($node->autoactivate) : 0;
        $node->autoactivate = is_numeric($node->autoactivate) ? $node->autoactivate : strtotime($node->autoactivate);
        $node->autoexpire = isset($node->autoexpire) ? trim($node->autoexpire) : 0;
        $node->autoexpire = is_numeric($node->autoexpire) ? $node->autoexpire : strtotime($node->autoexpire);

        // 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.
            drupal_set_message(t('This ad is already active, ignoring autoactivate date.'));
            $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->adstatus != 'expired' && $node->adstatus == 'expired') {

            // Ad has been manually expired.
            $activated = $ad->activated;
            $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_update('ads')
          ->fields(array(
          'uid' => $node->uid,
          'adstatus' => $node->adstatus,
          'adtype' => $node->adtype,
          'redirect' => url('ad/redirect/' . $node->nid, array(
            'absolute' => TRUE,
          )),
          'autoactivate' => $node->autoactivate ? strtotime($node->autoactivate) : 0,
          'autoexpire' => $node->autoexpire ? strtotime($node->autoexpire) : 0,
          'activated' => isset($activated) ? $activated : 0,
          'maxviews' => isset($node->maxviews) ? (int) $node->maxviews : 0,
          'maxclicks' => isset($node->maxclicks) ? (int) $node->maxclicks : 0,
          'expired' => isset($expired) ? (int) $expired : 0,
        ))
          ->condition('aid', $node->nid)
          ->execute();
        ad_statistics_increment($node->nid, 'update');
      }
      break;
    case 'delete':

      // All that's left of the ad is a single timestamp as to when it was
      // deleted.
      ad_statistics_increment($node->nid, 'delete');
      db_query("DELETE FROM {ads} WHERE aid = :aid", array(
        ':aid' => $node->nid,
      ));
      db_query("DELETE FROM {ad_statistics} WHERE aid = :aid", array(
        ':aid' => $node->nid,
      ));
      break;
    case 'view':
      if (isset($node->adtype)) {
        if (variable_get('ad_meta_noindex', 0)) {
          ad_noindex_meta();
        }
        $node->content['body'] = array(
          '#markup' => theme('node_ad', array(
            'node' => $node,
            'yield_form' => $page,
          )),
          '#weight' => 1,
        );
      }
      break;
    case 'validate':
      $autoactivate = isset($node->autoactivate) ? trim($node->autoactivate) : '';
      if (!empty($autoactivate)) {
        $timestamp = is_numeric($autoactivate) ? $autoactivate : strtotime($autoactivate);
        if ($timestamp <= 0) {
          form_set_error('autoactivate', 'Please select a valid time to automatically active this ad.');
        }
      }
      $autoexpire = isset($node->autoexpire) ? trim($node->autoexpire) : '';
      if (!empty($autoexpire)) {
        $timestamp = is_numeric($autoexpire) ? $autoexpire : strtotime($autoexpire);
        if ($timestamp <= 0) {
          form_set_error('autoexpire', 'Please select a valid time to automatically expire this ad.');
        }
      }
      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)) {
  if (isset($node->adtype) && $op != 'load') {
    $module = "ad_{$node->adtype}";
    module_invoke("ad_{$node->adtype}", 'adapi', $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 (variable_get('ad_cache_file_rebuild_realtime', 0) && isset($node->adtype)) {
        ad_rebuild_cache();
      }
  }
}