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