1 |
<?php |
2 |
|
3 |
/** |
4 |
* This contains the HTMLPageClass widget |
5 |
* |
6 |
* $Id: HTMLPageClass.inc,v 1.19 2002/11/22 21:51:08 hemna Exp $ |
7 |
* |
8 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
9 |
* @package phpHtmlLib |
10 |
* |
11 |
*/ |
12 |
|
13 |
/** |
14 |
* Get all the required files for this class |
15 |
*/ |
16 |
require_once($phphtmllib."/HTMLTagClass.inc"); |
17 |
|
18 |
|
19 |
/** |
20 |
* class the constructs and renders an entire |
21 |
* HTML/XHTML document. |
22 |
* |
23 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
24 |
* @package phpHtmlLib |
25 |
*/ |
26 |
class HTMLPageClass { |
27 |
|
28 |
/** |
29 |
* HEADtag object that holds all content |
30 |
* for the head. |
31 |
* @var object |
32 |
* @access private |
33 |
*/ |
34 |
var $_head = NULL; |
35 |
|
36 |
/** |
37 |
* TITLEtag object that holds the title |
38 |
* of the page. |
39 |
* @var object |
40 |
* @access private |
41 |
*/ |
42 |
var $_title = NULL; |
43 |
|
44 |
/** |
45 |
* SCRIPTtag object that holds javascript |
46 |
* code for the head tag. |
47 |
* @var object |
48 |
* @access private |
49 |
*/ |
50 |
var $_head_js = NULL; |
51 |
|
52 |
/** |
53 |
* STYLEtag object that holds css code |
54 |
* for the head. |
55 |
* @var object |
56 |
* @access private |
57 |
*/ |
58 |
var $_head_style = NULL; |
59 |
|
60 |
/** |
61 |
* holds an array of LINKtag objects |
62 |
* in the head tag that reference an |
63 |
* external stylesheet file. |
64 |
* @var array. |
65 |
* @access private |
66 |
*/ |
67 |
var $_head_css_link_arr = array(); |
68 |
|
69 |
/** |
70 |
* holds an array of SCRIPTtag objects |
71 |
* in the head tag that reference an |
72 |
* an extern javascript file. |
73 |
* @var array |
74 |
* @access private |
75 |
*/ |
76 |
var $_head_js_link_arr = array(); |
77 |
|
78 |
/** |
79 |
* character set to be used in this |
80 |
* page. This gets automatically |
81 |
* placed in the Content-Type |
82 |
* META tag. |
83 |
* @var string |
84 |
* @access private |
85 |
*/ |
86 |
var $_charset = "iso-8859-1"; |
87 |
|
88 |
/** |
89 |
* BODYtag object that holds all content |
90 |
* for the body tag. |
91 |
* @var object |
92 |
* @access private |
93 |
*/ |
94 |
var $_body = NULL; |
95 |
|
96 |
/** |
97 |
* DOCTYPEag object that sets the document |
98 |
* type. This gets rendered prior to <html> |
99 |
* @var object |
100 |
* @access private |
101 |
*/ |
102 |
var $_doctype = NULL; |
103 |
|
104 |
/** |
105 |
* flag to tell the class to try and |
106 |
* change the http headers to output |
107 |
* document of type text, instead of |
108 |
* html. This is helpfull for debugging. |
109 |
* @var boolean |
110 |
* @access private |
111 |
*/ |
112 |
var $_text_debug = FALSE; |
113 |
|
114 |
/** |
115 |
* This holds a FRAMESETtag object. |
116 |
* This is used if the person is trying |
117 |
* to render a frameset page. |
118 |
* |
119 |
* @var FRAMESETtag object. |
120 |
* @access private |
121 |
*/ |
122 |
var $_frameset = NULL; |
123 |
|
124 |
|
125 |
/** |
126 |
* This holds the text used to display |
127 |
* a body content when a browser doesn't |
128 |
* support framesets |
129 |
* |
130 |
* @var string |
131 |
* @access private |
132 |
*/ |
133 |
var $_noframes_text = "Sorry, your browser does not support frames"; |
134 |
|
135 |
|
136 |
/** |
137 |
* This holds the attributes for the |
138 |
* <html> tag. |
139 |
* |
140 |
* @var array |
141 |
*/ |
142 |
var $_html_attributes = array(); |
143 |
|
144 |
/** |
145 |
* keeps track of which widgets we have |
146 |
* automatically pulled in css for |
147 |
* |
148 |
* @var array |
149 |
*/ |
150 |
var $_widget_css_auto = array(); |
151 |
|
152 |
/** |
153 |
* keeps track of which widgets we have |
154 |
* automatically pulled in js for |
155 |
* |
156 |
* @var array |
157 |
*/ |
158 |
var $_widget_js_auto = array(); |
159 |
|
160 |
|
161 |
/** |
162 |
* Holds the value of the indent |
163 |
* style the user wants to render |
164 |
* the page w/ |
165 |
* |
166 |
* @var int |
167 |
*/ |
168 |
var $_indent_style = 0; |
169 |
|
170 |
|
171 |
|
172 |
/** |
173 |
* Class Constructor |
174 |
* @param mixed - $title Title string or TITLEtag object for the page. |
175 |
* @param string - one of 3 types of html to render. Setting this will |
176 |
* make the object declare the gobal define which tells |
177 |
* all of the tag objects what type of html tags to render. |
178 |
* some tags support special features. such as the <IMG> |
179 |
* tag. If xhtml is selected, the the IMGtag object and all |
180 |
* utility functions will not render "border=0" as a default |
181 |
* attribute, since this is not proper xhtml. |
182 |
* "html" - HTML 4.0 (default) |
183 |
* "xhtml_transitional" - render xhtml instead of html |
184 |
* - doctype is XHTML transitional. |
185 |
* "xhtml_strict" - render xhtml instead of html 4.0. |
186 |
* - doctype is XHTML strict. |
187 |
* @param int - one of 2 types. INDENT_NICE or INDENT_LEFT_JUSTIFY |
188 |
* This tells the page how to render the indenting of the |
189 |
* output. By default it is set to INDENT_NICE, which nicely |
190 |
* indents each nested tag. You can have all tags rendered |
191 |
* left justified (smaller size in output) by using |
192 |
* INDENT_LEFT_JUSTIFY |
193 |
* |
194 |
* @access public |
195 |
*/ |
196 |
function HTMLPageClass($title=NULL, $html_type=HTML, $indent_style=INDENT_NICE) { |
197 |
|
198 |
switch ($html_type) { |
199 |
case HTML: |
200 |
default: |
201 |
$this->build_doctype("-//W3C//DTD HTML 4.01 Transitional//EN"); |
202 |
define("HTML_RENDER_TYPE", HTML); |
203 |
break; |
204 |
|
205 |
case XHTML_STRICT: |
206 |
$this->build_doctype("-//W3C//DTD XHTML 1.0 Strict//EN", |
207 |
"DTD/xhtml1-strict.dtd"); |
208 |
define("HTML_RENDER_TYPE", XHTML_STRICT); |
209 |
$this->set_html_attributes( array( "xmlns" => "http://www.w3.org/1999/xhtml", |
210 |
"xml:lang" => "en", |
211 |
"lang" => "en") ); |
212 |
break; |
213 |
|
214 |
case XHTML: |
215 |
case XHTML_TRANSITIONAL: |
216 |
$this->build_doctype("-//W3C//DTD XHTML 1.0 Transitional//EN", |
217 |
"DTD/xhtml1-transitional.dtd"); |
218 |
define("HTML_RENDER_TYPE", XHTML); |
219 |
$this->set_html_attributes( array( "xmlns" => "http://www.w3.org/1999/xhtml", |
220 |
"xml:lang" => "en", |
221 |
"lang" => "en") ); |
222 |
break; |
223 |
|
224 |
//What else needs to be done to properly support |
225 |
//XHTML frameset? TODO LIST for 1.1 |
226 |
case XHTML_FRAMESET: |
227 |
$this->build_doctype("-//W3C//DTD XHTML 1.0 Frameset//EN", |
228 |
"DTD/xhtml1-frameset.dtd"); |
229 |
define("HTML_RENDER_TYPE", XHTML_FRAMESET); |
230 |
break; |
231 |
} |
232 |
|
233 |
$this->_head = new HEADtag; |
234 |
$this->_head_js = html_script(); |
235 |
$this->_head_style = html_style(); |
236 |
$this->_create_body(); |
237 |
if ($title != NULL) { |
238 |
$this->set_title( $title ); |
239 |
} |
240 |
|
241 |
$this->_indent_style = $indent_style; |
242 |
} |
243 |
|
244 |
//************************************************** |
245 |
//* HEAD tag related functions |
246 |
//************************************************** |
247 |
|
248 |
|
249 |
/** |
250 |
* this adds content to the head tag |
251 |
* of the page |
252 |
* |
253 |
* @param mixed - any content to add |
254 |
*/ |
255 |
function add_head_content( ) { |
256 |
$num_args = func_num_args(); |
257 |
for ($i=0;$i<$num_args;$i++) { |
258 |
$this->_head->add(func_get_arg($i)); |
259 |
} |
260 |
} |
261 |
|
262 |
/** |
263 |
* Same ass add_head_content() |
264 |
* |
265 |
* @deprecated - use add_head_content(); |
266 |
*/ |
267 |
function push_head_content( ) { |
268 |
$args = func_get_args(); |
269 |
call_user_func_array( array(&$this, "add_head_content"), $args); |
270 |
} |
271 |
|
272 |
|
273 |
/** |
274 |
* adds raw javascript to the head which |
275 |
* will automatically get wrapped in a |
276 |
* <script language="JavaScript"> tag. |
277 |
* @param mixed $content - raw javascript code to add to the head |
278 |
*/ |
279 |
function add_head_js( $content ) { |
280 |
$this->_head_js->push( $content ); |
281 |
} |
282 |
|
283 |
/** |
284 |
* Same ass add_head_js() |
285 |
* |
286 |
* @deprecated - use add_head_js(); |
287 |
*/ |
288 |
function push_head_js( $content ) { |
289 |
$this->add_head_js( $content ); |
290 |
} |
291 |
|
292 |
/** |
293 |
* this function adds raw css to the |
294 |
* <head> tag. It will automatically |
295 |
* be wrapped in a <style type="text/css"> |
296 |
* |
297 |
* @param string - the raw css |
298 |
*/ |
299 |
function add_head_css( $css ) { |
300 |
$this->_head_style->push( $css ); |
301 |
} |
302 |
|
303 |
/** |
304 |
* set the title of the page output. |
305 |
* @param mixed $title - the title of the html page |
306 |
* can be TITLEtag object. |
307 |
* |
308 |
*/ |
309 |
function set_title( $title ) { |
310 |
if (is_object($title)) { |
311 |
if ($title->_tag == "title") { |
312 |
$this->_title = $title; |
313 |
} else { |
314 |
//they did something funky here. |
315 |
return -1; |
316 |
} |
317 |
} else { |
318 |
$this->_title = html_title( $title ); |
319 |
} |
320 |
} |
321 |
|
322 |
/** |
323 |
* pushes a css external reference to the head area |
324 |
* @param mixed $link - link tag object or $url for a css. |
325 |
*/ |
326 |
function add_css_link( $link ) { |
327 |
if (is_object($link)) { |
328 |
$css = $link; |
329 |
} else { |
330 |
$css = html_link($link, "stylesheet", "text/css"); |
331 |
} |
332 |
$this->_head_css_link_arr[] = $css; |
333 |
} |
334 |
|
335 |
/** |
336 |
* Same ass add_css_link() |
337 |
* |
338 |
* @deprecated - use add_css_link(); |
339 |
*/ |
340 |
function push_css_link( $link ) { |
341 |
$this->add_css_link( $link ); |
342 |
|
343 |
} |
344 |
|
345 |
/** |
346 |
* pushes an link to an externally referenced javascript |
347 |
* file, which will get rendered in the head. |
348 |
* @param mixed $link - script tag object or $url of .js file. |
349 |
* |
350 |
*/ |
351 |
function push_js_link( $link ) { |
352 |
if (is_object($link)) { |
353 |
$js = $link; |
354 |
} else { |
355 |
$js = html_script($link); |
356 |
} |
357 |
$this->_head_js_link_arr[] = $js; |
358 |
} |
359 |
|
360 |
/** |
361 |
* Automatically set a page meta tag refresh |
362 |
* @param int $time - time in seconds to refresh |
363 |
* @param string $url - the url to go to. |
364 |
*/ |
365 |
function set_refresh( $time, $url=NULL ) { |
366 |
if ($url) { |
367 |
$time .= ";url=$url"; |
368 |
} |
369 |
$this->push_head_content( html_meta($time, "refresh") ); |
370 |
} |
371 |
|
372 |
/** |
373 |
* set the character set |
374 |
* @param string $charset - the charset for the meta tag |
375 |
* |
376 |
*/ |
377 |
function set_charset( $charset ) { |
378 |
$this->_charset = $charset; |
379 |
} |
380 |
|
381 |
/** |
382 |
* This function is used to set the FRAMSETtag |
383 |
* object for this page. This automatically |
384 |
* sets the output for this page object to |
385 |
* be a frameset. |
386 |
* |
387 |
* @param FRAMESETtag object - $frameset |
388 |
*/ |
389 |
function set_frameset( $frameset ) { |
390 |
if (is_object($frameset)) { |
391 |
if ($frameset->get_tag() == "frameset") { |
392 |
$this->_frameset = $frameset; |
393 |
} |
394 |
} |
395 |
} |
396 |
|
397 |
|
398 |
/** |
399 |
* This function returns the |
400 |
* attributes to be used for the |
401 |
* <html> tag. |
402 |
* |
403 |
* @return array(); |
404 |
*/ |
405 |
function get_html_attributes() { |
406 |
return $this->_html_attributes; |
407 |
} |
408 |
|
409 |
/** |
410 |
* This function sets the attributes |
411 |
* for the <html> tag |
412 |
* |
413 |
* @param array - the name=>value |
414 |
* pair for the |
415 |
* attributes |
416 |
*/ |
417 |
function set_html_attributes( $attributes ) { |
418 |
$this->_html_attributes = $attributes; |
419 |
} |
420 |
|
421 |
|
422 |
/** |
423 |
* this builds the content type meta tag. |
424 |
* |
425 |
*/ |
426 |
function _build_content_type_tag() { |
427 |
$content_type = "text/html; charset=" . $this->_charset; |
428 |
return html_meta($content_type, "Content-Type"); |
429 |
} |
430 |
|
431 |
//************************************************** |
432 |
//* BODY tag related functions |
433 |
//************************************************** |
434 |
|
435 |
|
436 |
/** |
437 |
* This function adds content to the <body> |
438 |
* area of the page. |
439 |
* |
440 |
* @param mixed - any # of parameters |
441 |
*/ |
442 |
function add() { |
443 |
$num_args = func_num_args(); |
444 |
for ($i=0;$i<$num_args;$i++) { |
445 |
$arg = func_get_arg($i); |
446 |
//see if the arg is an object |
447 |
//and a child of BaseWidget. |
448 |
if (is_object($arg) && is_subclass_of( $arg, "basewidget")) { |
449 |
//see if they have an js they need to include |
450 |
$js = ""; |
451 |
$js = $arg->get_javascript(); |
452 |
$class_name = get_class($arg); |
453 |
if ($js != "" && !$this->_widget_js_auto[$class_name]) { |
454 |
$this->add_head_js( str_replace(chr(9),'', $js) ); |
455 |
$this->_widget_js_auto[$class_name] = TRUE; |
456 |
} |
457 |
} |
458 |
$this->_body->add(func_get_arg($i)); |
459 |
} |
460 |
} |
461 |
|
462 |
/** |
463 |
* Same as add() |
464 |
* |
465 |
* @deprecated - use add() |
466 |
* |
467 |
*/ |
468 |
function push( ) { |
469 |
$args = func_get_args(); |
470 |
call_user_func_array( array(&$this, "add"), $args); |
471 |
} |
472 |
|
473 |
/** |
474 |
* Adds the content reference to the <body> |
475 |
* tag for later use. |
476 |
* |
477 |
* @param mixed $content - content to add |
478 |
*/ |
479 |
function add_reference( &$content ) { |
480 |
$this->_body->push_reference( $content ); |
481 |
} |
482 |
|
483 |
/** |
484 |
* Same as add() |
485 |
* |
486 |
* @deprecated - use add() |
487 |
* |
488 |
*/ |
489 |
function push_reference( &$content ) { |
490 |
$this->add_reference( $content ); |
491 |
} |
492 |
|
493 |
/** |
494 |
* This is responsible for creating the |
495 |
* BODYtag object. We only will |
496 |
* create a new $this->_body if it doesn't |
497 |
* already exist. |
498 |
* |
499 |
*/ |
500 |
function _create_body() { |
501 |
if ($this->_body == NULL) { |
502 |
$this->_body = new BODYtag; |
503 |
$this->_body->add( "\n" ); |
504 |
} |
505 |
} |
506 |
|
507 |
/** |
508 |
* set attributes of body tag |
509 |
* @param array $attributes the name=>value pairs |
510 |
* |
511 |
*/ |
512 |
function set_body_attributes( $attributes ) { |
513 |
if (!is_object( $this->_body )) { |
514 |
$this->_create_body(); |
515 |
} |
516 |
$this->_body->set_tag_attributes( $attributes ); |
517 |
} |
518 |
|
519 |
/** |
520 |
* This function is used to build the DOCTYPE |
521 |
* tag for the page. It will automatically |
522 |
* create a DOCTYPE with a document_element of "html" |
523 |
* and a source of "PUBLIC" |
524 |
* |
525 |
* @param string - link1 |
526 |
* @param string - link2 |
527 |
*/ |
528 |
function build_doctype($link1, $link2=NULL) { |
529 |
$this->_doctype = xml_doctype("html", "PUBLIC", $link1, $link2); |
530 |
} |
531 |
|
532 |
//************************************************** |
533 |
//* General functions |
534 |
//************************************************** |
535 |
|
536 |
/** |
537 |
* set the $_text_debug flag |
538 |
* @param $flag - boolean. |
539 |
*/ |
540 |
function set_text_debug( $flag ) { |
541 |
$this->_text_debug = $flag; |
542 |
} |
543 |
|
544 |
|
545 |
//************************************************** |
546 |
//* RENDERING of content related functions |
547 |
//************************************************** |
548 |
|
549 |
/** |
550 |
* builds the head object and its content. |
551 |
* |
552 |
*/ |
553 |
function _build_head() { |
554 |
|
555 |
$this->_head->add( $this->_build_content_type_tag() ); |
556 |
|
557 |
$this->_head->add( $this->_title ); |
558 |
if ( $this->_head_style->count_content() ) { |
559 |
$this->_head->add( $this->_head_style ); |
560 |
} |
561 |
if ( $this->_head_js->count_content() ) { |
562 |
$this->_head->add( $this->_head_js ); |
563 |
} |
564 |
|
565 |
if (count($this->_head_css_link_arr)) { |
566 |
foreach( $this->_head_css_link_arr as $css) { |
567 |
$this->_head->add( $css ); |
568 |
} |
569 |
} |
570 |
|
571 |
if (count($this->_head_js_link_arr)) { |
572 |
foreach( $this->_head_js_link_arr as $js) { |
573 |
$this->_head->add( $js ); |
574 |
} |
575 |
} |
576 |
} |
577 |
|
578 |
/** |
579 |
* This builds a frameset body tag |
580 |
* wrapped in a <noframes> tag. |
581 |
* |
582 |
* @return NOFRAMEStag object. |
583 |
*/ |
584 |
function _frameset_wrap_body() { |
585 |
$noframes = new NOFRAMEStag; |
586 |
$body = new BODYtag; |
587 |
$body->add( $this->_noframes_text ); |
588 |
$noframes->add( $body ); |
589 |
return $noframes; |
590 |
} |
591 |
|
592 |
/** |
593 |
* render the page. |
594 |
* |
595 |
* |
596 |
* @return string the raw html output. |
597 |
*/ |
598 |
function render() { |
599 |
|
600 |
//lets use ourself to render the debug page! |
601 |
if ($this->_text_debug) { |
602 |
$page = new HTMLPageClass; |
603 |
$page->add_css_link("/phphtmllib/css/HTMLPageClass.css"); |
604 |
} |
605 |
|
606 |
$newline = "\n"; |
607 |
$attributes = $this->get_html_attributes(); |
608 |
$html = new HTMLtag( $attributes ); |
609 |
$html->add( $newline ); |
610 |
$this->_build_head(); |
611 |
$html->add( $this->_head ); |
612 |
|
613 |
//Check to see if we are rendering |
614 |
//a frameset page. |
615 |
if ($this->_frameset != NULL) { |
616 |
//Looks like they have set the frameset |
617 |
//member, so we are rendering a frameset |
618 |
//we will automatically wrap the <body> |
619 |
$html->add( $newline ); |
620 |
$html->add( $this->_frameset ); |
621 |
$html->add( $newline ); |
622 |
$html->add( $this->_frameset_wrap_body() ); |
623 |
} else { |
624 |
$html->add( $newline ); |
625 |
$this->_body->add( $newline ); |
626 |
$html->add( $this->_body ); |
627 |
} |
628 |
|
629 |
$html-> add( $newline ); |
630 |
|
631 |
if ($this->_text_debug) { |
632 |
if (HTML_RENDER_TYPE == XHTML_STRICT) { |
633 |
$xml = new XMLtag(array("version" => "1.0", |
634 |
"encoding"=>"UTF-8")); |
635 |
$page->add( $xml->render(0,1) ); |
636 |
} |
637 |
$page->add( $this->_doctype->render(0,1) ); |
638 |
$page->add( $html->render($this->_indent_style,1) ); |
639 |
return $page->render(); |
640 |
} else { |
641 |
$output = ''; |
642 |
if (HTML_RENDER_TYPE == XHTML_STRICT) { |
643 |
$xml = new XMLtag(array("version" => "1.0", |
644 |
"encoding"=>"UTF-8")); |
645 |
$output = $xml->render(0); |
646 |
} |
647 |
$output .= $this->_doctype->render(); |
648 |
$output .= $html->render($this->_indent_style); |
649 |
return $output; |
650 |
} |
651 |
} |
652 |
} |
653 |
|
654 |
|
655 |
?> |