You are here

function yandex_metrics_reports_block in Yandex.Metrics 6.2

Implementation of hook_block().

Display the most popular pages.

File

yandex_metrics_reports/yandex_metrics_reports.module, line 89
The main code of Yandex.Metrics Reports module.

Code

function yandex_metrics_reports_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $block[0]['info'] = t('Popular content');
      return $block;
    case 'configure':
      $form = array();
      if ($delta == 0) {
        $options = array(
          'day' => t('Today'),
          'yesterday' => t('Yesterday'),
          'week' => t('Week'),
          'month' => t('Month'),
        );
        $form['yandex_metrics_reports_popular_content_date_period'] = array(
          '#type' => 'select',
          '#title' => t('Select date period'),
          '#description' => t('This date period will be used to filter popular content.'),
          '#options' => $options,
          '#default_value' => variable_get('yandex_metrics_reports_popular_content_date_period', 'week'),
        );
        $form['yandex_metrics_reports_popular_content_links_count'] = array(
          '#type' => 'select',
          '#title' => t('Select count of links to show'),
          '#description' => t('The count of links that shown in the block.'),
          '#options' => drupal_map_assoc(array(
            5,
            10,
            15,
          )),
          '#default_value' => variable_get('yandex_metrics_reports_popular_content_links_count', 10),
        );
      }
      return $form;
    case 'save':
      if ($delta == 0) {
        variable_set('yandex_metrics_reports_popular_content_links_count', $edit['yandex_metrics_reports_popular_content_links_count']);
        $previous_date_period = variable_get('yandex_metrics_reports_popular_content_date_period', 'week');
        $new_date_period = $edit['yandex_metrics_reports_popular_content_date_period'];

        // Update already saved Popular content data if the date period has been changed.
        if ($previous_date_period != $new_date_period) {
          variable_set('yandex_metrics_reports_popular_content_date_period', $new_date_period);
          yandex_metrics_reports_save_popular_content();
        }
      }
      break;
    case 'view':
    default:
      if (user_access('access content')) {
        $check_links_count_variable = variable_get('yandex_metrics_reports_popular_content_links_count', 'undefined');
        $check_date_period_variable = variable_get('yandex_metrics_reports_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_reports_save_popular_content();
          variable_set('yandex_metrics_reports_popular_content_links_count', 10);
          variable_set('yandex_metrics_reports_popular_content_date_period', 'week');
        }
        $links = array();
        $links_count = variable_get('yandex_metrics_reports_popular_content_links_count', 10);
        $result = db_query_range("SELECT url, page_title FROM {yandex_metrics_reports_popular_content} ORDER BY page_views DESC", 0, YANDEX_METRICS_POPULAR_CONTENT_BLOCK_LIMIT);
        while ($data = db_fetch_object($result)) {
          if (count($links) >= $links_count) {
            break;
          }
          $links[] = array(
            'title' => $data->page_title,
            'href' => $data->url,
          );
        }
        $block = array();
        if (!empty($links)) {
          $block['subject'] = t('Popular content');
          $block['content'] = theme('links', $links, array(
            'class' => 'item-list action-links',
          ));
        }
        return $block;
      }
  }
}