You are here

function adsense_click_by_day in Google AdSense integration 7

Same name and namespace in other branches
  1. 5.3 contrib/adsense_click/adsense_click.logs.inc \adsense_click_by_day()
  2. 5 adsense.module \adsense_click_by_day()
  3. 5.2 adsense.module \adsense_click_by_day()
  4. 6 contrib/adsense_click/adsense_click.logs.inc \adsense_click_by_day()

Click by day.

1 string reference to 'adsense_click_by_day'
adsense_click_menu in contrib/adsense_click/adsense_click.module
Implements hook_menu().

File

contrib/adsense_click/adsense_click.logs.inc, line 99
Functions to display the logs tracked by the adsense_click module.

Code

function adsense_click_by_day() {
  $header = array(
    array(
      'data' => t('Day'),
      'field' => 'day',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Clicks'),
      'field' => 'count',
    ),
  );
  $rows = array();
  $query = db_select('adsense_clicks', 'ads');
  $count_query = clone $query;
  $query = $query
    ->extend('PagerDefault')
    ->extend('TableSort');

  // @TODO Must be an easier and prettier way of doing this.
  $database_driver = db_driver();
  switch ($database_driver) {
    case 'mysql':
      $count_query
        ->addExpression("COUNT(DISTINCT(FROM_UNIXTIME(timestamp, '%y-%m-%d')))");
      $query
        ->addExpression("FROM_UNIXTIME(timestamp, '%Y-%m-%d')", 'day');
      break;
    case 'pgsql':
      $count_query
        ->addExpression("COUNT(DISTINCT(TO_CHAR(timestamp, 'YYYY-MM-DD')))");
      $query
        ->addExpression("TO_CHAR(timestamp, 'YYYY-MM-DD')", 'day');
      break;
    case 'sqlite':
      $count_query
        ->addExpression("COUNT(DISTINCT(datetime(timestamp, 'unixepoch', 'localtime')))");
      $query
        ->addExpression("datetime(timestamp, 'unixepoch', 'localtime')", 'day');
      break;
  }

  // Extend our query and add our count expression.
  $query
    ->addExpression('COUNT(*)', 'count');
  $query
    ->groupBy('day')
    ->limit(50)
    ->orderByHeader($header)
    ->setCountQuery($count_query);
  $result = $query
    ->execute();
  foreach ($result as $log) {
    $rows[] = array(
      $log->day,
      array(
        'data' => $log->count,
        'align' => 'right',
      ),
    );
  }
  return theme('table', array(
    'header' => $header,
    'rows' => $rows,
  )) . theme('pager');
}