Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
6a488035 | 13 | * @package CRM |
ca5cec67 | 14 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
15 | */ |
16 | ||
17 | /** | |
18 | * This class is for displaying alphabetical bar | |
6a488035 TO |
19 | */ |
20 | class CRM_Utils_PagerAToZ { | |
21 | ||
22 | /** | |
fe482240 | 23 | * Returns the alphabetic array for sorting by character. |
6a488035 | 24 | * |
77855840 TO |
25 | * @param array $query |
26 | * The query object. | |
27 | * @param string $sortByCharacter | |
28 | * The character that we are potentially sorting on. | |
6a488035 | 29 | * |
f4aaa82a EM |
30 | * @param bool $isDAO |
31 | * | |
a6c01b45 CW |
32 | * @return string |
33 | * The html formatted string | |
6a488035 | 34 | */ |
00be9182 | 35 | public static function getAToZBar(&$query, $sortByCharacter, $isDAO = FALSE) { |
6a488035 TO |
36 | $AToZBar = self::createLinks($query, $sortByCharacter, $isDAO); |
37 | return $AToZBar; | |
38 | } | |
39 | ||
40 | /** | |
fe482240 | 41 | * Return the all the static characters. |
6a488035 | 42 | * |
a6c01b45 | 43 | * @return array |
50bfb460 | 44 | * is an array of static characters |
6a488035 | 45 | */ |
00be9182 | 46 | public static function getStaticCharacters() { |
be2fb01f | 47 | $staticAlphabets = [ |
353ffa53 TO |
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', | |
af9b09df | 73 | 'Z', |
be2fb01f | 74 | ]; |
6a488035 TO |
75 | return $staticAlphabets; |
76 | } | |
77 | ||
78 | /** | |
fe482240 | 79 | * Return the all the dynamic characters. |
6a488035 | 80 | * |
f4aaa82a EM |
81 | * @param $query |
82 | * @param $isDAO | |
83 | * | |
a6c01b45 | 84 | * @return array |
50bfb460 | 85 | * is an array of dynamic characters |
6a488035 | 86 | */ |
00be9182 | 87 | public static function getDynamicCharacters(&$query, $isDAO) { |
6a488035 TO |
88 | if ($isDAO) { |
89 | $result = $query; | |
90 | } | |
91 | else { | |
92 | $result = $query->alphabetQuery(); | |
93 | } | |
94 | if (!$result) { | |
95 | return NULL; | |
96 | } | |
97 | ||
be2fb01f | 98 | $dynamicAlphabets = []; |
6a488035 | 99 | while ($result->fetch()) { |
52cda5dc | 100 | $dynamicAlphabets[] = strtoupper($result->sort_name); |
6a488035 TO |
101 | } |
102 | return $dynamicAlphabets; | |
103 | } | |
104 | ||
105 | /** | |
fe482240 | 106 | * Create the links. |
6a488035 | 107 | * |
77855840 TO |
108 | * @param array $query |
109 | * The form values for search. | |
110 | * @param string $sortByCharacter | |
111 | * The character that we are potentially sorting on. | |
6a488035 | 112 | * |
f4aaa82a EM |
113 | * @param $isDAO |
114 | * | |
a6c01b45 CW |
115 | * @return array |
116 | * with links | |
6134693a | 117 | * @throws \CRM_Core_Exception |
6a488035 | 118 | */ |
00be9182 | 119 | public static function createLinks(&$query, $sortByCharacter, $isDAO) { |
6a488035 TO |
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 | ||
50bfb460 | 131 | // get the current path |
6a488035 TO |
132 | $path = CRM_Utils_System::currentPath(); |
133 | ||
e7292422 | 134 | $qfKey = NULL; |
6a488035 | 135 | if (isset($query->_formValues)) { |
9c1bc317 | 136 | $qfKey = $query->_formValues['qfKey'] ?? NULL; |
6a488035 TO |
137 | } |
138 | if (empty($qfKey)) { | |
1be22cfb | 139 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String'); |
6a488035 TO |
140 | } |
141 | ||
be2fb01f | 142 | $aToZBar = []; |
6a488035 TO |
143 | foreach ($AToZBar as $key => $link) { |
144 | if ($link === NULL) { | |
145 | continue; | |
146 | } | |
147 | ||
c936cb59 | 148 | $element = ['class' => '']; |
6a488035 TO |
149 | if (in_array($link, $dynamicAlphabets)) { |
150 | $klass = ''; | |
151 | if ($link == $sortByCharacter) { | |
152 | $element['class'] = "active"; | |
153 | $klass = 'class="active"'; | |
154 | } | |
6134693a | 155 | $urlParams = [ |
156 | 'force' => 1, | |
157 | 'qfKey' => $qfKey, | |
158 | ]; | |
e5844ec8 | 159 | if (($query->_context ?? '') === 'amtg') { |
6134693a | 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); | |
6a488035 TO |
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 | ||
c6b70fc9 DL |
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') | |
6a488035 | 188 | ); |
c936cb59 | 189 | $aToZBar[] = ['item' => $url, 'class' => '']; |
6a488035 TO |
190 | return $aToZBar; |
191 | } | |
96025800 | 192 | |
6a488035 | 193 | } |