You are here

function adserve_select_ad in Advertisement 5

Same name and namespace in other branches
  1. 6 adserve.inc \adserve_select_ad()

Simple default function to randomly select an ad. Provides a hook to allow the definition of external display methods.

Parameters

An array of valid ad IDs, ie array(5, 8, 9, 11).:

Optional, how many unique ads to select.:

Optional, an array of invalid IDs.:

3 calls to adserve_select_ad()
adserve_ad in ./adserve.inc
The main adserve logic.
ad_cache_file in cache/file/ad_cache_file.inc
This is the actual cache function called by adserve.php that displays ads without bootstrapping Drupal.
ad_cache_memcache in cache/memcache/ad_cache_memcache.inc
Called by adserve.inc, display an ad from memcache.

File

./adserve.inc, line 537

Code

function adserve_select_ad($ads, $quantity = 1, $invalid = array()) {

  //adserve_invoke_weight($ads, $quantity, $invalid);
  $ids = array();
  $id = 0;
  $total = sizeof($ads);
  _debug_echo("Selecting {$quantity} ad(s) from {$total} total ad(s).");
  if (is_array($ads)) {
    $ads = adserve_select_reindex($ads, $invalid);
    $total = sizeof($ads);
    for ($i = 0; $i < $quantity; $i++) {
      _debug_echo('Randomly selecting ad: ' . ($i + 1) . " of {$quantity}.");
      $id = 0;

      // Randomly select a unique banner to display.  We subtract 1 as arrays
      // start at 0.
      $return = adserve_invoke_hook('adserve_select', $ads, $invalid);
      if (is_array($return) && !empty($return)) {
        foreach ($return as $id) {

          // First come first serve.
          if ((int) $id) {
            break;
          }
        }
      }
      if ($id == 0) {
        _debug_echo("Default ID selection in adserve.inc.");
        $id = $total > 1 ? $ads[mt_rand(0, $total - 1)] : $ads[0];
        _debug_echo("Randomly selected ID: {$id}.");
      }
      if ($id > 0) {
        $ids[] = $id;
      }
      else {
        if ($id < 0) {

          // There are no more valid advertisements left to display.
          break;
        }
      }
      $invalid[] = $id;
      $ads = adserve_select_reindex($ads, $id);
      $total = sizeof($ads);

      // We're out of ads to display.
      if ($total <= 0) {
        break;
      }
    }
  }
  return $ids;
}