Merge pull request #4606 from johanv/CRM-15636-price_set_event_and_contribution
[civicrm-core.git] / CRM / Utils / PagerAToZ.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class is for displaying alphabetical bar
38 *
39 */
40 class CRM_Utils_PagerAToZ {
41
42 /**
43 * Returns the alphabetic array for sorting by character
44 *
45 * @param array $query The query object
46 * @param string $sortByCharacter The character that we are potentially sorting on
47 *
48 * @param bool $isDAO
49 *
50 * @return string The html formatted string
51 * @static
52 */
53 public static function getAToZBar(&$query, $sortByCharacter, $isDAO = FALSE) {
54 $AToZBar = self::createLinks($query, $sortByCharacter, $isDAO);
55 return $AToZBar;
56 }
57
58 /**
59 * Return the all the static characters
60 *
61 * @return array $staticAlphabets is a array of static characters
62 * @static
63 */
64 public static function getStaticCharacters() {
65 $staticAlphabets = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
66 return $staticAlphabets;
67 }
68
69 /**
70 * Return the all the dynamic characters
71 *
72 * @param $query
73 * @param $isDAO
74 *
75 * @return array $dynamicAlphabets is a array of dynamic characters
76 * @static
77 */
78 public static function getDynamicCharacters(&$query, $isDAO) {
79 if ($isDAO) {
80 $result = $query;
81 }
82 else {
83 $result = $query->alphabetQuery();
84 }
85 if (!$result) {
86 return NULL;
87 }
88
89 $dynamicAlphabets = array();
90 while ($result->fetch()) {
91 $dynamicAlphabets[] = $result->sort_name;
92 }
93 return $dynamicAlphabets;
94 }
95
96 /**
97 * Create the links
98 *
99 * @param array $query The form values for search
100 * @param string $sortByCharacter The character that we are potentially sorting on
101 *
102 * @param $isDAO
103 *
104 * @return array with links
105 * @static
106 */
107 public static function createLinks(&$query, $sortByCharacter, $isDAO) {
108 $AToZBar = self::getStaticCharacters();
109 $dynamicAlphabets = self::getDynamicCharacters($query, $isDAO);
110
111 if (!$dynamicAlphabets) {
112 return NULL;
113 }
114
115 $AToZBar = array_merge($AToZBar, $dynamicAlphabets);
116 sort($AToZBar, SORT_STRING);
117 $AToZBar = array_unique($AToZBar);
118
119 //get the current path
120 $path = CRM_Utils_System::currentPath();
121
122 $qfKey = null;
123 if (isset($query->_formValues)) {
124 $qfKey = CRM_Utils_Array::value('qfKey', $query->_formValues);
125 }
126 if (empty($qfKey)) {
127 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this, FALSE, NULL, $_REQUEST);
128 }
129
130 $aToZBar = array();
131 foreach ($AToZBar as $key => $link) {
132 if ($link === NULL) {
133 continue;
134 }
135
136 $element = array();
137 if (in_array($link, $dynamicAlphabets)) {
138 $klass = '';
139 if ($link == $sortByCharacter) {
140 $element['class'] = "active";
141 $klass = 'class="active"';
142 }
143 $url = CRM_Utils_System::url($path, "force=1&qfKey=$qfKey&sortByCharacter=");
144 // we do it this way since we want the url to be encoded but not the link character
145 // since that seems to mess up drupal utf-8 encoding etc
146 $url .= urlencode($link);
147 $element['item'] = sprintf('<a href="%s" %s>%s</a>',
148 $url,
149 $klass,
150 $link
151 );
152 }
153 else {
154 $element['item'] = $link;
155 }
156 $aToZBar[] = $element;
157 }
158
159 $url = sprintf(
160 '<a href="%s">%s</a>',
161 CRM_Utils_System::url(
162 $path,
163 "force=1&qfKey=$qfKey&sortByCharacter=all"
164 ),
165 ts('All')
166 );
167 $aToZBar[] = array('item' => $url);
168 return $aToZBar;
169 }
170 }