| // +----------------------------------------------------------------------+ // // $Id: Common.php,v 1.14.2.1 2002/04/09 19:04:19 ssb Exp $ /** * Base class for all HTML classes * * @author Adam Daniel * @version 1.6 * @since PHP 4.0.3pl1 * @abstract */ class HTML_Common { /** * Associative array of table attributes * @var array * @access private */ var $_attributes = array(); /** * Tab offset of the table * @var int * @access private */ var $_tabOffset = 0; /** * HTML comment on the object * @var string * @since 1.5 * @access private */ var $_comment = ""; /** * Class constructor * @param mixed $attributes Associative array of table tag attributes * or HTML attributes name="value" pairs * @access public */ function HTML_Common($attributes=null, $tabOffset=0) { $this->setTabOffset($tabOffset); $this->setAttributes($attributes); } // end constructor /** * Returns the current API version * @access public * @returns double */ function apiVersion() { return 1.6; } // end func apiVersion /** * Returns a string of \t for the tabOffset property * @access private */ function _getTabs() { return $this->_tabOffset > 0 ? str_repeat("\t", $this->_tabOffset) : ""; } // end func _getTabs /** * Returns an HTML formatted attribute string * @param array $attributes * @return string * @access private */ function _getAttrString($attributes) { $strAttr = ''; if (is_array($attributes)) { while (list($key, $value) = each($attributes)) { if (is_int($key)) { $strAttr .= ' ' . strtolower($value) . '="' . strtolower($value) . '"'; } else { $strAttr .= ' ' . strtolower($key) . '="' . htmlspecialchars($value) . '"'; } } } return $strAttr; } // end func _getAttrString /** * Returns a valid atrributes array from either a string or array * @param mixed $attributes Either a typical HTML attribute string or an associative array * @access private */ function _parseAttributes($attributes) { if (is_array($attributes)) { return $attributes; } elseif (is_string($attributes)) { $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" . "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/"; if (preg_match_all($preg, $attributes, $regs)) { $valCounter = 0; for ($counter=0; $counter_getAttrKey($value, $attr1); if (isset($key)) { $attr1[$key] = $value; } else { if (is_array($attr1)) { array_unshift($attr1, $value); } else { $attr1[] = $value; } } } } } // end func _updateAtrrArray /** * Removes the given attribute from the given array * * @param string $attr Attribute name * @param array $attributes Attribute array * @since 1.4 * @access public * @return void * @throws */ function _removeAttr($attr, &$attributes) { if (in_array(strtolower($attr), array_keys($attributes))) { unset($attributes[$attr]); } else { unset($attributes[$this->_getAttrKey($attr, $attributes)]); } } //end func _removeAttr /** * Returns the value of the given attribute * * @param string $attr Attribute name * @since 1.5 * @access public * @return void * @throws */ function getAttribute($attr) { $attr = strtolower($attr); if (is_array($this->_attributes) && isset($this->_attributes[$attr])) { return $this->_attributes[$attr]; } return; } //end func getAttribute /** * Sets the HTML attributes * @param mixed $attributes Either a typical HTML attribute string or an associative array * @access public */ function setAttributes($attributes) { $this->_attributes = $this->_parseAttributes($attributes); } // end func _setAttributes /** * Returns an assoc array of attributes * * @since 1.6 * @access public * @return void * @throws */ function getAttributes() { return $this->_attributes; } //end func getAttributes /** * Updates the passed attributes without changing the other existing attributes * @param mixed $attributes Either a typical HTML attribute string or an associative array * @access public */ function updateAttributes($attributes) { $attributes = $this->_parseAttributes($attributes); $this->_updateAttrArray($this->_attributes, $attributes); } // end func updateAttributes /** * Removes an attribute from * * @param string $attr Attribute name * @since 1.4 * @access public * @return void * @throws */ function removeAttribute($attr) { $this->_removeAttr($attr, $this->_attributes); } //end func removeAttribute /** * Sets the tab offset * @param int $offset * @access public */ function setTabOffset($offset) { $this->_tabOffset = $offset; } // end func setTabOffset /** * Returns the tabOffset * * @since 1.5 * @access public * @return void * @throws */ function getTabOffset() { return $this->_tabOffset; } //end func getTabOffset /** * Sets the HTML comment to be displayed at the beginning of the HTML string * * @param string * @since 1.4 * @access public * @return void * @throws */ function setComment($comment) { $this->_comment = $comment; } // end func setHtmlComment /** * Returns the HTML comment * * @since 1.5 * @access public * @return void * @throws */ function getComment() { return $this->_comment; } //end func getComment /** * Abstract method. Must be extended to return the objects HTML * * @access public * @return string * @abstract */ function toHtml() { return ""; } // end func toHtml /** * Displays the HTML to the screen * * @access public */ function display() { print $this->toHtml(); } // end func display } // end class HTML_Common ?>