1 |
joko |
1.1 |
<? |
2 |
|
|
/** |
3 |
|
|
* This file contains a Data::Lift actor component. |
4 |
|
|
* |
5 |
|
|
* @author Andreas Motl <andreas.motl@ilo.de> |
6 |
|
|
* @package org.netfrag.glib |
7 |
|
|
* @name Data::Lift::hash::topic::Tree |
8 |
|
|
* |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
/** |
12 |
|
|
* Cvs-Log: |
13 |
|
|
* |
14 |
|
|
* $Id: Generic.php,v 1.18 2003/04/11 00:55:49 joko Exp $ |
15 |
|
|
* |
16 |
|
|
* $Log: Generic.php,v $ |
17 |
|
|
* |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
/** |
21 |
|
|
* Data::Lift::hash::topic::Tree |
22 |
|
|
* |
23 |
|
|
* |
24 |
|
|
* @author Andreas Motl <andreas.motl@ilo.de> |
25 |
|
|
* @copyright (c) 2003 - All Rights reserved. |
26 |
|
|
* @license GNU LGPL (GNU Lesser General Public License) |
27 |
|
|
* |
28 |
|
|
* @link http://www.netfrag.org/~joko/ |
29 |
|
|
* @link http://www.gnu.org/licenses/lgpl.txt |
30 |
|
|
* |
31 |
|
|
* @package org.netfrag.glib |
32 |
|
|
* @subpackage DataLift |
33 |
|
|
* @name Data::Lift::hash::topic::Tree |
34 |
|
|
* |
35 |
|
|
* @link http://cvs.netfrag.org/php/libs/org.netfrag.glib |
36 |
|
|
* |
37 |
|
|
*/ |
38 |
|
|
class Data_Lift_hash_topic_EasyTree { |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
function transform($source, $atnode = null) { |
42 |
|
|
|
43 |
|
|
//print Dumper($source); |
44 |
|
|
|
45 |
|
|
$buffer = array(); |
46 |
|
|
|
47 |
|
|
foreach ($source as $key => $val) { |
48 |
|
|
|
49 |
|
|
//print "Key=$key, Val=" . Dumper($val) . "<br/>"; |
50 |
|
|
|
51 |
|
|
//print Dumper($item); |
52 |
|
|
//continue; |
53 |
|
|
|
54 |
|
|
// V1 |
55 |
|
|
$node = $this->mkNode($key, $val); |
56 |
|
|
|
57 |
|
|
/* |
58 |
|
|
// V2 - 2003-04-16 |
59 |
|
|
$name = $key; |
60 |
|
|
$caption = $name; |
61 |
|
|
if (!is_array($val)) { $caption = "$key: $val"; } |
62 |
|
|
$node = $this->mkNode($name, $val); |
63 |
|
|
*/ |
64 |
|
|
|
65 |
|
|
/* |
66 |
|
|
// container? |
67 |
|
|
if ($val[attrib][name]) { |
68 |
|
|
//$caption = $val[attrib][name]; |
69 |
|
|
//$node = $this->mkContainer($node, $node); |
70 |
|
|
} |
71 |
|
|
*/ |
72 |
|
|
|
73 |
|
|
array_push($buffer, $node); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
//print Dumper($container); |
77 |
|
|
return $buffer; |
78 |
|
|
|
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
function mkNode($name, $payload = null) { |
82 |
|
|
|
83 |
|
|
static $level; |
84 |
|
|
static $anchor; |
85 |
|
|
|
86 |
|
|
if (!is_array($anchor)) { $anchor = array(); } |
87 |
|
|
//if (!$caption) { $caption = $name; } |
88 |
|
|
|
89 |
|
|
// TODO: propagate to this place inside some argument container |
90 |
|
|
// (make adjustable from View) |
91 |
|
|
$level_max = 5; |
92 |
|
|
//print "level: $level<br/>"; |
93 |
|
|
|
94 |
|
|
// build url to single node |
95 |
|
|
//$url = linkargs::topic($name); |
96 |
|
|
|
97 |
|
|
// FIXME: HACK !!! |
98 |
|
|
// Do we already have a Nirvana-API for such things? |
99 |
|
|
// Propagating this through the lift seems impossible... |
100 |
|
|
$parent_identifier = $_GET[ecdid]; |
101 |
|
|
|
102 |
|
|
/* |
103 |
|
|
$main_buf = array(); |
104 |
|
|
//for ($i = 0; $i < $level - 1; $i++) { |
105 |
|
|
for ($i = 0; $i <= 2; $i++) { |
106 |
|
|
if (!$anchor[$i]) { continue; } |
107 |
|
|
array_push($main_buf, $anchor[$i]); |
108 |
|
|
} |
109 |
|
|
$main = join('.', $main_buf); |
110 |
|
|
|
111 |
|
|
//$filter = join('.', $anchor); |
112 |
|
|
// FIXME: this is limited to three levels only |
113 |
|
|
//$appendix = $anchor[2] ? (':' . join('.', array( $anchor[2], $anchor[3] ))) : ''; |
114 |
|
|
$appendix = ''; |
115 |
|
|
$appendix_buf = array(); |
116 |
|
|
for ($i = 2; $i <= sizeof($anchor); $i++) { |
117 |
|
|
//for ($i = $level - 1; $i <= sizeof($anchor); $i++) { |
118 |
|
|
if (!$anchor[$i]) { continue; } |
119 |
|
|
array_push($appendix_buf, $anchor[$i]); |
120 |
|
|
} |
121 |
|
|
if ($appendix_buf) { |
122 |
|
|
$appendix = ':' . join('.', $appendix_buf); |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
//$filter = join('.', array( $anchor[0], $anchor[1] )) . $appendix; |
126 |
|
|
$filter = $main . $appendix; |
127 |
|
|
*/ |
128 |
|
|
|
129 |
|
|
$buf = array(); |
130 |
|
|
for ($i = 0; $i <= $level; $i = $i + 3) { |
131 |
|
|
$part = array(); |
132 |
|
|
for ($j = $i; $j <= $i + 3; $j++) { |
133 |
|
|
if (!$anchor[$j]) { continue; } |
134 |
|
|
array_push($part, $anchor[$j]); |
135 |
|
|
} |
136 |
|
|
array_push($buf, join('.', $part)); |
137 |
|
|
} |
138 |
|
|
$filter = join(':', $buf); |
139 |
|
|
|
140 |
|
|
// build proper link arguments to refer to a node as item to let it become editable |
141 |
|
|
$link_args = array( ecat => 'item', ecdm => $parent_identifier, ecdf => $filter ); |
142 |
|
|
|
143 |
|
|
// The identfier (by now the filename) transitions to a meta-argument here |
144 |
|
|
// to make room for the sub-nodename becoming the identifier. ($name!) |
145 |
|
|
$url = url::viewdatanode($name, $link_args); |
146 |
|
|
|
147 |
|
|
// vivify single node |
148 |
|
|
$node = array( name => $name, attributes => array( url => $url ) ); |
149 |
|
|
|
150 |
|
|
//print "<hr/>" . Dumper($payload); |
151 |
|
|
|
152 |
|
|
if (is_array($payload)) { |
153 |
|
|
// count the tree-level |
154 |
|
|
$level++; |
155 |
|
|
array_push($anchor, $name); |
156 |
|
|
if ($level <= $level_max) { |
157 |
|
|
$childnode = $this->transform($payload); |
158 |
|
|
//array_push($buffer, $cnode); |
159 |
|
|
//array_push($buffer, $this->mkContainer(array())); |
160 |
|
|
$node = $this->mkContainer($childnode, $node); |
161 |
|
|
} |
162 |
|
|
$level--; |
163 |
|
|
array_pop($anchor); |
164 |
|
|
|
165 |
|
|
} else { |
166 |
|
|
// TODO: !! |
167 |
|
|
//$node[attributes][alt] = $name; |
168 |
|
|
//print "no array: " . Dumper($payload) . "<br/>"; |
169 |
|
|
$node[name] = "$name: $payload"; |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
return $node; |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
function mkContainer($data = array(), $parent = null) { |
176 |
|
|
|
177 |
|
|
//print "<hr/>" . Dumper($data); |
178 |
|
|
|
179 |
|
|
// autocreate parent if undef |
180 |
|
|
if (!$parent) { $parent = $this->mkNode('dummy'); } |
181 |
|
|
|
182 |
|
|
// sort keys of children alphabetically |
183 |
|
|
// TODO: add behavior to this |
184 |
|
|
//asort($data); |
185 |
|
|
|
186 |
|
|
// vivify list of children |
187 |
|
|
$node = array( |
188 |
|
|
'children' => $data, |
189 |
|
|
); |
190 |
|
|
|
191 |
|
|
//print "<hr/>"; |
192 |
|
|
//print "parent: " . Dumper($parent) . "<br/>"; |
193 |
|
|
//print "node: " . Dumper($node) . "<br/>"; |
194 |
|
|
|
195 |
|
|
// merge them together and return result |
196 |
|
|
$result = php::array_join_merge($parent, $node); |
197 |
|
|
|
198 |
|
|
//print "<hr/>"; |
199 |
|
|
//print "result: " . Dumper($result); |
200 |
|
|
|
201 |
|
|
return $result; |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
function perform(&$data) { |
205 |
|
|
|
206 |
|
|
//print Dumper($data); |
207 |
|
|
|
208 |
|
|
//array_multisort($data); |
209 |
|
|
//asort($data); |
210 |
|
|
|
211 |
|
|
$caption = 'root'; |
212 |
|
|
if ($data[name]) { |
213 |
|
|
$caption = $data[name]; |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
// V1 |
217 |
|
|
$data = $this->transform($data); |
218 |
|
|
$data = $this->mkContainer( $data, $this->mkNode($caption) ); |
219 |
|
|
|
220 |
|
|
// V2 |
221 |
|
|
//$data = $this->mkNode('root', $data); |
222 |
|
|
|
223 |
|
|
return $data; |
224 |
|
|
|
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
?> |