function adserve_ad in Advertisement 6
Same name and namespace in other branches
- 5.2 adserve.inc \adserve_ad()
- 5 adserve.inc \adserve_ad()
- 6.3 adserve.inc \adserve_ad()
- 6.2 adserve.inc \adserve_ad()
- 7 adserve.inc \adserve_ad()
The main adserve logic.
2 calls to adserve_ad()
File
- ./
adserve.inc, line 27 - Configuration.
Code
function adserve_ad($options = array()) {
static $displayed_count = 0;
// If no $options are passed in, assume we're using JavaScript.
if (!empty($options)) {
adserve_variable('variable_load', $options);
}
else {
adserve_variable('variable_load');
}
adserve_bootstrap(0);
adserve_debug();
adserve_variable('error', FALSE);
$output = NULL;
if (adserve_variable('adcache') != 'none') {
/**
* Ad caches are defined through external modules. Ad caches are composed
* of a module 'ad_cache_TYPE.module' and an include file
* 'ad_cache_TYPE.inc' that live in the 'cache/TYPE' subdirectory where
* 'TYPE' is replaced with the type of cache. For example, the included
* file cache lives in 'cache/file'.
*
* The ad_cache_TYPE.inc file must have a function named ad_cache_TYPE()
* which is used to display ads. It can optionally include a function
* titled ad_cache_TYPE_variables used to extract any necessary
* variables from the global $_GET array (this can also be used to override
* values that would normally be set from $_GET). Any functions used
* by this code without bootstrapping Drupal should also be in this file.
*
* The ad_cache_TYPE.module file should define the drupal _help() hook
* so the module can be enabled. It should also define the _adcacheapi()
* hook allowing for configuration and processing. Any functions used by
* this code after bootstrapping Drupal should also be in this module.
*
* Refer to cache/file/* for an implementation example.
*/
$function = 'ad_cache_' . adserve_variable('adcache');
$output = adserve_invoke_file($function);
}
// If there's no output, we assume either there's no cache enabled, or the
// cache failed.
// TODO: Log failures with the watchdog.
if ($output == NULL) {
if (adserve_variable('debug')) {
echo "No cache enabled.<br />\n";
}
adserve_bootstrap();
if (adserve_variable('nids')) {
$id = adserve_variable('nids');
$type = 'nids';
adserve_variable('group', "n{$id}");
// Retrieve all active advertisements from the provided nid list.
$sql = "SELECT aid FROM {ads} WHERE adstatus = 'active' AND aid IN (%s)";
$result = db_query($sql, $id);
if (adserve_variable('debug')) {
echo "Searching for ad from nid list: {$id}.<br />\n";
echo "Query: \"{$sql};\"<br />\n";
}
}
else {
if (adserve_variable('tids')) {
$id = adserve_variable('tids');
$type = 'tids';
adserve_variable('group', "t{$id}");
// Retrieve all active advertisements from the provided tid list.
$sql = "SELECT a.aid FROM {ads} a INNER JOIN {term_node} n ON a.aid = n.nid WHERE a.adstatus = 'active' AND n.tid IN (%s)";
$result = db_query($sql, $id);
if (adserve_variable('debug')) {
echo "Searching for ad from tid list: {$id}.<br />\n";
echo "Query: \"{$sql};\"<br />\n";
}
}
else {
$id = 0;
$type = 'default';
adserve_variable('group', "{$id}");
// Randomly determine which ad to display from those that do not have
// any tid assigned to them.
$sql = "SELECT a.aid FROM {ads} a LEFT JOIN {term_node} n ON a.aid = n.nid WHERE a.adstatus = 'active' AND n.tid IS NULL";
$result = db_query($sql);
if (adserve_variable('debug')) {
echo "Searching for ads with no tids.<br />\n";
echo "Query: \"{$sql};\"<br />\n";
}
}
}
// Build list of all available ads to choose from.
$available = array();
while ($ad = db_fetch_object($result)) {
$available[$ad->aid] = $ad->aid;
}
if (adserve_variable('debug')) {
echo 'Available ads: ';
if (sizeof($ads)) {
echo implode(', ', $available) . "<br />";
}
else {
echo 'none<br />';
}
}
// Randomly select from available advertisements.
$selected = adserve_select_ad($available, adserve_variable('quantity'));
$output = '';
$ads = 0;
$details = array();
$ids = array();
// Include appropriate module for displaying selected ad.
foreach ($selected as $aid) {
$ids[$aid] = $aid;
$ads++;
$detail = $details[$aid] = node_load($aid);
if (!isset($modules[$detail->adtype])) {
$modules[$detail->adtype] = db_result(db_query("SELECT filename FROM {system} WHERE name = '%s'", 'ad_' . $detail->adtype));
}
if (adserve_variable('debug')) {
echo 'ad: <pre>';
print_r($detail);
echo '</pre>';
echo "Loading module '" . $modules[$detail->adtype] . "'.<br />\n";
}
include_once $modules[$detail->adtype];
if ($output) {
// Add a div between ads that themers can use to arrange ads when
// displaying more than one at a time.
$displayed_count++;
$output .= "<div class=\"advertisement-space\" id=\"space-{$id}-{$displayed_count}\"></div>";
}
$output .= module_invoke("ad_{$detail->adtype}", 'display_ad', $detail);
// Update the ad's impressions counter.
if (adserve_variable('ad_display') == 'raw') {
$output .= ad_display_image($detail);
}
else {
adserve_increment($detail);
}
}
adserve_variable("{$type}-ids", $ids);
if (empty($ads)) {
adserve_variable('error', TRUE);
$output = 'No active ads were found in the ' . (empty($nids) ? 'tids' : 'nids') . " '{$id}'.";
adserve_increment(NULL, 'count');
}
if (adserve_variable('debug')) {
echo "Ads displayed: {$ads}<br />";
}
}
$hostid = adserve_variable('hostid');
$group = adserve_variable('group');
$replace = "/{$group}";
if (!empty($hostid)) {
$replace .= "/{$hostid}";
}
if ($url = htmlentities(adserve_variable('url'))) {
$replace .= "?u={$url}";
}
$output = preg_replace('&/@HOSTID___&', $replace, $output);
if (adserve_variable('error')) {
$output = "<!-- {$output} -->";
}
/**
* Modules can add custom code to be displayed before or after ads are
* displayed. For example, you many want to add a tagline, "Powered by
* Drupal". To do so, define 'adserve_exit_text' within your module's
* adapi hook.
*
* Code sample for adserve_exit_text example:
*
* sample_adapi($op, $ad) {
* case 'adserve_exit_text':
* return array(
* 'sample' => array(
* 'text' => t('Powered by Drupal'),
* )
* );
* }
*
* As another example use case, you could also use the _init_text and
* _exit_text hooks to wrap all advertisements in a custom div.
*/
$init = TRUE;
foreach (array(
'adserve_init_text',
'adserve_exit_text',
) as $hook) {
$result = adserve_invoke_hook($hook);
if (is_array($result)) {
$append = '';
foreach ($result as $text) {
if ($text['text']) {
$append .= $text['text'];
}
}
if ($init) {
$output = $append . $output;
}
else {
$output .= $append;
}
}
$init = FALSE;
}
switch (adserve_variable('ad_display')) {
case 'iframe':
case 'jquery':
if (!adserve_variable('debug')) {
// Tell the web browser not to cache this frame so the ad refreshes
// each time the page is viewed.
// Expires in the past:
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
// Last load:
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
// HTTP 1.1:
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
// HTTP 1.0:
header('Pragma: no-cache');
}
print "{$output}";
exit(0);
case 'javascript':
default:
$output = str_replace(array(
"\r",
"\n",
"<",
">",
"&",
), array(
'\\r',
'\\n',
'\\x3c',
'\\x3e',
'\\x26',
), addslashes($output));
if (!adserve_variable('debug')) {
// Tell the web browser not to cache this script so the ad refreshes
// each time the page is viewed.
// Expires in the past:
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
// Last load:
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
// HTTP 1.1:
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
// HTTP 1.0:
header('Pragma: no-cache');
// Output is a JavaScript:
header('Content-Type: application/x-javascript; charset=utf-8');
}
print "document.write('{$output}');";
exit(0);
case 'raw':
chdir(adserve_variable('ad_dir'));
return $output;
}
}