Merge pull request #24022 from colemanw/afformFrontend
[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 * @throws \CRM_Core_Exception
118 */
119 public static function createLinks(&$query, $sortByCharacter, $isDAO) {
120 $AToZBar = self::getStaticCharacters();
121 $dynamicAlphabets = self::getDynamicCharacters($query, $isDAO);
122
123 if (!$dynamicAlphabets) {
124 return NULL;
125 }
126
127 $AToZBar = array_merge($AToZBar, $dynamicAlphabets);
128 sort($AToZBar, SORT_STRING);
129 $AToZBar = array_unique($AToZBar);
130
131 // get the current path
132 $path = CRM_Utils_System::currentPath();
133
134 $qfKey = NULL;
135 if (isset($query->_formValues)) {
136 $qfKey = $query->_formValues['qfKey'] ?? NULL;
137 }
138 if (empty($qfKey)) {
139 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String');
140 }
141
142 $aToZBar = [];
143 foreach ($AToZBar as $key => $link) {
144 if ($link === NULL) {
145 continue;
146 }
147
148 $element = ['class' => ''];
149 if (in_array($link, $dynamicAlphabets)) {
150 $klass = '';
151 if ($link == $sortByCharacter) {
152 $element['class'] = "active";
153 $klass = 'class="active"';
154 }
155 $urlParams = [
156 'force' => 1,
157 'qfKey' => $qfKey,
158 ];
159 if (($query->_context ?? '') === 'amtg') {
160 // See https://lab.civicrm.org/dev/core/-/issues/2333
161 // Seems to be needed in add to group flow.
162 $urlParams['_qf_Basic_display'] = 1;
163 }
164 $urlParams['sortByCharacter'] = '';
165 $url = CRM_Utils_System::url($path, $urlParams);
166 // we do it this way since we want the url to be encoded but not the link character
167 // since that seems to mess up drupal utf-8 encoding etc
168 $url .= urlencode($link);
169 $element['item'] = sprintf('<a href="%s" %s>%s</a>',
170 $url,
171 $klass,
172 $link
173 );
174 }
175 else {
176 $element['item'] = $link;
177 }
178 $aToZBar[] = $element;
179 }
180
181 $url = sprintf(
182 '<a href="%s">%s</a>',
183 CRM_Utils_System::url(
184 $path,
185 "force=1&qfKey=$qfKey&sortByCharacter=all"
186 ),
187 ts('All')
188 );
189 $aToZBar[] = ['item' => $url, 'class' => ''];
190 return $aToZBar;
191 }
192
193 }