Merge pull request #4875 from civicrm/minor-fix
[civicrm-core.git] / CRM / Utils / PagerAToZ.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class is for displaying alphabetical bar
38 *
39 */
40 class CRM_Utils_PagerAToZ {
41
42 /**
43 * Returns the alphabetic array for sorting by character
44 *
45 * @param array $query
46 * The query object.
47 * @param string $sortByCharacter
48 * The character that we are potentially sorting on.
49 *
50 * @param bool $isDAO
51 *
52 * @return string
53 * The html formatted string
54 * @static
55 */
56 public static function getAToZBar(&$query, $sortByCharacter, $isDAO = FALSE) {
57 $AToZBar = self::createLinks($query, $sortByCharacter, $isDAO);
58 return $AToZBar;
59 }
60
61 /**
62 * Return the all the static characters
63 *
64 * @return array
65 * is a array of static characters
66 * @static
67 */
68 public static function getStaticCharacters() {
69 $staticAlphabets = array(
70 'A',
71 'B',
72 'C',
73 'D',
74 'E',
75 'F',
76 'G',
77 'H',
78 'I',
79 'J',
80 'K',
81 'L',
82 'M',
83 'N',
84 'O',
85 'P',
86 'Q',
87 'R',
88 'S',
89 'T',
90 'U',
91 'V',
92 'W',
93 'X',
94 'Y',
95 'Z'
96 );
97 return $staticAlphabets;
98 }
99
100 /**
101 * Return the all the dynamic characters
102 *
103 * @param $query
104 * @param $isDAO
105 *
106 * @return array
107 * is a array of dynamic characters
108 * @static
109 */
110 public static function getDynamicCharacters(&$query, $isDAO) {
111 if ($isDAO) {
112 $result = $query;
113 }
114 else {
115 $result = $query->alphabetQuery();
116 }
117 if (!$result) {
118 return NULL;
119 }
120
121 $dynamicAlphabets = array();
122 while ($result->fetch()) {
123 $dynamicAlphabets[] = $result->sort_name;
124 }
125 return $dynamicAlphabets;
126 }
127
128 /**
129 * Create the links
130 *
131 * @param array $query
132 * The form values for search.
133 * @param string $sortByCharacter
134 * The character that we are potentially sorting on.
135 *
136 * @param $isDAO
137 *
138 * @return array
139 * with links
140 * @static
141 */
142 public static function createLinks(&$query, $sortByCharacter, $isDAO) {
143 $AToZBar = self::getStaticCharacters();
144 $dynamicAlphabets = self::getDynamicCharacters($query, $isDAO);
145
146 if (!$dynamicAlphabets) {
147 return NULL;
148 }
149
150 $AToZBar = array_merge($AToZBar, $dynamicAlphabets);
151 sort($AToZBar, SORT_STRING);
152 $AToZBar = array_unique($AToZBar);
153
154 //get the current path
155 $path = CRM_Utils_System::currentPath();
156
157 $qfKey = NULL;
158 if (isset($query->_formValues)) {
159 $qfKey = CRM_Utils_Array::value('qfKey', $query->_formValues);
160 }
161 if (empty($qfKey)) {
162 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this, FALSE, NULL, $_REQUEST);
163 }
164
165 $aToZBar = array();
166 foreach ($AToZBar as $key => $link) {
167 if ($link === NULL) {
168 continue;
169 }
170
171 $element = array();
172 if (in_array($link, $dynamicAlphabets)) {
173 $klass = '';
174 if ($link == $sortByCharacter) {
175 $element['class'] = "active";
176 $klass = 'class="active"';
177 }
178 $url = CRM_Utils_System::url($path, "force=1&qfKey=$qfKey&sortByCharacter=");
179 // we do it this way since we want the url to be encoded but not the link character
180 // since that seems to mess up drupal utf-8 encoding etc
181 $url .= urlencode($link);
182 $element['item'] = sprintf('<a href="%s" %s>%s</a>',
183 $url,
184 $klass,
185 $link
186 );
187 }
188 else {
189 $element['item'] = $link;
190 }
191 $aToZBar[] = $element;
192 }
193
194 $url = sprintf(
195 '<a href="%s">%s</a>',
196 CRM_Utils_System::url(
197 $path,
198 "force=1&qfKey=$qfKey&sortByCharacter=all"
199 ),
200 ts('All')
201 );
202 $aToZBar[] = array('item' => $url);
203 return $aToZBar;
204 }
205 }