Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | 27 | |
6a488035 TO |
28 | /** |
29 | * | |
b8c71ffa | 30 | * Base class to provide generic sort functionality. |
31 | * | |
32 | * Note that some ideas have been borrowed from the drupal tablesort.inc code. | |
33 | * | |
34 | * Also note that since the Pager and Sort class are similar, do match the function names | |
6a488035 TO |
35 | * if introducing additional functionality |
36 | * | |
37 | * @package CRM | |
fa938177 | 38 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
39 | */ |
40 | class CRM_Utils_Sort { | |
41 | ||
42 | /** | |
100fef9d | 43 | * Constants to determine what direction each variable |
6a488035 TO |
44 | * is to be sorted |
45 | * | |
46 | * @var int | |
47 | */ | |
7da04cde | 48 | const ASCENDING = 1, DESCENDING = 2, DONTCARE = 4, |
6a488035 | 49 | |
353ffa53 TO |
50 | /** |
51 | * The name for the sort GET/POST param | |
52 | * | |
53 | * @var string | |
54 | */ | |
55 | SORT_ID = 'crmSID', SORT_DIRECTION = 'crmSortDirection', SORT_ORDER = 'crmSortOrder'; | |
6a488035 TO |
56 | |
57 | /** | |
100fef9d | 58 | * Name of the sort function. Used to isolate session variables |
6a488035 TO |
59 | * @var string |
60 | */ | |
61 | protected $_name; | |
62 | ||
63 | /** | |
100fef9d | 64 | * Array of variables that influence the query |
6a488035 TO |
65 | * |
66 | * @var array | |
67 | */ | |
68 | public $_vars; | |
69 | ||
70 | /** | |
100fef9d | 71 | * The newly formulated base url to be used as links |
6a488035 TO |
72 | * for various table elements |
73 | * | |
74 | * @var string | |
75 | */ | |
76 | protected $_link; | |
77 | ||
78 | /** | |
100fef9d | 79 | * What's the name of the sort variable in a REQUEST |
6a488035 TO |
80 | * |
81 | * @var string | |
82 | */ | |
83 | protected $_urlVar; | |
84 | ||
85 | /** | |
86 | * What variable are we currently sorting on | |
87 | * | |
88 | * @var string | |
89 | */ | |
90 | protected $_currentSortID; | |
91 | ||
92 | /** | |
93 | * What direction are we sorting on | |
94 | * | |
95 | * @var string | |
96 | */ | |
97 | protected $_currentSortDirection; | |
98 | ||
99 | /** | |
100 | * The output generated for the current form | |
101 | * | |
102 | * @var array | |
103 | */ | |
104 | public $_response; | |
105 | ||
106 | /** | |
107 | * The constructor takes an assoc array | |
108 | * key names of variable (which should be the same as the column name) | |
109 | * value: ascending or descending | |
110 | * | |
77855840 TO |
111 | * @param mixed $vars |
112 | * Assoc array as described above. | |
113 | * @param string $defaultSortOrder | |
114 | * Order to sort. | |
6a488035 | 115 | * |
f4aaa82a | 116 | * @return \CRM_Utils_Sort |
450f494d | 117 | */ |
00be9182 | 118 | public function __construct(&$vars, $defaultSortOrder = NULL) { |
6a488035 TO |
119 | $this->_vars = array(); |
120 | $this->_response = array(); | |
121 | ||
122 | foreach ($vars as $weight => $value) { | |
123 | $this->_vars[$weight] = array( | |
a33b83c5 | 124 | 'name' => CRM_Utils_Type::validate($value['sort'], 'MysqlColumnNameOrAlias'), |
6a488035 TO |
125 | 'direction' => CRM_Utils_Array::value('direction', $value), |
126 | 'title' => $value['name'], | |
127 | ); | |
128 | } | |
129 | ||
130 | $this->_currentSortID = 1; | |
131 | if (isset($this->_vars[$this->_currentSortID])) { | |
132 | $this->_currentSortDirection = $this->_vars[$this->_currentSortID]['direction']; | |
133 | } | |
134 | $this->_urlVar = self::SORT_ID; | |
5340376b | 135 | $this->_link = CRM_Utils_System::makeURL($this->_urlVar, TRUE); |
6a488035 TO |
136 | |
137 | $this->initialize($defaultSortOrder); | |
138 | } | |
139 | ||
140 | /** | |
fe482240 | 141 | * Function returns the string for the order by clause. |
6a488035 | 142 | * |
a6c01b45 CW |
143 | * @return string |
144 | * the order by clause | |
6a488035 | 145 | */ |
00be9182 | 146 | public function orderBy() { |
a7488080 | 147 | if (empty($this->_vars[$this->_currentSortID])) { |
6a488035 TO |
148 | return ''; |
149 | } | |
150 | ||
151 | if ($this->_vars[$this->_currentSortID]['direction'] == self::ASCENDING || | |
152 | $this->_vars[$this->_currentSortID]['direction'] == self::DONTCARE | |
153 | ) { | |
154 | $this->_vars[$this->_currentSortID]['name'] = str_replace(' ', '_', $this->_vars[$this->_currentSortID]['name']); | |
668c2561 | 155 | return CRM_Utils_Type::escape($this->_vars[$this->_currentSortID]['name'], 'MysqlColumnNameOrAlias') . ' asc'; |
6a488035 TO |
156 | } |
157 | else { | |
158 | $this->_vars[$this->_currentSortID]['name'] = str_replace(' ', '_', $this->_vars[$this->_currentSortID]['name']); | |
668c2561 | 159 | return CRM_Utils_Type::escape($this->_vars[$this->_currentSortID]['name'], 'MysqlColumnNameOrAlias') . ' desc'; |
6a488035 TO |
160 | } |
161 | } | |
162 | ||
163 | /** | |
fe482240 | 164 | * Create the sortID string to be used in the GET param. |
6a488035 | 165 | * |
77855840 TO |
166 | * @param int $index |
167 | * The field index. | |
168 | * @param int $dir | |
169 | * The direction of the sort. | |
6a488035 | 170 | * |
a6c01b45 CW |
171 | * @return string |
172 | * the string to append to the url | |
6a488035 | 173 | */ |
00be9182 | 174 | public static function sortIDValue($index, $dir) { |
6a488035 TO |
175 | return ($dir == self::DESCENDING) ? $index . '_d' : $index . '_u'; |
176 | } | |
177 | ||
178 | /** | |
fe482240 | 179 | * Init the sort ID values in the object. |
6a488035 | 180 | * |
77855840 TO |
181 | * @param string $defaultSortOrder |
182 | * The sort order to use by default. | |
6a488035 | 183 | */ |
00be9182 | 184 | public function initSortID($defaultSortOrder) { |
6a488035 TO |
185 | $url = CRM_Utils_Array::value(self::SORT_ID, $_GET, $defaultSortOrder); |
186 | ||
187 | if (empty($url)) { | |
188 | return; | |
189 | } | |
190 | ||
191 | list($current, $direction) = explode('_', $url); | |
192 | ||
b44e3f84 | 193 | // if current is weird and does not exist in the vars array, skip |
6a488035 TO |
194 | if (!array_key_exists($current, $this->_vars)) { |
195 | return; | |
196 | } | |
197 | ||
198 | if ($direction == 'u') { | |
199 | $direction = self::ASCENDING; | |
200 | } | |
201 | elseif ($direction == 'd') { | |
202 | $direction = self::DESCENDING; | |
203 | } | |
204 | else { | |
205 | $direction = self::DONTCARE; | |
206 | } | |
207 | ||
208 | $this->_currentSortID = $current; | |
209 | $this->_currentSortDirection = $direction; | |
210 | $this->_vars[$current]['direction'] = $direction; | |
211 | } | |
212 | ||
213 | /** | |
fe482240 | 214 | * Init the object. |
6a488035 | 215 | * |
77855840 TO |
216 | * @param string $defaultSortOrder |
217 | * The sort order to use by default. | |
6a488035 | 218 | */ |
00be9182 | 219 | public function initialize($defaultSortOrder) { |
6a488035 TO |
220 | $this->initSortID($defaultSortOrder); |
221 | ||
222 | $this->_response = array(); | |
223 | ||
224 | $current = $this->_currentSortID; | |
225 | foreach ($this->_vars as $index => $item) { | |
226 | $name = $item['name']; | |
227 | $this->_response[$name] = array(); | |
228 | ||
229 | $newDirection = ($item['direction'] == self::ASCENDING) ? self::DESCENDING : self::ASCENDING; | |
230 | ||
231 | if ($current == $index) { | |
232 | if ($item['direction'] == self::ASCENDING) { | |
233 | $class = 'sorting_asc'; | |
234 | } | |
235 | else { | |
236 | $class = 'sorting_desc'; | |
237 | } | |
238 | } | |
239 | else { | |
240 | $class = 'sorting'; | |
241 | } | |
242 | ||
243 | $this->_response[$name]['link'] = '<a href="' . $this->_link . $this->sortIDValue($index, $newDirection) . '" class="' . $class . '">' . $item['title'] . '</a>'; | |
244 | } | |
245 | } | |
246 | ||
247 | /** | |
fe482240 | 248 | * Getter for currentSortID. |
6a488035 | 249 | * |
a6c01b45 CW |
250 | * @return int |
251 | * returns of the current sort id | |
6a488035 | 252 | */ |
00be9182 | 253 | public function getCurrentSortID() { |
6a488035 TO |
254 | return $this->_currentSortID; |
255 | } | |
256 | ||
257 | /** | |
fe482240 | 258 | * Getter for currentSortDirection. |
6a488035 | 259 | * |
a6c01b45 CW |
260 | * @return int |
261 | * returns of the current sort direction | |
6a488035 | 262 | */ |
00be9182 | 263 | public function getCurrentSortDirection() { |
6a488035 TO |
264 | return $this->_currentSortDirection; |
265 | } | |
266 | ||
267 | /** | |
ac51fa38 | 268 | * Universal callback function for sorting by weight, id, title or name |
6a488035 | 269 | * |
f4aaa82a EM |
270 | * @param $a |
271 | * @param $b | |
272 | * | |
a6c01b45 CW |
273 | * @return int |
274 | * (-1 or 1) | |
6a488035 | 275 | */ |
00be9182 | 276 | public static function cmpFunc($a, $b) { |
ac51fa38 | 277 | $cmp_order = array('weight', 'id', 'title', 'name'); |
278 | foreach ($cmp_order as $attribute) { | |
279 | if (isset($a[$attribute]) && isset($b[$attribute])) { | |
280 | if ($a[$attribute] < $b[$attribute]) { | |
281 | return -1; | |
0db6c3e1 TO |
282 | } |
283 | elseif ($a[$attribute] > $b[$attribute]) { | |
ac51fa38 | 284 | return 1; |
285 | } // else: $a and $b are equal wrt to this attribute, try next... | |
1d45f936 | 286 | } |
e39d4fad | 287 | } |
50bfb460 | 288 | // if we get here, $a and $b are equal for all we know |
ac51fa38 | 289 | // however, as I understand we don't want equality here: |
290 | return -1; | |
6a488035 | 291 | } |
96025800 | 292 | |
6a488035 | 293 | } |