You are here

function _paging_nodeapi in Paging 6

Same name and namespace in other branches
  1. 5 paging.module \_paging_nodeapi()

Helper function for paging_nodeapi().

1 call to _paging_nodeapi()
paging_nodeapi in ./paging.module
Implementation of hook_nodeapi().

File

./paging.module, line 482
Allows users to use a tag to break up a node into multiple pages.

Code

function _paging_nodeapi(&$node, &$node_body, &$node_teaser, $op, $teaser, $page) {
  switch ($op) {
    case 'load':
      $paging_separator = variable_get('paging_separator_' . $node->type, '<!--pagebreak-->');

      // Check if manual page separators were used.
      if (strpos($teaser ? $node_teaser : $node_body, $paging_separator) !== FALSE) {
        $node->pages = explode($paging_separator, $node_body);
        $node->page_count = count($node->pages);
      }
      else {
        $body_parts = $node_body;

        // Automatic paging based on character count.
        if (variable_get('paging_automatic_method_' . $node->type, 0) == 1 && ($max_chars = variable_get('paging_automatic_chars_' . $node->type, 4000)) != 0) {
          $total_chars = drupal_strlen($node_body);

          // Check if pagination is possible.
          if ($total_chars > $max_chars) {
            $body = $node_body;
            $breaks = (int) ($total_chars / $max_chars);
            $bodypart = array();
            $i = 0;
            while ($body) {
              $bodypart[$i] = _paging_body_shift($body, $max_chars);
              $bodycount = drupal_strlen($bodypart[$i]);
              $body = drupal_substr($body, $bodycount);
              $i++;
            }
            $body_parts = implode($paging_separator, $bodypart);
          }
        }
        elseif (variable_get('paging_automatic_method_' . $node->type, 0) == 2 && ($max_words = variable_get('paging_automatic_words_' . $node->type, 400)) != 0) {
          $words = explode(' ', $node_body);
          $total_words = count($words);

          // Check if pagination is possible.
          if ($total_words > $max_words) {
            $breaks = (int) ($total_words / $max_words);
            for ($i = 1; $i < $breaks; $i++) {
              $index = $i * $max_words;
              $words[$index] .= $paging_separator;
            }
          }
          $body_parts = implode(' ', $words);
        }
        $node->pages = explode($paging_separator, $body_parts);
        $node->page_count = count($node->pages);
      }
      break;
    case 'view':

      // Fetch a structured array containing page names.
      $node->page_names = paging_fetch_names($node->body);

      // Check if node is being viewed as a teaser (and not as a preview).
      if ($teaser && property_exists($node, 'page_count') && $node->page_count > 1 && drupal_strlen($node->teaser) > drupal_strlen($node->pages[0])) {

        // If the teaser is longer than our first page, there are more pages.
        $node->pagemore = TRUE;
      }
      if (property_exists($node, 'page_count') && isset($node->page_count)) {

        // Element value to distinguish between multiple pagers on one page.
        $element = 1;
        $page = isset($_GET['page']) ? $_GET['page'] : '0,0';
        $page_elements = explode(',', $page);
        $node->page_current = $page_elements[1];

        // Only do paging
        // a) if not in teaser mode;
        // b) if there is more than one page;
        // c) if a printable version is not being requested; or
        // d) if a non-paged version is not being explicitly requested
        //    e.g. http://www.example.com/node/1?page=full or node/1/full.
        if (!$teaser && $node->page_count > 1 && arg(2) != 'print' && arg(2) != 'full' && $page != 'full') {
          global $pager_page_array, $pager_total;
          $pager_page_array = explode(',', $page);
          $pager_total[$element] = $node->page_count;
          $page = isset($pager_page_array[$element]) ? $pager_page_array[$element] : 0;

          // Put the current page contents into the node body.
          $node->content['body']['#value'] = check_markup($node->pages[$page], $node->format, FALSE);

          // Mapping the pages in $node->page_names and $node->page_count to set number of pages as the array length.
          $fake = array_fill(0, $node->page_count - 1 + 1, '');
          $length = count($fake) > count($node->page_names) ? count($fake) : count($node->page_names);
          for ($i = 0; $i < $length; ++$i) {
            $merged[$i] = $node->page_names[$i];
          }

          // Fill the empty names with node title and page number.
          $node->page_names = _paging_populate_empty_names($merged, $node->title);

          // For use in AJAX.
          $pager_id = 'paging-pager-' . $node->nid;
          $return_json = FALSE;

          // Capture pager JSON request
          if (isset($_REQUEST['paging_json_request']) && $_REQUEST['paging_json_request'] == $pager_id) {

            // Unset before calling a pager theming function to prevent unecessarily cluttered link URLs.
            unset($_REQUEST['paging_json_request']);
            $return_json = TRUE;
          }

          // Load the page navigation links into $node->paging. Also accessible in node theming.
          $node->paging = paging_pager_style($node, $element);

          // Find the position to display the page navigation links at.
          $position = variable_get('paging_pager_widget_position_' . $node->type, 'below');
          if ($position == 'above' || $position == 'both') {
            $node->content['paging_above']['#value'] = $node->paging;

            // Get possible manual weight for paging field from CCK setting.
            if (function_exists('content_extra_field_weight')) {
              $node->content['paging_above']['#weight'] = content_extra_field_weight($node->type, 'paging_above');
            }
            else {
              $node->content['paging_above']['#weight'] = $node->content['body']['#weight'] - 1;
            }
          }
          if ($position == 'below' || $position == 'both') {
            $node->content['paging']['#value'] = $node->paging;

            // Get possible manual weight for paging field from CCK setting.
            if (function_exists('content_extra_field_weight')) {
              $node->content['paging']['#weight'] = content_extra_field_weight($node->type, 'paging');
            }
            else {
              $node->content['paging']['#weight'] = $node->content['body']['#weight'] + 1;
            }
          }
          $module_path = drupal_get_path('module', 'paging');
          drupal_add_css($module_path . '/paging.css', 'module');
          if (variable_get('paging_ajax_enabled_' . $node->type, 0)) {
            _paging_content_wrap($node);
            if ($return_json) {
              $content = array(
                'paging_above' => $node->content['paging_above'],
                'body' => $node->content['body'],
                'paging' => $node->content['paging'],
              );
              $response = array(
                'content' => drupal_render($content),
              );

              // Exit with replacement data.
              exit(drupal_json($response));
            }

            // Add scripts for AJAX driven page loading.
            drupal_add_js($module_path . '/paging.js', 'module');
          }

          // Set a global value for block visibility.
          $GLOBALS['_paging_display_block'] = TRUE;
          if (variable_get('paging_name_title_' . $node->type, 0) && !empty($page)) {

            // Set the browser title to page's name.
            drupal_set_title($node->page_names[$page]);
          }
        }
      }
      break;
  }
}