+/**
+ * Retrieve a list of writable backends
+ * @since 1.5.2
+ */
+function getWritableBackends () {
+ global $abook;
+
+ $write = array();
+ $backends = $abook->get_backend_list();
+ while (list($undef,$v) = each($backends)) {
+ if ($v->writeable) {
+ $write[$v->bnum]=$v->sname;
+ }
+ }
+
+ return $write;
+}
+
+/**
+ * Sort array by the key "name"
+ */
+function alistcmp($a,$b) {
+ $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;
+ } else {
+ if ($a['backend'] < $b['backend']) {
+ return -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 options
+ *
+ * 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_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 {
+ /* get previous sorting options. default to unsorted */
+ $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8);
+ }
+
+ return $abook_sort_order;
+}
+
+/**
+ * This function shows the address book sort button.
+ *
+ * @param integer $abook_sort_order Current sort value
+ * @param string $alt_tag The 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
+ * @param array $uri_extra Any additional parameters to add
+ * to the button's link, as an
+ * associative array of key/value pairs
+ * (OPTIONAL; default none)
+ *
+ * @return string html code with sorting images and urls
+ *
+ */
+function show_abook_sort_button($abook_sort_order, $alt_tag,
+ $Down, $Up, $uri_extra=array() ) {
+
+ global $form_url, $icon_theme_path;
+
+ /* Figure out which image we want to use. */
+ if ($abook_sort_order != $Up && $abook_sort_order != $Down) {
+ $img = 'sort_none.png';
+ $text_icon = '◻'; // U+25FB WHITE MEDIUM SQUARE
+ $which = $Up;
+ } elseif ($abook_sort_order == $Up) {
+ $img = 'up_pointer.png';
+ $text_icon = '⇧'; // U+21E7 UPWARDS WHITE ARROW
+ $which = $Down;
+ } else {
+ $img = 'down_pointer.png';
+ $text_icon = '⇩'; // U+21E9 DOWNWARDS WHITE ARROW
+ $which = 8;
+ }
+
+ $uri_extra['abook_sort_order'] = $which;
+ $uri = set_uri_vars($form_url, $uri_extra, FALSE);
+
+ /* Now that we have everything figured out, show the actual button. */
+ return create_hyperlink($uri,
+ getIcon($icon_theme_path, $img, $text_icon, $alt_tag),
+ '', '', '', '', '',
+ array('style' => 'text-decoration:none',
+ 'title' => $alt_tag),
+ FALSE);
+}
+