_vars = array(); $this->_response = array(); foreach ($vars as $weight => $value) { $this->_vars[$weight] = array( 'name' => CRM_Utils_Type::validate($value['sort'], 'MysqlColumnNameOrAlias'), '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 */ 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 CRM_Utils_Type::escape($this->_vars[$this->_currentSortID]['name'], 'MysqlColumnNameOrAlias') . ' asc'; } else { $this->_vars[$this->_currentSortID]['name'] = str_replace(' ', '_', $this->_vars[$this->_currentSortID]['name']); return CRM_Utils_Type::escape($this->_vars[$this->_currentSortID]['name'], 'MysqlColumnNameOrAlias') . ' 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 */ 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. */ 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 weird 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. */ 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 */ public function getCurrentSortID() { return $this->_currentSortID; } /** * Getter for currentSortDirection. * * @return int * returns of the current sort direction */ public function getCurrentSortDirection() { return $this->_currentSortDirection; } /** * Universal callback function for sorting by weight, id, title or name * * @param $a * @param $b * * @return int * (-1 or 1) */ public static function cmpFunc($a, $b) { $cmp_order = array('weight', 'id', 'title', 'name'); foreach ($cmp_order as $attribute) { if (isset($a[$attribute]) && isset($b[$attribute])) { if ($a[$attribute] < $b[$attribute]) { return -1; } elseif ($a[$attribute] > $b[$attribute]) { return 1; } // else: $a and $b are equal wrt to this attribute, try next... } } // if we get here, $a and $b are equal for all we know // however, as I understand we don't want equality here: return -1; } }