You are here

function mostpopular_block in Drupal Most Popular 6

Implements hook_block() to create 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.

This block checks the URL and cookies to set its state, and adds CSS and Javascript to the page. Therefore, it should never be cached.

File

./mostpopular.module, line 200

Code

function mostpopular_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      return array(
        0 => array(
          'info' => t('Most Popular'),
          'cache' => BLOCK_NO_CACHE,
        ),
      );
    case 'view':
      switch ($delta) {
        case 0:

          // If the page is fully cached, don't use any service or interval.
          // Note that this will only happe in Javascript is enabled so we
          // don't need to worry about fallbacks.
          global $is_page_authcache;
          if ($is_page_authcache) {
            $sid = 0;
            $iid = 0;
          }
          else {

            // If we're looking at the mostpopular/items page, bypass the cookies
            // because the block gets loaded first before the cookies are set.
            if (arg(0) == 'mostpopular' && arg(1) == 'items') {
              $service = MostPopularService::fetch(arg(2));
              $interval = MostPopularInterval::fetch(arg(3));
            }
            elseif (isset($_COOKIE['mostpopular'])) {
              $cookie = $_COOKIE['mostpopular'];
              $parts = split('/', $cookie);
              if (count($parts) != 2) {
                drupal_set_message(t('You have an invalid cookie for the most popular service'), 'error');
              }
              else {
                $service = MostPopularService::fetch($parts[0]);
                $interval = MostPopularInterval::fetch($parts[1]);
              }
            }

            // Otherwise, use the defaults
            if (!isset($service)) {
              $service = MostPopularService::getDefault();
            }
            if (!isset($interval)) {
              $interval = MostPopularInterval::getDefault();
            }
            $sid = $service->sid;
            $iid = $interval->iid;
          }
          return array(
            'subject' => t('Most Popular'),
            'content' => theme('mostpopular_block', $sid, $iid),
          );
      }
      break;
  }
}