0ad600d4bda05841d444342d1c13e56d98b2a653
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
38 * Base class to provide generic sort functionality. Note that some ideas
39 * have been borrowed from the drupal tablesort.inc code. Also note that
40 * since the Pager and Sort class are similar, do match the function names
41 * if introducing additional functionality
44 * @copyright CiviCRM LLC (c) 2004-2014
48 class CRM_Utils_Sort
{
51 * Constants to determine what direction each variable
56 const ASCENDING
= 1, DESCENDING
= 2, DONTCARE
= 4,
59 * The name for the sort GET/POST param
63 SORT_ID
= 'crmSID', SORT_DIRECTION
= 'crmSortDirection', SORT_ORDER
= 'crmSortOrder';
66 * Name of the sort function. Used to isolate session variables
72 * Array of variables that influence the query
79 * The newly formulated base url to be used as links
80 * for various table elements
87 * What's the name of the sort variable in a REQUEST
94 * What variable are we currently sorting on
98 protected $_currentSortID;
101 * What direction are we sorting on
105 protected $_currentSortDirection;
108 * The output generated for the current form
115 * The constructor takes an assoc array
116 * key names of variable (which should be the same as the column name)
117 * value: ascending or descending
120 * Assoc array as described above.
121 * @param string $defaultSortOrder
124 * @return \CRM_Utils_Sort
127 public function __construct(&$vars, $defaultSortOrder = NULL) {
128 $this->_vars
= array();
129 $this->_response
= array();
131 foreach ($vars as $weight => $value) {
132 $this->_vars
[$weight] = array(
133 'name' => $value['sort'],
134 'direction' => CRM_Utils_Array
::value('direction', $value),
135 'title' => $value['name'],
139 $this->_currentSortID
= 1;
140 if (isset($this->_vars
[$this->_currentSortID
])) {
141 $this->_currentSortDirection
= $this->_vars
[$this->_currentSortID
]['direction'];
143 $this->_urlVar
= self
::SORT_ID
;
144 $this->_link
= CRM_Utils_System
::makeURL($this->_urlVar
, TRUE);
146 $this->initialize($defaultSortOrder);
150 * Function returns the string for the order by clause
152 * @return string the order by clause
154 public function orderBy() {
155 if (empty($this->_vars
[$this->_currentSortID
])) {
159 if ($this->_vars
[$this->_currentSortID
]['direction'] == self
::ASCENDING ||
160 $this->_vars
[$this->_currentSortID
]['direction'] == self
::DONTCARE
162 $this->_vars
[$this->_currentSortID
]['name'] = str_replace(' ', '_', $this->_vars
[$this->_currentSortID
]['name']);
163 return $this->_vars
[$this->_currentSortID
]['name'] . ' asc';
166 $this->_vars
[$this->_currentSortID
]['name'] = str_replace(' ', '_', $this->_vars
[$this->_currentSortID
]['name']);
167 return $this->_vars
[$this->_currentSortID
]['name'] . ' desc';
172 * Create the sortID string to be used in the GET param
177 * The direction of the sort.
179 * @return string the string to append to the url
182 public static function sortIDValue($index, $dir) {
183 return ($dir == self
::DESCENDING
) ?
$index . '_d' : $index . '_u';
187 * Init the sort ID values in the object
189 * @param string $defaultSortOrder
190 * The sort order to use by default.
192 * @return returns null if $url- (sort url) is not found
194 public function initSortID($defaultSortOrder) {
195 $url = CRM_Utils_Array
::value(self
::SORT_ID
, $_GET, $defaultSortOrder);
201 list($current, $direction) = explode('_', $url);
203 // if current is wierd and does not exist in the vars array, skip
204 if (!array_key_exists($current, $this->_vars
)) {
208 if ($direction == 'u') {
209 $direction = self
::ASCENDING
;
211 elseif ($direction == 'd') {
212 $direction = self
::DESCENDING
;
215 $direction = self
::DONTCARE
;
218 $this->_currentSortID
= $current;
219 $this->_currentSortDirection
= $direction;
220 $this->_vars
[$current]['direction'] = $direction;
226 * @param string $defaultSortOrder
227 * The sort order to use by default.
231 public function initialize($defaultSortOrder) {
232 $this->initSortID($defaultSortOrder);
234 $this->_response
= array();
236 $current = $this->_currentSortID
;
237 foreach ($this->_vars
as $index => $item) {
238 $name = $item['name'];
239 $this->_response
[$name] = array();
241 $newDirection = ($item['direction'] == self
::ASCENDING
) ? self
::DESCENDING
: self
::ASCENDING
;
243 if ($current == $index) {
244 if ($item['direction'] == self
::ASCENDING
) {
245 $class = 'sorting_asc';
248 $class = 'sorting_desc';
255 $this->_response
[$name]['link'] = '<a href="' . $this->_link
. $this->sortIDValue($index, $newDirection) . '" class="' . $class . '">' . $item['title'] . '</a>';
260 * Getter for currentSortID
262 * @return int returns of the current sort id
265 public function getCurrentSortID() {
266 return $this->_currentSortID
;
270 * Getter for currentSortDirection
272 * @return int returns of the current sort direction
275 public function getCurrentSortDirection() {
276 return $this->_currentSortDirection
;
280 * Universal callback function for sorting by weight, id, title or name
285 * @return int (-1 or 1)
287 public static function cmpFunc($a, $b) {
288 $cmp_order = array('weight', 'id', 'title', 'name');
289 foreach ($cmp_order as $attribute) {
290 if (isset($a[$attribute]) && isset($b[$attribute])) {
291 if ($a[$attribute] < $b[$attribute]) {
293 } elseif ($a[$attribute] > $b[$attribute]) {
295 } // else: $a and $b are equal wrt to this attribute, try next...
298 // if we get here, $a and $b es equal for all we know
299 // however, as I understand we don't want equality here: