From 24d6cf8513058317af0afd16f15dd2f04d38514d Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 26 Oct 2012 13:50:25 -0400
Subject: [PATCH] Progress on VUFIND-603: allow custom alphabet in Browse
 module.

Also fixed bug that was breaking alphabet list display, and fixed an outdated comment.
---
 config/vufind/config.ini                      |  2 +
 .../VuFind/Controller/BrowseController.php    | 48 ++++++++-----------
 2 files changed, 23 insertions(+), 27 deletions(-)

diff --git a/config/vufind/config.ini b/config/vufind/config.ini
index dcde0581105..7eca3ac6b0f 100644
--- a/config/vufind/config.ini
+++ b/config/vufind/config.ini
@@ -640,6 +640,8 @@ topic           = true      ; allow browsing of subject headings
 genre           = true      ; allow browsing of genre subdivisions
 region          = true      ; allow browsing of region subdivisions
 era             = true      ; allow browsing of era subdivisions
+; You can use this setting to change the default alphabet provided for browsing:
+;alphabet_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
 ; This section controls which record export methods are displayed on the Record
 ; view screen.  Note that some options may be disabled for records that do not
diff --git a/module/VuFind/src/VuFind/Controller/BrowseController.php b/module/VuFind/src/VuFind/Controller/BrowseController.php
index f5eaca51ef0..5ff95f4e48e 100644
--- a/module/VuFind/src/VuFind/Controller/BrowseController.php
+++ b/module/VuFind/src/VuFind/Controller/BrowseController.php
@@ -281,7 +281,7 @@ class BrowseController extends AbstractBase
                 if (isset($params['query'])) {
                     // Note -- this does not need to be escaped because
                     // $params['query'] has already been validated against
-                    // the _getAlphabetList() method below!
+                    // the getAlphabetList() method below!
                     $tags = $tagTable->matchText($params['query']);
                     $tagList = array();
                     foreach ($tags as $tag) {
@@ -504,7 +504,7 @@ class BrowseController extends AbstractBase
         $category = $this->getCategory();
         switch($facet) {
         case 'alphabetical':
-            return $this->getAlphabetList();
+            return array('', $this->getAlphabetList());
         case 'dewey':
             return array(
                 'dewey-tens', $this->quoteValues(
@@ -643,31 +643,25 @@ class BrowseController extends AbstractBase
      */
     protected function getAlphabetList()
     {
-        // ALPHABET TO ['value','displayText']
-        $alphabet = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
-        foreach ($alphabet as $index=>$letter) {
-            $alphabet[$index] = array(
-                'value'       => $letter,
-                'displayText' => $letter
-            );
-        }
-        if ($this->getCurrentAction() == 'Tag') {
-            return $alphabet;
-        }
-        // ADD ASTERISK FOR THOSE THAT NEED IT
-        foreach ($alphabet as $index=>$letter) {
-            $letter['value'] .= '*';
-            $alphabet[$index] = $letter;
-        }
-        if ($this->getCurrentAction() != 'Era') {
-            return $alphabet;
-        }
-        // PUT NUMBERS FIRST FOR YEARS
-        array_unshift($alphabet, $alphabet[count($alphabet)-10]);
-        unset($alphabet[count($alphabet)-10]);
-        for ($i=0;$i<9;$i++) {
-            array_unshift($alphabet, array_pop($alphabet));
+        // Get base alphabet:
+        $chars = isset($this->config->Browse->alphabet_letters)
+            ? $this->config->Browse->alphabet_letters
+            : 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
+
+        // Put numbers in the front for Era since years are important:
+        if ($this->getCurrentAction() == 'Era') {
+            $chars = '0123456789' . $chars;
+        } else {
+            $chars .= '0123456789';
         }
-        return $alphabet;
+
+        // ALPHABET TO ['value','displayText']
+        // (value has asterix appended for Solr, but is unmodified for tags)
+        $suffix = $this->getCurrentAction() == 'Tag' ? '' : '*';
+        $callback = function ($letter) use ($suffix) {
+            return array('value' => $letter . $suffix, 'displayText' => $letter);
+        };
+        preg_match_all('/(.)/u', $chars, $matches);
+        return array_map($callback, $matches[1]);
     }
 }
\ No newline at end of file
-- 
GitLab