1 |
<?php |
2 |
|
3 |
/** |
4 |
* Holds the Container class. |
5 |
* |
6 |
* $Id: ContainerClass.inc,v 1.15 2003/02/20 23:23:42 hemna Exp $ |
7 |
* |
8 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
9 |
* @package phpHtmlLib |
10 |
* |
11 |
* @copyright LGPL - See LICENCE |
12 |
* |
13 |
*/ |
14 |
|
15 |
|
16 |
/** |
17 |
* This class is nothing more then a |
18 |
* container widget. It lets you |
19 |
* push data into it, and it will |
20 |
* render each item indented properly |
21 |
* so it works with the rest of the libs. |
22 |
* |
23 |
* This is helpfull when you have a function |
24 |
* that wants to return multiple Tag Objects |
25 |
* or widgets. Just wrap them in this container |
26 |
* and they will all get rendered with the |
27 |
* current indentation level. |
28 |
* |
29 |
* Base Class for phpHtmlLib |
30 |
* |
31 |
* @link http://phphtmllib.sourceforge.net |
32 |
* |
33 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
34 |
* @package phpHtmlLib |
35 |
* |
36 |
*/ |
37 |
|
38 |
class Container { |
39 |
|
40 |
|
41 |
/** |
42 |
* Tag content as a stack |
43 |
* ie <span> content here </span> |
44 |
* @var array |
45 |
* @access private |
46 |
*/ |
47 |
var $_content = array(); |
48 |
|
49 |
/** |
50 |
* This keeps track of how much content |
51 |
* data has been pushed into the content |
52 |
* portion of the tag. |
53 |
* |
54 |
* @var int |
55 |
* @access private |
56 |
*/ |
57 |
var $_data_count = 0; |
58 |
|
59 |
/** |
60 |
* The flags that tell us |
61 |
* how to render the tag |
62 |
* its contents, and the close |
63 |
*/ |
64 |
var $_flags = _NEWLINEAFTERCONTENT; |
65 |
|
66 |
|
67 |
/** |
68 |
* The constructor. |
69 |
* |
70 |
* This lets you pass in data |
71 |
* that you want automatically |
72 |
* added to the container. This |
73 |
* works in the same manner as |
74 |
* the push() method. |
75 |
*/ |
76 |
function Container() { |
77 |
//We do the adding to the content var |
78 |
//here instead of calling $this->push() |
79 |
//to save some cpu cycles. |
80 |
$num = func_num_args(); |
81 |
for ($i=0;$i<$num;$i++) { |
82 |
$arg = func_get_arg($i); |
83 |
array_push($this->_content, $arg); |
84 |
} |
85 |
$this->_data_count += $num; |
86 |
|
87 |
//set the flag bitmask |
88 |
$this->_set_flags(); |
89 |
} |
90 |
|
91 |
|
92 |
/** |
93 |
* This function is compatible with the |
94 |
* rest of the phpHtmllib API spec. |
95 |
* It just walks through each of the |
96 |
* class' data and renders it with the |
97 |
* appropriate indentation. |
98 |
* |
99 |
* @param int - the indentation level for |
100 |
* the container. |
101 |
* @param int - the output debug flag to |
102 |
* maintain compatibility w/ the API. |
103 |
* |
104 |
* @return string the raw html output. |
105 |
*/ |
106 |
function render($indent_level=0, $output_debug=0) { |
107 |
$html = ''; |
108 |
|
109 |
for ($x=0; $x<=$this->_data_count-1; $x++) { |
110 |
$item = &$this->_content[$x]; |
111 |
if (method_exists($item, "render") ) { |
112 |
if (($this->_flags & _COLLAPSE) && method_exists($item, "set_collapse")) { |
113 |
$item->set_collapse(TRUE, FALSE); |
114 |
} |
115 |
$html .= $item->render($indent_level, $output_debug); |
116 |
} else { |
117 |
if ($this->_flags & _COLLAPSE) { |
118 |
$html .= $item; |
119 |
} else { |
120 |
$indent = $this->_render_indent($indent_level, $output_debug); |
121 |
$html .= $indent.$item; |
122 |
if ($this->_flags & _NEWLINEAFTERCONTENT) { |
123 |
$html .= "\n"; |
124 |
} |
125 |
} |
126 |
} |
127 |
} |
128 |
if ($this->_flags & _COLLAPSE) { |
129 |
$indent = $this->_render_indent($indent_level, $output_debug); |
130 |
if ($this->_flags & _NEWLINEAFTERCONTENT) { |
131 |
if ($output_debug) { |
132 |
$html = $indent . $html . "<br>\n"; |
133 |
} else { |
134 |
$html = $indent . $html . "\n"; |
135 |
} |
136 |
} else { |
137 |
$html = $indent . $html; |
138 |
} |
139 |
|
140 |
} |
141 |
return $html; |
142 |
} |
143 |
|
144 |
|
145 |
|
146 |
/***************************/ |
147 |
/* DATA specific functions */ |
148 |
/***************************/ |
149 |
|
150 |
/** |
151 |
* Same as add(). |
152 |
* NOTE: only exists for 1.1.x compatibility |
153 |
* |
154 |
* @deprecated |
155 |
* @param mixed $content - either string, or tag object. |
156 |
* @access public |
157 |
*/ |
158 |
function push( ) { |
159 |
$args = func_get_args(); |
160 |
call_user_func_array( array(&$this, "add"), $args); |
161 |
} |
162 |
|
163 |
/** |
164 |
* add content onto content stack |
165 |
* |
166 |
* adds content to tag as a FIFO. |
167 |
* You can have n number of parameters. |
168 |
* each one will get added in succession to the content. |
169 |
* @param mixed $content - either string, or tag object. |
170 |
* @access public |
171 |
*/ |
172 |
function add( ) { |
173 |
$args = func_get_args(); |
174 |
$num = 0; |
175 |
foreach( $args as $arg ) { |
176 |
$this->_content[] = $arg; |
177 |
$num++; |
178 |
} |
179 |
//keep track of how much data we have added. |
180 |
$this->_data_count += $num; |
181 |
} |
182 |
|
183 |
|
184 |
/** |
185 |
* Same as add_reference |
186 |
* NOTE : only exists for compatibility with 1.1.x |
187 |
* |
188 |
* @deprecated |
189 |
* |
190 |
* @access public |
191 |
* @param mixed - a reference to some variable. |
192 |
*/ |
193 |
function push_reference( &$content ) { |
194 |
$this->add_reference( $content ); |
195 |
} |
196 |
|
197 |
/** |
198 |
* Add content onto content stack |
199 |
* so you can change the item later. |
200 |
* |
201 |
* adds content to tag as a FIFO |
202 |
* You can only add 1 element at a time, and |
203 |
* it will be added as a reference. So you can't do |
204 |
* push_reference("something");, since "something" is a |
205 |
* static. |
206 |
* |
207 |
* @access public |
208 |
* |
209 |
* @param mixed $content - either string, or tag object. |
210 |
* the tag object gets stored as a |
211 |
* reference to the original, so you |
212 |
* can push it, then modify it later. |
213 |
*/ |
214 |
function add_reference( &$content ) { |
215 |
$this->_content[] = &$content; |
216 |
$this->_data_count++; |
217 |
} |
218 |
|
219 |
|
220 |
/** |
221 |
* destroy existing content and start with new content. |
222 |
* |
223 |
* @access public |
224 |
* @param mixed $content can be tag object, or raw (string). |
225 |
*/ |
226 |
function reset_content( ) { |
227 |
$this->_content = array(); |
228 |
$this->_data_count = 0; |
229 |
$args = func_get_args(); |
230 |
call_user_func_array( array(&$this, "add"), $args); |
231 |
} |
232 |
|
233 |
/** |
234 |
* counts the number of content objects |
235 |
* |
236 |
* @access public |
237 |
* @return int |
238 |
*/ |
239 |
function count_content( ) { |
240 |
return $this->_data_count; |
241 |
} |
242 |
|
243 |
|
244 |
/** |
245 |
* This method is used to set the bitmask |
246 |
* flags for this tag. It tells the |
247 |
* class how to render the tag. |
248 |
* |
249 |
* NOTE: the child class can override this |
250 |
* to set the options |
251 |
*/ |
252 |
function _set_flags() { |
253 |
$this->_flags = _NEWLINEAFTERCONTENT | _INDENT; |
254 |
} |
255 |
|
256 |
|
257 |
/** |
258 |
* function to set the indent flag |
259 |
* |
260 |
* @access public |
261 |
* @param boolean $flag TRUE or FALSE |
262 |
*/ |
263 |
function set_indent_flag( $flag ) { |
264 |
if ($flag) { |
265 |
$this->_flags |= _INDENT; |
266 |
} else { |
267 |
$this->_flags &= ~_INDENT; |
268 |
} |
269 |
} |
270 |
|
271 |
|
272 |
/** |
273 |
* This flag gets the current value |
274 |
* of the indent flag |
275 |
* |
276 |
* @access public |
277 |
* |
278 |
* @return boolean |
279 |
*/ |
280 |
function get_indent_flag() { |
281 |
return $this->_flags & _INDENT; |
282 |
} |
283 |
|
284 |
|
285 |
/** |
286 |
* This function turns on the collapse flag |
287 |
* |
288 |
* @access public |
289 |
* |
290 |
* @param boolean - the collapse flag |
291 |
* @param boolean - the indent flag |
292 |
* DEFAULT: TRUE; |
293 |
*/ |
294 |
function set_collapse($collapse=TRUE, $indent=TRUE) { |
295 |
if ($collapse) { |
296 |
$this->_flags |= _COLLAPSE; |
297 |
} else { |
298 |
$this->_flags &= ~_COLLAPSE; |
299 |
} |
300 |
|
301 |
if (!$indent) { |
302 |
$this->set_indent_flag($indent); |
303 |
$this->_flags &= ~_NEWLINEAFTERCONTENT; |
304 |
} |
305 |
} |
306 |
|
307 |
|
308 |
|
309 |
/** |
310 |
* returns leading indent for tag |
311 |
* |
312 |
* @access private |
313 |
* |
314 |
* @param int the indentation level for this tag. |
315 |
* @return string |
316 |
*/ |
317 |
function _render_indent($indent_level, $debug_flag=0) { |
318 |
$indent = ""; |
319 |
if ( $debug_flag && $indent_level > 0) { |
320 |
$indent_level *=2; |
321 |
} |
322 |
if ( ($this->_flags & _INDENT) && $indent_level > 0) { |
323 |
$indent = str_repeat(_INDENT_STR, $indent_level); |
324 |
} |
325 |
if ( $debug_flag && $indent_level > 0) { |
326 |
$indent = str_replace(_INDENT_STR, " ", $indent); |
327 |
} |
328 |
return $indent; |
329 |
} |
330 |
} |
331 |
?> |