Happy 2017
[squirrelmail.git] / functions / template / paginator_util.php
index c43b845b9483c98d3545479c8749588ec253e54b..2c50927792734e4846f1e4c36a1d709b3528aab6 100644 (file)
@@ -6,7 +6,7 @@
  * The following functions are utility functions for templates. Do not
  * echo output in these functions.
  *
- * @copyright © 2005-2006 The SquirrelMail Project Team
+ * @copyright 2005-2017 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
@@ -20,14 +20,20 @@ include_once(SM_PATH . 'functions/forms.php');
  /**
   * Generate a paginator link.
   *
-  * @param string  $box Mailbox name
+  * @param string  $box       Mailbox name
   * @param integer $start_msg Message Offset
-  * @param string  $text text used for paginator link
+  * @param string  $text      The text used for paginator link
+  * @param string  $accesskey The access key for the link, if any
   * @return string
   */
-function get_paginator_link($box, $start_msg, $text) {
+function get_paginator_link($box, $start_msg, $text, $accesskey='NONE') {
     sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
-    return create_hyperlink("$php_self?startMessage=$start_msg&mailbox=$box", $text);
+    return create_hyperlink("$php_self?startMessage=$start_msg&mailbox=$box"
+                            . (strpos($php_self, 'src/search.php') ? '&smtoken=' . sm_generate_security_token() : ''),
+                            $text, '', '', '', '', '',
+                            ($accesskey == 'NONE'
+                            ? array()
+                            : array('accesskey' => $accesskey)));
 }
 
 
@@ -48,7 +54,10 @@ function get_paginator_link($box, $start_msg, $text) {
  */
 function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
 
-    global $oTemplate;
+    static $accesskeys_constructed = FALSE;
+
+    /* This will be used as a space. */
+    global $oTemplate, $nbsp;
 
     // keeps count of how many times
     // the paginator is used, avoids
@@ -68,8 +77,6 @@ function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,
     $box = urlencode($box);
 
     /* Create simple strings that will be creating the paginator. */
-    /* This will be used as a space. */
-    $spc = $oTemplate->fetch('non_breaking_space.tpl');
     /* This will be used as a seperator. */
     $sep = '|';
 
@@ -81,16 +88,27 @@ function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,
     $prev_grp = $iOffset - $iLimit;
 
     if (!$bShowAll) {
+
         /* Compute the basic previous and next strings. */
+
+        global $accesskey_mailbox_previous, $accesskey_mailbox_next;
         if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
-            $prv_str = get_paginator_link($box, $prev_grp, '<');
-            $nxt_str = get_paginator_link($box, $next_grp, '>');
+            $prv_str = get_paginator_link($box, $prev_grp, '<',
+                                          ($accesskeys_constructed
+                                          ? 'NONE' : $accesskey_mailbox_previous));
+            $nxt_str = get_paginator_link($box, $next_grp, '>',
+                                          ($accesskeys_constructed
+                                          ? 'NONE' : $accesskey_mailbox_next));
         } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
-            $prv_str = get_paginator_link($box, $prev_grp, '<');
+            $prv_str = get_paginator_link($box, $prev_grp, '<',
+                                          ($accesskeys_constructed
+                                          ? 'NONE' : $accesskey_mailbox_previous));
             $nxt_str = '>';
         } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
             $prv_str = '<';
-            $nxt_str = get_paginator_link($box, $next_grp, '>');
+            $nxt_str = get_paginator_link($box, $next_grp, '>',
+                                          ($accesskeys_constructed
+                                          ? 'NONE' : $accesskey_mailbox_next));
         }
 
         /* Page selector block. Following code computes page links. */
@@ -104,7 +122,8 @@ function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,
             $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
         }
     } else {
-        $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box", _("Paginate"));
+        global $accesskey_mailbox_all_paginate;
+        $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box" . (strpos($php_self, 'src/search.php') ? '&amp;smtoken=' . sm_generate_security_token() : ''), _("Paginate"), '', '', '', '', '', ($accesskeys_constructed ? array() : array('accesskey' => $accesskey_mailbox_all_paginate)));
     }
 
     /* Put all the pieces of the paginator string together. */
@@ -116,12 +135,13 @@ function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,
     if ( $prv_str || $nxt_str ) {
 
         /* Compute the 'show all' string. */
-        $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box", _("Show All"));
+        global $accesskey_mailbox_all_paginate;
+        $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box" . (strpos($php_self, 'src/search.php') ? '&amp;smtoken=' . sm_generate_security_token() : ''), _("Show All"), '', '', '', '', '', ($accesskeys_constructed ? array() : array('accesskey' => $accesskey_mailbox_all_paginate)));
 
         $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
         $result .= '[' . $prv_str . ']';
 
-        $pg_url = $php_self . '?mailbox=' . $box;
+        $pg_url = $php_self . '?mailbox=' . $box . (strpos($php_self, 'src/search.php') ? '&smtoken=' . sm_generate_security_token() : '');
 
         $result .= '[' . $nxt_str . ']';
         $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
@@ -131,7 +151,7 @@ function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,
             for ($p = 0; $p < $tot_pgs; $p++) {
                 $options[(($p*$iLimit)+1) . '_' . $box] = ($p+1) . "/$tot_pgs";
             }
-            $result .= $spc . addSelect('startMessage_' . $display_iterations, 
+            $result .= $nbsp . addSelect('startMessage_' . $display_iterations, 
                                         $options, 
                                         ((($cur_pg-1)*$iLimit)+1), 
                                         TRUE, 
@@ -152,13 +172,16 @@ function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,
         }
     }
 
-    $result .= ($pg_str  != '' ? '['.$pg_str.']' .  $spc : '');
-    $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
+    $result .= ($pg_str  != '' ? '['.$pg_str.']' .  $nbsp : '');
+    $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
 
     /* If the resulting string is blank, return a non-breaking space. */
     if ($result == '') {
         $result = '&nbsp;';
     }
+
+    $accesskeys_constructed = TRUE;
+
     /* Return our final magical paginator string. */
     return ($result);
 }
