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