This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataProvider.php
143 lines (127 loc) · 4.93 KB
/
DataProvider.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
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
<?php
namespace ext\activedocument;
use \CActiveDataProvider;
class DataProvider extends CActiveDataProvider {
/**
* @var string the primary Document class name. The {@link getData()} method
* will return a list of objects of this class.
*/
public $modelClass;
/**
* @var \ext\activedocument\Document the finder instance (eg <code>Post::model()</code>).
* This property can be set by passing the finder instance as the first parameter
* to the constructor. For example, <code>Post::model()->published()</code>.
*/
public $model;
/**
* @var string the name of key attribute for {@link modelClass}. If not set,
* it means the primary key of the corresponding database table will be used.
*/
public $keyAttribute;
private $_criteria;
private $_sort;
/**
* Constructor.
* @param mixed $modelClass the model class (e.g. 'Post') or the model finder instance
* (e.g. <code>Post::model()</code>, <code>Post::model()->published()</code>).
* @param array $config configuration (name=>value) to be applied as the initial property values of this class.
*/
public function __construct($modelClass, $config=array()) {
if (is_string($modelClass)) {
$this->modelClass = $modelClass;
$this->model = Document::model($this->modelClass);
} else if ($modelClass instanceof Document) {
$this->modelClass = get_class($modelClass);
$this->model = $modelClass;
}
$this->setId($this->modelClass);
foreach ($config as $key => $value)
$this->$key = $value;
}
/**
* Returns the query criteria.
* @return \ext\activedocument\Criteria the query criteria
*/
public function getCriteria() {
if ($this->_criteria === null)
$this->_criteria = new Criteria;
return $this->_criteria;
}
/**
* Sets the query criteria.
* @param mixed $value the query criteria. This can be either a DbCriteria object or an array
* representing the query criteria.
*/
public function setCriteria($value) {
$this->_criteria = $value instanceof Criteria ? $value : new Criteria($value);
}
/**
* Returns the sorting object.
* @return \ext\activedocument\Sort the sorting object. If this is false, it means the sorting is disabled.
*/
public function getSort() {
if ($this->_sort === null) {
$this->_sort = new Sort;
if (($id = $this->getId()) != '')
$this->_sort->sortVar = $id . '_sort';
$this->_sort->modelClass = $this->modelClass;
}
return $this->_sort;
}
/**
* Fetches the data from the persistent data storage.
* @return array list of data items
*/
protected function fetchData() {
$criteria = clone $this->getCriteria();
if (($pagination = $this->getPagination()) !== false) {
$pagination->setItemCount($this->getTotalItemCount());
$pagination->applyLimit($criteria);
/**
* Ensure pagination params don't add up to greater than item count
*/
if(($criteria->offset+$criteria->limit)>$pagination->getItemCount())
$criteria->limit = $pagination->getItemCount()-$criteria->offset;
}
$baseCriteria = $this->model->getCriteria(false);
if (($sort = $this->getSort()) !== false) {
// set model criteria so that CSort can use its table alias setting
if ($baseCriteria !== null) {
$c = clone $baseCriteria;
$c->mergeWith($criteria);
$this->model->setCriteria($c);
}
else
$this->model->setCriteria($criteria);
$sort->applyOrder($criteria);
}
$this->model->setCriteria($baseCriteria !== null ? clone $baseCriteria : null);
$data = $this->model->findAll($criteria);
$this->model->setCriteria($baseCriteria); // restore original criteria
return $data;
}
/**
* Fetches the data item keys from the persistent data storage.
* @return array list of data item keys.
*/
protected function fetchKeys() {
$keys = array();
foreach ($this->getData() as $i => $data) {
$key = $this->keyAttribute === null ? $data->getPrimaryKey() : $data->{$this->keyAttribute};
$keys[$i] = is_array($key) ? implode(',', $key) : $key;
}
return $keys;
}
/**
* Calculates the total number of data items.
* @return integer the total number of data items.
*/
protected function calculateTotalItemCount() {
$baseCriteria = $this->model->getCriteria(false);
if ($baseCriteria !== null)
$baseCriteria = clone $baseCriteria;
$count = $this->model->count($this->getCriteria());
$this->model->setCriteria($baseCriteria);
return $count;
}
}