function adserve_increment in Advertisement 6
Same name and namespace in other branches
- 5 adserve.inc \adserve_increment()
Increment ad counters. Increment in cache if enabled.
2 calls to adserve_increment()
- adserve_ad in ./
adserve.inc - The main adserve logic.
- adserve_counter_image in ./
imageserve.inc - Generate a tiny image with GD, used to count when an ad has been displayed on a cached page.
File
- ./
adserve.inc, line 646 - Configuration.
Code
function adserve_increment($ad, $action = 'view') {
$cache = adserve_variable('adcache');
if (adserve_variable('debug')) {
echo "adserve_increment action({$action}) cache({$cache})<br />\n";
}
if (is_object($ad) && isset($ad->aid)) {
$aid = $ad->aid;
}
else {
$aid = 0;
}
if ($cache != 'none') {
$rc = adserve_invoke_file("ad_cache_{$cache}_increment", $action, $aid);
if ($rc) {
return;
}
}
adserve_bootstrap();
// Update impressions statistics.
db_query("UPDATE {ad_statistics} SET count = count + 1 WHERE aid = %d AND action = '%s' AND date = %d AND adgroup = '%s' AND hostid = '%s'", $aid, $action, date('YmdH'), adserve_variable('group'), adserve_variable('hostid'));
// If column doesn't already exist, we need to add it.
if (!db_affected_rows()) {
db_query("INSERT INTO {ad_statistics} (aid, date, action, adgroup, hostid, count) VALUES(%d, %d, '%s', '%s', '%s', 1)", $aid, date('YmdH'), $action, adserve_variable('hostid'), adserve_variable('hostid'));
// If another process already added this row our INSERT will fail, if
// so we still need to increment it so we don't loose an impression.
if (!db_affected_rows()) {
db_query("UPDATE {ad_statistics} SET count = count + 1 WHERE aid = %d AND action = '%s' AND date = %d AND adgroup = '%s' AND hostid = '%s'", $aid, $action, date('YmdH'), adserve_variable('group'), adserve_variable('hostid'));
}
}
if ($action == 'view') {
// See if we need to perform additional queries.
if (isset($ad->maxviews) && $ad->maxviews > 0) {
$views = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d", $aid, date('YmdH', $ad->activated)));
if ($views >= $ad->maxviews) {
db_query("UPDATE {ads} SET adstatus = 'expired', autoexpire = 0, autoexpired = %d, expired = %d WHERE aid = %d", time(), time(), $aid);
ad_statistics_increment($aid, 'autoexpired');
ad_statistics_increment($aid, 'expired');
}
}
}
// TODO: Do we need to do this here? Can it happen when a new click is
// registered?
if (isset($ad->maxclicks) && $ad->maxclicks > 0) {
$clicks = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d", $aid, date('YmdH', $ad->activated)));
if ($clicks >= $ad->maxclicks) {
db_query("UPDATE {ads} SET adstatus = 'expired', autoexpire = 0, autoexpired = %d, expired = %d WHERE aid = %d", time(), time(), $aid);
ad_statistics_increment($aid, 'autoexpired');
ad_statistics_increment($aid, 'expired');
}
}
}