-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeGrid.php
147 lines (134 loc) · 4.81 KB
/
TreeGrid.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
<?php
// Lucas Tiago de Moraes
// include class DHX
include_once 'DHX.php';
// class TreeGrid
class TreeGrid extends DHX {
private $document; // ref document
private $rows; // ref rows
private $array = array(); // array
private $num = 0;
public $parent; // set parent
// auto construct
function __construct($value = ''){
if($value){
$this->encoding($value);
}
$this->document = $this->document();
$this->rows = $this->document->appendChild(new DOMElement('rows'));
}
// public row
public function row(){
$data = func_get_args();
foreach($data as $r){
$total = count($this->array);
$row = '';
if($total > 0){
$row = $this->array[$this->num]->appendChild(new DOMElement('row'));
}else{
$row = $this->rows->appendChild(new DOMElement('row'));
}
foreach($r as $key => $value){
if($key == 'row'){
$this->rowArray($row, $value);
}elseif($key == 'cell'){
foreach($value as $r){
$cell = $row->appendChild(new DOMElement($key));
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));
}
}
}else{
$row->setAttributeNode(new DOMAttr($key, $value));
}
}
}
}
// private rowArray
private function rowArray($ref, $data){
foreach($data as $r){
$row = $ref->appendChild(new DOMElement('row'));
foreach($r as $key => $value){
if($key == 'row'){
$this->rowArray($row, $value);
}elseif($key == 'cell'){
foreach($value as $r){
$cell = $row->appendChild(new DOMElement($key));
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));
}
}
}else{
$row->setAttributeNode(new DOMAttr($key, $value));
}
}
}
}
// public cell
public function cell(){
$data = func_get_args();
$total = count($this->array);
if($total > 0){
foreach($data as $row){
$cell = $this->array[$this->num]->appendChild(new DOMElement('cell'));
if(is_array($row)){
foreach($row 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($row));
}
}
}
}
// public start
public function start($data){
$total = count($this->array);
$row = '';
if($total > 0){
$row = $this->array[$this->num]->appendChild(new DOMElement('row'));
}else{
$row = $this->rows->appendChild(new DOMElement('row'));
}
foreach($data as $key => $value){
$row->setAttributeNode(new DOMAttr($key, $value));
}
$this->num++;
$this->array[$this->num] = $row;
}
// public end
public function end(){
$total = count($this->array);
unset($this->array[$this->num]);
$this->num--;
}
// public result
public function result(){
if($this->parent){
$this->rows->setAttributeNode(new DOMAttr('parent', $this->parent));
}else{
$this->rows->setAttributeNode(new DOMAttr('parent', 0));
}
return $this->document->saveXML();
}
}