You are here

function custom_pager_build_nav in Custom Pagers 5

Same name and namespace in other branches
  1. 6 custom_pagers.module \custom_pager_build_nav()
  2. 7 custom_pagers.module \custom_pager_build_nav()
2 calls to custom_pager_build_nav()
custom_pagers_block in ./custom_pagers.module
Implementation of hook_block().
custom_pagers_nodeapi in ./custom_pagers.module

File

./custom_pagers.module, line 405

Code

function custom_pager_build_nav($pager, $node) {
  static $pager_cache;
  $list = array();

  // First we check the static function cache for this pager.
  // If it's already been built for this page-load, we'll use it.
  if (isset($pager_cache[$pager->pid])) {
    $list = explode(',', $pager_cache[$pager->pid]);
  }

  // If it doesn't give us a list, and the pager is set to cache its
  // data, we'll try to load from the current user's session. We do it
  // that way rather than via cache_set() because of the potential for
  // node_access violations. Each user's node list *could* be different.
  if (empty($list) && $pager->cache_list) {
    if ($cache = $_SESSION['custom_pagers'][$pager->pid]) {

      // We should probably set the pager cache lifetime to a configurable
      // value. If any nodes drop through the cracks, users won't see the
      // pager when they visit them. Five minutes should keep the pager from
      // thrashing. In the future, we'll want to develop a better strategy
      // for this.
      if ($cache['timestamp'] < time() - 300) {
        unset($_SESSION['custom_pagers'][$pager->pid]);
      }
      else {
        $list = explode(',', $_SESSION['custom_pagers'][$pager->pid]['data']);
      }
    }
  }

  // If $list is still empty, neither caching strategy produced a list.
  // Let's build it from scratch!
  if (empty($list)) {

    // If the pager uses PHP, execute the PHP and run with the list.
    // Otherwise, use a view to get a list of node ids.
    if (!empty($pager->list_php)) {

      // Use PHP code to generate the list.
      ob_start();
      $result = eval(trim($pager->list_php));
      if (is_array($result)) {
        $list = $result;
      }
      ob_end_clean();
    }
    elseif (module_exists('views')) {

      // Use a view to generate the list.
      $args = explode("\n", $pager->args);
      if (module_exists('token')) {
        $args = token_replace($args, 'node', $node);
      }
      $view = views_get_view($pager->view);
      $tmp = views_build_view('items', $view, $args);
      if (count($tmp['items'])) {
        foreach ($tmp['items'] as $item) {
          $list[] = $item->nid;
        }
      }
    }
    if ($pager->reverse_list) {
      $list = array_reverse($list);
    }
  }

  // If we get to this point, we want to cache what we've made.
  if ($pager->cache_list) {
    $_SESSION['custom_pagers'][$pager->pid]['data'] = implode(',', $list);
    $_SESSION['custom_pagers'][$pager->pid]['timestamp'] = time();
  }
  $pager_cache[$pager->pid] = $list;
  return pager_entries_by_val($node->nid, $list);
}