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