From 0febf0d8bbc60feb019668785d345faea17e2e45 Fri, 9 Sep 2011 23:52:07 +0200
From: Jorrit Schippers <jorrit@161217.no-reply.drupal.org>
Date: Fri, 9 Sep 2011 23:51:29 +0200
Subject: [PATCH] issue #1274702: Hide the first page in pagebreak/h3 mode if it is empty. Add an administrator option to not have the first page name set to the node title.


diff --git a/pagination.install b/pagination.install
index da51560..7321b83 100644
--- a/pagination.install
+++ b/pagination.install
@@ -19,6 +19,7 @@
   variable_del('pagination_list_type');
   variable_del('pagination_collapsed');
   variable_del('pagination_filter');
+  variable_del('pagination_nodetitlepage1');
 }
 
 /**
diff --git a/pagination.module b/pagination.module
index 9daedd7..8f246ae 100644
--- a/pagination.module
+++ b/pagination.module
@@ -176,6 +176,12 @@
     '#description'    =>  t('Filters out old manual break pagination syntax. This prevents syntax like "[pagebreak]" from showing up in node types that are no longer paginated. (Default, filter old syntax)'),
     '#default_value'  =>  variable_get('pagination_filter', 1),
   );
+  $form['pagination_nodetitlepage1'] = array(
+    '#type'           =>  'checkbox',
+    '#title'          =>  t('Use the node title as the title for page one'),
+    '#description'    =>  t('Replaces the title of page one with the node title'),
+    '#default_value'  =>  variable_get('pagination_nodetitlepage1', 1),
+  );
   $form['submit'] = array(
     '#type'   =>  'submit',
     '#value'  =>  t('Set pagination'),
@@ -196,6 +202,7 @@
   $listtype   = $form_state['values']['pagination_list_type'];
   $showhelp   = $form_state['values']['pagination_collapsed'];
   $filter     = $form_state['values']['pagination_filter'];
+  $nodetitlepage1 = $form_state['values']['pagination_nodetitlepage1'];
 
   // Sadly, it's probably faster to just wipe the table and rebuild it than
   // checking for existing records first.
@@ -208,6 +215,7 @@
   variable_set('pagination_list_type', $listtype);
   variable_set('pagination_collapsed', (int) $showhelp);
   variable_set('pagination_filter', (int) $filter);
+  variable_set('pagination_nodetitlepage1', (int) $nodetitlepage1);
   foreach($pagination as $type=>$value)	{
     if ($value > 0) {
       $query = "INSERT INTO {pagination} (type, paginate, style) VALUES ('%s', %d, %d)";
@@ -525,9 +533,13 @@
   function paginate($text, $cutoff)    {
     $textsize = strlen($text);
     do {
-      $section      = $this->parseText($text, $cutoff);
-      $this->page[] = $section;
-      $textsize     = strlen($text);
+      $section = $this->parseText($text, $cutoff);
+
+      if (false !== $section) {
+        $this->page[] = $section;
+      }
+
+      $textsize = strlen($text);
     }
     while ($textsize > 0);
   }
@@ -646,7 +658,10 @@
     drupal_add_css(drupal_get_path('module', 'pagination').'/pagination.css');
     $toc        = array();
     $headers    = $this->getHeaders($nid);
-    $headers[0] = menu_get_active_title();
+    
+    if (variable_get('pagination_nodetitlepage1', 1) == 1) {
+      $headers[0] = menu_get_active_title();
+    }
 
     foreach($headers as $key=>$header)  {
       $page   = $key + 1;
@@ -700,13 +715,7 @@
    *  @void
    *  @private
    */
-  function setHeader($header) {
-    static $first;
-    if (!$first and $this->value < PAGINATION_AUTO)  {
-      // Manual paging should have a default header set for page 1
-      $first = true;
-      $this->headers[]  = t('Page 1');
-    }
+  function addHeader($header) {
     $this->headers[] = $header;
   }
 
@@ -729,9 +738,21 @@
         // 0:  [ pagebreak ]
         preg_match($this->re_custom, $text, $matches);
         if (isset($matches[0]) )  {
-          $header   = isset($matches[4]) && $matches[4] ? $matches[4] : t('Page !num', array('!num' => $page_count + 1) );
-          $this->setHeader($header);
           $section  = $this->parseSection($text, $matches, 1, 5);
+          $header   = isset($matches[4]) && $matches[4] ? $matches[4] : t('Page !num', array('!num' => $page_count + 1) );
+          
+          if (empty($this->headers)) {
+            if (empty($section)) {
+              // make paginate() ignore this page
+              $section = FALSE;
+            }
+            else {
+              // add a default page header for the first (empty) page
+              $this->addHeader(t('Page !num', array('!num' => 1)));
+            }
+          }
+          
+          $this->addHeader($header);
         }
         else {
           // No matches, send back full text
@@ -750,8 +771,20 @@
         preg_match($this->re_tag, $text, $matches);
         if (isset($matches[0]) )  {
           $header   = isset($matches[2]) ? $matches[2] : t('Page !num', array('!num' => $page_count + 1) );
-          $this->setHeader($header);
           $section  = $this->parseSection($text, $matches, 1, 3);
+
+          if (empty($this->headers)) {
+            if (empty($section)) {
+              // make paginate() ignore this page
+              $section = FALSE;
+            }
+            else {
+              // add a default page header for the first (empty) page
+              $this->addHeader(t('Page !num', array('!num' => 1)));
+            }
+          }
+
+          $this->addHeader($header);
         }
         else {
           // No matches, send back full text
@@ -763,7 +796,7 @@
         // Paging by words, we'll use the english approximate 5 chars per word
         // + space.
         $header   = t('Page !num', array('!num' => $page_count + 1) );
-        $this->setHeader($header);
+        $this->addHeader($header);
         $section  = $this->parseChunk($text, $cutoff * 6);
         $text     = substr($text, strlen($section) );
         break;
