Merge pull request #16162 from colemanw/contactFields
[civicrm-core.git] / CRM / Utils / PagerAToZ.php
CommitLineData
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 */
20class 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
6a488035 117 */
00be9182 118 public static function createLinks(&$query, $sortByCharacter, $isDAO) {
6a488035
TO
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
50bfb460 130 // get the current path
6a488035
TO
131 $path = CRM_Utils_System::currentPath();
132
e7292422 133 $qfKey = NULL;
6a488035
TO
134 if (isset($query->_formValues)) {
135 $qfKey = CRM_Utils_Array::value('qfKey', $query->_formValues);
136 }
137 if (empty($qfKey)) {
d057419e
SL
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);
6a488035
TO
141 }
142
be2fb01f 143 $aToZBar = [];
6a488035
TO
144 foreach ($AToZBar as $key => $link) {
145 if ($link === NULL) {
146 continue;
147 }
148
be2fb01f 149 $element = [];
6a488035
TO
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
c6b70fc9
DL
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')
6a488035 179 );
be2fb01f 180 $aToZBar[] = ['item' => $url];
6a488035
TO
181 return $aToZBar;
182 }
96025800 183
6a488035 184}