-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEDTSort.php
103 lines (96 loc) · 3.15 KB
/
EDTSort.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* EDTSort class file.
*
* @author Jan Was <[email protected]>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2011-2012 Jan Was
* @license http://www.yiiframework.com/license/
*/
/**
* EDTSort represents information relevant to sorting.
* Changes:
* - added property columns used instead of attributes
*
* @see CSort
*/
class EDTSort extends CSort
{
/**
* @var boolean whether the sorting can be applied to multiple attributes simultaneously.
* Defaults to false, which means each time the data can only be sorted by one attribute.
*/
public $multiSort=true;
/**
* @var array
*/
public $columns;
/**
* @var string the name of the GET parameter that specifies which attributes to be sorted
* in which direction. Defaults to 'sort'.
*/
public $sortVar='iSortingCols';
/**
* @var string prefix for each GET parameter denoting index in $columns property by which to sort
*/
public $sortVarIdxPrefix='iSortCol_';
/**
* @var string prefix for each GET parameter denoting direction of sort for a column
*/
public $sortVarDirPrefix='sSortDir_';
private $_directions;
/**
* Constructor.
* @param string $modelClass the class name of data models that need to be sorted.
* This should be a child class of {@link CActiveRecord}.
* @param array $columns
*/
public function __construct($modelClass=null,$columns=null)
{
$this->modelClass=$modelClass;
$this->columns=$columns;
}
/**
* Returns the currently requested sort information.
* @return array sort directions indexed by attribute names.
* The sort direction is true if the corresponding attribute should be
* sorted in descending order.
*/
public function getDirections()
{
if($this->_directions===null)
{
$this->_directions=array();
// treat columns as an indexed array, even if it's associative
$columns = is_array($this->columns) ? array_values($this->columns) : $this->columns;
if ( $columns !== null && isset( $_GET[$this->sortVar] ) && ($iSortingCols = intval($_GET[$this->sortVar])) > 0) {
for ($i = 0; $i < $iSortingCols && isset($_GET[$this->sortVarIdxPrefix.$i]) && isset($columns[intval( $_GET[$this->sortVarIdxPrefix.$i] )]); ++$i) {
$index = intval($_GET[$this->sortVarIdxPrefix.$i]);
$column = $columns[$index];
$attribute = null;
if (is_string($column) || isset($column['name'])) {
if (is_string($column)) {
$params = explode(':',$column);
if (isset($params[0]))
$attribute = $params[0];
} else {
$attribute = $column['name'];
}
} else {
// checkbox or operations (buttons) column
//! @todo use FK for checkbox column? need to find it first
}
if($attribute !== null && ($this->resolveAttribute($attribute))!==false) {
$descending = isset($_GET[$this->sortVarDirPrefix.$i]) && $_GET[$this->sortVarDirPrefix.$i] == "desc";
$this->_directions[$attribute]=$descending;
if(!$this->multiSort)
return $this->_directions;
}
}
}
if($this->_directions===array() && is_array($this->defaultOrder))
$this->_directions=$this->defaultOrder;
}
return $this->_directions;
}
}