From 4423c6e4b4b53847c1c8aa7e77c792e2f63ec26c Mon Sep 17 00:00:00 2001
From: wulff <wulff@ratatosk.net>
Date: Sun, 20 Mar 2011 18:34:06 +0100
Subject: [PATCH] Issue #806944 by wulff: Added alphabetic pager to the project overview page.

---
 l10n_community/l10n_community.css |    8 +++++
 l10n_community/pages.inc          |   52 ++++++++++++++++++++++++++++++++++---
 l10n_server/l10n_server.module    |   16 +++++++++--
 3 files changed, 69 insertions(+), 7 deletions(-)

diff --git a/l10n_community/l10n_community.css b/l10n_community/l10n_community.css
index 418fd4a..2349b35 100644
--- a/l10n_community/l10n_community.css
+++ b/l10n_community/l10n_community.css
@@ -134,3 +134,11 @@ em.l10n-community-marker {
 #block-l10n_community-contributors tr.even, #block-l10n_community-contributors tr.odd {
   background-color: transparent;
 }
+
+/* Alphabetic pager for browsing projects */
+.item-list ul.pager-alphabetic {
+  margin: -1em 0 1em;
+  padding: 0.5em;
+  font-size: 14px;
+  background-color: #f6f6f2;
+}
diff --git a/l10n_community/pages.inc b/l10n_community/pages.inc
index cb4c9f3..3ec5628 100644
--- a/l10n_community/pages.inc
+++ b/l10n_community/pages.inc
@@ -94,13 +94,20 @@ function l10n_server_sort_by_column_language($a, $b) {
  * a pager on this screen.
  */
 function l10n_community_explore_projects() {
+  $output = '';
+
   $pager_setting = variable_get('l10n_community_project_per_page', 10);
 
-  $output = '';
-  if (!$projects = l10n_server_get_projects(array('pager' => $pager_setting))) {
+  $options = array('pager' => $pager_setting);
+  if (isset($_GET['initial'])) {
+    $options['initial'] = $_GET['initial'];
+  }
+
+  if (!$projects = l10n_server_get_projects($options)) {
     drupal_set_message(t('No projects found.'), 'error');
     return '';
   }
+
   $languages = l10n_community_get_languages('name');
   $language_count = count($languages);
   $string_counts = l10n_community_get_string_count('projects');
@@ -111,7 +118,11 @@ function l10n_community_explore_projects() {
     $output .= drupal_get_form('l10n_community_explore_projects_jump_form');
   }
 
-  $output .= theme('pager', NULL, $pager_setting, 0);
+  $pager = theme('pager', NULL, $pager_setting, 0);
+
+  $output .= _l10n_community_alphabetic_pager();
+  $output .= $pager;
+
   $table = array();
   foreach ($projects as $project) {
     $table[] = array_merge(
@@ -129,7 +140,7 @@ function l10n_community_explore_projects() {
     $table,
     array('class' => 'l10n-community-overview')
   );
-  $output .= theme('pager', NULL, $pager_setting, 0);
+  $output .= $pager;
   return $output .'<div class="clear-block"></div>';
 }
 
@@ -669,3 +680,36 @@ function l10n_community_get_string_count($type, $id = NULL) {
       return $accounts;
   }
 }
+
+/**
+ * Create an alphabetic pager for browsing projects.
+ */
+function _l10n_community_alphabetic_pager() {
+  $output = '';
+
+  // Grab the unique initials of all active projects
+  $result = db_query('SELECT DISTINCT(SUBSTR(uri, 1, 1)) AS initial FROM l10n_server_project WHERE status = 1 ORDER BY initial ASC');
+
+  // Create an array of pager elements, setting a special class on the active element
+  $items = array();
+  while ($initial = db_fetch_object($result)) {
+    if (isset($_GET['initial']) && $_GET['initial'] == $initial->initial) {
+      $items[] = array(
+        'class' => 'pager-current',
+        'data' => strtoupper($initial->initial),
+      );
+    }
+    else {
+      $items[] = array(
+        'class' => 'pager-item',
+        'data' => l(strtoupper($initial->initial), 'translate/projects', array('query' => array('initial' => $initial->initial))),
+      );
+    }
+  }
+
+  if (!empty($items)) {
+    $output .= theme('item_list', $items, NULL, 'ul', array('class' => 'pager pager-alphabetic'));
+  }
+
+  return $output;
+}
diff --git a/l10n_server/l10n_server.module b/l10n_server/l10n_server.module
index 66c6612..dd58762 100644
--- a/l10n_server/l10n_server.module
+++ b/l10n_server/l10n_server.module
@@ -487,11 +487,21 @@ function l10n_server_get_projects($options = array()) {
   static $projects = array();
 
   // Consider returning all projects or just published ones.
-  $published = (empty($options['all']) ? 'WHERE status = 1 ' : '');
+  $where = (empty($options['all']) ? 'WHERE status = 1 ' : '');
 
   if (isset($options['pager'])) {
+    $arguments = array();
+
+    if (isset($options['initial'])) {
+      if (!empty($where)) {
+        $where .= 'AND ';
+      }
+      $where .= "SUBSTR(uri, 1, 1) = '%s' ";
+      $arguments[] = $options['initial'];
+    }
+
     // If a pager view was asked for, collect data independently.
-    $results = pager_query('SELECT * FROM {l10n_server_project} '. $published .'ORDER BY title', $options['pager'], 0, NULL);
+    $results = pager_query('SELECT * FROM {l10n_server_project} '. $where .'ORDER BY title', $options['pager'], 0, NULL, $arguments);
     $pager_results = array();
     while ($project = db_fetch_object($results)) {
       $pager_results[$project->uri] = $project;
@@ -516,7 +526,7 @@ function l10n_server_get_projects($options = array()) {
     }
     else {
       // A list of *all* projects was asked for.
-      $results = db_query('SELECT * FROM {l10n_server_project} '. $published .'ORDER BY uri');
+      $results = db_query('SELECT * FROM {l10n_server_project} '. $where .'ORDER BY uri');
       while ($project = db_fetch_object($results)) {
         $projects[$project->uri] = $project;
       }
-- 
1.7.0.3

