Merge pull request #6385 from eileenmcnaughton/cleanup
[civicrm-core.git] / CRM / Utils / PagerAToZ.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class is for displaying alphabetical bar
38 *
39 */
40class CRM_Utils_PagerAToZ {
41
42 /**
fe482240 43 * Returns the alphabetic array for sorting by character.
6a488035 44 *
77855840
TO
45 * @param array $query
46 * The query object.
47 * @param string $sortByCharacter
48 * The character that we are potentially sorting on.
6a488035 49 *
f4aaa82a
EM
50 * @param bool $isDAO
51 *
a6c01b45
CW
52 * @return string
53 * The html formatted string
6a488035 54 */
00be9182 55 public static function getAToZBar(&$query, $sortByCharacter, $isDAO = FALSE) {
6a488035
TO
56 $AToZBar = self::createLinks($query, $sortByCharacter, $isDAO);
57 return $AToZBar;
58 }
59
60 /**
fe482240 61 * Return the all the static characters.
6a488035 62 *
a6c01b45
CW
63 * @return array
64 * is a array of static characters
6a488035 65 */
00be9182 66 public static function getStaticCharacters() {
353ffa53
TO
67 $staticAlphabets = array(
68 'A',
69 'B',
70 'C',
71 'D',
72 'E',
73 'F',
74 'G',
75 'H',
76 'I',
77 'J',
78 'K',
79 'L',
80 'M',
81 'N',
82 'O',
83 'P',
84 'Q',
85 'R',
86 'S',
87 'T',
88 'U',
89 'V',
90 'W',
91 'X',
92 'Y',
af9b09df 93 'Z',
353ffa53 94 );
6a488035
TO
95 return $staticAlphabets;
96 }
97
98 /**
fe482240 99 * Return the all the dynamic characters.
6a488035 100 *
f4aaa82a
EM
101 * @param $query
102 * @param $isDAO
103 *
a6c01b45
CW
104 * @return array
105 * is a array of dynamic characters
6a488035 106 */
00be9182 107 public static function getDynamicCharacters(&$query, $isDAO) {
6a488035
TO
108 if ($isDAO) {
109 $result = $query;
110 }
111 else {
112 $result = $query->alphabetQuery();
113 }
114 if (!$result) {
115 return NULL;
116 }
117
118 $dynamicAlphabets = array();
119 while ($result->fetch()) {
120 $dynamicAlphabets[] = $result->sort_name;
121 }
122 return $dynamicAlphabets;
123 }
124
125 /**
fe482240 126 * Create the links.
6a488035 127 *
77855840
TO
128 * @param array $query
129 * The form values for search.
130 * @param string $sortByCharacter
131 * The character that we are potentially sorting on.
6a488035 132 *
f4aaa82a
EM
133 * @param $isDAO
134 *
a6c01b45
CW
135 * @return array
136 * with links
6a488035 137 */
00be9182 138 public static function createLinks(&$query, $sortByCharacter, $isDAO) {
6a488035
TO
139 $AToZBar = self::getStaticCharacters();
140 $dynamicAlphabets = self::getDynamicCharacters($query, $isDAO);
141
142 if (!$dynamicAlphabets) {
143 return NULL;
144 }
145
146 $AToZBar = array_merge($AToZBar, $dynamicAlphabets);
147 sort($AToZBar, SORT_STRING);
148 $AToZBar = array_unique($AToZBar);
149
150 //get the current path
151 $path = CRM_Utils_System::currentPath();
152
e7292422 153 $qfKey = NULL;
6a488035
TO
154 if (isset($query->_formValues)) {
155 $qfKey = CRM_Utils_Array::value('qfKey', $query->_formValues);
156 }
157 if (empty($qfKey)) {
158 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this, FALSE, NULL, $_REQUEST);
159 }
160
161 $aToZBar = array();
162 foreach ($AToZBar as $key => $link) {
163 if ($link === NULL) {
164 continue;
165 }
166
167 $element = array();
168 if (in_array($link, $dynamicAlphabets)) {
169 $klass = '';
170 if ($link == $sortByCharacter) {
171 $element['class'] = "active";
172 $klass = 'class="active"';
173 }
174 $url = CRM_Utils_System::url($path, "force=1&qfKey=$qfKey&sortByCharacter=");
175 // we do it this way since we want the url to be encoded but not the link character
176 // since that seems to mess up drupal utf-8 encoding etc
177 $url .= urlencode($link);
178 $element['item'] = sprintf('<a href="%s" %s>%s</a>',
179 $url,
180 $klass,
181 $link
182 );
183 }
184 else {
185 $element['item'] = $link;
186 }
187 $aToZBar[] = $element;
188 }
189
c6b70fc9
DL
190 $url = sprintf(
191 '<a href="%s">%s</a>',
192 CRM_Utils_System::url(
193 $path,
194 "force=1&qfKey=$qfKey&sortByCharacter=all"
195 ),
196 ts('All')
6a488035
TO
197 );
198 $aToZBar[] = array('item' => $url);
199 return $aToZBar;
200 }
96025800 201
6a488035 202}