_vars = array(); $this->_response = array(); foreach ($vars as $weight => $value) { $this->_vars[$weight] = array( 'name' => $value['sort'], 'direction' => CRM_Utils_Array::value('direction', $value), 'title' => $value['name'], ); } $this->_currentSortID = 1; if (isset($this->_vars[$this->_currentSortID])) { $this->_currentSortDirection = $this->_vars[$this->_currentSortID]['direction']; } $this->_urlVar = self::SORT_ID; $this->_link = CRM_Utils_System::makeURL($this->_urlVar, TRUE); $this->initialize($defaultSortOrder); } /** * Function returns the string for the order by clause * * @return string the order by clause * @access public */ function orderBy() { if (empty($this->_vars[$this->_currentSortID])) { return ''; } if ($this->_vars[$this->_currentSortID]['direction'] == self::ASCENDING || $this->_vars[$this->_currentSortID]['direction'] == self::DONTCARE ) { $this->_vars[$this->_currentSortID]['name'] = str_replace(' ', '_', $this->_vars[$this->_currentSortID]['name']); return $this->_vars[$this->_currentSortID]['name'] . ' asc'; } else { $this->_vars[$this->_currentSortID]['name'] = str_replace(' ', '_', $this->_vars[$this->_currentSortID]['name']); return $this->_vars[$this->_currentSortID]['name'] . ' desc'; } } /** * create the sortID string to be used in the GET param * * @param int $index the field index * @param int $dir the direction of the sort * * @return string the string to append to the url * @static * @access public */ static function sortIDValue($index, $dir) { return ($dir == self::DESCENDING) ? $index . '_d' : $index . '_u'; } /** * init the sort ID values in the object * * @param string $defaultSortOrder the sort order to use by default * * @return returns null if $url- (sort url) is not found * @access public */ function initSortID($defaultSortOrder) { $url = CRM_Utils_Array::value(self::SORT_ID, $_GET, $defaultSortOrder); if (empty($url)) { return; } list($current, $direction) = explode('_', $url); // if current is wierd and does not exist in the vars array, skip if (!array_key_exists($current, $this->_vars)) { return; } if ($direction == 'u') { $direction = self::ASCENDING; } elseif ($direction == 'd') { $direction = self::DESCENDING; } else { $direction = self::DONTCARE; } $this->_currentSortID = $current; $this->_currentSortDirection = $direction; $this->_vars[$current]['direction'] = $direction; } /** * init the object * * @param string $defaultSortOrder the sort order to use by default * * @return void * @access public */ function initialize($defaultSortOrder) { $this->initSortID($defaultSortOrder); $this->_response = array(); $current = $this->_currentSortID; foreach ($this->_vars as $index => $item) { $name = $item['name']; $this->_response[$name] = array(); $newDirection = ($item['direction'] == self::ASCENDING) ? self::DESCENDING : self::ASCENDING; if ($current == $index) { if ($item['direction'] == self::ASCENDING) { $class = 'sorting_asc'; } else { $class = 'sorting_desc'; } } else { $class = 'sorting'; } $this->_response[$name]['link'] = '' . $item['title'] . ''; } } /** * getter for currentSortID * * @return int returns of the current sort id * @acccess public */ function getCurrentSortID() { return $this->_currentSortID; } /** * getter for currentSortDirection * * @return int returns of the current sort direction * @acccess public */ function getCurrentSortDirection() { return $this->_currentSortDirection; } /** * Universal callback function for sorting by weight * * @param $a * @param $b * * @return array of items sorted by weight * @access public */ static function cmpFunc($a, $b) { return ($a['weight'] <= $b['weight']) ? -1 : 1; } }