-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.php
243 lines (231 loc) · 8.94 KB
/
Grid.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
// Lucas Tiago de Moraes
// include class DHX
include_once 'DHX.php';
// class Grid
class Grid extends DHX {
private $document; // ref document
private $rows; // ref rows
private $head; // ref head
private $beforeInit; // ref beforeInit
private $afterInit; // ref afterInit
public $pos; // set pos
public $total_count; // set total_count
// auto construct
function __construct($value = ''){
if($value){
$this->encoding($value);
}
$this->document = $this->document();
$this->rows = $this->document->appendChild(new DOMElement('rows'));
}
// public column
public function column(){
$data = func_get_args();
foreach($data as $row){
$column = '';
if($this->head){
$column = $this->head->appendChild(new DOMElement('column'));
}else{
$this->head = $this->rows->appendChild(new DOMElement('head'));
$column = $this->head->appendChild(new DOMElement('column'));
}
foreach($row as $key => $value){
if($key == 'option'){
if(array_key_exists(0, $value)){
foreach($value as $r){
$option = $column->appendChild(new DOMElement('option'));
foreach($r as $k => $v){
if($k == 'text'){
$option->appendChild($this->document->createTextNode($v));
}else{
$option->setAttributeNode(new DOMAttr($k, $v));
}
}
}
}else{
$option = $column->appendChild(new DOMElement('option'));
foreach($value as $k => $v){
if($k == 'text'){
$option->appendChild($this->document->createTextNode($v));
}else{
$option->setAttributeNode(new DOMAttr($k, $v));
}
}
}
}elseif($key == 'label'){
$column->appendChild($this->document->createTextNode($value));
}else{
$column->setAttributeNode(new DOMAttr($key, $value));
}
}
}
}
// public settings
public function settings($data){
$settings = '';
if($this->head){
$settings = $this->head->appendChild(new DOMElement('settings'));
}else{
$this->head = $this->rows->appendChild(new DOMElement('head'));
$settings = $this->head->appendChild(new DOMElement('settings'));
}
foreach($data as $key => $value){
$s = $settings->appendChild(new DOMElement($key));
$s->appendChild($this->document->createTextNode($value));
}
}
// public beforeInit
public function beforeInit(){
$data = func_get_args();
$call = '';
if($this->beforeInit){
$call = $this->beforeInit->appendChild(new DOMElement('call'));
}else{
if($this->head){
$this->beforeInit = $this->head->appendChild(new DOMElement('beforeInit'));
}else{
$this->head = $this->rows->appendChild(new DOMElement('head'));
$this->beforeInit = $this->head->appendChild(new DOMElement('beforeInit'));
}
$call = $this->beforeInit->appendChild(new DOMElement('call'));
}
$param = $call->appendChild(new DOMElement('param'));
$values = '';
$num = 1;
foreach($data as $value){
if($num == 1){
$call->setAttributeNode(new DOMAttr('command', $value));
}else{
if($values){
$values .= ',' . $value;
}else{
$values .= $value;
}
}
$num++;
}
$param->appendChild($this->document->createTextNode($values));
}
// public afterInit
public function afterInit(){
$data = func_get_args();
$call = '';
if($this->afterInit){
$call = $this->afterInit->appendChild(new DOMElement('call'));
}else{
if($this->head){
$this->afterInit = $this->head->appendChild(new DOMElement('afterInit'));
}else{
$this->head = $this->rows->appendChild(new DOMElement('head'));
$this->afterInit = $this->head->appendChild(new DOMElement('afterInit'));
}
$call = $this->afterInit->appendChild(new DOMElement('call'));
}
$call->setAttributeNode(new DOMAttr('command', $command));
$param = $call->appendChild(new DOMElement('param'));
$values = '';
$num = 1;
foreach($data as $value){
if($num == 1){
$call->setAttributeNode(new DOMAttr('command', $value));
}else{
if($values){
$values .= ',' . $value;
}else{
$values .= $value;
}
}
$num++;
}
$param->appendChild($this->document->createTextNode($values));
}
// public row
public function row(){
$data = func_get_args();
foreach($data as $rows){
$row = $this->rows->appendChild(new DOMElement('row'));
foreach($rows as $key => $value){
if($key == 'cell'){
foreach($value as $r){
$cell = $row->appendChild(new DOMElement('cell'));
if(is_array($r)){
foreach($r as $k => $v){
if($k == 'text'){
$cell->appendChild($this->document->createTextNode($v));
}else{
$cell->setAttributeNode(new DOMAttr($k, $v));
}
}
}else{
$cell->appendChild($this->document->createTextNode($r));
}
}
}elseif($key == 'userdata'){
if(array_key_exists(0, $value)){
foreach($value as $r){
$userdata = $row->appendChild(new DOMElement('userdata'));
foreach($r as $k => $v){
if($k == 'value'){
$userdata->appendChild($this->document->createTextNode($v));
}else{
$userdata->setAttributeNode(new DOMAttr($k, $v));
}
}
}
}else{
$userdata = $row->appendChild(new DOMElement('userdata'));
foreach($value as $k => $v){
if($k == 'value'){
$userdata->appendChild($this->document->createTextNode($v));
}else{
$userdata->setAttributeNode(new DOMAttr($k, $v));
}
}
}
}else{
$row->setAttributeNode(new DOMAttr($key, $value));
}
}
}
}
// public cell
public function cell(){
$data = func_get_args();
$row = $this->rows->appendChild(new DOMElement('row'));
$num = 1;
foreach($data as $value){
if($num == 1){
$row->setAttributeNode(new DOMAttr('id', $value));
}else{
$cell = $row->appendChild(new DOMElement('cell'));
$cell->appendChild($this->document->createTextNode($value));
}
$num++;
}
}
// public userdata
public function userdata(){
$data = func_get_args();
foreach($data as $row){
$userdata = $this->rows->appendChild(new DOMElement('userdata'));
foreach($row as $key => $value){
if($key == 'value'){
$userdata->appendChild($this->document->createTextNode($value));
}else{
$userdata->setAttributeNode(new DOMAttr($key, $value));
}
}
}
}
// public result
public function result(){
if($this->pos){
$this->rows->setAttributeNode(new DOMAttr('pos', $this->pos));
}
if($this->total_count){
$this->rows->setAttributeNode(new DOMAttr('total_count', $this->total_count));
}
return $this->document->saveXML();
}
}