-
Notifications
You must be signed in to change notification settings - Fork 12
/
Sort.php
118 lines (111 loc) · 3.72 KB
/
Sort.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace sammaye\mongoyii;
use Yii;
use CSort;
use sammaye\mongoyii\Document;
use sammaye\mongoyii\Exception;
/**
* EMongoSort
* @author Andrea Cardinale <[email protected]>
* corresponding to Csort for MongoYii
* @see yii/framework/web/CSort
*/
/**
* This is only ever used in conjunction with CGridView and CListView. It is not designed to be used independantly
*/
class Sort extends CSort
{
/**
* @see CSort::resolveAttribute()
* @param string $attribute
* @return bool|string|array
*/
public function resolveAttribute($attribute)
{
if($this->attributes !== array()){
$attributes = $this->attributes;
}elseif($this->modelClass !== null){
$attributes = Document::model($this->modelClass)->attributeNames();
if(empty($attributes)){
// The previous statement can return null in certain models. So this is used as backup.
$attributes = Document::model($this->modelClass)->safeAttributeNames;
}
}else{
return false;
}
foreach($attributes as $name => $definition){
if(is_string($name)){
if($name === $attribute){
return $definition;
}
}elseif($definition === '*'){
if($this->modelClass !== null && Document::model($this->modelClass)->hasAttribute($attribute)){
return $attribute;
}
}elseif($definition === $attribute){
return $attribute;
}
}
return false;
}
/**
* @see CSort::resolveLabel()
* @param string $attribute
* @return string
*/
public function resolveLabel($attribute)
{
$definition = $this->resolveAttribute($attribute);
if(is_array($definition)){
if(isset($definition['label'])){
return $definition['label'];
}
}elseif(is_string($definition)){
$attribute = $definition;
}
if($this->modelClass !== null){
return Document::model($this->modelClass)->getAttributeLabel($attribute);
}
return $attribute;
}
/**
* @see CSort::getOrderBy()
* @param EMongoCriteria $criteria
* @return array|string
* @throws EMongoException
*/
public function getOrderBy($criteria = null)
{
$directions = $this->getDirections();
if(empty($directions)){
return is_string($this->defaultOrder) ? $this->defaultOrder : array();
}
$schema = null; // ATM the schema aspect of this function has been disabled, the code below for schema isset is left in for future reference
$orders = array();
foreach($directions as $attribute => $descending){
$definition = $this->resolveAttribute($attribute);
if(is_array($definition)){
// Atm only single cell sorting is allowed, this will change to allow you to define
// a true definition of multiple fields to sort when one sort field is triggered but atm that is not possible
if($descending){
$orders[$attribute] = isset($definition['desc']) ? -1 : 1;
}else{
$orders[$attribute] = isset($definition['asc']) ? 1 : -1;
}
}elseif($definition !== false){
$attribute = $definition;
if(isset($schema)){
if(($pos = strpos($attribute,'.')) !== false){
throw new Exception('MongoDB cannot sort on joined fields please modify ' . $attribute . ' to not be sortable');
//$attribute=$schema->quoteTableName(substr($attribute,0,$pos)).'.'.$schema->quoteColumnName(substr($attribute,$pos+1));
}else{
// MongoDB does not need these escaping or table namespacing elements at all so they have been commented out for the second
//$attribute=($criteria===null || $criteria->alias===null ? EMongoDocument::model($this->modelClass)->getTableAlias(true) : $schema->quoteTableName($criteria->alias)).'.'.$schema->quoteColumnName($attribute);
}
}
$orders[$attribute] = $descending ? -1 : 1;
}
}
return $orders;
}
}