Merge pull request #2842 from totten/master-api-rollback-soft-errors
[civicrm-core.git] / CRM / Utils / PagerAToZ.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * @access public
52 * @static
53 */
54 static function getAToZBar(&$query, $sortByCharacter, $isDAO = FALSE) {
55 $AToZBar = self::createLinks($query, $sortByCharacter, $isDAO);
56 return $AToZBar;
57 }
58
59 /**
60 * Function to return the all the static characters
61 *
62 * @return array $staticAlphabets is a array of static characters
63 * @access private
64 * @static
65 */
66 static function getStaticCharacters() {
67 $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');
68 return $staticAlphabets;
69 }
70
71 /**
72 * Function to return the all the dynamic characters
73 *
74 * @param $query
75 * @param $isDAO
76 *
77 * @return array $dynamicAlphabets is a array of dynamic characters
78 * @access private
79 * @static
80 */
81 static function getDynamicCharacters(&$query, $isDAO) {
82 if ($isDAO) {
83 $result = $query;
84 }
85 else {
86 $result = $query->alphabetQuery();
87 }
88 if (!$result) {
89 return NULL;
90 }
91
92 $dynamicAlphabets = array();
93 while ($result->fetch()) {
94 $dynamicAlphabets[] = $result->sort_name;
95 }
96 return $dynamicAlphabets;
97 }
98
99 /**
100 * create the links
101 *
102 * @param array $query The form values for search
103 * @param string $sortByCharacter The character that we are potentially sorting on
104 *
105 * @param $isDAO
106 *
107 * @return array with links
108 * @access private
109 * @static
110 */
111 static function createLinks(&$query, $sortByCharacter, $isDAO) {
112 $AToZBar = self::getStaticCharacters();
113 $dynamicAlphabets = self::getDynamicCharacters($query, $isDAO);
114
115 if (!$dynamicAlphabets) {
116 return NULL;
117 }
118
119 $AToZBar = array_merge($AToZBar, $dynamicAlphabets);
120 sort($AToZBar, SORT_STRING);
121 $AToZBar = array_unique($AToZBar);
122
123 //get the current path
124 $path = CRM_Utils_System::currentPath();
125
126 $qfKey = null;
127 if (isset($query->_formValues)) {
128 $qfKey = CRM_Utils_Array::value('qfKey', $query->_formValues);
129 }
130 if (empty($qfKey)) {
131 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this, FALSE, NULL, $_REQUEST);
132 }
133
134 $aToZBar = array();
135 foreach ($AToZBar as $key => $link) {
136 if ($link === NULL) {
137 continue;
138 }
139
140 $element = array();
141 if (in_array($link, $dynamicAlphabets)) {
142 $klass = '';
143 if ($link == $sortByCharacter) {
144 $element['class'] = "active";
145 $klass = 'class="active"';
146 }
147 $url = CRM_Utils_System::url($path, "force=1&qfKey=$qfKey&sortByCharacter=");
148 // we do it this way since we want the url to be encoded but not the link character
149 // since that seems to mess up drupal utf-8 encoding etc
150 $url .= urlencode($link);
151 $element['item'] = sprintf('<a href="%s" %s>%s</a>',
152 $url,
153 $klass,
154 $link
155 );
156 }
157 else {
158 $element['item'] = $link;
159 }
160 $aToZBar[] = $element;
161 }
162
163 $url = sprintf(
164 '<a href="%s">%s</a>',
165 CRM_Utils_System::url(
166 $path,
167 "force=1&qfKey=$qfKey&sortByCharacter=all"
168 ),
169 ts('All')
170 );
171 $aToZBar[] = array('item' => $url);
172 return $aToZBar;
173 }
174 }
175