@@ -181,7 +204,10 @@ function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,
  */
 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
 
-    global $oTemplate;
+    static $accesskeys_constructed = FALSE;
+
+    /* This will be used as a space. */
+    global $oTemplate, $nbsp;
     sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
 
     /* Initialize paginator string chunks. */
@@ -193,8 +219,6 @@ function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_sel
     $box = urlencode($box);
 
     /* Create simple strings that will be creating the paginator. */
-    /* This will be used as a space. */
-    $spc = $oTemplate->fetch('non_breaking_space.tpl');
     /* This will be used as a seperator. */
     $sep = '|';
 
@@ -206,17 +230,27 @@ function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_sel
     $prev_grp = $iOffset - $iLimit;
 
     if (!$bShowAll) {
+
         /* Compute the basic previous and next strings. */
 
+        global $accesskey_mailbox_previous, $accesskey_mailbox_next;
         if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
-            $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
-            $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
+            $prv_str = get_paginator_link($box, $prev_grp, _("Previous"),
+                                          ($accesskeys_constructed
+                                          ? 'NONE' : $accesskey_mailbox_previous));
+            $nxt_str = get_paginator_link($box, $next_grp, _("Next"),
+                                          ($accesskeys_constructed
+                                          ? 'NONE' : $accesskey_mailbox_next));
         } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
-            $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
+            $prv_str = get_paginator_link($box, $prev_grp, _("Previous"),
+                                          ($accesskeys_constructed
+                                          ? 'NONE' : $accesskey_mailbox_previous));
             $nxt_str = _("Next");
         } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
             $prv_str = _("Previous");
-            $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
+            $nxt_str = get_paginator_link($box, $next_grp, _("Next"),
+                                          ($accesskeys_constructed
+                                          ? 'NONE' : $accesskey_mailbox_next));
         }
 
         /* Page selector block. Following code computes page links. */
@@ -295,49 +329,51 @@ function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_sel
 
             /* Start with the first quarter. */
             if (($q1_pgs == 0) && ($cur_pg > 1)) {
-                $pg_str .= "...$spc";
+                $pg_str .= "...$nbsp";
             } else {
                 for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
                     $start = (($pg-1) * $iLimit) + 1;
-                    $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
+                    $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
                 }
                 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
-                    $pg_str .= "...$spc";
+                    $pg_str .= "...$nbsp";
                 }
             }
 
             /* Continue with the second quarter. */
             for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
                 $start = (($pg-1) * $iLimit) + 1;
-                $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
+                $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
             }
 
             /* Now print the current page. */
-            $pg_str .= $cur_pg . $spc;
+            $pg_str .= $cur_pg . $nbsp;
 
             /* Next comes the third quarter. */
             for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
                 $start = (($pg-1) * $iLimit) + 1;
-                $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
+                $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
             }
 
             /* And last, print the forth quarter page links. */
             if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
-                $pg_str .= "...$spc";
+                $pg_str .= "...$nbsp";
             } else {
                 if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
-                    $pg_str .= "...$spc";
+                    $pg_str .= "...$nbsp";
                 }
                 for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
                     $start = (($pg-1) * $iLimit) + 1;
-                    $pg_str .= get_paginator_link($box, $start,$pg) . $spc;
+                    $pg_str .= get_paginator_link($box, $start,$pg) . $nbsp;
                 }
             }
 
             $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
         }
     } else {
-        $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box", _("Paginate"));
+        global $accesskey_mailbox_all_paginate;
+        $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box" . (strpos($php_self, 'src/search.php') ? '&amp;smtoken=' . sm_generate_security_token() : ''), _("Paginate"), '', '', '', '', '', ($accesskeys_constructed ? array() : array('accesskey' =>
+$accesskey_mailbox_all_paginate)));
     }
 
     /* Put all the pieces of the paginator string together. */
@@ -349,21 +385,26 @@ function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_sel
     if ( $prv_str || $nxt_str ) {
 
         /* Compute the 'show all' string. */
-        $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box", _("Show All"));
+        global $accesskey_mailbox_all_paginate;
+        $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box" . (strpos($php_self, 'src/search.php') ? '&amp;smtoken=' . sm_generate_security_token() : ''), _("Show All"), '', '', '', '', '', ($accesskeys_constructed ? array() : array('accesskey' =>
+$accesskey_mailbox_all_paginate)));
 
         $result .= '[';
-        $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : '');
+        $result .= ($prv_str != '' ? $prv_str . $nbsp . $sep . $nbsp : '');
         $result .= ($nxt_str != '' ? $nxt_str : '');
-        $result .= ']' . $spc ;
+        $result .= ']' . $nbsp ;
     }
 
-    $result .= ($pg_str  != '' ? $spc . '['.$spc.$pg_str.']' .  $spc : '');
-    $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
+    $result .= ($pg_str  != '' ? $nbsp . '['.$nbsp.$pg_str.']' .  $nbsp : '');
+    $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
 
     /* If the resulting string is blank, return a non-breaking space. */
     if ($result == '') {
-        $result = $oTemplate->fetch('non_breaking_space.tpl');
+        $result = $nbsp;
     }
+
+    $accesskeys_constructed = TRUE;
+
     /* Return our final magical compact paginator string. */
     return ($result);
 }