You are here

function recently_read_block in Recently Read 6

Implementation of hook_block().

File

./recently_read.module, line 49
Recently read module file. Displays a history of recently read nodes by currently logged in user.

Code

function recently_read_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':

      // each enabled content type has its own block
      $types = node_get_types();
      $enabled = variable_get('recently_read_node_types', array(
        'page',
        'story',
      ));
      $blocks = array();
      foreach ($enabled as $key) {
        $blocks[$key]['info'] = t('Recently read - @type', array(
          '@type' => $types[$key]->name,
        ));
        $blocks[$key]['cache'] = BLOCK_NO_CACHE;
      }
      return $blocks;
    case 'configure':

      // allow user to customize the length of a list for each node type
      $max_entries = variable_get('recently_read_max_entries', 10);
      $max_count = variable_get("recently_read_max_length", array(
        'page' => 10,
        'story' => 10,
      ));
      $form['items_count'] = array(
        '#type' => 'textfield',
        '#title' => t('Maximum number of links to display in the block'),
        '#description' => t('Enter the positive integer value, less or equal to %limit.', array(
          '%limit' => $max_entries,
        )),
        '#default_value' => key_exists($delta, $max_count) ? $max_count[$delta] : $max_entries,
      );
      return $form;
    case 'save':

      // save configuration settings
      $max_entries = variable_get('recently_read_max_entries', 10);
      $value = max(1, min($edit['items_count'], $max_entries));
      $max_count = variable_get('recently_read_max_length', array(
        'page' => 10,
        'story' => 10,
      ));
      $max_count[$delta] = $value;
      variable_set('recently_read_max_length', $max_count);
      return;
    case 'view':

      // disable caching of entire page if recently read block is being displayed
      recently_read_disable_page_cache();

      // view block containing links to recently visited nodes
      global $user;
      $max_entries = variable_get('recently_read_max_entries', 10);
      $max_count = variable_get('recently_read_max_length', array(
        'page' => 10,
        'story' => 10,
      ));
      isset($max_count[$delta]) ? $limit = $max_count[$delta] : ($limit = $max_entries);
      $items = recently_read_get_read_items(array(
        $delta,
      ), $user->uid, $limit);
      $types = node_get_types();
      return array(
        'subject' => t('Recently read - @type', array(
          '@type' => $types[$delta]->name,
        )),
        'content' => theme('recently_read_item_list', $items),
      );
  }
}