You are here

function yandex_metrics_block_view in Yandex.Metrics 7

Implements hook_block_view().

Display the most popular pages.

File

./yandex_metrics.module, line 169
The main code of Yandex.Metrics module.

Code

function yandex_metrics_block_view($delta = '') {
  if (user_access('access content')) {
    $check_links_count_variable = variable_get('yandex_metrics_popular_content_links_count', 'undefined');
    $check_date_period_variable = variable_get('yandex_metrics_popular_content_date_period', 'undefined');

    // We can figure out that block is showing for the first time when config variables aren't defined
    if ($check_links_count_variable == 'undefined' && $check_date_period_variable == 'undefined') {

      // If the block is showing for the first time then fetch popular content from Yandex
      yandex_metrics_save_popular_content();
      variable_set('yandex_metrics_popular_content_links_count', 10);
      variable_set('yandex_metrics_popular_content_date_period', 'week');
    }
    $links = array();
    $links_count = variable_get('yandex_metrics_popular_content_links_count', 10);

    //$result = db_query_range("SELECT url, page_title FROM {yandex_metrics_popular_content} ORDER BY page_views DESC", 0, YANDEX_METRICS_POPULAR_CONTENT_BLOCK_LIMIT);
    $records = db_select('yandex_metrics_popular_content', 'y')
      ->fields('y', array(
      'url',
      'page_title',
    ))
      ->orderBy('page_views', 'DESC')
      ->range(0, YANDEX_METRICS_POPULAR_CONTENT_BLOCK_LIMIT)
      ->execute();
    foreach ($records as $record) {
      if (count($links) < $links_count) {
        $links[] = array(
          'title' => $record->page_title,
          'href' => $record->url,
        );
      }
    }
    $block = array();
    if (!empty($links)) {
      $block['subject'] = t('Popular content');
      $block['content'] = theme('links__popular_content', array(
        'links' => $links,
      ));
    }
    return $block;
  }
}