Advertisement
Guest User

PHP HTML OOP

a guest
Oct 2nd, 2012
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /*
  4. <------------------------------------------------------->
  5.     $Creation       = 28/4/2012 (02:35)
  6.     $LastUpdate     = 28/4/2012 (02:35)
  7.     $Revision       = 1
  8.     $Author         = Deji
  9.    
  10.     > Desc
  11.         Generates and processes HTML code displayed on the browser.
  12.         This is even more awful than raw HTML. I'd use templates instead.
  13.  
  14. <------------------------------------------------------->
  15. */
  16.  
  17. if(defined('HTML_API_SCRIPT')) exit();
  18. define('HTML_API_SCRIPT', __FILE__);
  19.  
  20. define('HTML_3_2', 1);
  21. define('HTML4', 2);
  22. define('XHTML', 3);
  23.  
  24. class html
  25. {
  26.     private     $output                 = "";
  27. }
  28.  
  29. class html_tag extends html
  30. {
  31.     protected   $content                = "";
  32.     protected   $tagname                = "";
  33.     protected   $required_attributes    = array();
  34.     protected   $attributes             = array();
  35.     protected   $standard_attributes    = array();
  36.     protected   $event_attributes       = array();
  37.    
  38.     function __construct($name)
  39.     {
  40.         $this->tagname = $name;
  41.     }
  42.    
  43.     function __clone()
  44.     {
  45.         /*foreach($this->content as &$v)
  46.         {
  47.             if(is_object($v)) $v = clone $v;
  48.         }*/
  49.     }
  50.    
  51.     public function build()
  52.     {
  53.         $this->output .= "<". $this->tagname;
  54.        
  55.         // Add attributes in order of importance :)
  56.         $this->build_attributes($this->required_attributes);
  57.         $this->build_attributes($this->attributes);
  58.         $this->build_attributes($this->standard_attributes);
  59.         $this->build_attributes($this->event_attributes);
  60.        
  61.         // Add any content.
  62.         if($this->is_tag_self_closing()) $this->output .= " />";
  63.         else $this->output .= ">". $this->build_content($this->content) ."</{$this->tagname}>";
  64.         return $this->output;
  65.     }
  66.    
  67.     private function build_attributes(&$attributes)
  68.     {
  69.         if(sizeof($attributes))
  70.         {
  71.             foreach($attributes as $k => &$v)
  72.             {
  73.                 $this->output .= " {$k}=\"{$v}\"";
  74.             }
  75.         }
  76.     }
  77.    
  78.     private function build_content(&$content)
  79.     {
  80.         if(is_array($content))
  81.         {
  82.             $html = '';
  83.             foreach($content as &$v)
  84.             {
  85.                 if(is_array($v)) $html .= $this->build_content($v);
  86.                 elseif(is_object($v)) $html .= $v->build();
  87.                 else $html .= $v;
  88.             }
  89.             return $html;
  90.         }
  91.         elseif(is_object($content)) return $content->build();
  92.         else return $content;
  93.     }
  94.    
  95.     public function set_content($v)
  96.     {
  97.         $this->content = array();
  98.         if($this->is_tag_self_closing()) return FALSE;
  99.         if(func_num_args() > 1) $v = func_get_args();
  100.         if(is_array($v))
  101.         {
  102.             foreach($v as &$a)
  103.             {
  104.                 $this->add_content($a);
  105.             }
  106.         }
  107.         else $this->content[] = $v;
  108.     }
  109.    
  110.     public function add_content($v)
  111.     {
  112.         if($this->is_tag_self_closing()) return FALSE;      // no sir, this element can't contain anything
  113.         if(func_num_args() > 1) $v = func_get_args();
  114.         if(is_array($v))
  115.         {
  116.             foreach($v as &$a)
  117.             {
  118.                 $this->add_content($a);
  119.             }
  120.         }
  121.         else $this->content[] = $v;
  122.     }
  123.    
  124.     public function add_class($v)
  125.     {
  126.         $a = $this->get_attr_class();
  127.         if($a != '') $a .= " ";
  128.         $this->set_attr_class($a ."{$v}");
  129.     }
  130.    
  131.     function set_attr_class($v)                 {$this->set_attribute('class', $v);}
  132.     function set_attr_name($v)                  {$this->set_attribute('name', $v);}
  133.     function set_attr_selected($v)              {$this->set_attribute('selected', $v);}
  134.     function set_attr_value($v)                 {$this->set_attribute('value', $v);}
  135.     function set_attr_id($v)                    {$this->set_standard_attribute('id', $v);}
  136.     function set_attr_style($v)                 {$this->set_standard_attribute('style', $v);}
  137.     function set_attr_title($v)                 {$this->set_standard_attribute('title', $v);}
  138.    
  139.     function onchange($v)                       {$this->set_event_attribute('onchange', $v);}
  140.     function onclick($v)                        {$this->set_event_attribute('onclick', $v);}
  141.     function ondblclick($v)                     {$this->set_event_attribute('ondblclick', $v);}
  142.     function onmousedown($v)                    {$this->set_event_attribute('onmousedown', $v);}
  143.     function onmousemove($v)                    {$this->set_event_attribute('onmousemove', $v);}
  144.     function onmouseout($v)                     {$this->set_event_attribute('onmouseout', $v);}
  145.     function onmouseover($v)                    {$this->set_event_attribute('onmouseover', $v);}
  146.     function onmouseup($v)                      {$this->set_event_attribute('onmouseup', $v);}
  147.     function onkeydown($v)                      {$this->set_event_attribute('onkeydown', $v);}
  148.     function onkeypress($v)                     {$this->set_event_attribute('onkeypress', $v);}
  149.     function onkeyup($v)                        {$this->set_event_attribute('onkeyup', $v);}
  150.    
  151.     function set_required_attribute($k, $v)     {$this->required_attributes[$k] = $v;}
  152.     function set_attribute($k, $v)              {$this->attributes[$k] = $v;}
  153.     function set_standard_attribute($k, $v)     {$this->standard_attributes[$k] = $v;}
  154.     function set_event_attribute($k, $v)        {$this->event_attributes[$k] = $v;}
  155.    
  156.     function get_attr_class()                   {return $this->get_attribute('class');}
  157.     function get_attr_name()                    {return $this->get_attribute('name');}
  158.     function get_attr_selected()                {return $this->get_attribute('selected');}
  159.     function get_attr_value()                   {return $this->get_attribute('value');}
  160.     function get_attr_id()                      {return $this->get_standard_attribute('id');}
  161.     function get_attr_style()                   {return $this->get_standard_attribute('style');}
  162.     function get_attr_title()                   {return $this->get_standard_attribute('title');}
  163.    
  164.     function get_required_attribute($k)         {return isset($this->required_attributes[$k]) ? $this->required_attributes[$k] : FALSE;}
  165.     function get_attribute($k)                  {return isset($this->attributes[$k]) ? $this->attributes[$k] : FALSE;}
  166.     function get_standard_attribute($k)         {return isset($this->standard_attributes[$k]) ? $this->standard_attributes[$k] : FALSE;}
  167.     function get_event_attribute($k)            {return isset($this->event_attributes[$k]) ? $this->event_attributes[$k] : FALSE;}
  168.    
  169.     function clear_attr_selected()              {$this->clear_attribute('selected');}
  170.    
  171.     function clear_required_attribute($k)       {if(isset($this->required_attributes[$k])) unset($this->required_attributes[$k]);}
  172.     function clear_attribute($k)                {if(isset($this->attributes[$k])) unset($this->attributes[$k]);}
  173.     function clear_standard_attribute($k)       {if(isset($this->standard_attributes[$k])) unset($this->standard_attributes[$k]);}
  174.     function clear_event_attribute($k)          {if(isset($this->event_attributes[$k])) unset($this->event_attributes[$k]);}
  175.    
  176.     function clear_all_attributes()
  177.     {
  178.         $this->required_attributes = array();
  179.         $this->attributes = array();
  180.         $this->standard_attributes = array();
  181.         $this->event_attributes = array();
  182.     }
  183.    
  184.     /********************************************************************************************/
  185.     // Determines whether the tag should be closed on opening (XHTML style '/>')
  186.     /********************************************************************************************/
  187.    
  188.     function is_tag_self_closing()
  189.     {
  190.         switch($this->tagname)
  191.         {
  192.             case 'area':
  193.             case 'base':
  194.             case 'basefont':
  195.             case 'br':
  196.             case 'hr':
  197.             case 'input':
  198.             case 'img':
  199.             case 'link':
  200.             case 'meta':
  201.                 return TRUE;
  202.             default:
  203.                 return FALSE;
  204.         }
  205.     }
  206. }
  207.  
  208. class html_tag_div extends html_tag
  209. {
  210.     function __construct($content = '')
  211.     {
  212.         if($content != '') $this->set_content($content);
  213.         parent::__construct("div");
  214.     }
  215. }
  216.  
  217. class html_tag_span extends html_tag
  218. {
  219.     function __construct($content = '')
  220.     {
  221.         if($content != '') $this->set_content($content);
  222.         parent::__construct("span");
  223.     }
  224. }
  225.  
  226. class html_tag_header extends html_tag
  227. {
  228.     function __construct($size, $content = '')
  229.     {
  230.         if($content != '') $this->set_content($content);
  231.         if($size < 1 || $size > 7) $size = 1;
  232.         parent::__construct("h{$size}");
  233.     }
  234. }
  235.  
  236. class html_tag_paragraph extends html_tag
  237. {
  238.     function __construct($content = '')
  239.     {
  240.         if($content != '') $this->set_content($content);
  241.         parent::__construct("p");
  242.     }
  243. }
  244.  
  245. class html_tag_a extends html_tag
  246. {
  247.     function __construct($href = '', $content = '')
  248.     {
  249.         $this->set_required_attribute('href', $href);
  250.         if($content != '') $this->set_content($content);
  251.         parent::__construct("a");
  252.     }
  253. }
  254.  
  255. class html_tag_img extends html_tag
  256. {
  257.     function __construct($src = '', $alt = '')
  258.     {
  259.         $this->set_attr_src($src);
  260.         $this->set_attr_alt($alt);
  261.         parent::__construct("img");
  262.     }
  263.    
  264.     function set_attr_src($v)                   {$this->set_required_attribute('src', $v);}
  265.     function get_attr_src()                     {return $this->get_required_attribute('src');}
  266.     function set_attr_alt($v)                   {$this->set_required_attribute('alt', $v);}
  267.     function get_attr_alt()                     {return $this->get_required_attribute('alt');}
  268.     function set_attr_width($v)                 {$this->set_attribute('width', $v);}
  269.     function get_attr_width()                   {return $this->get_attribute('width');}
  270.     function set_attr_height($v)                {$this->set_attribute('height', $v);}
  271.     function get_attr_height()                  {return $this->get_attribute('height');}
  272. }
  273.  
  274. class html_tag_linebreak extends html_tag
  275. {
  276.     function __construct()
  277.     {
  278.         parent::__construct("br");
  279.     }
  280. }
  281.  
  282. class html_tag_form extends html_tag
  283. {
  284.     function __construct($action = '', $method = 'get')
  285.     {
  286.         $this->set_attr_action($action);
  287.         if($method != 'get') $this->set_attr_method('post');
  288.         parent::__construct('form');
  289.     }
  290.    
  291.     function set_attr_action($v)                {$this->set_required_attribute('action', $v);}
  292.     function get_attr_action($v)                {return $this->get_required_attribute('action');}
  293.     function set_attr_method($v)                {$this->set_attribute('method', $v);}
  294.     function get_attr_method($v)                {return $this->get_attribute('method');}
  295.     function set_attr_enctype($v)               {$this->set_standard_attribute('enctype', $v);}
  296.     function get_attr_enctype($v)               {return $this->set_standard_attribute('enctype');}
  297.    
  298.     function onreset($v)                        {$this->set_event_attribute('onreset', $v);}
  299.     function onsubmit($v)                       {$this->set_event_attribute('onsubmit', $v);}
  300. }
  301.  
  302. class html_tag_fieldset extends html_tag
  303. {
  304.     private     $legend;
  305.    
  306.     function __construct($legend = '')
  307.     {
  308.         if($legend != '') $this->legend = new html_tag_legend($legend);
  309.         parent::__construct('fieldset');
  310.     }
  311.    
  312.     public function get_legend()
  313.     {
  314.         return $this->legend ? $this->legend : FALSE;
  315.     }
  316. }
  317.  
  318. class html_tag_input extends html_tag
  319. {
  320.     function __construct($type = '')
  321.     {
  322.         if($type != '') $this->set_attribute('type', $type);
  323.         parent::__construct('input');
  324.     }
  325.    
  326.     public function set_attr_checked($checked = 1)
  327.     {
  328.         if($checked) $this->set_attribute('checked', 'checked');
  329.         else $this->clear_attribute('checked');
  330.     }
  331.    
  332.     function set_attr_alt($v)                   {$this->set_required_attribute('alt', $v);}
  333.     function get_attr_alt($v)                   {return $this->get_required_attribute('alt');}
  334.     function set_attr_src($v)                   {$this->set_required_attribute('src', $v);}
  335.     function get_attr_src($v)                   {return $this->get_required_attribute('src');}
  336. }
  337.  
  338. class html_tag_legend extends html_tag
  339. {
  340.     function __construct($text = '')
  341.     {
  342.         if($text != '') $this->set_content($text);
  343.         parent::__construct('legend');
  344.     }
  345. }
  346.  
  347. class html_tag_select extends html_tag
  348. {
  349.     private     $options                = array();
  350.    
  351.     function __construct()
  352.     {
  353.         if(func_num_args())
  354.         {
  355.             $v = func_get_args();
  356.             $this->add_option($v);
  357.         }
  358.         parent::__construct('select');
  359.     }
  360.    
  361.     public function build()
  362.     {
  363.         foreach($this->options as &$opt)
  364.         {
  365.             $this->add_content($opt->build());
  366.         }
  367.         return parent::build();
  368.     }
  369.    
  370.     public function add_option(&$option)
  371.     {
  372.         if(is_array($option))
  373.         {
  374.             foreach($option as $opt) $this->add_option($opt);
  375.         }
  376.         else $this->options[] = $option;
  377.     }
  378.    
  379.     public function set_selected_option($i)
  380.     {
  381.         foreach($this->options as $k => &$opt) $opt->set_selected($k == $i);
  382.     }
  383.    
  384.     public function set_selected_option_value($v)
  385.     {
  386.         foreach($this->options as $k => &$opt) $opt->set_selected($opt->get_attr_value() == $v);
  387.     }
  388. }
  389.  
  390. class html_tag_option extends html_tag
  391. {
  392.     function __construct($content='', $value='')
  393.     {
  394.         parent::__construct('option');
  395.         if($value != '') $this->set_attr_value($value);
  396.         if($content != '') $this->set_content($content);
  397.     }
  398.    
  399.     public function set_selected($b)
  400.     {
  401.         if($b) $this->set_attr_selected('selected');
  402.         else $this->clear_attr_selected();
  403.     }
  404. }
  405.  
  406. class html_tag_table extends html_tag
  407. {
  408.     private     $thead = array();
  409.     private     $tbody = array();
  410.     private     $tfoot = array();
  411.    
  412.     function __construct($tbody=array(), $thead=array(), $tfoot=array())
  413.     {
  414.         parent::__construct('table');
  415.         foreach($tbody as &$row) $this->tbody[] = $row;
  416.         foreach($thead as &$row) $this->thead[] = $row;
  417.         foreach($tfoot as &$row) $this->tfoot[] = $row;
  418.     }
  419.    
  420.     public function add_header_row($cols = array())
  421.     {
  422.         $this->thead[] = new html_tag_tr($cols);
  423.     }
  424.    
  425.     public function add_footer_row($cols = array())
  426.     {
  427.         $this->tfoot[] = new html_tag_tr($cols);
  428.     }
  429.    
  430.     public function add_row($cols = array())
  431.     {
  432.         $this->tbody[] = new html_tag_tr($cols);
  433.     }
  434.    
  435.     public function build()
  436.     {
  437.         if($this->thead)
  438.         {
  439.             $this->add_content('<thead>');
  440.             foreach($this->thead as &$row)
  441.             {
  442.                 $this->add_content($row->build());
  443.             }
  444.             $this->add_content('</thead>');
  445.         }
  446.         if($this->tfoot)
  447.         {
  448.             $this->add_content('<tfoot>');
  449.             foreach($this->tfoot as &$row)
  450.             {
  451.                 $this->add_content($row->build());
  452.             }
  453.             $this->add_content('</tfoot>');
  454.         }
  455.         if($this->tbody)
  456.         {
  457.             $this->add_content('<tbody>');
  458.             foreach($this->tbody as &$row)
  459.             {
  460.                 $this->add_content($row->build());
  461.             }
  462.             $this->add_content('</tbody');
  463.         }
  464.         return parent::build();
  465.     }
  466. }
  467.  
  468. class html_tag_tr extends html_tag
  469. {
  470.     private     $cols   = array();
  471.  
  472.     function __construct(&$cols = array())
  473.     {
  474.         foreach($cols as &$col) $this->cols[] = $col;
  475.         parent::__construct('tr');
  476.     }
  477.    
  478.     public function build()
  479.     {
  480.         foreach($this->cols as &$col)
  481.         {
  482.             if(is_object($col)) $this->add_content($col->build());
  483.             else $this->add_content($col);
  484.         }
  485.         return parent::build();
  486.     }
  487. }
  488.  
  489. class html_tag_table_cell extends html_tag
  490. {
  491.     private     $cols   = array();
  492.  
  493.     function __construct($content='', $header=0)
  494.     {
  495.         if($header) parent::__construct('th');
  496.         else parent::__construct('td');
  497.         if($content != '') $this->set_content($content);
  498.     }
  499.    
  500.     function set_attr_colspan($v)               {$this->set_attribute('colspan', $v);}
  501.     function get_attr_colspan()                 {return $this->get_attribute('colspan');}
  502. }
  503.  
  504. class html_tag_td extends html_tag_table_cell
  505. {
  506.     function __construct($content='')
  507.     {
  508.         parent::__construct($content, 0);
  509.     }
  510. }
  511.  
  512. class html_tag_th extends html_tag_table_cell
  513. {
  514.     function __construct($content='')
  515.     {
  516.         parent::__construct($content, 1);
  517.     }
  518. }
  519.  
  520. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement