|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Nestedset_Model_Output |
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* Convert a tree array (with depth) into a hierarchical array. |
|
7
|
|
|
* |
|
8
|
|
|
* @param $nodes|array Array with depth value. |
|
9
|
|
|
* |
|
10
|
|
|
* @return array |
|
11
|
|
|
*/ |
|
12
|
|
|
public function toArray(array $nodes) |
|
13
|
|
|
{ |
|
14
|
|
|
$result = array(); |
|
15
|
|
|
$stackLevel = 0; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
// Node Stack. Used to help building the hierarchy |
|
18
|
|
|
$stack = array(); |
|
19
|
|
|
|
|
20
|
|
|
foreach ($nodes as $node) { |
|
21
|
|
|
$node['children'] = array(); |
|
22
|
|
|
|
|
23
|
|
|
// Number of stack items |
|
24
|
|
|
$stackLevel = count($stack); |
|
25
|
|
|
|
|
26
|
|
|
// Check if we're dealing with different levels |
|
27
|
|
|
while ($stackLevel > 0 && $stack[$stackLevel - 1]['depth'] >= $node['depth']) { |
|
28
|
|
|
array_pop($stack); |
|
29
|
|
|
$stackLevel--; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// Stack is empty (we are inspecting the root) |
|
33
|
|
|
if ($stackLevel == 0) { |
|
34
|
|
|
// Assigning the root node |
|
35
|
|
|
$i = count($result); |
|
36
|
|
|
|
|
37
|
|
|
$result[$i] = $node; |
|
38
|
|
|
$stack[] =& $result[$i]; |
|
39
|
|
|
} else { |
|
40
|
|
|
// Add node to parent |
|
41
|
|
|
$i = count($stack[$stackLevel - 1]['children']); |
|
42
|
|
|
|
|
43
|
|
|
$stack[$stackLevel - 1]['children'][$i] = $node; |
|
44
|
|
|
$stack[] =& $stack[$stackLevel - 1]['children'][$i]; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $result; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Convert a tree array (with depth) into a hierarchical XML string. |
|
53
|
|
|
* |
|
54
|
|
|
* @param $nodes|array Array with depth value. |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function toXml(array $nodes) |
|
59
|
|
|
{ |
|
60
|
|
|
$xml = new DomDocument('1.0'); |
|
61
|
|
|
$xml->preserveWhiteSpace = false; |
|
62
|
|
|
$root = $xml->createElement('root'); |
|
63
|
|
|
$xml->appendChild($root); |
|
64
|
|
|
|
|
65
|
|
|
$depth = 0; |
|
66
|
|
|
$currentChildren = array(); |
|
67
|
|
|
|
|
68
|
|
|
foreach ($nodes as $node) { |
|
69
|
|
|
$element = $xml->createElement('element'); |
|
70
|
|
|
$element->setAttribute('id', $node['id']); |
|
71
|
|
|
$element->setAttribute('name', $node['name']); |
|
72
|
|
|
$element->setAttribute('lft', $node['lft']); |
|
73
|
|
|
$element->setAttribute('rgt', $node['rgt']); |
|
74
|
|
|
|
|
75
|
|
|
$children = $xml->createElement('children'); |
|
76
|
|
|
$element->appendChild($children); |
|
77
|
|
|
|
|
78
|
|
|
if ($node['depth'] == 0) { |
|
79
|
|
|
// Handle root |
|
80
|
|
|
$root->appendChild($element); |
|
81
|
|
|
$currentChildren[0] = $children; |
|
82
|
|
|
} elseif ($node['depth'] > $depth) { |
|
83
|
|
|
// is a new sub level |
|
84
|
|
|
$currentChildren[$depth]->appendChild($element); |
|
85
|
|
|
$currentChildren[$node['depth']] = $children; |
|
86
|
|
|
} elseif ($node['depth'] == $depth || $node['depth'] < $depth) { |
|
87
|
|
|
// is at the same level |
|
88
|
|
|
$currentChildren[$node['depth'] - 1]->appendChild($element); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$depth = $node['depth']; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $xml->saveXML(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Return nested set as JSON |
|
99
|
|
|
* |
|
100
|
|
|
* @params $nodes|array Original 'flat' nested tree |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function toJson(array $nodes) |
|
105
|
|
|
{ |
|
106
|
|
|
$nestedArray = $this->toArray($nodes); |
|
107
|
|
|
$result = json_encode($nestedArray); |
|
108
|
|
|
|
|
109
|
|
|
return $result; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns all elements as HTML structure |
|
114
|
|
|
* |
|
115
|
|
|
* Possible options: |
|
116
|
|
|
* - list (simple <ul><li>) |
|
117
|
|
|
* |
|
118
|
|
|
* @param $model|NestedSet_Model Nested set model |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
public function toHtml(array $nodes, $method = 'list') |
|
123
|
|
|
{ |
|
124
|
|
|
switch ($method) { |
|
125
|
|
|
case 'list': |
|
126
|
|
|
default: |
|
127
|
|
|
return $this->_toHtmlList($nodes); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Returns all elements as <ul>/<li> structure |
|
133
|
|
|
* |
|
134
|
|
|
* @param $nodes|array |
|
135
|
|
|
* |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function _toHtmlList(array $nodes) |
|
139
|
|
|
{ |
|
140
|
|
|
$result = "<ul>"; |
|
141
|
|
|
$depth = $nodes[0]['depth']; |
|
142
|
|
|
|
|
143
|
|
|
foreach ($nodes as $node) { |
|
144
|
|
|
if ($depth < $node['depth']) { |
|
145
|
|
|
$result .= "<ul>"; |
|
146
|
|
|
} elseif ($depth == $node['depth'] && $depth > $nodes[0]['depth']) { |
|
147
|
|
|
$result .= "</li>"; |
|
148
|
|
|
} elseif ($depth > $node['depth']) { |
|
149
|
|
|
for ($i = 0; $i < ($depth - $node['depth']); $i++) { |
|
150
|
|
|
$result .= "</li></ul>"; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// XXX Currently it outputs results according to my actual needs |
|
155
|
|
|
// for testing purpose. |
|
156
|
|
|
$result .= "<li>{$node['name']} (id: {$node['id']} left: {$node['lft']} right: {$node['rgt']})"; |
|
157
|
|
|
|
|
158
|
|
|
$depth = $node['depth']; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$result .= "</li></ul>"; |
|
162
|
|
|
$result .= "</ul>"; |
|
163
|
|
|
|
|
164
|
|
|
return $result; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.