You are here

function adserve_increment in Advertisement 5

Same name and namespace in other branches
  1. 6 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 632

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 ($cache != 'none') {
    $rc = adserve_invoke_file("ad_cache_{$cache}_increment", $action);
    if ($rc) {
      return;
    }
  }
  adserve_bootstrap();

  // Update view 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'", $ad->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)", $ad->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 a view.
    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'", $ad->aid, $action, date('YmdH'), adserve_variable('group'), adserve_variable('hostid'));
    }
  }
  if ($action == 'view') {

    // See if we need to perform additional queries.
    if ($ad->maxviews) {
      $views = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d", $ad->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(), $ad->aid);
        ad_statistics_increment($ad->aid, 'autoexpired');
        ad_statistics_increment($ad->aid, 'expired');
      }
    }
  }

  // TODO: Do we need to do this here?  Can it happen when a new click is
  // registered?
  if ($ad->maxclicks) {
    $clicks = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d", $ad->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(), $ad->aid);
      ad_statistics_increment($ad->aid, 'autoexpired');
      ad_statistics_increment($ad->aid, 'expired');
    }
  }
}