forked from nineinchnick/edatatables
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEDTSort.php
More file actions
193 lines (170 loc) · 6.76 KB
/
Copy pathEDTSort.php
File metadata and controls
193 lines (170 loc) · 6.76 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* EDTSort class file.
*
* @author Jan Was <janek.jan@gmail.com>
*
* @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 bool 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 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;
}
/**
* Creates a URL that can lead to generating sorted data.
*
* @param CController $controller the controller that will be used to create the URL.
* @param array $directions the sort directions indexed by attribute names.
* The sort direction can be either CSort::SORT_ASC for ascending order or
* CSort::SORT_DESC for descending order.
*
* @return string the URL for sorting
*/
public function createUrl($controller, $directions)
{
$params = $this->params === null ? $_GET : $this->params;
$columns = is_array($this->columns) ? array_values($this->columns) : $this->columns;
if (empty($columns)) {
return $controller->createUrl($this->route);
}
$i = 0;
foreach ($params as $key => $val) {
if (strpos($key, $this->sortVarIdxPrefix) === false && strpos($key, $this->sortVarDirPrefix) === false) {
continue;
}
unset($params[$key]);
}
unset($params[$this->sortVar]);
foreach ($columns as $key => $column) {
if (!is_string($column) && !isset($column['name'])) {
continue;
}
$attribute = null;
if (is_string($column)) {
$columnConfig = explode(':', $column);
if (!isset($columnConfig[0])) {
continue;
}
$attribute = $columnConfig[0];
} else {
$attribute = $column['name'];
}
if ($attribute === null || !isset($directions[$attribute])) {
continue;
}
/* Check if we need to disable sorting for this column */
$currentDirection = false;
foreach ($_GET as $k => $v) {
if (strpos($k, $this->sortVarIdxPrefix) === false || intval($v) !== $key) {
continue;
}
$index = intval(substr($k, strlen($this->sortVarIdxPrefix)));
$currentDirection = isset($_GET[$this->sortVarDirPrefix.$index]) && $_GET[$this->sortVarDirPrefix.$index] == 'desc';
break;
}
/** Do not sort at all by this column when current direction (in $_GET) is different than one set in $directions */
if ($currentDirection && $currentDirection != $directions[$attribute]) {
continue;
}
$params[$this->sortVarIdxPrefix.$i] = $key;
if ($directions[$attribute]) {
$params[$this->sortVarDirPrefix.$i] = 'desc';
}
$i++;
if (!$this->multiSort) {
break;
}
}
$params[$this->sortVar] = $i;
return $controller->createUrl($this->route, $params);
}
}