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_SimpleTree { |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
function transform($source, $atnode = null) { |
42 |
|
|
|
43 |
|
|
$buffer = array(); |
44 |
|
|
|
45 |
|
|
foreach ($source as $key => $val) { |
46 |
|
|
//print "Key=$key, Val=$val<br/>"; |
47 |
|
|
$node = $this->mkNode($key, $val); |
48 |
|
|
array_push($buffer, $node); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
//print Dumper($container); |
52 |
|
|
return $buffer; |
53 |
|
|
|
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
function mkNode($name, $payload = null) { |
57 |
|
|
|
58 |
|
|
static $level; |
59 |
|
|
static $anchor; |
60 |
|
|
|
61 |
|
|
if (!is_array($anchor)) { $anchor = array(); } |
62 |
|
|
|
63 |
|
|
// TODO: propagate to this place inside some argument container |
64 |
|
|
// (make adjustable from View) |
65 |
|
|
$level_max = 3; |
66 |
|
|
//print "level: $level<br/>"; |
67 |
|
|
|
68 |
|
|
// build url to single node |
69 |
|
|
//$url = linkargs::topic($name); |
70 |
|
|
|
71 |
|
|
// FIXME: HACK !!! |
72 |
|
|
// Do we already have a Nirvana-API for such things? |
73 |
|
|
// Propagating this through the lift seems impossible... |
74 |
|
|
$parent_identifier = $_GET[ecdid]; |
75 |
|
|
|
76 |
|
|
/* |
77 |
|
|
$main_buf = array(); |
78 |
|
|
//for ($i = 0; $i < $level - 1; $i++) { |
79 |
|
|
for ($i = 0; $i <= 2; $i++) { |
80 |
|
|
if (!$anchor[$i]) { continue; } |
81 |
|
|
array_push($main_buf, $anchor[$i]); |
82 |
|
|
} |
83 |
|
|
$main = join('.', $main_buf); |
84 |
|
|
|
85 |
|
|
//$filter = join('.', $anchor); |
86 |
|
|
// FIXME: this is limited to three levels only |
87 |
|
|
//$appendix = $anchor[2] ? (':' . join('.', array( $anchor[2], $anchor[3] ))) : ''; |
88 |
|
|
$appendix = ''; |
89 |
|
|
$appendix_buf = array(); |
90 |
|
|
for ($i = 2; $i <= sizeof($anchor); $i++) { |
91 |
|
|
//for ($i = $level - 1; $i <= sizeof($anchor); $i++) { |
92 |
|
|
if (!$anchor[$i]) { continue; } |
93 |
|
|
array_push($appendix_buf, $anchor[$i]); |
94 |
|
|
} |
95 |
|
|
if ($appendix_buf) { |
96 |
|
|
$appendix = ':' . join('.', $appendix_buf); |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
//$filter = join('.', array( $anchor[0], $anchor[1] )) . $appendix; |
100 |
|
|
$filter = $main . $appendix; |
101 |
|
|
*/ |
102 |
|
|
|
103 |
|
|
$buf = array(); |
104 |
|
|
for ($i = 0; $i <= $level; $i = $i + 3) { |
105 |
|
|
$part = array(); |
106 |
|
|
for ($j = $i; $j <= $i + 3; $j++) { |
107 |
|
|
if (!$anchor[$j]) { continue; } |
108 |
|
|
array_push($part, $anchor[$j]); |
109 |
|
|
} |
110 |
|
|
array_push($buf, join('.', $part)); |
111 |
|
|
} |
112 |
|
|
$filter = join(':', $buf); |
113 |
|
|
|
114 |
|
|
// build propare link arguments to refer to a node as item to let it become editable |
115 |
|
|
$link_args = array( ecat => 'item', ecdm => $parent_identifier, ecdf => $filter ); |
116 |
|
|
|
117 |
|
|
// The identfier (by now the filename) transitions to a meta-argument here |
118 |
|
|
// to make room for the sub-nodename becoming the identifier. ($name!) |
119 |
|
|
$url = url::viewdatanode($name, $link_args); |
120 |
|
|
|
121 |
|
|
// vivify single node |
122 |
|
|
$node = array( name => $name, attributes => array( url => $url ) ); |
123 |
|
|
|
124 |
|
|
if (is_array($payload)) { |
125 |
|
|
// count the tree-level |
126 |
|
|
$level++; |
127 |
|
|
array_push($anchor, $name); |
128 |
|
|
if ($level <= $level_max) { |
129 |
|
|
$cnode = $this->transform($payload); |
130 |
|
|
//array_push($buffer, $cnode); |
131 |
|
|
//array_push($buffer, $this->mkContainer(array())); |
132 |
|
|
$node = $this->mkContainer($cnode, $node); |
133 |
|
|
} |
134 |
|
|
$level--; |
135 |
|
|
array_pop($anchor); |
136 |
|
|
} else { |
137 |
|
|
// TODO: !! |
138 |
|
|
$node[attributes][alt] = $name; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
return $node; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
function mkContainer($data = array(), $parent = null) { |
145 |
|
|
|
146 |
|
|
// autocreate parent if undef |
147 |
|
|
if (!$parent) { $parent = $this->mkNode('dummy'); } |
148 |
|
|
|
149 |
|
|
// sort keys of children alphabetically |
150 |
|
|
// TODO: add behavior to this |
151 |
|
|
asort($data); |
152 |
|
|
|
153 |
|
|
// vivify list of children |
154 |
|
|
$node = array( |
155 |
|
|
'children' => $data, |
156 |
|
|
); |
157 |
|
|
// merge them together and return result |
158 |
|
|
$result = php::array_join_merge($parent, $node); |
159 |
|
|
return $result; |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
function perform(&$data) { |
163 |
|
|
|
164 |
|
|
//print Dumper($data); |
165 |
|
|
|
166 |
|
|
//array_multisort($data); |
167 |
|
|
//asort($data); |
168 |
|
|
|
169 |
|
|
// V1 |
170 |
|
|
$data = $this->transform($data); |
171 |
|
|
$data = $this->mkContainer( $data, $this->mkNode('root') ); |
172 |
|
|
|
173 |
|
|
// V2 |
174 |
|
|
//$data = $this->mkNode('root', $data); |
175 |
|
|
|
176 |
|
|
return $data; |
177 |
|
|
|
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
?> |