GridState.php
3.22 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
<?php
/**
* This class is a snapshot of the current status of a {@link GridField}.
*
* It's designed to be inserted into a Form as a HiddenField and passed through
* to actions such as the {@link GridField_FormAction}.
*
* @see GridField
*
* @package forms
* @subpackage fields-gridfield
*/
class GridState extends HiddenField {
/**
* @var GridField
*/
protected $grid;
/**
* @var GridState_Data
*/
protected $data = null;
/**
*
* @param GridField $name
* @param string $data - json encoded string
*/
public function __construct($grid, $value = null) {
$this->grid = $grid;
if ($value) $this->setValue($value);
parent::__construct($grid->getName() . '[GridState]');
}
/**
* @param mixed $d
* @return object
*/
public static function array_to_object($d) {
if(is_array($d)) {
return (object) array_map(array('GridState', 'array_to_object'), $d);
}
return $d;
}
/**
* @param mixed $value
*/
public function setValue($value) {
if (is_string($value)) {
$this->data = new GridState_Data(json_decode($value, true));
}
parent::setValue($value);
}
/**
* @var GridState_Data
*/
public function getData() {
if(!$this->data) {
$this->data = new GridState_Data();
}
return $this->data;
}
/**
* @return DataList
*/
public function getList() {
return $this->grid->getList();
}
/**
* Returns a json encoded string representation of this state.
*
* @return string
*/
public function Value() {
if(!$this->data) {
return json_encode(array());
}
return json_encode($this->data->toArray());
}
/**
* Returns a json encoded string representation of this state.
*
* @return string
*/
public function dataValue() {
return $this->Value();
}
/**
*
* @return type
*/
public function attrValue() {
return Convert::raw2att($this->Value());
}
/**
*
* @return type
*/
public function __toString() {
return $this->Value();
}
}
/**
* Simple set of data, similar to stdClass, but without the notice-level
* errors.
*
* @see GridState
*
* @package forms
* @subpackage fields-gridfield
*/
class GridState_Data {
/**
* @var array
*/
protected $data;
public function __construct($data = array()) {
$this->data = $data;
}
public function __get($name) {
if(!isset($this->data[$name])) {
$this->data[$name] = new GridState_Data();
} else if(is_array($this->data[$name])) {
$this->data[$name] = new GridState_Data($this->data[$name]);
}
return $this->data[$name];
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function __isset($name) {
return isset($this->data[$name]);
}
public function __toString() {
if(!$this->data) {
return "";
}
return json_encode($this->toArray());
}
public function toArray() {
$output = array();
foreach($this->data as $k => $v) {
$output[$k] = (is_object($v) && method_exists($v, 'toArray')) ? $v->toArray() : $v;
}
return $output;
}
}
/**
* @see GridState
*
* @package forms
* @subpackage fields-gridfield
*/
class GridState_Component implements GridField_HTMLProvider {
public function getHTMLFragments($gridField) {
return array(
'before' => $gridField->getState(false)->Field()
);
}
}