forked from sieppl/yii-json-dataprovider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSonActiveDataProvider.php
82 lines (72 loc) · 2.81 KB
/
JSonActiveDataProvider.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
<?php
Yii::import('ext.yii-json-dataprovider.ModelToArrayConverter');
class JSonActiveDataProvider extends CActiveDataProvider
{
/**
*
* @var mixed If is set to null (default) all attributes of the model will be added.
* If a string is given, only this attribute will added.
* If an array of strings is given, all elements will be retrieved.
*/
public $attributes;
/**
*
* @var mixed If is set to null (default) no relations of the model will be added.
* If a string is given, only the mentioned relation will be added.
* If an array is given, there a two valid array formats:
*
* 1. array('relation1', 'relation2') will return the mentioned relations with all attributes, but no sub relations
* 2. array('relation1', 'relation2' => array('attributes' => array('foo', 'bar'), 'relations' => array('subRelation'))) will return configured attributes and relations for relation2
*
* Sub configurations of relations follow the same rules like the global configuration for attributes and relations
*/
public $relations;
/**
*
* @var array An array where the key is the original attribute name and the value the alias to be used instead when retrieving it. This will affect all retrieved models recursively.
*/
public $attributeAliases;
/**
*
* @var boolean When set to true the root of the json will have meta informations from the data provider for counts and pagination.
*/
public $includeDataProviderInformation = true;
/**
*
* @var callable Callback to be called after an model was processed for json, the callback will receive the model itself, the attribute array and the relation array
*/
public $onAfterModelToArray = null;
public function getArrayCountData()
{
return array(
'itemCount' => $this->getItemCount(),
'totalItemCount' => (int) $this->getTotalItemCount(),
'currentPage' => $this->pagination ? $this->pagination->currentPage : 1,
'pageCount' => $this->pagination ? $this->pagination->pageCount : 1,
'pageSize' => $this->pagination ? $this->pagination->pageSize : $this->getItemCount(),
);
}
public function getArrayData($refresh=false)
{
$arrayData = array();
if ($data = $this->getData($refresh))
{
$converter = new ModelToArrayConverter($data, $this->attributes, $this->relations);
$converter->attributeAliases = $this->attributeAliases;
$converter->onAfterModelToArray = $this->onAfterModelToArray;
$arrayData = $converter->convert();
}
if ($this->includeDataProviderInformation)
{
return array_merge($this->getArrayCountData(), array(
'data' => $arrayData
));
}
else
return $arrayData;
}
public function getJsonData($refresh=false)
{
return json_encode($this->getArrayData($refresh));
}
}