You are here

function ad_cache_memcache in Advertisement 5

Same name and namespace in other branches
  1. 5.2 cache/memcache/ad_cache_memcache.inc \ad_cache_memcache()
  2. 6 cache/memcache/ad_cache_memcache.inc \ad_cache_memcache()

Called by adserve.inc, display an ad from memcache.

1 string reference to 'ad_cache_memcache'
ad_cache_memcache_requirements in cache/memcache/ad_cache_memcache.module
@file A plug in for the ad.module, integrating the ad module with memcache.

File

cache/memcache/ad_cache_memcache.inc, line 11

Code

function ad_cache_memcache() {
  _debug_echo('Memcache: entering ad_cache_memcache().');

  // TODO: Move the meat of this function into adserve.php, simplifying what
  // cache plugins have to do and removing duplicated logic.
  $init_cache = array();
  $init_func = ad_cache_memcache_hook($init_cache, 'include_file_init', 'include_func_init');
  $hostid = adserve_variable('hostid') ? adserve_variable('hostid') : 'none';
  if ($hostid == 'none' || ad_memcache_get("ad-hostid-{$hostid}")) {
    if (function_exists($init_func)) {
      $init = $init_func($init_cache, $hostid);
    }
    if (!empty($init)) {
      if (adserve_variable('debug')) {
        echo "Memcache: initialized externally:<pre>\n";
        print_r($init);
        echo '</pre>';
      }
      $type = $init['type'];
      $id = $init['id'];
      $group = $init['group'];
      $aids = explode(',', $id);
      adserve_variable('quantity', $init['quantity']);
    }
    else {
      if ($id = adserve_variable('nids')) {
        $type = 'node';
      }
      else {
        if ($id = adserve_variable('tids')) {
          $type = 'taxonomy';
        }
        else {
          $type = 'default';
          $id = 0;
        }
      }
      $aids = ad_cache_memcache_get_ids($type, $id);
      $group = $id;
    }
    adserve_variable('group', $group);
    if (adserve_variable('debug')) {
      echo 'Memcache: selecting from the following ad id(s): ';
      if (empty($aids)) {
        echo 'none.<br />';
      }
      else {
        echo implode(', ', $aids) . '.<br />';
      }
    }
    $ids = adserve_variable("{$type}-ids");
    if ($ids == NULL) {
      $ids = array();
    }
    $output = '';
    $selected = adserve_select_ad($aids, adserve_variable('quantity'), $ids);
    adserve_variable("{$type}-ids", array_merge($selected, $ids));
    foreach ($selected as $aid) {
      if ($aid = (int) $aid) {
        $ad = ad_cache_memcache_get_ad($aid);
        if (!empty($output)) {
          $display_count++;
          $output .= "<div class=\"space\" id=\"{$id}-{$displayed_count}\"></div>";
        }
        $output .= $ad->display;
      }
      else {
        $ad = array();
      }
      _debug_echo("Displaying AID: {$aid}.");
      $action = $aid ? 'view' : 'count';
      ad_cache_memcache_increment($action, $aid, $group, $hostid, $ad);
    }
    if (empty($output)) {
      adserve_variable('error', TRUE);
      $output = 'No active ads were found in the ' . (empty($nids) ? 'tids' : 'nids') . " '{$id}'.";
      _debug_echo("Memcache: {$output}");
    }
  }
  else {
    _debug_echo("Memcache: invalid hostid: '{$hostid}'.");
    $output = 'You do not have permission to display ads.';
  }
  return $output;
}