Merge pull request #15833 from yashodha/participant_edit
[civicrm-core.git] / CRM / Utils / PagerAToZ.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
6a488035 29 * @package CRM
f299f7db 30 * @copyright CiviCRM LLC (c) 2004-2020
6a488035
TO
31 */
32
33/**
34 * This class is for displaying alphabetical bar
6a488035
TO
35 */
36class CRM_Utils_PagerAToZ {
37
38 /**
fe482240 39 * Returns the alphabetic array for sorting by character.
6a488035 40 *
77855840
TO
41 * @param array $query
42 * The query object.
43 * @param string $sortByCharacter
44 * The character that we are potentially sorting on.
6a488035 45 *
f4aaa82a
EM
46 * @param bool $isDAO
47 *
a6c01b45
CW
48 * @return string
49 * The html formatted string
6a488035 50 */
00be9182 51 public static function getAToZBar(&$query, $sortByCharacter, $isDAO = FALSE) {
6a488035
TO
52 $AToZBar = self::createLinks($query, $sortByCharacter, $isDAO);
53 return $AToZBar;
54 }
55
56 /**
fe482240 57 * Return the all the static characters.
6a488035 58 *
a6c01b45 59 * @return array
50bfb460 60 * is an array of static characters
6a488035 61 */
00be9182 62 public static function getStaticCharacters() {
be2fb01f 63 $staticAlphabets = [
353ffa53
TO
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',
af9b09df 89 'Z',
be2fb01f 90 ];
6a488035
TO
91 return $staticAlphabets;
92 }
93
94 /**
fe482240 95 * Return the all the dynamic characters.
6a488035 96 *
f4aaa82a
EM
97 * @param $query
98 * @param $isDAO
99 *
a6c01b45 100 * @return array
50bfb460 101 * is an array of dynamic characters
6a488035 102 */
00be9182 103 public static function getDynamicCharacters(&$query, $isDAO) {
6a488035
TO
104 if ($isDAO) {
105 $result = $query;
106 }
107 else {
108 $result = $query->alphabetQuery();
109 }
110 if (!$result) {
111 return NULL;
112 }
113
be2fb01f 114 $dynamicAlphabets = [];
6a488035 115 while ($result->fetch()) {
52cda5dc 116 $dynamicAlphabets[] = strtoupper($result->sort_name);
6a488035
TO
117 }
118 return $dynamicAlphabets;
119 }
120
121 /**
fe482240 122 * Create the links.
6a488035 123 *
77855840
TO
124 * @param array $query
125 * The form values for search.
126 * @param string $sortByCharacter
127 * The character that we are potentially sorting on.
6a488035 128 *
f4aaa82a
EM
129 * @param $isDAO
130 *
a6c01b45
CW
131 * @return array
132 * with links
6a488035 133 */
00be9182 134 public static function createLinks(&$query, $sortByCharacter, $isDAO) {
6a488035
TO
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
50bfb460 146 // get the current path
6a488035
TO
147 $path = CRM_Utils_System::currentPath();
148
e7292422 149 $qfKey = NULL;
6a488035
TO
150 if (isset($query->_formValues)) {
151 $qfKey = CRM_Utils_Array::value('qfKey', $query->_formValues);
152 }
153 if (empty($qfKey)) {
d057419e
SL
154 // CRM-20943 Can only pass variables by reference and also cannot use $this so using $empty setting to NULL which is default.
155 $emptyVariable = NULL;
156 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $emptyVariable, FALSE, NULL, $_REQUEST);
6a488035
TO
157 }
158
be2fb01f 159 $aToZBar = [];
6a488035
TO
160 foreach ($AToZBar as $key => $link) {
161 if ($link === NULL) {
162 continue;
163 }
164
be2fb01f 165 $element = [];
6a488035
TO
166 if (in_array($link, $dynamicAlphabets)) {
167 $klass = '';
168 if ($link == $sortByCharacter) {
169 $element['class'] = "active";
170 $klass = 'class="active"';
171 }
172 $url = CRM_Utils_System::url($path, "force=1&qfKey=$qfKey&sortByCharacter=");
173 // we do it this way since we want the url to be encoded but not the link character
174 // since that seems to mess up drupal utf-8 encoding etc
175 $url .= urlencode($link);
176 $element['item'] = sprintf('<a href="%s" %s>%s</a>',
177 $url,
178 $klass,
179 $link
180 );
181 }
182 else {
183 $element['item'] = $link;
184 }
185 $aToZBar[] = $element;
186 }
187
c6b70fc9
DL
188 $url = sprintf(
189 '<a href="%s">%s</a>',
190 CRM_Utils_System::url(
191 $path,
192 "force=1&qfKey=$qfKey&sortByCharacter=all"
193 ),
194 ts('All')
6a488035 195 );
be2fb01f 196 $aToZBar[] = ['item' => $url];
6a488035
TO
197 return $aToZBar;
198 }
96025800 199
6a488035 200}