CRM-20932 - CRM_Utils_System_Joomla - Fix warning. Define JDEBUG
[civicrm-core.git] / CRM / Utils / PagerAToZ.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2017
31 */
32
33 /**
34 * This class is for displaying alphabetical bar
35 */
36 class CRM_Utils_PagerAToZ {
37
38 /**
39 * Returns the alphabetic array for sorting by character.
40 *
41 * @param array $query
42 * The query object.
43 * @param string $sortByCharacter
44 * The character that we are potentially sorting on.
45 *
46 * @param bool $isDAO
47 *
48 * @return string
49 * The html formatted string
50 */
51 public static function getAToZBar(&$query, $sortByCharacter, $isDAO = FALSE) {
52 $AToZBar = self::createLinks($query, $sortByCharacter, $isDAO);
53 return $AToZBar;
54 }
55
56 /**
57 * Return the all the static characters.
58 *
59 * @return array
60 * is an array of static characters
61 */
62 public static function getStaticCharacters() {
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',
89 'Z',
90 );
91 return $staticAlphabets;
92 }
93
94 /**
95 * Return the all the dynamic characters.
96 *
97 * @param $query
98 * @param $isDAO
99 *
100 * @return array
101 * is an array of dynamic characters
102 */
103 public static function getDynamicCharacters(&$query, $isDAO) {
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 /**
122 * Create the links.
123 *
124 * @param array $query
125 * The form values for search.
126 * @param string $sortByCharacter
127 * The character that we are potentially sorting on.
128 *
129 * @param $isDAO
130 *
131 * @return array
132 * with links
133 */
134 public static function createLinks(&$query, $sortByCharacter, $isDAO) {
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
146 // get the current path
147 $path = CRM_Utils_System::currentPath();
148
149 $qfKey = NULL;
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
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')
193 );
194 $aToZBar[] = array('item' => $url);
195 return $aToZBar;
196 }
197
198 }