View source
<?php
require_once 'ad_token.inc';
function ad_theme() {
return array(
'ad_box' => array(
'file' => 'ad.module',
'variables' => array(
'title' => NULL,
'content' => NULL,
'region' => 'main',
),
),
'ad_display' => array(
'file' => 'ad.module',
'variables' => array(
'group' => NULL,
'display' => NULL,
'method' => 'javascript',
),
),
'ad_status_display' => array(
'file' => 'ad.module',
'variables' => array(
'node' => NULL,
),
),
'ad_statistics_display' => array(
'file' => 'ad.pages.inc',
'variables' => array(
'statistics' => NULL,
),
),
'node_ad' => array(
'file' => 'ad.pages.inc',
'variables' => array(
'node' => NULL,
'yield_form' => TRUE,
),
),
'ad_filters' => array(
'file' => 'ad.admin.inc',
'variables' => array(
'form' => NULL,
),
),
'ad_filter_form' => array(
'file' => 'ad.admin.inc',
'variables' => array(
'form' => NULL,
),
),
);
}
function ad($group = FALSE, $quantity = 1, $options = array()) {
global $base_url;
$adserve = variable_get('adserve', '');
$adserveinc = variable_get('adserveinc', '');
if (empty($adserve) || empty($adserveinc)) {
_ad_check_installation();
$adserve = variable_get('adserve', '');
$adserveinc = variable_get('adserveinc', '');
}
if (!file_exists($adserve) || !file_exists($adserveinc)) {
drupal_set_message(t('Ads cannot be displayed. The ad module is <a href="@misconfigured">misconfigured</a>, failed to locate the required <em>serve.php</em> ond/or <em>adserve.inc</em> file.', array(
'@misconfigured' => url('admin/content/ad/configure'),
)), 'error');
_ad_check_installation();
return t('The ad module is <a href="@misconfigured">misconfigured</a>.', array(
'@misconfigured' => url('admin/content/ad/configure'),
));
}
if (!isset($options['ad_display'])) {
$options['ad_display'] = variable_get('ad_display', 'javascript');
}
$options['quantity'] = isset($quantity) ? $quantity : 1;
if (!isset($options['tids'])) {
$options['tids'] = $group;
}
$options['cache'] = variable_get('ad_cache', 'none');
switch ($options['ad_display']) {
case 'raw':
require_once drupal_get_path('module', 'ad') . '/adserve.inc';
require_once drupal_get_path('module', 'ad') . '/adcache.inc';
$output = adserve_ad($options);
break;
case 'iframe':
case 'jquery':
$query['m'] = $options['ad_display'];
case 'javascript':
default:
$query['q'] = $quantity;
if (isset($options['hostid'])) {
$query['k'] = $options['hostid'];
}
if ($options['cache'] != 'none') {
$query['c'] = $options['cache'];
$cache_variables = module_invoke('ad_cache_' . $options['cache'], 'adcacheapi', 'display_variables', array());
if (is_array($cache_variables)) {
foreach ($cache_variables as $key => $value) {
$query[$key] = $value;
}
}
}
$type_variables = module_invoke_all('adapi', 'display_variables', array());
if (is_array($type_variables)) {
foreach ($type_variables as $key => $value) {
$query[$key] = $value;
}
}
if (isset($options['nids'])) {
$nids = $options['nids'];
$query['n'] = $nids;
$group = "nids-{$nids}";
}
else {
if (isset($options['tids'])) {
$tids = $options['tids'];
$query['t'] = $tids;
$group = "tids-{$tids}";
}
else {
$query['t'] = $group;
$options['tids'] = $group;
}
}
if (isset($options['url'])) {
$query['u'] = $options['url'];
}
else {
$query['u'] = $_GET['q'];
if ($alias = drupal_get_path_alias($_GET['q'])) {
$query['l'] = $alias;
}
}
$src = url($base_url . '/' . $adserve, array(
'query' => $query,
));
if ($options['ad_display'] == 'iframe') {
$append = 'frameborder="' . variable_get('ad_iframe_frameborder', 0) . '" ';
$append .= 'scrolling="' . variable_get('ad_iframe_scroll', 'auto') . '" ';
$append .= 'name="' . $group . '" ';
if ($height = variable_get('ad_iframe_height', '')) {
$append .= 'height="' . $height . '" ';
}
if ($width = variable_get('ad_iframe_width', '')) {
$append .= 'width="' . $width . '" ';
}
$output = '<iframe src="' . htmlentities($src) . "\" {$append}></iframe>";
}
else {
if ($options['ad_display'] == 'jquery') {
$output = $src;
}
else {
$output = "<script type='text/javascript' src='" . htmlentities($src) . "'></script>";
}
}
break;
}
if (user_access('show advertisements')) {
return theme('ad_display', array(
'group' => $group,
'display' => $output,
'method' => $options['ad_display'],
));
return theme('ad_display', array(
'group' => $group,
'display' => $output,
'method' => $options['ad_display'],
));
}
else {
return theme('ad_display', array(
'group' => 'none',
'display' => "<!-- Enable 'show advertisements' permission if you wish to display ads here. -->",
));
}
}
function theme_ad_box($variables) {
$output = '<h2 class="title">' . $variables['title'] . '</h2><div>' . $variables['content'] . '</div>';
return $output;
}
function theme_ad_display($variables) {
static $id = -1;
$id++;
$group = preg_replace('/[,]/', '+', $variables['group']);
if ($variables['method'] == 'jquery') {
return "\n<div class=\"advertisement group-{$variables['group']}\" id=\"group-id-{$id}\">\n <script type=\"text/javascript\">\n//<![CDATA[\n \$(document).ready(function(){ jQuery(\"div#group-id-{$id}\").load(\"{$display}\"); });\n //]]>\n </script>\n</div>\n";
}
else {
if ($variables['method'] == 'raw') {
return $variables['display'];
}
else {
return "\n<div class=\"advertisement group-{$variables['group']}\" id=\"group-id-{$variables['group']}\">{$variables['display']}</div>\n";
}
}
}
function ad_redirect($aid, $group = NULL) {
global $user;
$hostid = isset($_GET['hostid']) ? $_GET['hostid'] : '';
$extra = isset($_GET['extra']) ? $_GET['extra'] : '';
if (function_exists('click_filter_status')) {
$status = click_filter_status($aid, $hostid);
}
else {
$status = 0;
}
$url = isset($_GET['u']) ? $_GET['u'] : '';
if (!isset($url) || !valid_url($url)) {
$url = $_SERVER['HTTP_REFERER'];
}
db_insert('ad_clicks')
->fields(array(
'aid' => $aid,
'uid' => $user->uid,
'status' => $status,
'hostname' => ip_address(),
'user_agent' => $_SERVER['HTTP_USER_AGENT'],
'adgroup' => $group,
'extra' => $extra,
'hostid' => $hostid,
'url' => $url,
'timestamp' => time(),
))
->execute();
watchdog('ad', 'Clicked ad aid %aid hostid %hostid.', array(
'%aid' => $aid,
'%hostid' => $hostid,
));
if (function_exists('click_filter_status') && $status == CLICK_VALID) {
ad_statistics_increment($aid, 'click', $group, $hostid);
}
else {
if (!function_exists('click_filter_status')) {
ad_statistics_increment($aid, 'click', $group, $hostid);
}
}
$adtype = db_query('SELECT adtype FROM {ads} WHERE aid = :aid', array(
':aid' => $aid,
))
->fetchField();
$node = new stdClass();
$node->nid = $node->aid = $aid;
$node->hostid = $hostid;
$url = module_invoke('ad_' . $adtype, 'adapi', 'redirect', $node);
if (isset($url)) {
header('Location: ' . $url);
}
else {
watchdog('ad', 'Ad redirection failed for aid %aid hostid %hostid, failed to load destination URL. ', array(
'%aid' => $aid,
'%hostid' => $hostid,
));
drupal_goto('');
}
}
function ad_link_attributes() {
return array_merge(ad_link_target(TRUE), ad_link_nofollow(TRUE));
}
function ad_link_target() {
switch (variable_get('ad_link_target', '_self')) {
case '_blank':
$target = array(
'onclick' => 'window.open(this.href); return false;',
);
break;
case '_parent':
$target = array(
'onclick' => 'window.parent.location = this.href; return false;',
);
break;
case '_top':
$target = array(
'onclick' => 'window.top.location = this.href; return false;',
);
break;
default:
$target = array();
break;
}
return $target;
}
function ad_rebuild_cache($verbose = FALSE) {
$cache = variable_get('ad_cache', 'none');
$build = "ad_cache_{$cache}_build";
if (function_exists($build)) {
if ($verbose) {
drupal_set_message('Rebuilding ad cache.');
}
$build();
}
}
function ad_link_nofollow() {
if (variable_get('ad_link_nofollow', 0)) {
$nofollow = array(
'rel' => 'nofollow',
);
}
else {
$nofollow = array();
}
return $nofollow;
}
function ad_statistics_increment($aid, $action, $group = NULL, $hostid = NULL) {
if (empty($hostid)) {
$hostid = ' ';
}
$result = db_query("UPDATE {ad_statistics} SET count = count + 1 WHERE date = :date AND aid = :aid AND action = :action AND adgroup = :adgroup AND hostid = :hostid", array(
':date' => date('YmdH'),
':aid' => $aid,
':action' => $action,
':adgroup' => $group,
':hostid' => $hostid,
));
if (!$result
->rowCount()) {
$id = db_insert('ad_statistics')
->fields(array(
'aid' => $aid,
'adgroup' => $group,
'hostid' => $hostid,
'date' => date('YmdH'),
'action' => $action,
'count' => 1,
))
->execute();
if (!$id) {
db_query("UPDATE {ad_statistics} SET count = count + 1 WHERE date = :date AND aid = :aid AND action = :action AND adgroup = :adgroup AND hostid = :hostid", array(
':date' => date('YmdH'),
':aid' => $aid,
':action' => $action,
':adgroup' => $group,
':hostid' => $hostid,
));
}
}
$event = array(
'aid' => $aid,
'action' => $action,
'hostid' => $hostid,
);
module_invoke_all('adapi', 'statistics_increment', $event);
}
function ad_status_array($aid = 0, $status = NULL) {
$permissions = array();
if (user_access('administer advertisements') || $status == 'pending' || $status == NULL || ad_check_permission($aid, 'set status as pending')) {
$permissions['pending'] = t('This advertisement is currently waiting for administrative approval.');
}
if (user_access('administer advertisements') || $status == 'approved' || $status == 'pending' && ad_check_permission($aid, 'set status from pending to approved')) {
$permissions['approved'] = t('This advertisement has been approved and is currently waiting to be activated.');
}
if (user_access('administer advertisements') || $status == 'active' || $status == 'approved' && ad_check_permission($aid, 'set status from approved to active') || $status == 'offline' && ad_check_permission($aid, 'set status from offline to active')) {
$permissions['active'] = t('This advertisement is actively being displayed.');
}
if (user_access('administer advertisements') || $status == 'offline' || $status == 'approved' && ad_check_permission($aid, 'set status from approved to offline') || $status == 'active' && ad_check_permission($aid, 'set status from active to offline')) {
$permissions['offline'] = t('This advertisement has been temporarily disabled by its owner and is not currently being displayed.');
}
if (user_access('administer advertisements') || $status == 'expired' || $status == 'active' && ad_check_permission($aid, 'set status from active to expired') || $status == 'offline' && ad_check_permission($aid, 'set status from offline to expired')) {
$permissions['expired'] = t('This advertisement has expired and is no longer being displayed.');
}
if (user_access('administer advertisements') || $status == 'denied' || $status == 'pending' && ad_check_permission($aid, 'set status from pending to denied') || ad_check_permission($aid, 'set status as denied')) {
$permissions['denied'] = t('This advertisement was refused by the site administrator and will not be displayed.');
}
return $permissions;
}
function theme_ad_status_display($variables) {
$node = $variables['node'];
if (isset($node->adstatus)) {
$status_array = ad_status_array($node->nid, $node->adstatus);
$output = '<div class="adstatus">';
$output .= '<p>' . t($status_array[$node->adstatus]) . '</p>';
switch ($node->adstatus) {
case 'approved':
if ($node->autoactivate) {
$output .= '<p>' . t('This advertisement will be automatically activated on %timestamp, in %time.', array(
'%timestamp' => format_date($node->autoactivate, 'large'),
'%time' => format_interval($node->autoactivate - time()),
)) . '</p>';
}
break;
case 'active':
$activated = db_query("SELECT activated FROM {ads} WHERE aid = :aid", array(
':aid' => $node->nid,
))
->fetchField();
if ($activated) {
$output .= '<p>' . t('This advertisement has been active since %date.', array(
'%date' => format_date($activated, 'large'),
)) . '</p>';
}
if ($node->autoexpire) {
$output .= '<p>' . t('This advertisement will expire on %timestamp, in %time.', array(
'%timestamp' => format_date($node->autoexpire, 'large'),
'%time' => format_interval($node->autoexpire - time()),
)) . '</p>';
}
if ($node->maxviews) {
$views = (int) db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = :aid AND action = 'view' AND date >= :date", array(
':aid' => $node->nid,
':date' => date('YmdH', $node->activated),
))
->fetchField();
$output .= '<p>' . t('This advertisement will expire after %left more impressions.', array(
'%left' => $node->maxviews - $views,
)) . '</p>';
}
if ($node->maxclicks) {
$clicks = (int) db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = :aid AND action = 'click' AND date >= :date", array(
':aid' => $node->nid,
':date' => date('YmdH', $node->activated),
))
->fetchField();
$output .= '<p>' . t('This advertisement will expire after %left more clicks.', array(
'%left' => $node->maxclicks - $clicks,
)) . '</p>';
}
break;
case 'expired':
$expired = db_query("SELECT expired FROM {ads} WHERE aid = :aid", array(
':aid' => $node->nid,
))
->fetchField();
if ($expired) {
$output .= '<p>' . t('This advertisement expired %date.', array(
'%date' => format_date($expired, 'large'),
)) . '</p>';
}
break;
}
$output .= '</div>';
return theme('ad_box', array(
'title' => t('Status'),
'content' => $output,
));
}
}
function ad_init() {
$method = variable_get('ad_display', 'javascript');
if ($method == 'jquery') {
drupal_add_js('misc/jquery.js', 'core');
}
}
function ad_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/help#ad':
$output = '<p>' . t('The ad module provides a complete advertising system for Drupal powered websites. It does this through an API that allow other modules to handle various types of advertising content. For example, if enabled together with the ad_image module you will be able to display image based advertisements such as banner ads.') . '</p>';
break;
case 'node/add/ad':
$output = '<p>' . t('Advertisements can be randomly displayed to visitors of your website.') . '</p>';
break;
}
return $output;
}
function ad_cron() {
if (time() - variable_get('ad_cron_timestamp', 0) >= 60) {
$result = db_query('SELECT aid, adstatus, adtype, autoactivate, autoactivated, autoexpire, autoexpired FROM {ads} WHERE autoactivate <> 0 OR autoexpire <> 0');
while ($ad = $result
->fetch()) {
switch ($ad->adstatus) {
case 'approved':
if ($ad->autoactivate && $ad->autoactivate <= time()) {
$node = node_load($ad->aid);
db_query("UPDATE {ads} SET adstatus = 'active', autoactivate = 0, autoactivated = %d, activated = %d WHERE aid = %d", time(), time(), $ad->aid);
ad_statistics_increment($ad->aid, 'autoactivated');
ad_statistics_increment($ad->aid, 'active');
watchdog('ad', 'Automatically activated ad %title with nid %nid.', array(
'%title' => $node->title,
'%nid' => $node->nid,
));
module_invoke('ad_' . $ad->adtype, 'adapi', 'autoactivate', $node);
}
else {
if (!$ad->autoactivate) {
if (time() - variable_get("ad_autoactivate_warning_{$ad->aid}", 0) >= 8600) {
watchdog('ad', 'Warning: ad %title with nid %nid in approved state has no autoactivate date set.', array(
'%title' => $node->title,
'%nid' => $node->nid,
));
variable_set("ad_autoactivate_warning_{$ad->aid}", time());
}
}
}
break;
case 'active':
if ($ad->autoexpire && $ad->autoexpire <= time()) {
$node = node_load($ad->aid);
db_query("UPDATE {ads} SET adstatus = 'expired', autoexpire = 0, autoexpired = %d, expired = %d WHERE aid = %d", time(), time(), $ad->aid);
ad_statistics_increment($ad->aid, 'autoexpired');
ad_statistics_increment($ad->aid, 'expired');
watchdog('ad', 'Automatically expired ad %title with nid %nid.', array(
'%title' => $node->title,
'%nid' => $node->nid,
));
module_invoke('ad_' . $ad->adtype, 'adapi', 'autoexpire', $node);
}
else {
if (!$ad->autoexpire) {
db_query("UPDATE {ads} SET autoactivate = 0 WHERE aid = %d", $ad->aid);
}
}
break;
default:
$node = node_load($ad->aid);
db_query('UPDATE {ads} SET autoactivate = 0, autoexpire = 0 WHERE aid = %d', $ad->aid);
watchdog('ad', 'Warning: reset %type timestamp on advertisement %title with nid %nid because it is in %state state.', array(
'%title' => $node->title,
'%nid' => $node->nid,
'%type' => $ad->autoactivate ? 'autoactivate' : 'autoexpire',
'%state' => $ad->adstatus,
));
}
}
variable_set('ad_cron_timestamp', time());
}
}
function ad_permission() {
return array(
'administer advertisements' => array(
'title' => t('Administer advertisements'),
),
'create advertisements' => array(
'title' => t('Create advertisements'),
),
'edit own advertisements' => array(
'title' => t('Edit own advertisements'),
),
'edit any advertisement' => array(
'title' => t('Edit any advertisement'),
),
'delete own advertisements' => array(
'title' => t('Delete own advertisements'),
),
'delete any advertisement' => array(
'title' => t('Delete any advertisement'),
),
'show advertisements' => array(
'title' => t('Show advertisements'),
),
);
}
function ad_node_info() {
return array(
'ad' => array(
'name' => t('Advertisement'),
'base' => 'ad',
'description' => t('Advertisements can be randomly displayed to visitors of your website.'),
),
);
}
function ad_node_access($node, $op, $account) {
switch ($op) {
case 'create':
return user_access('create advertisements', $account) || user_access('administer advertisements');
case 'update':
return user_access('edit any advertisement', $account) || user_access('edit own advertisements', $account) && is_ad_owner($node->nid) || user_access('administer advertisements', $account);
case 'delete':
return user_access('delete any advertisement', $account) || user_access('delete own advertisements', $account) && is_ad_owner($node->nid) || user_access('administer advertisements', $account);
case 'view':
if (!user_access('show advertisements', $account) && !user_access('administer advertisements', $account)) {
return false;
}
}
}
function ad_form($node, &$form_state) {
$form = array();
$type = node_type_get_type($node);
if (!empty($form_state['values'])) {
$node = (object) $form_state['values'];
}
$form['aid'] = array(
'#type' => 'value',
'#value' => isset($node->nid) ? $node->nid : 0,
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#default_value' => !empty($node->title) ? $node->title : '',
'#required' => TRUE,
'#weight' => -5,
);
if (!isset($adtype)) {
$adtypes = ad_get_types();
if (count($adtypes) == 1) {
$adtype = key($adtypes);
}
else {
if (!count($adtypes)) {
drupal_set_message(t('At least one ad type module must be enabled before you can create advertisements. For example, try <a href="!url">enabling</a> the ad_text or ad_image module.', array(
'!url' => url('admin/build/modules'),
)), 'error');
}
}
}
if ((!isset($node->adtype) || isset($node->adtype_select)) && count($adtypes) > 1) {
$adtypes = array(
0 => '---',
);
$adtypes += ad_get_types('name');
$form['select'] = array(
'#type' => 'fieldset',
'#title' => t('Select Ad type'),
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
'#weight' => 3,
);
$form['select']['adtype_select'] = array(
'#type' => 'select',
'#required' => TRUE,
'#options' => $adtypes,
'#default_value' => isset($node->adtype_select) ? $node->adtype_select : '',
);
$form['select']['adtype_submit'] = array(
'#type' => 'submit',
'#value' => t('Select'),
'#validate' => array(
'ad_select_adtype',
),
'#ahah' => array(
'path' => 'node/add/ad/ahah',
'wrapper' => 'adtype-ahah-wrapper',
),
);
}
if (isset($node->adtype) && $node->adtype || count($adtypes) == 1) {
if (isset($node->adtype_select) && $node->adtype_select && $node->adtype_select != $node->adtype) {
$node->adtype = $node->adtype_select;
}
if (count($adtypes) == 1) {
$node->adtype = $adtype;
}
ad_form_add_adtype_elements($form, $node->adtype, $node);
$form['adtype_elements']['#prefix'] = '<div id="adtype-ahah-wrapper">';
$form['adtype_elements']['#suffix'] = '</div>';
}
if (!isset($form['adtype_elements'])) {
$form['adtype_elements'] = array(
'#value' => '<div id="adtype-ahah-wrapper"></div>',
'#weight' => 3.1,
);
}
$form['adstatus'] = array(
'#type' => 'fieldset',
'#title' => t('Status'),
'#collapsible' => TRUE,
'#weight' => 4,
);
$nid = isset($node->nid) ? $node->nid : 0;
$adstatus = isset($node->adstatus) ? $node->adstatus : '';
foreach (ad_status_array($nid, $adstatus) as $status => $description) {
$form['adstatus']["ad{$status}"] = array(
'#type' => 'radio',
'#title' => t("{$status}"),
'#return_value' => $status,
'#default_value' => isset($node->adstatus) ? $node->adstatus : 'pending',
'#description' => "{$description}",
'#parents' => array(
"adstatus",
),
);
}
$form['schedule'] = array(
'#type' => 'fieldset',
'#title' => t('Scheduling'),
'#collapsible' => TRUE,
'#collapsed' => isset($node->autoactivate) || isset($form_state['values']['autoactivate']) || isset($node->autoexpire) || isset($form_state['values']['autoexpire']) || isset($node->maxviews) || isset($form_state['values']['maxviews']) || isset($node->maxclicks) || isset($form_state['values']['maxclicks']) ? FALSE : TRUE,
);
$date_api = FALSE;
if (module_exists('date_popup')) {
$date_api = TRUE;
}
if (isset($node->nid) && ad_check_permission($node->nid, 'manage status') || user_access('administer advertisements')) {
if (!$date_api) {
$form['schedule']['current'] = array(
'#type' => 'markup',
'#prefix' => '<div>',
'#suffix' => '</div>',
'#value' => t('The current date and time is "%date".', array(
'%date' => format_date(time(), 'custom', 'F j, Y H:i'),
)),
);
}
$form['schedule']['autoactivate'] = array(
'#type' => $date_api ? 'date_popup' : 'textfield',
'#title' => t('Automatically activate ad'),
'#required' => FALSE,
'#description' => t('You can specify a date and time for this advertisement to be automatically activated. The advertisement needs to be in an <em>approved</em> state before it can be automatically activated. If you prefer to activate the advertisement immediately, leave this field empty.'),
);
if ($date_api) {
$form['schedule']['autoactivate']['#date_format'] = 'F j, Y H:i';
$form['schedule']['autoactivate']['#default_value'] = isset($node->autoactivate) && $node->autoactivate > 0 ? format_date((int) $node->autoactivate, 'custom', 'Y-m-j H:i') : '';
}
else {
$form['schedule']['autoactivate']['#default_value'] = isset($node->autoactivate) && $node->autoactivate > 0 ? format_date((int) $node->autoactivate, 'custom', 'F j, Y H:i') : 0;
}
}
if (user_access('administer advertisements')) {
$form['schedule']['autoexpire'] = array(
'#type' => $date_api ? 'date_popup' : 'textfield',
'#title' => t('Automatically expire ad'),
'#required' => FALSE,
'#description' => t('You can specify a date and time for this advertisement to be automatically expired. If you don\'t want the advertisement to expire, leave this field empty.'),
);
if ($date_api) {
$form['schedule']['autoexpire']['#date_format'] = 'F j, Y H:i';
$form['schedule']['autoexpire']['#default_value'] = isset($node->autoexpire) && $node->autoexpire > 0 ? format_date((int) $node->autoexpire, 'custom', 'Y-m-j H:i') : '';
}
else {
$form['schedule']['autoexpire']['#default_value'] = isset($node->autoexpire) && $node->autoexpire > 0 ? format_date((int) $node->autoexpire, 'custom', 'F j, Y H:i') : 0;
}
$form['schedule']['maxviews'] = array(
'#type' => 'textfield',
'#title' => t('Maximum impressions'),
'#required' => FALSE,
'#size' => 10,
'#maxlength' => 11,
'#default_value' => isset($node->maxviews) ? $node->maxviews : 0,
'#description' => t('You can specify the maximum number of times this advertisement should be displayed, after which it will be automatically expired. If you don\'t want this advertisement to expire after a certain number of impressions, leave this field set to %zero.', array(
'%zero' => '0',
)),
);
$form['schedule']['maxclicks'] = array(
'#type' => 'textfield',
'#title' => t('Maximum clicks'),
'#required' => FALSE,
'#size' => 10,
'#maxlength' => 11,
'#default_value' => isset($node->maxclicks) ? $node->maxclicks : 0,
'#description' => t('You can specify the maximum number of times this advertisement should be clicked, after which it will be automatically expired. If you don\'t want this advertisement to expire after a certain number of clicks leave this field set to %zero.', array(
'%zero' => '0',
)),
);
}
else {
$form['schedule']['autoexpire_display'] = array(
'#type' => 'markup',
'#prefix' => '<div>',
'#suffix' => '</div>',
'#value' => theme('ad_status_display', $node),
);
$form['schedule']['autoexpire'] = array(
'#type' => 'hidden',
'#value' => isset($node->autoexpire) ? $node->autoexpire : 0,
);
}
$form['#validate'][] = 'ad_select_adtype';
return $form;
}
function ad_select_adtype(&$form, &$form_state) {
if (!isset($form_state['values']['adtype']) && !isset($form_state['values']['adtype_select'])) {
form_set_error('adtype_select', t('Please, select an Ad type.'));
}
if (!isset($form_state['values']['adtype']) || isset($form_state['values']['adtype_select']) && $form_state['values']['adtype'] != $form_state['values']['adtype_select']) {
$form_state['values']['adtype'] = $form_state['values']['adtype_select'];
$form_state['rebuild'] = TRUE;
}
}
function ad_form_ahah() {
$form_state = array(
'storage' => NULL,
'submitted' => FALSE,
);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
ad_form_add_adtype_elements($form, $_POST['adtype_select']);
form_set_cache($form_build_id, $form, $form_state);
$form += array(
'#post' => $_POST,
'#programmed' => FALSE,
);
$form = form_builder($_POST['form_id'], $form, $form_state);
$output = drupal_render($form['adtype_elements']);
drupal_json(array(
'status' => TRUE,
'data' => $output,
));
}
function ad_form_add_adtype_elements(&$form, $adtype, $node = NULL) {
unset($form['adtype_elements']);
$form['adtype_elements'] = module_invoke('ad_' . $adtype, 'adapi', 'form', $node);
$form['adtype'] = array(
'#type' => 'hidden',
'#value' => $adtype,
);
$form['adtype_elements']['#weight'] = 3.1;
}
function ad_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'taxonomy_form_vocabulary') {
if (!empty($form['vid']['#value']) && $form['vid']['#value'] == _ad_get_vid()) {
$form['help_ad_vocab'] = array(
'#value' => t('This vocabulary was automatically created for use by the ad module. Only applicable options are available.'),
'#weight' => -1,
);
$form['nodes']['ad'] = array(
'#type' => 'checkbox',
'#title' => t('ad group'),
'#value' => 1,
'#attributes' => array(
'disabled' => '',
),
'#description' => t('Type %type is required to use this vocabulary.', array(
'%type' => t('ad group'),
)),
);
$form['tags']['#description'] = t('If enabled, ads are categorized by typing ad group names instead of choosing them from a list.');
$form['multiple']['#description'] = t('If enabled, allows ads to have more than one ad group (always true for free tagging).');
$form['required']['#description'] = t('If enabled, every ad <strong>must</strong> be assigned to at least one ad group.');
$form['hierarchy'] = array(
'#type' => 'value',
'#value' => 0,
);
unset($form['relations']);
}
else {
unset($form['nodes']['ad']);
}
}
else {
if ($form_id == 'taxonomy_form_term') {
if ($form['vid']['#value'] == _ad_get_vid()) {
$form['name']['#title'] = t('Ad group name');
$form['name']['#description'] = t('The name for this ad group. Example: "Linux".');
$form['description']['#description'] = t('A description of the ad group.');
$form['description']['#required'] = TRUE;
$form['weight']['#description'] = t('In listings, the heavier ad groups will sink and the lighter ad groups will be positioned nearer the top.');
unset($form['synonyms']);
}
}
else {
if ($form_id == 'search_form' && variable_get('ad_no_search', 1) && !user_access('administer advertisements') && !user_access('administer any advertisement')) {
$vid = _ad_get_vid();
$vocabulary = db_query('SELECT name FROM {taxonomy_vocabulary} WHERE vid = %d', $vid)
->fetchField();
unset($form['advanced']['category']['#options'][$vocabulary]);
if (empty($form['advanced']['category']['#options'])) {
unset($form['advanced']['category']);
}
unset($form['advanced']['type']['#options']['ad']);
}
}
}
}
function ad_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
if (variable_get('ad_no_search', 1) && !user_access('administer advertisements') && !user_access('edit any advertisement') && $query == '' && $primary_table == 'n' && ($primary_field = 'nid' && empty($args))) {
return array(
'where' => " n.type != 'ad'",
);
}
}
function ad_node_load($nodes, $types) {
foreach ($nodes as &$node) {
ad_nodeapi($node, 'load');
}
}
function ad_node_view($node, $view_mode, $langcode) {
ad_nodeapi($node, 'view', $view_mode == 'teaser', $view_mode == 'full');
}
function ad_node_insert($node) {
ad_nodeapi($node, 'insert');
}
function ad_node_update($node) {
ad_nodeapi($node, 'update');
}
function ad_node_delete($node) {
ad_nodeapi($node, 'delete');
}
function ad_node_validate($node, $form, &$form_state) {
ad_nodeapi($node, 'validate');
}
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);
if ($node->adstatus != 'approved' && $node->autoactivate) {
if ($node->adstatus == 'active') {
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 ($node->status != 1 && $node->adstatus == 'active') {
$node->adstatus = 'expired';
}
if ($ad->adstatus != 'active' && $node->adstatus == 'active') {
$activated = time();
}
else {
if ($ad->adstatus != 'expired' && $node->adstatus == 'expired') {
$activated = $ad->activated;
$expired = time();
}
else {
$activated = $ad->activated;
$expired = $ad->expired;
}
}
if ($ad->adstatus != $node->adstatus) {
ad_statistics_increment($node->nid, $node->adstatus);
}
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':
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;
}
if (isset($node->adtype) && $op != 'load') {
$module = "ad_{$node->adtype}";
module_invoke("ad_{$node->adtype}", 'adapi', $op, $node);
}
$cache = variable_get('ad_cache', 'none');
if ($cache != 'none') {
$function = "ad_cache_{$cache}" . '_adcacheapi';
if (function_exists($function)) {
$function($op, $node);
}
}
switch ($op) {
case 'insert':
case 'update':
case 'delete':
if (variable_get('ad_cache_file_rebuild_realtime', 0) && isset($node->adtype)) {
ad_rebuild_cache();
}
}
}
function ad_noindex_meta() {
static $added = FALSE;
if (!$added) {
drupal_set_html_head('<meta name="robots" content="noindex" />');
$added = TRUE;
}
}
function ad_adapi($op, $node = NULL) {
switch ($op) {
case 'permissions':
return array(
'access statistics' => TRUE,
'access click history' => TRUE,
'access activity log' => TRUE,
'set status as pending' => FALSE,
'set status as denied' => FALSE,
'set status from pending to approved' => FALSE,
'set status from pending to denied' => FALSE,
'set status from approved to active' => TRUE,
'set status from approved to offline' => TRUE,
'set status from active to offline' => TRUE,
'set status from active to expired' => FALSE,
'set status from offline to active' => TRUE,
'set status from offline to expired' => FALSE,
);
break;
}
}
function ad_menu() {
$items = array();
$items['admin/content/ad'] = array(
'title' => 'Ads',
'page callback' => 'ad_admin_list',
'access arguments' => array(
'administer advertisements',
),
'description' => 'Configure and manage your advertising system.',
'file' => 'ad.admin.inc',
);
$items['admin/content/ad/list'] = array(
'title' => 'List',
'page callback' => 'ad_admin_list',
'access arguments' => array(
'administer advertisements',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'ad.admin.inc',
);
$items['admin/content/ad/configure'] = array(
'title' => 'Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ad_admin_configure_settings',
),
'access arguments' => array(
'administer advertisements',
),
'type' => MENU_LOCAL_TASK,
'weight' => 3,
'file' => 'ad.admin.inc',
);
$items['node/add/ad/ahah'] = array(
'access arguments' => array(
'create advertisements',
),
'page callback' => 'ad_form_ahah',
'type' => MENU_CALLBACK,
);
ad_menu_add_global_settings($items);
$items['admin/content/ad/groups'] = array(
'title' => 'Ad groups',
'page callback' => 'ad_admin_groups_list',
'access arguments' => array(
'administer advertisements',
),
'type' => MENU_LOCAL_TASK,
'weight' => 5,
'file' => 'ad.admin.inc',
);
$items['admin/content/ad/groups/list'] = array(
'title' => 'List',
'page callback' => 'ad_admin_groups_list',
'access arguments' => array(
'administer advertisements',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
'file' => 'ad.admin.inc',
);
$items['admin/content/ad/groups/add'] = array(
'title' => 'Create group',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ad_admin_group_form',
),
'access arguments' => array(
'administer advertisements',
),
'type' => MENU_LOCAL_TASK,
'weight' => 3,
'file' => 'ad.admin.inc',
);
$items["admin/content/ad/groups/%ad_group/edit"] = array(
'title' => 'Edit group',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ad_admin_group_form',
4,
),
'access arguments' => array(
'administer advertisements',
),
'weight' => 1,
'file' => 'ad.admin.inc',
);
$items["admin/content/ad/groups/%ad_group/delete"] = array(
'title' => 'Delete group',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ad_confirm_group_delete',
4,
),
'access arguments' => array(
'administer advertisements',
),
'weight' => 2,
'file' => 'ad.admin.inc',
);
$items['admin/content/ad/configure/global'] = array(
'title' => 'Global settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ad_admin_configure_settings',
),
'access arguments' => array(
'administer advertisements',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
'file' => 'ad.admin.inc',
);
$items["node/%ad_menu/details/%"] = array(
'title' => 'Click details',
'page callback' => 'ad_click_details',
'page arguments' => array(
1,
3,
),
'access arguments' => array(
1,
'access click history',
),
'access callback' => 'ad_permission',
'type' => MENU_CALLBACK,
'file' => 'ad.pages.inc',
);
$items["node/%ad_menu/activity"] = array(
'title' => t('Activity log'),
'page callback' => 'ad_activity_details',
'page arguments' => array(
1,
),
'access arguments' => array(
1,
'access activity log',
),
'access callback' => 'ad_permission',
'type' => MENU_LOCAL_TASK,
'file' => 'ad.pages.inc',
);
$items["ad/redirect/%"] = array(
'access arguments' => array(
'show advertisements',
),
'type' => MENU_CALLBACK,
'page callback' => 'ad_redirect',
'page arguments' => array(
2,
),
);
return $items;
}
function ad_menu_load($nid) {
if (!is_numeric($nid)) {
return FALSE;
}
$node = node_load($nid);
if (!isset($node->type) || $node->type != 'ad') {
return FALSE;
}
return $node;
}
function ad_menu_add_global_settings(&$menu_items) {
$adtypes = ad_get_types();
foreach ($adtypes as $type => $name) {
$settings = 'ad_' . $type . '_global_settings';
$file = 'ad_image.module';
if (!function_exists($settings)) {
$settings = 'ad_no_global_settings';
$file = 'ad.admin.inc';
}
$menu_items['admin/content/ad/configure/' . $type] = array(
'title' => $name,
'page callback' => 'drupal_get_form',
'page arguments' => array(
$settings,
),
'access arguments' => array(
'administer advertisements',
),
'type' => MENU_LOCAL_TASK,
'weight' => 2,
'file' => 'ad.admin.inc',
);
}
}
function ad_group_load($tid) {
if (!is_numeric($tid)) {
return FALSE;
}
$group = ad_groups_list(TRUE, $tid);
if (!isset($group)) {
return FALSE;
}
return $group;
}
function ad_block_info() {
$blocks = array();
$groups = ad_groups_list();
foreach ($groups as $tid => $name) {
$blocks[$tid] = array(
'info' => t('ad group: @name', array(
'@name' => $name,
)),
'cache' => DRUPAL_CACHE_PER_PAGE,
);
}
return $blocks;
}
function ad_block_configure($delta = '') {
$form['ad_block_quantity_' . $delta] = array(
'#type' => 'select',
'#title' => t('Number of ads'),
'#default_value' => variable_get('ad_block_quantity_' . $delta, 1),
'#options' => drupal_map_assoc(array(
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
)),
'#description' => t('Select the maximum number of unique ads that should be displayed together in this block. If you specify a number larger than the maximum number of ads in this ad group, all ads will be displayed once.'),
);
return $form;
}
function ad_block_save($delta = '', $edit = array()) {
variable_set('ad_block_quantity_' . $delta, $edit['ad_block_quantity_' . $delta]);
}
function ad_block_view($delta = '') {
return array(
'content' => ad($delta, variable_get('ad_block_quantity_' . $delta, 1)),
);
}
function ad_check_permission($aid, $string, $account = NULL) {
global $user;
$access = FALSE;
if (!isset($account)) {
$account = $user;
}
if ($account->uid == 1) {
return TRUE;
}
if (user_access('administer advertisements', $account)) {
return TRUE;
}
if (is_object($aid) && isset($aid->aid)) {
$aid = $aid->aid;
}
else {
if (is_object($aid) && isset($aid->nid)) {
$aid = $aid->nid;
}
else {
if (is_object($aid)) {
watchdog('ad', 'Invalid aid object passed into ad_permission, no aid->aid set.');
$aid = 0;
}
}
}
if (module_exists('ad_owners') && function_exists('ad_owners_permission')) {
$access = ad_owners_permission($aid, $string, $account);
}
else {
if (in_array($string, array(
'access statistics',
'access click history',
))) {
$access = TRUE;
}
}
return $access;
}
function ad_get_types($op = 'name', $type = NULL) {
$adtypes = module_invoke_all('adapi', 'type', array());
switch ($op) {
case 'name':
if (isset($type)) {
return $adtypes[$type]['name'];
}
else {
foreach ($adtypes as $type => $data) {
$adtypes[$type] = $data['name'];
}
return $adtypes;
}
case 'data':
if (isset($type)) {
return $adtypes[$type];
}
else {
return $adtypes;
}
}
}
function ad_groups_list($object = FALSE, $tid = NULL) {
static $groups = array();
static $names = array();
if ($object) {
if (empty($groups)) {
$tids = taxonomy_get_tree(_ad_get_vid());
if (is_array($tids)) {
foreach ($tids as $group) {
$groups[$group->tid] = new stdClass();
$groups[$group->tid]->name = $group->name;
$groups[$group->tid]->description = $group->description;
$groups[$group->tid]->tid = $group->tid;
$groups[$group->tid]->weight = $group->weight;
}
}
$groups[0] = new stdClass();
$groups[0]->name = t('default');
$groups[0]->description = t('The default ad group is comprised of all ads not assigned to any other ad group.');
$groups[0]->tid = 0;
$groups[0]->weight = 0;
}
if ((int) $tid) {
return $groups[$tid];
}
else {
return $groups;
}
}
else {
if (empty($names)) {
$tids = taxonomy_get_tree(_ad_get_vid());
if (is_array($tids)) {
foreach ($tids as $group) {
$names[$group->tid] = $group->name;
}
}
$names[0] = t('default');
}
if ((int) $tid) {
return $names[$tid];
}
else {
return $names;
}
}
}
function _ad_check_installation() {
$adserve = variable_get('adserve', '');
$adserveinc = variable_get('adserveinc', '');
if (!file_exists($adserve)) {
$adserve = drupal_get_path('module', 'ad') . '/serve.php';
variable_set('adserve', $adserve);
}
if (!is_readable($adserve)) {
variable_set('adserve', '');
drupal_set_message(t('Failed to read the required file %filename. Please make the file readable by the webserver process. No ads can be displayed until this problem is resolved.', array(
'%filename' => $adserve,
)), 'error');
}
if (!file_exists($adserveinc)) {
$adserveinc = drupal_get_path('module', 'ad') . '/adserve.inc';
variable_set('adserveinc', $adserveinc);
}
if (!is_readable($adserveinc)) {
variable_set('adserveinc', '');
drupal_set_message(t('Failed to read the required file %filename. Please make the file readable by the webserver process. No ads can be displayed until this problem is resolved.', array(
'%filename' => $adserveinc,
)), 'error');
}
$vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE module = 'ad'")
->fetchField();
if ($vid != variable_get('ad_group_vid', '')) {
drupal_set_message(t('Invalid vocabulary defined for advertisements, attempting to auto-fix.'), 'error');
if ($vid) {
variable_set('ad_group_vid_restore', variable_get('ad_group_vid', ''));
}
variable_del('ad_group_vid');
}
else {
$vocab = taxonomy_vocabulary_load_multiple(array(), array(
'module' => 'ad',
));
if (!empty($vocab) && ($vocab = array_shift($vocab)) && $vocab->vid == variable_get('ad_group_vid', '')) {
$found = TRUE;
}
if (!$found) {
drupal_set_message(t('Missing vocabulary node type for advertisements, attempting to auto-fix.'), 'error');
db_query("DELETE FROM {taxonomy_vocabulary} WHERE vid = %d", variable_get('ad_group_vid', ''));
variable_set('ad_group_vid_restore', variable_get('ad_group_vid', ''));
variable_del('ad_group_vid');
}
}
_ad_get_vid();
if (($old = variable_get('ad_group_vid_restore', '')) && ($vid = variable_get('ad_group_vid', ''))) {
drupal_set_message(t('Restoring orphaned ad group configuration.'));
db_query('UPDATE {term_data} SET vid = %d WHERE vid = %d', $vid, $old);
variable_set('ad_group_vid_restore', '');
}
$rid = db_query_range("SELECT r.rid FROM {users_roles} r INNER JOIN {role_permission} p ON (p.rid = r.rid) WHERE p.permission = 'show advertisements' AND p.module = 'ad'", 0, 1)
->fetchField();
if (!$rid) {
drupal_set_message(t('Be sure to enable "!show" permissions for all roles that you wish to see advertisements.', array(
'!show' => l(t('show advertisements'), 'admin/user/permissions'),
)));
}
$hooks = array(
'init_text',
'select',
'filter',
'exit_text',
);
foreach ($hooks as $hook) {
$adserve_actions = module_invoke_all('adapi', "adserve_{$hook}", array());
$actions = array();
foreach ($adserve_actions as $name => $action) {
if (is_numeric($action['weight'])) {
$weight = $action['weight'];
}
else {
$weight = $action['weight'] = 0;
}
$actions[$weight . '.' . $name] = $action;
$actions[$weight . '.' . $name]['name'] = $name;
}
ksort($actions);
variable_set("adserve_{$hook}", serialize($actions));
}
module_invoke_all('adapi', 'check_install', array());
}
function _ad_get_vid() {
$vid = variable_get('ad_group_vid', '');
if (empty($vid)) {
$vocab = taxonomy_vocabulary_load_multiple(array(), array(
'module' => 'ad',
));
if (empty($vocab)) {
$edit = (object) array(
'name' => t('Ad groups'),
'machine_name' => 'ad_group',
'multiple' => 1,
'required' => 0,
'hierarchy' => 0,
'relations' => 0,
'module' => 'ad',
'nodes' => array(
'ad' => 1,
),
);
taxonomy_vocabulary_save($edit);
$vid = $edit->vid;
}
else {
$vocab = array_shift($vocab);
$vid = $vocab->vid;
}
variable_set('ad_group_vid', $vid);
}
return $vid;
}
function ad_display_image($aid, $css = TRUE) {
global $base_url;
$adserve = variable_get('adserve', '');
$cache = variable_get('ad_cache', 'none');
$variables = "?o=image";
$variables .= "&a={$aid}";
if ($cache != 'none') {
$variables .= '&c=' . $cache . module_invoke('ad_cache_' . $cache, 'adcacheapi', 'display_variables', array());
}
$output = '<img src="' . htmlentities(url($base_url . '/' . $adserve . $variables)) . '" height="0" width="0" alt="view counter" />';
if ($css) {
return '<div class="ad-image-counter">' . $output . '</div>';
}
else {
return $output;
}
}
function _ad_get_group($nid) {
static $groups = array();
if (!isset($groups[$nid])) {
$result = db_query('SELECT d.name FROM {taxonomy_term_data} d LEFT JOIN {taxonomy_index} n ON d.tid = n.tid WHERE n.nid = :nid AND d.vid = :vid', array(
':nid' => $nid,
':vid' => _ad_get_vid(),
));
while ($term = $result
->fetch()) {
$terms[] = $term->name;
}
if (!empty($terms)) {
$groups[$nid] = implode(', ', $terms);
}
else {
$groups[$nid] = t('default');
}
}
return $groups[$nid];
}