Merge pull request #18200 from seamuslee001/5.29
[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 = $query->_formValues['qfKey'] ?? NULL;
136 }
137 if (empty($qfKey)) {
138 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String');
139 }
140
141 $aToZBar = [];
142 foreach ($AToZBar as $key => $link) {
143 if ($link === NULL) {
144 continue;
145 }
146
147 $element = [];
148 if (in_array($link, $dynamicAlphabets)) {
149 $klass = '';
150 if ($link == $sortByCharacter) {
151 $element['class'] = "active";
152 $klass = 'class="active"';
153 }
154 $url = CRM_Utils_System::url($path, "force=1&qfKey=$qfKey&sortByCharacter=");
155 // we do it this way since we want the url to be encoded but not the link character
156 // since that seems to mess up drupal utf-8 encoding etc
157 $url .= urlencode($link);
158 $element['item'] = sprintf('<a href="%s" %s>%s</a>',
159 $url,
160 $klass,
161 $link
162 );
163 }
164 else {
165 $element['item'] = $link;
166 }
167 $aToZBar[] = $element;
168 }
169
170 $url = sprintf(
171 '<a href="%s">%s</a>',
172 CRM_Utils_System::url(
173 $path,
174 "force=1&qfKey=$qfKey&sortByCharacter=all"
175 ),
176 ts('All')
177 );
178 $aToZBar[] = ['item' => $url];
179 return $aToZBar;
180 }
181
182 }