Merge pull request #15969 from eileenmcnaughton/utfmb8
[civicrm-core.git] / CRM / Utils / PagerAToZ.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * This class is for displaying alphabetical bar
19 */
20 class CRM_Utils_PagerAToZ {
21
22 /**
23 * Returns the alphabetic array for sorting by character.
24 *
25 * @param array $query
26 * The query object.
27 * @param string $sortByCharacter
28 * The character that we are potentially sorting on.
29 *
30 * @param bool $isDAO
31 *
32 * @return string
33 * The html formatted string
34 */
35 public static function getAToZBar(&$query, $sortByCharacter, $isDAO = FALSE) {
36 $AToZBar = self::createLinks($query, $sortByCharacter, $isDAO);
37 return $AToZBar;
38 }
39
40 /**
41 * Return the all the static characters.
42 *
43 * @return array
44 * is an array of static characters
45 */
46 public static function getStaticCharacters() {
47 $staticAlphabets = [
48 'A',
49 'B',
50 'C',
51 'D',
52 'E',
53 'F',
54 'G',
55 'H',
56 'I',
57 'J',
58 'K',
59 'L',
60 'M',
61 'N',
62 'O',
63 'P',
64 'Q',
65 'R',
66 'S',
67 'T',
68 'U',
69 'V',
70 'W',
71 'X',
72 'Y',
73 'Z',
74 ];
75 return $staticAlphabets;
76 }
77
78 /**
79 * Return the all the dynamic characters.
80 *
81 * @param $query
82 * @param $isDAO
83 *
84 * @return array
85 * is an array of dynamic characters
86 */
87 public static function getDynamicCharacters(&$query, $isDAO) {
88 if ($isDAO) {
89 $result = $query;
90 }
91 else {
92 $result = $query->alphabetQuery();
93 }
94 if (!$result) {
95 return NULL;
96 }
97
98 $dynamicAlphabets = [];
99 while ($result->fetch()) {
100 $dynamicAlphabets[] = strtoupper($result->sort_name);
101 }
102 return $dynamicAlphabets;
103 }
104
105 /**
106 * Create the links.
107 *
108 * @param array $query
109 * The form values for search.
110 * @param string $sortByCharacter
111 * The character that we are potentially sorting on.
112 *
113 * @param $isDAO
114 *
115 * @return array
116 * with links
117 */
118 public static function createLinks(&$query, $sortByCharacter, $isDAO) {
119 $AToZBar = self::getStaticCharacters();
120 $dynamicAlphabets = self::getDynamicCharacters($query, $isDAO);
121
122 if (!$dynamicAlphabets) {
123 return NULL;
124 }
125
126 $AToZBar = array_merge($AToZBar, $dynamicAlphabets);
127 sort($AToZBar, SORT_STRING);
128 $AToZBar = array_unique($AToZBar);
129
130 // get the current path
131 $path = CRM_Utils_System::currentPath();
132
133 $qfKey = NULL;
134 if (isset($query->_formValues)) {
135 $qfKey = CRM_Utils_Array::value('qfKey', $query->_formValues);
136 }
137 if (empty($qfKey)) {
138 // CRM-20943 Can only pass variables by reference and also cannot use $this so using $empty setting to NULL which is default.
139 $emptyVariable = NULL;
140 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $emptyVariable, FALSE, NULL, $_REQUEST);
141 }
142
143 $aToZBar = [];
144 foreach ($AToZBar as $key => $link) {
145 if ($link === NULL) {
146 continue;
147 }
148
149 $element = [];
150 if (in_array($link, $dynamicAlphabets)) {
151 $klass = '';
152 if ($link == $sortByCharacter) {
153 $element['class'] = "active";
154 $klass = 'class="active"';
155 }
156 $url = CRM_Utils_System::url($path, "force=1&qfKey=$qfKey&sortByCharacter=");
157 // we do it this way since we want the url to be encoded but not the link character
158 // since that seems to mess up drupal utf-8 encoding etc
159 $url .= urlencode($link);
160 $element['item'] = sprintf('<a href="%s" %s>%s</a>',
161 $url,
162 $klass,
163 $link
164 );
165 }
166 else {
167 $element['item'] = $link;
168 }
169 $aToZBar[] = $element;
170 }
171
172 $url = sprintf(
173 '<a href="%s">%s</a>',
174 CRM_Utils_System::url(
175 $path,
176 "force=1&qfKey=$qfKey&sortByCharacter=all"
177 ),
178 ts('All')
179 );
180 $aToZBar[] = ['item' => $url];
181 return $aToZBar;
182 }
183
184 }