| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file contains the CSSBuilder class |
| 4 |
* |
| 5 |
* $Id: CSSBuilder.inc,v 1.5 2003/03/31 21:30:12 hemna Exp $ |
| 6 |
* |
| 7 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
| 8 |
* @package phpHtmlLib |
| 9 |
* |
| 10 |
*/ |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
/** |
| 15 |
* This class is a widget for constructing and |
| 16 |
* rendering css. It exports API's for adding |
| 17 |
* classes, ids and their attributes. |
| 18 |
* It can render as either a stand alone |
| 19 |
* foo.css type of file, with the appropriate |
| 20 |
* httpd header, or as part of a <style> tag's |
| 21 |
* content that lives as part of an html document. |
| 22 |
* |
| 23 |
* This class lets you create classes in the format |
| 24 |
* |
| 25 |
* name extends_list { |
| 26 |
* property: value; |
| 27 |
* } |
| 28 |
* |
| 29 |
* |
| 30 |
* ie. |
| 31 |
* #foo div, span { |
| 32 |
* font-family: arial, helvetica; |
| 33 |
* font-size: 10pt; |
| 34 |
* } |
| 35 |
* |
| 36 |
* |
| 37 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
| 38 |
* @package phpHtmlLib |
| 39 |
* |
| 40 |
*/ |
| 41 |
class CSSBuilder { |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* This holds the entries for the css |
| 46 |
* |
| 47 |
* @var array |
| 48 |
*/ |
| 49 |
var $_entries = array(); |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* Flag to let us know if we should |
| 54 |
* output a header content-type value |
| 55 |
* header("Content-Type: text/css"); |
| 56 |
* |
| 57 |
* @var boolean |
| 58 |
*/ |
| 59 |
var $_header_flag = FALSE; |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* String to use as indent string. |
| 64 |
* can be any char. defaulted to 1 space |
| 65 |
* @var string |
| 66 |
* @access private |
| 67 |
*/ |
| 68 |
var $_indent_str = " "; |
| 69 |
|
| 70 |
/** |
| 71 |
* Flag for pretty (indented) output |
| 72 |
* @var boolean |
| 73 |
* @access public |
| 74 |
*/ |
| 75 |
var $indent_flag = TRUE; |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* The CSSBuilder constructor |
| 80 |
* |
| 81 |
* @param boolean - output the http header |
| 82 |
* content type mime type |
| 83 |
* or not. |
| 84 |
* |
| 85 |
*/ |
| 86 |
function CSSBuilder($header_flag=FALSE) { |
| 87 |
$this->set_header_flag( $header_flag ); |
| 88 |
$this->user_setup(); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
/** |
| 93 |
* The render method for compatibility |
| 94 |
* with the rest of the phphtmllib api |
| 95 |
* |
| 96 |
* @param int - the indentation level for |
| 97 |
* the container. |
| 98 |
* @param int - the output debug flag to |
| 99 |
* maintain compatibility w/ the API. |
| 100 |
* |
| 101 |
* @return string the raw html output. |
| 102 |
*/ |
| 103 |
function render($indent_level=1, $output_debug=0) { |
| 104 |
if ($this->_header_flag) { |
| 105 |
header("Content-Type: text/css"); |
| 106 |
} |
| 107 |
|
| 108 |
$indent = $this->_render_indent( $indent_level ); |
| 109 |
|
| 110 |
$css = ""; |
| 111 |
foreach( $this->_entries as $class => $properties ) { |
| 112 |
$css .= $indent.$this->_build_name( $class ); |
| 113 |
$css .= $this->_build_properties( $properties, $indent ); |
| 114 |
$css .= "\n"; |
| 115 |
} |
| 116 |
|
| 117 |
return $css; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* This is used to set/update the header |
| 123 |
* flag. |
| 124 |
* |
| 125 |
* @param boolean |
| 126 |
*/ |
| 127 |
function set_header_flag( $flag ) { |
| 128 |
$this->_header_flag = $flag; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Call the child class' setup function |
| 133 |
* to build the actual css classes and their |
| 134 |
* attributes |
| 135 |
*/ |
| 136 |
function user_setup() { |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
/*******************************************/ |
| 143 |
/* functions for building/updating entries */ |
| 144 |
/*******************************************/ |
| 145 |
|
| 146 |
/** |
| 147 |
* This function is used to construct the css name |
| 148 |
* declaration string. |
| 149 |
* ie #foo div,span { |
| 150 |
* |
| 151 |
* @param string the name-extends string |
| 152 |
* @return the css name declaration |
| 153 |
*/ |
| 154 |
function _build_name( $class ) { |
| 155 |
$temp = explode('-', $class); |
| 156 |
|
| 157 |
$css = $temp[0]." ".$temp[1]." {\n"; |
| 158 |
return $css; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* This function is used to construct the |
| 163 |
* property: value strings for the class |
| 164 |
* |
| 165 |
* @param array - the properties |
| 166 |
* @param array - the indent string. |
| 167 |
* @return string |
| 168 |
*/ |
| 169 |
function _build_properties( $properties, $indent_str ) { |
| 170 |
$css = ""; |
| 171 |
foreach( $properties as $property => $value ) { |
| 172 |
$css .= $indent_str." ".$property.": ".$value.";\n"; |
| 173 |
} |
| 174 |
$css .= $indent_str."}\n"; |
| 175 |
return $css; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/** |
| 180 |
* This function adds a new class entry |
| 181 |
* |
| 182 |
* @param string - class/id name |
| 183 |
* @param string - the list of extends |
| 184 |
* ie #foo div, a { } |
| 185 |
* where "div, a" are the extends |
| 186 |
* |
| 187 |
* @param array - the attributes |
| 188 |
*/ |
| 189 |
function add_entry($name, $extends, $attributes) { |
| 190 |
$this->_entries[$name."-".$extends] = $attributes; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* This function updates a css property value |
| 196 |
* for a specific class/id |
| 197 |
* |
| 198 |
* @param string - the class/id name |
| 199 |
* @param string - the list of extends |
| 200 |
* ie #foo div, a { } |
| 201 |
* where "div, a" are the extends |
| 202 |
* @param string - the property to adjust |
| 203 |
* @param string - the value for the property |
| 204 |
*/ |
| 205 |
function update_property($name, $extends, $property, $value ) { |
| 206 |
$this->_entries[$name."-".$extends][$property] = $value; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* This function updates a css property value |
| 211 |
* for a specific class/id |
| 212 |
* |
| 213 |
* @param string - the class/id name |
| 214 |
* @param string - the list of extends |
| 215 |
* ie #foo div, a { } |
| 216 |
* where "div, a" are the extends |
| 217 |
* @param array - the array of array(property => value ) pairs. |
| 218 |
*/ |
| 219 |
function update_properties($name, $extends, $properties) { |
| 220 |
|
| 221 |
foreach ( $properties as $property => $value ) { |
| 222 |
$this->_entries[$name."-".$extends][$property] = $value; |
| 223 |
|
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* This function is a macro for walking the entire |
| 229 |
* list of classes, looking for particular property |
| 230 |
* in each class. It then tries to match |
| 231 |
* the current value of that property and then |
| 232 |
* replaces the value with the new value. |
| 233 |
* |
| 234 |
* NOTE: this is a way of templating themes. |
| 235 |
* you create the original css w/ a value |
| 236 |
* of something like _DARK_COLOR_ |
| 237 |
* and use this function to replace all occurrences |
| 238 |
* with #dbdbdb |
| 239 |
* |
| 240 |
* @param string - the property that the search lives in |
| 241 |
* @param string - the original value to find |
| 242 |
* @param string - the new value |
| 243 |
*/ |
| 244 |
function update_all_values( $property, $search, $value ) { |
| 245 |
|
| 246 |
array_walk( $this->_entries, array($this, "_replace_value"), |
| 247 |
array("property" => $property, |
| 248 |
"search" => $search, |
| 249 |
"value" => $value ) ); |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
/** |
| 255 |
* This function does the main work for update_all_values |
| 256 |
* |
| 257 |
*/ |
| 258 |
function _replace_value( &$item, $key, $blah ) { |
| 259 |
if (@strcasecmp($item[$blah["property"]], $blah["search"]) == 0) { |
| 260 |
$item[$blah["property"]] = $blah["value"]; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
/** |
| 268 |
* returns leading indent for tag |
| 269 |
* |
| 270 |
* @access private |
| 271 |
* |
| 272 |
* @param int the indentation level for this tag. |
| 273 |
* @return string |
| 274 |
*/ |
| 275 |
function _render_indent($indent_level) { |
| 276 |
$indent = ""; |
| 277 |
if ( $this->indent_flag && $indent_level > 0) { |
| 278 |
$indent = str_repeat($this->_indent_str, $indent_level); |
| 279 |
} |
| 280 |
return $indent; |
| 281 |
} |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
?> |