adding address book sorting options. changed some parts of patch written
authortokul <tokul@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Wed, 5 May 2004 11:27:56 +0000 (11:27 +0000)
committertokul <tokul@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Wed, 5 May 2004 11:27:56 +0000 (11:27 +0000)
by Brian Loniewski

git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@7384 7612ce4b-ef26-0410-bec9-ea0150e637f0

ChangeLog
functions/addressbook.php
src/addressbook.php

index fed91a8a61c692a758f004d221344f7bd72658ce..f5b749a0e8f30a7514e6260e95f207e7d1c36633 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -50,6 +50,8 @@ Version 1.5.1 -- CVS
   - Fixed date display bug for messages of today. Show short format in case
     of long format. (only occures in the timeframe around 0:00 AM till
     timezone).
+  - Added address book sorting options. Ascending/descending sorting code 
+    written by Bryan Loniewski 
 
 Version 1.5.0
 --------------------
index 705742df6bbd111ec26dab27f38b44f31b360f6e..9bce2351bab9a360eb51c219d31a2520e33aa8e1 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-
 /**
  * addressbook.php
  *
@@ -550,7 +549,27 @@ class addressbook_backend {
  * Sort array by the key "name"
  */
 function alistcmp($a,$b) {
-    $abook_sort=get_abook_sort();
+    $abook_sort_order=get_abook_sort();
+
+    switch ($abook_sort_order) {
+    case 0:
+    case 1:
+      $abook_sort='nickname';
+      break;
+    case 4:
+    case 5:
+      $abook_sort='email';
+      break;
+    case 6:
+    case 7:
+      $abook_sort='label';
+      break;
+    case 2:
+    case 3:
+    case 8:
+    default:
+      $abook_sort='name';
+    }
 
     if ($a['backend'] > $b['backend']) {
         return 1;
@@ -559,30 +578,68 @@ function alistcmp($a,$b) {
             return -1;
         }
     }
-    return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1;
+
+    if( (($abook_sort_order+2) % 2) == 1) {
+      return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1;
+    } else {
+      return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1;
+    }
 }
 
 /**
- * Address book sorting order
+ * Address book sorting options
  *
- * returns name of field that should be used for sorting.
- * @return string name of address book field
+ * returns address book sorting order
+ * @return integer book sorting options order
  */
 function get_abook_sort() {
     global $data_dir, $username;
 
     /* get sorting order */
-    if(sqgetGlobalVar('abook_sort', $abook_sort,SQ_GET)) {
-       sqgetGlobalVar('abook_sort', $abook_sort,SQ_GET);
-       setPref($data_dir, $username, 'abook_sort', $abook_sort);
+    if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) {
+      $abook_sort_order = (int) $temp;
+
+      if ($abook_sort_order < 0 or $abook_sort_order > 8)
+        $abook_sort_order=8;
+
+      setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order);
     } else {
-       $abook_sort = getPref($data_dir, $username, 'abook_sort', 'name');
+      /* get previous sorting options. default to unsorted */
+      $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8);
     }
 
-    if ($abook_sort != "nickname" && $abook_sort != "email" && $abook_sort != "label")
-        $abook_sort = "name";
+    return $abook_sort_order;
+}
+
+/**
+ * This function shows the address book sort button.
+ *
+ * @param integer $abook_sort_order current sort value
+ * @param string $alt_tag alt tag value (string visible to text only browsers)
+ * @param integer $Down sort value when list is sorted ascending
+ * @param integer $Up sort value when list is sorted descending
+ * @return string html code with sorting images and urls 
+ */
+function show_abook_sort_button($abook_sort_order, $alt_tag, $Down, $Up ) {
+    global $form_url;
+
+     /* Figure out which image we want to use. */
+    if ($abook_sort_order != $Up && $abook_sort_order != $Down) {
+        $img = 'sort_none.png';
+        $which = $Up;
+    } elseif ($abook_sort_order == $Up) {
+        $img = 'up_pointer.png';
+        $which = $Down;
+    } else {
+        $img = 'down_pointer.png';
+        $which = 8;
+    }
 
-    return $abook_sort;
+      /* Now that we have everything figured out, show the actual button. */
+    return ' <a href="' . $form_url .'?abook_sort_order=' . $which
+         . '"><img src="../images/' . $img
+         . '" border="0" width="12" height="10" alt="' . $alt_tag . '" title="'
+         . _("Click here to change the sorting of the address list") .'"></a>';
 }
 
 /*
index 262eb2d1a928a841336d41a614ba1597e83ec1fc..a3eab16157e83c13572e75b3b7fb1589a707cf60 100644 (file)
@@ -43,6 +43,9 @@ sqgetGlobalVar('oldnick',   $oldnick,   SQ_POST);
 sqgetGlobalVar('backend',   $backend,   SQ_POST);
 sqgetGlobalVar('doedit',    $doedit,    SQ_POST);
 
+/* Get sorting order */
+$abook_sort_order = get_abook_sort();
+
 /**
  * Make an input field
  * @param string $label
@@ -377,28 +380,17 @@ if ($showaddrlist) {
                       html_tag( 'tr', "\n" .
                           html_tag( 'th', '&nbsp;', 'left', '', 'width="1%"' ) . "\n" .
                           html_tag( 'th', _("Nickname") .
-                                    " <a href=\"$form_url?abook_sort=nickname\">".
-                                    "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" alt=\"sort by nickname\" title=\"" .
-                                    _("Click here to change the sorting of the address list") . 
-                                    "\" /></a>", 'left', '', 'width="1%"' ) . "\n" .
+                                    show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1)
+                                    , 'left', '', 'width="1%"' ) . "\n" .
                           html_tag( 'th', _("Name") . 
-                                    " <a href=\"$form_url?abook_sort=name\">" .
-                                    "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" " .
-                                    "alt=\"sort by name\" title=\"" .
-                                    _("Click here to change the sorting of the address list") . 
-                                    "\" /></a>", 'left', '', 'width="1%"' ) . "\n" .
+                                    show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3)
+                                    , 'left', '', 'width="1%"' ) . "\n" .
                           html_tag( 'th', _("E-mail") . 
-                                    " <a href=\"$form_url?abook_sort=email\">" .
-                                    "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" " .
-                                    "alt=\"sort by email\" title=\"" .
-                                    _("Click here to change the sorting of the address list") . 
-                                    "\" /></a>", 'left', '', 'width="1%"' ) . "\n" .
+                                    show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5)
+                                    , 'left', '', 'width="1%"' ) . "\n" .
                           html_tag( 'th', _("Info") .
-                                    " <a href=\"$form_url?abook_sort=label\">" .
-                                    "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" " .
-                                    "alt=\"sort by info\" title=\"" .
-                                    _("Click here to change the sorting of the address list") . 
-                                    "\" /></a>", 'left', '', 'width="1%"' ) . "\n",
+                                    show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7)
+                                    , 'left', '', 'width="1%"' ) . "\n",
                                 '', $color[9] ) . "\n";
                 }