You are here

function adserve_cache_get_ad_ids in Advertisement 6.2

Same name and namespace in other branches
  1. 5.2 adcache.inc \adserve_cache_get_ad_ids()
  2. 6.3 adcache.inc \adserve_cache_get_ad_ids()
  3. 7 adcache.inc \adserve_cache_get_ad_ids()

Default wrapper function for displaying advertisements. This generally is not replaced by ad caches modules.

File

./adcache.inc, line 372

Code

function adserve_cache_get_ad_ids() {
  static $displayed_count = 0;
  _debug_echo('Entering default adserve_display.');

  // open the cache
  adserve_cache('open');
  $hostid = adserve_variable('hostid') ? adserve_variable('hostid') : 'none';
  _debug_echo("Hostid: '{$hostid}'.");

  // invoke hook_init
  $init = adserve_invoke_hook('init', 'first', $hostid);

  // start with list of advertisements provided externally
  if (is_array($init) && !empty($init)) {
    _debug_echo('Initialized externally.');
    $quantity = $init['quantity'];
    $id = $init['id'];
    $aids = explode(',', $id);
    $type = $init['type'];
  }
  else {

    // build list of ad ids to choose from
    $quantity = adserve_variable('quantity');

    // use list for specific host
    if ($ids = adserve_cache('id', 'host', NULL, $hostid)) {
      $id = implode(',', $ids);
      $type = 'host';
    }
    else {
      if ($id = adserve_variable('nids')) {
        $type = 'nids';
        adserve_variable('group', "n{$id}");
      }
      else {
        if ($id = adserve_variable('tids')) {
          $type = 'tids';
          adserve_variable('group', "t{$id}");
        }
        else {
          $id = 0;
          $type = 'default';
          adserve_variable('group', "{$id}");
        }
      }
    }
    _debug_echo("Searching {$type}: {$id}");
    $aids = adserve_cache('id', $type, $id, $hostid);
  }

  // prepare to select advertisements
  $number_of_ads = sizeof($aids);
  _debug_echo("Total ads: '{$number_of_ads}'.");
  $displayed = adserve_variable("{$type}-displayed");
  if (!is_array($displayed)) {
    $displayed = array();
  }
  _debug_echo('Already displayed: ' . sizeof($displayed));

  // validate available advertisements
  $aids = adserve_cache('validate', $aids, $displayed, $hostid);
  $number_of_ads = sizeof($aids);
  _debug_echo("Validated ads: '{$number_of_ads}'.");

  // filter advertisements
  $aids = adserve_invoke_hook('filter', 'intersect', $aids, $hostid);
  $number_of_ads = sizeof($aids);
  _debug_echo("Filtered ads: '{$number_of_ads}'.");

  // apply weight to advertisements
  $aids = adserve_invoke_hook('weight', 'first', $aids, $hostid);
  $number_of_ads = sizeof($aids);
  _debug_echo("Weighted ads: '{$number_of_ads}'.");

  // select advertisements
  $aids = adserve_invoke_hook('select', 'first', $aids, $quantity, $hostid);
  $number_of_ads = sizeof($aids);
  _debug_echo("Selected ads: '{$number_of_ads}'.");

  // track which advertisements have been "displayed"
  adserve_variable("{$type}-displayed", array_merge($aids, $displayed));
  return $aids;
}