0byt3m1n1-V2
Path:
/
home
/
nlpacade
/
www
/
calendar
/
bcup2
/
includes
/
[
Home
]
File: html.php
<?php /* * Copyright 2009 Sean Proctor * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ if(!defined('IN_PHPC')) { die("Hacking attempt"); } $HtmlInline = array('a', 'strong'); /* * data structure to display XHTML * see function tag() below for usage * Cateat: this class does not understand HTML, it just approximates it. * do not give an empty tag that needs to be closed without content, it * won't be closed. ex tag('div') to get a closed tag use tag('div', '') */ class Html { var $tagName; var $attributeList; var $childElements; var $error_func; function Html() { $args = func_get_args(); return call_user_func_array(array(&$this, '__construct'), $args); } function __construct() { $this->error_func = array(&$this, 'default_error_handler'); $args = func_get_args(); $this->tagName = array_shift($args); if($this->tagName === NULL) $this->tagName = ''; $this->attributeList = array(); $this->childElements = array(); $arg = array_shift($args); if($arg === NULL) return; while($arg !== NULL) { $this->add($arg); $arg = array_shift($args); } } function add() { $htmlElements = func_get_args(); foreach($htmlElements as $htmlElement) { if(is_a($htmlElement, 'AttributeList')) { $this->attributeList = $htmlElement; } elseif(is_array($htmlElement)) { foreach($htmlElement as $element) { $this->add($element); } } elseif(is_object($htmlElement) && !is_a($htmlElement, 'Html')) { $this->html_error('Invalid class: ' . get_class($htmlElement)); } else { $this->childElements[] = $htmlElement; } } } function prepend() { $htmlElements = func_get_args(); foreach(array_reverse($htmlElements) as $htmlElement) { if(is_array($htmlElement)) { foreach(array_reverse($htmlElement) as $element) { $this->prepend($element); } } elseif(is_object($htmlElement) && !is_a($htmlElement, 'Html')) { $this->html_error('Invalid class: ' . get_class($htmlElement)); } else { array_unshift($this->childElements, $htmlElement); } } } function toString() { global $HtmlInline; $output = ''; if($this->tagName != '') { $output .= "<{$this->tagName}"; if($this->attributeList != NULL) { $output .= ' ' . $this->attributeList->toString(); } if(sizeof($this->childElements) == 0) { $output .= ">\n"; return $output; } $output .= ">"; } foreach($this->childElements as $child) { if(is_object($child)) { if(is_a($child, 'Html')) { $output .= $child->toString(); } else { $this->html_error('Invalid class: ' . get_class($child)); } } else { $output .= $child; } } if($this->tagName != '') { $output .= "</{$this->tagName}>"; if(!in_array($this->tagName, $HtmlInline)) { $output .= "\n"; } } return $output; } function default_error_handler($str) { exit; } /* call this function if you want a non-default error handler */ function html_set_error_handler($func) { $this->error_func = $func; } function html_error() { $args = func_get_args(); return call_user_func_array($this->error_func, $args); } } /* * Data structure to display XML style attributes * see function attributes() below for usage */ class AttributeList { var $list; function AttributeList() { $args = func_get_args(); return call_user_func_array(array(&$this, '__construct'), $args); } function __construct() { $this->list = array(); $args = func_get_args(); $this->add($args); } function add() { $args = func_get_args(); foreach($args as $arg) { if(is_array($arg)) { foreach($arg as $attr) { $this->add($attr); } } else { $this->list[] = $arg; } } } function toString() { return implode(' ', $this->list); } } /* * creates an Html data structure * arguments are tagName [AttributeList] [Html | array | string] ... * where array contains an array, Html, or a string, same requirements for that * array */ function tag() { $args = func_get_args(); $html = new Html(); call_user_func_array(array(&$html, '__construct'), $args); return $html; } /* * creates an AttributeList data structure * arguments are [attribute | array] ... * where attribute is a string of name="value" and array contains arrays or * attributes */ function attributes() { $args = func_get_args(); $attrs = new AttributeList(); call_user_func_array(array(&$attrs, '__construct'), $args); return $attrs; } function attrs() { $args = func_get_args(); $attrs = new AttributeList(); call_user_func_array(array(&$attrs, '__construct'), $args); return $attrs; } // creates a select element for a form // returns HTML data for the element function create_select($name, $arr, $default) { $html = tag('select', attributes('size="1"', "name=\"$name\"", "id=\"$name\"")); foreach($arr as $value => $text) { $attributes = attributes("value=\"$value\""); if($value == $default) $attributes->add('selected'); $html->add(tag('option', $attributes, $text)); } return $html; } // creates a select element for a form given a certain range // returns HTML data for the element function create_select_range($name, $lbound, $ubound, $increment = 1, $default = false, $name_function = false) { $arr = array(); for ($i = $lbound; $i <= $ubound; $i += $increment){ if($name_function !== false) { $text = $name_function($i); } else { $text = $i; } $arr[$i] = $text; } return create_select($name, $arr, $default); } ?>
©
2018.