## Apache Zeta Components Manual :: File Source for xhtml.php

## Source for file xhtml.php

Documentation is available at xhtml.php

1. <?php
2. /**
3. * File containing the ezcTreeVisitorXHTML class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
23. * @version //autogentag//
24. * @filesource
25. * @package Tree
26. */
28. /**
29. * An implementation of the ezcTreeVisitor interface that generates
30. * an XHTML representatation of a tree structure.
31. *
32. * <code>
33. * <?php
34. * $options = new ezcTreeVisitorXHTMLOptions;
35. * $options->xmlId = 'menu_tree';
36. * $visitor = new ezcTreeVisitorXHTML( $options );
37. * $tree->accept( $visitor );
38. * echo (string) $visitor; // print the plot
39. * ?>
40. * </code>
41. *
42. * Shows (something like):
43. * <code>
44. * </code>
45. *
46. * @package Tree
47. * @version //autogentag//
48. */
49. class ezcTreeVisitorXHTML implements ezcTreeVisitor
50. {
51. /**
52. * Holds all the edges of the graph.
53. *
54. * @var array(string=>array(string))
55. */
56. protected $edges = array();
58. /**
59. * Holds the root ID.
60. *
61. * @var string
62. */
63. protected $root = null;
65. /**
66. * Whether the XML ID has been set.
67. *
68. * @var bool
69. */
70. private $treeIdSet;
72. /**
73. * Holds the options for this class
74. *
75. * @var ezcTreeVisitorXHTMLOptions
76. */
77. public $options ;
79. /**
80. * Constructs a new ezcTreeVisitorXHTML visualizer.
81. *
82. * @param ezcTreeVisitorXHTMLOptions $options
83. */
84. public function __construct ( ezcTreeVisitorXHTMLOptions $options = null )
85. {
86. if ( $options === null )
87. {
88. $this-> options = new ezcTreeVisitorXHTMLOptions ;
89. }
90. else
91. {
92. $this-> options = $options;
93. }
94. }
96. /**
97. * Formats a node's data.
98. *
99. * It is just a simple method, that provide an easy way to change the way
100. * on how data is formatted when this class is extended. The data is passed
101. * in the $data argument, and whether the node should be highlighted is
102. * passed in the $highlight argument.
103. *
104. * @param mixed $data
105. * @param bool $highlight
106. * @return string
107. */
108. protected function formatData ( $data, $highlight )
109. {
110. $data = htmlspecialchars ( $data );
111. return $highlight ? "<div class=\"highlight\">$data</div>" : $data;
112. }
114. /**
115. * Visits the node and sets the the member variables according to the node
116. * type and contents.
117. *
118. * @param ezcTreeVisitable $visitable
119. * @return bool
120. */
121. public function visit ( ezcTreeVisitable $visitable )
122. {
123. if ( $visitable instanceof ezcTree )
124. {
125. }
127. if ( $visitable instanceof ezcTreeNode )
128. {
129. if ( $this-> root === null )
130. {
131. $this-> root = $visitable->id;
132. }
134. $parent = $visitable->fetchParent();
135. if ( $parent )
136. {
137. $this-> edges [$parent->id][] = array( $visitable->id, $visitable->data, $visitable->fetchPath() );
138. }
139. }
141. return true;
142. }
144. /**
145. * Formats the path to the node
146. *
147. * @param array $child
148. */
149. protected function formatPath ( $child )
150. {
151. $path = $child[2]->nodes;
152. if ( !$this-> options ->displayRootNode )
153. {
154. array_shift ( $path );
155. }
156. if ( $this-> options ->selectedNodeLink )
157. {
158. $slice = array_slice ( $path, -1 );
159. $path = htmlspecialchars ( $this-> options ->basePath . '/' . array_pop ( $slice ) );
160. }
161. else
162. {
163. $path = htmlspecialchars ( $this-> options ->basePath . '/' . join ( '/', $path ) );
164. }
165. return $path;
166. }
168. /**
169. * Loops over the children of the node with ID $id.
170. *
171. * This methods loops over all the node's children and adds the correct
172. * layout for each node depending on the state that is collected in the
173. * $level and $levelLast variables.
174. *
175. * @param string $id
176. * @param int $level
177. * @param array(int=>bool) $levelLast
178. *
179. * @return string
180. */
181. protected function doChildren ( $id, $level = 0, $levelLast = array() )
182. {
183. $text = '';
185. $children = $this-> edges [$id];
186. $numChildren = count ( $children );
188. if ( $numChildren > 0 )
189. {
190. $text .= str_repeat ( ' ', $level + 1 );
192. $idPart = '';
193. if ( !$this->treeIdSet )
194. {
195. $idPart = $this-> options ->xmlId ? " id=\"{$this-> options ->xmlId}\"

" : '';
- $this->treeIdSet = true;
- }
- $text .= "<ul{$idPart}>\n";
- foreach ( $children as $child )
- {
- $text .= str_repeat( ' ', $level + 2 );
- $path = $this-> formatPath ( $child );
- $data = $this-> formatData ( $child[1], in_array( $child[0], $this-> options ->highlightNodeIds ) );
- $linkStart = $linkEnd = '';
- if ( $this-> options ->addLinks )
- {
- $linkStart = "<a href=\"{$path}\">";
- $linkEnd = "</a>";
- }
- $highlightPart = '';
- if ( in_array( $child[0], $this-> options ->subtreeHighlightNodeIds ) )
- {
- $highlightPart = ' class="highlight"';
- }
- if ( isset( $this-> edges [$child[0]] ) )
- {
- $text .= "<li{$highlightPart}>{$linkStart}{$data}{$linkEnd}\n";
- $text .= $this-> doChildren ( $child[0], $level + 2, $levelLast );
- $text .= str_repeat( ' ', $level + 2 );
- $text .= "</li>\n";
- }
- else
- {
- $text .= "<li{$highlightPart}>{$linkStart}{$data}{$linkEnd}</li>\n";
- }
- }
- $text .= str_repeat( ' ', $level + 1 );
- $text .= "</ul>\n";
- }
- return $text;
- }
- /**
- * Returns the XHTML representation of a tree.
- *
- * @return string
- * @ignore
- */
- public function __toString()
- {
- $tree = '';
- $this->treeIdSet = false;
- if ( $this-> options ->displayRootNode )
- {
- $idPart = $this-> options ->xmlId ? " id=\"{$this-> options ->xmlId}\"" : '';
- $tree .= "<ul{$idPart}>\n";
- $tree .= "<li>{$this-> root }</li>\n";
- $this->treeIdSet = true;
- }
- $tree .= $this-> doChildren ( $this-> root );
- if ( $this-> options ->displayRootNode )
- {
- $tree .= "</ul>\n";
- }
- return $tree;
- }
- }
Documentation generated by phpDocumentor 1.4.3