You are here

function mostpopular_block_view in Drupal Most Popular 7

Implements hook_block_view().

Creates the most popular block.

The service and interval to show by default are loaded from the user's cookie. If they don't have a cookie set, the first service and first interval will be selected by default.

Parameters

integer $bid The ID of the block.:

File

./mostpopular.module, line 837
The main file for the Most Popular module.

Code

function mostpopular_block_view($bid = '') {
  $block = mostpopular_blocks($bid);
  if ($block) {
    if (!empty($block->data['remote_database'])) {
      if (!db_set_active($block->data['remote_database'])) {
        watchdog('mostpopular', 'Missing remote database @database', array(
          '@database' => $block->data['remote_database'],
        ), WATCHDOG_ERROR);
        db_set_active('default');
        return NULL;
      }
      $services = mostpopular_service_load_by_block($block->remote_bid);
      $intervals = mostpopular_interval_load_by_block($block->remote_bid);
      db_set_active('default');
    }
    else {
      $services = mostpopular_service_load_by_block($bid);
      $intervals = mostpopular_interval_load_by_block($bid);
    }
    $out = array(
      'subject' => !empty($block->title) ? $block->title : t('Most Popular'),
      'content' => array(
        '#theme' => 'mostpopular_block',
        '#services' => $services,
        '#intervals' => $intervals,
        '#bid' => $bid,
      ),
    );

    // Attach the stylesheets and javascript
    $path = drupal_get_path('module', 'mostpopular');
    switch (variable_get('mostpopular_styling', MOSTPOPULAR_STYLE_FULL)) {
      case MOSTPOPULAR_STYLE_BASIC:
        $out['content']['#attached']['css'][] = "{$path}/css/mostpopular-basic.css";
        break;
      case MOSTPOPULAR_STYLE_FULL:
        $out['content']['#attached']['css'][] = "{$path}/css/mostpopular-basic.css";
        $out['content']['#attached']['css'][] = "{$path}/css/mostpopular-full.css";
        break;
    }
    $out['content']['#attached']['js'][] = 'misc/jquery.cookie.js';
    $out['content']['#attached']['js'][] = "{$path}/js/fade.js";
    $out['content']['#attached']['js'][] = "{$path}/js/mostpopular.js";
    $out['content']['#attached']['js'][] = array(
      'data' => array(
        'mostpopular' => array(
          'url' => url('mostpopular/ajax'),
        ),
      ),
      'type' => 'setting',
    );
    return $out;
  }
  return NULL;
}