Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- <------------------------------------------------------->
- $Creation = 28/4/2012 (02:35)
- $LastUpdate = 28/4/2012 (02:35)
- $Revision = 1
- $Author = Deji
- > Desc
- Generates and processes HTML code displayed on the browser.
- This is even more awful than raw HTML. I'd use templates instead.
- <------------------------------------------------------->
- */
- if(defined('HTML_API_SCRIPT')) exit();
- define('HTML_API_SCRIPT', __FILE__);
- define('HTML_3_2', 1);
- define('HTML4', 2);
- define('XHTML', 3);
- class html
- {
- private $output = "";
- }
- class html_tag extends html
- {
- protected $content = "";
- protected $tagname = "";
- protected $required_attributes = array();
- protected $attributes = array();
- protected $standard_attributes = array();
- protected $event_attributes = array();
- function __construct($name)
- {
- $this->tagname = $name;
- }
- function __clone()
- {
- /*foreach($this->content as &$v)
- {
- if(is_object($v)) $v = clone $v;
- }*/
- }
- public function build()
- {
- $this->output .= "<". $this->tagname;
- // Add attributes in order of importance :)
- $this->build_attributes($this->required_attributes);
- $this->build_attributes($this->attributes);
- $this->build_attributes($this->standard_attributes);
- $this->build_attributes($this->event_attributes);
- // Add any content.
- if($this->is_tag_self_closing()) $this->output .= " />";
- else $this->output .= ">". $this->build_content($this->content) ."</{$this->tagname}>";
- return $this->output;
- }
- private function build_attributes(&$attributes)
- {
- if(sizeof($attributes))
- {
- foreach($attributes as $k => &$v)
- {
- $this->output .= " {$k}=\"{$v}\"";
- }
- }
- }
- private function build_content(&$content)
- {
- if(is_array($content))
- {
- $html = '';
- foreach($content as &$v)
- {
- if(is_array($v)) $html .= $this->build_content($v);
- elseif(is_object($v)) $html .= $v->build();
- else $html .= $v;
- }
- return $html;
- }
- elseif(is_object($content)) return $content->build();
- else return $content;
- }
- public function set_content($v)
- {
- $this->content = array();
- if($this->is_tag_self_closing()) return FALSE;
- if(func_num_args() > 1) $v = func_get_args();
- if(is_array($v))
- {
- foreach($v as &$a)
- {
- $this->add_content($a);
- }
- }
- else $this->content[] = $v;
- }
- public function add_content($v)
- {
- if($this->is_tag_self_closing()) return FALSE; // no sir, this element can't contain anything
- if(func_num_args() > 1) $v = func_get_args();
- if(is_array($v))
- {
- foreach($v as &$a)
- {
- $this->add_content($a);
- }
- }
- else $this->content[] = $v;
- }
- public function add_class($v)
- {
- $a = $this->get_attr_class();
- if($a != '') $a .= " ";
- $this->set_attr_class($a ."{$v}");
- }
- function set_attr_class($v) {$this->set_attribute('class', $v);}
- function set_attr_name($v) {$this->set_attribute('name', $v);}
- function set_attr_selected($v) {$this->set_attribute('selected', $v);}
- function set_attr_value($v) {$this->set_attribute('value', $v);}
- function set_attr_id($v) {$this->set_standard_attribute('id', $v);}
- function set_attr_style($v) {$this->set_standard_attribute('style', $v);}
- function set_attr_title($v) {$this->set_standard_attribute('title', $v);}
- function onchange($v) {$this->set_event_attribute('onchange', $v);}
- function onclick($v) {$this->set_event_attribute('onclick', $v);}
- function ondblclick($v) {$this->set_event_attribute('ondblclick', $v);}
- function onmousedown($v) {$this->set_event_attribute('onmousedown', $v);}
- function onmousemove($v) {$this->set_event_attribute('onmousemove', $v);}
- function onmouseout($v) {$this->set_event_attribute('onmouseout', $v);}
- function onmouseover($v) {$this->set_event_attribute('onmouseover', $v);}
- function onmouseup($v) {$this->set_event_attribute('onmouseup', $v);}
- function onkeydown($v) {$this->set_event_attribute('onkeydown', $v);}
- function onkeypress($v) {$this->set_event_attribute('onkeypress', $v);}
- function onkeyup($v) {$this->set_event_attribute('onkeyup', $v);}
- function set_required_attribute($k, $v) {$this->required_attributes[$k] = $v;}
- function set_attribute($k, $v) {$this->attributes[$k] = $v;}
- function set_standard_attribute($k, $v) {$this->standard_attributes[$k] = $v;}
- function set_event_attribute($k, $v) {$this->event_attributes[$k] = $v;}
- function get_attr_class() {return $this->get_attribute('class');}
- function get_attr_name() {return $this->get_attribute('name');}
- function get_attr_selected() {return $this->get_attribute('selected');}
- function get_attr_value() {return $this->get_attribute('value');}
- function get_attr_id() {return $this->get_standard_attribute('id');}
- function get_attr_style() {return $this->get_standard_attribute('style');}
- function get_attr_title() {return $this->get_standard_attribute('title');}
- function get_required_attribute($k) {return isset($this->required_attributes[$k]) ? $this->required_attributes[$k] : FALSE;}
- function get_attribute($k) {return isset($this->attributes[$k]) ? $this->attributes[$k] : FALSE;}
- function get_standard_attribute($k) {return isset($this->standard_attributes[$k]) ? $this->standard_attributes[$k] : FALSE;}
- function get_event_attribute($k) {return isset($this->event_attributes[$k]) ? $this->event_attributes[$k] : FALSE;}
- function clear_attr_selected() {$this->clear_attribute('selected');}
- function clear_required_attribute($k) {if(isset($this->required_attributes[$k])) unset($this->required_attributes[$k]);}
- function clear_attribute($k) {if(isset($this->attributes[$k])) unset($this->attributes[$k]);}
- function clear_standard_attribute($k) {if(isset($this->standard_attributes[$k])) unset($this->standard_attributes[$k]);}
- function clear_event_attribute($k) {if(isset($this->event_attributes[$k])) unset($this->event_attributes[$k]);}
- function clear_all_attributes()
- {
- $this->required_attributes = array();
- $this->attributes = array();
- $this->standard_attributes = array();
- $this->event_attributes = array();
- }
- /********************************************************************************************/
- // Determines whether the tag should be closed on opening (XHTML style '/>')
- /********************************************************************************************/
- function is_tag_self_closing()
- {
- switch($this->tagname)
- {
- case 'area':
- case 'base':
- case 'basefont':
- case 'br':
- case 'hr':
- case 'input':
- case 'img':
- case 'link':
- case 'meta':
- return TRUE;
- default:
- return FALSE;
- }
- }
- }
- class html_tag_div extends html_tag
- {
- function __construct($content = '')
- {
- if($content != '') $this->set_content($content);
- parent::__construct("div");
- }
- }
- class html_tag_span extends html_tag
- {
- function __construct($content = '')
- {
- if($content != '') $this->set_content($content);
- parent::__construct("span");
- }
- }
- class html_tag_header extends html_tag
- {
- function __construct($size, $content = '')
- {
- if($content != '') $this->set_content($content);
- if($size < 1 || $size > 7) $size = 1;
- parent::__construct("h{$size}");
- }
- }
- class html_tag_paragraph extends html_tag
- {
- function __construct($content = '')
- {
- if($content != '') $this->set_content($content);
- parent::__construct("p");
- }
- }
- class html_tag_a extends html_tag
- {
- function __construct($href = '', $content = '')
- {
- $this->set_required_attribute('href', $href);
- if($content != '') $this->set_content($content);
- parent::__construct("a");
- }
- }
- class html_tag_img extends html_tag
- {
- function __construct($src = '', $alt = '')
- {
- $this->set_attr_src($src);
- $this->set_attr_alt($alt);
- parent::__construct("img");
- }
- function set_attr_src($v) {$this->set_required_attribute('src', $v);}
- function get_attr_src() {return $this->get_required_attribute('src');}
- function set_attr_alt($v) {$this->set_required_attribute('alt', $v);}
- function get_attr_alt() {return $this->get_required_attribute('alt');}
- function set_attr_width($v) {$this->set_attribute('width', $v);}
- function get_attr_width() {return $this->get_attribute('width');}
- function set_attr_height($v) {$this->set_attribute('height', $v);}
- function get_attr_height() {return $this->get_attribute('height');}
- }
- class html_tag_linebreak extends html_tag
- {
- function __construct()
- {
- parent::__construct("br");
- }
- }
- class html_tag_form extends html_tag
- {
- function __construct($action = '', $method = 'get')
- {
- $this->set_attr_action($action);
- if($method != 'get') $this->set_attr_method('post');
- parent::__construct('form');
- }
- function set_attr_action($v) {$this->set_required_attribute('action', $v);}
- function get_attr_action($v) {return $this->get_required_attribute('action');}
- function set_attr_method($v) {$this->set_attribute('method', $v);}
- function get_attr_method($v) {return $this->get_attribute('method');}
- function set_attr_enctype($v) {$this->set_standard_attribute('enctype', $v);}
- function get_attr_enctype($v) {return $this->set_standard_attribute('enctype');}
- function onreset($v) {$this->set_event_attribute('onreset', $v);}
- function onsubmit($v) {$this->set_event_attribute('onsubmit', $v);}
- }
- class html_tag_fieldset extends html_tag
- {
- private $legend;
- function __construct($legend = '')
- {
- if($legend != '') $this->legend = new html_tag_legend($legend);
- parent::__construct('fieldset');
- }
- public function get_legend()
- {
- return $this->legend ? $this->legend : FALSE;
- }
- }
- class html_tag_input extends html_tag
- {
- function __construct($type = '')
- {
- if($type != '') $this->set_attribute('type', $type);
- parent::__construct('input');
- }
- public function set_attr_checked($checked = 1)
- {
- if($checked) $this->set_attribute('checked', 'checked');
- else $this->clear_attribute('checked');
- }
- function set_attr_alt($v) {$this->set_required_attribute('alt', $v);}
- function get_attr_alt($v) {return $this->get_required_attribute('alt');}
- function set_attr_src($v) {$this->set_required_attribute('src', $v);}
- function get_attr_src($v) {return $this->get_required_attribute('src');}
- }
- class html_tag_legend extends html_tag
- {
- function __construct($text = '')
- {
- if($text != '') $this->set_content($text);
- parent::__construct('legend');
- }
- }
- class html_tag_select extends html_tag
- {
- private $options = array();
- function __construct()
- {
- if(func_num_args())
- {
- $v = func_get_args();
- $this->add_option($v);
- }
- parent::__construct('select');
- }
- public function build()
- {
- foreach($this->options as &$opt)
- {
- $this->add_content($opt->build());
- }
- return parent::build();
- }
- public function add_option(&$option)
- {
- if(is_array($option))
- {
- foreach($option as $opt) $this->add_option($opt);
- }
- else $this->options[] = $option;
- }
- public function set_selected_option($i)
- {
- foreach($this->options as $k => &$opt) $opt->set_selected($k == $i);
- }
- public function set_selected_option_value($v)
- {
- foreach($this->options as $k => &$opt) $opt->set_selected($opt->get_attr_value() == $v);
- }
- }
- class html_tag_option extends html_tag
- {
- function __construct($content='', $value='')
- {
- parent::__construct('option');
- if($value != '') $this->set_attr_value($value);
- if($content != '') $this->set_content($content);
- }
- public function set_selected($b)
- {
- if($b) $this->set_attr_selected('selected');
- else $this->clear_attr_selected();
- }
- }
- class html_tag_table extends html_tag
- {
- private $thead = array();
- private $tbody = array();
- private $tfoot = array();
- function __construct($tbody=array(), $thead=array(), $tfoot=array())
- {
- parent::__construct('table');
- foreach($tbody as &$row) $this->tbody[] = $row;
- foreach($thead as &$row) $this->thead[] = $row;
- foreach($tfoot as &$row) $this->tfoot[] = $row;
- }
- public function add_header_row($cols = array())
- {
- $this->thead[] = new html_tag_tr($cols);
- }
- public function add_footer_row($cols = array())
- {
- $this->tfoot[] = new html_tag_tr($cols);
- }
- public function add_row($cols = array())
- {
- $this->tbody[] = new html_tag_tr($cols);
- }
- public function build()
- {
- if($this->thead)
- {
- $this->add_content('<thead>');
- foreach($this->thead as &$row)
- {
- $this->add_content($row->build());
- }
- $this->add_content('</thead>');
- }
- if($this->tfoot)
- {
- $this->add_content('<tfoot>');
- foreach($this->tfoot as &$row)
- {
- $this->add_content($row->build());
- }
- $this->add_content('</tfoot>');
- }
- if($this->tbody)
- {
- $this->add_content('<tbody>');
- foreach($this->tbody as &$row)
- {
- $this->add_content($row->build());
- }
- $this->add_content('</tbody');
- }
- return parent::build();
- }
- }
- class html_tag_tr extends html_tag
- {
- private $cols = array();
- function __construct(&$cols = array())
- {
- foreach($cols as &$col) $this->cols[] = $col;
- parent::__construct('tr');
- }
- public function build()
- {
- foreach($this->cols as &$col)
- {
- if(is_object($col)) $this->add_content($col->build());
- else $this->add_content($col);
- }
- return parent::build();
- }
- }
- class html_tag_table_cell extends html_tag
- {
- private $cols = array();
- function __construct($content='', $header=0)
- {
- if($header) parent::__construct('th');
- else parent::__construct('td');
- if($content != '') $this->set_content($content);
- }
- function set_attr_colspan($v) {$this->set_attribute('colspan', $v);}
- function get_attr_colspan() {return $this->get_attribute('colspan');}
- }
- class html_tag_td extends html_tag_table_cell
- {
- function __construct($content='')
- {
- parent::__construct($content, 0);
- }
- }
- class html_tag_th extends html_tag_table_cell
- {
- function __construct($content='')
- {
- parent::__construct($content, 1);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement