-class rcube_css
- {
- var $css_data = array();
-
- var $css_groups = array();
-
- var $include_files = array();
-
- var $grouped_output = TRUE;
-
- var $content_type = 'text/css';
-
- var $base_path = '';
-
- var $indent_chars = "\t";
-
-
- // add or overwrite a css definition
- // either pass porperty and value as separate arguments
- // or provide an associative array as second argument
- function set_style($selector, $property, $value='')
- {
- $a_elements = $this->_parse_selectors($selector);
- foreach ($a_elements as $element)
- {
- if (!is_array($property))
- $property = array($property => $value);
-
- foreach ($property as $name => $value)
- $this->css_data[$element][strtolower($name)] = $value;
- }
-
- // clear goups array
- $this->css_groups = array();
- }
-
-
- // unset a style property
- function remove_style($selector, $property)
- {
- if (!is_array($property))
- $property = array($property);
-
- foreach ($property as $key)
- unset($this->css_data[$selector][strtolower($key)]);
-
- // clear goups array
- $this->css_groups = array();
- }
-
-
- // define base path for external css files
- function set_basepath($path)
- {
- $this->base_path = preg_replace('/\/$/', '', $path);
- }
-
-
- // enable/disable grouped output
- function set_grouped_output($grouped)
- {
- $this->grouped_output = $grouped;
- }
-
-
- // add a css file as external source
- function include_file($filename, $media='')
- {
- // include multiple files
- if (is_array($filename))
- {
- foreach ($filename as $file)
- $this->include_file($file, $media);
- }
- // add single file
- else if (!in_array($filename, $this->include_files))
- $this->include_files[] = array('file' => $filename,
- 'media' => $media);
- }
-
-
- // parse css code
- function import_string($str)
- {
- $ret = FALSE;
- if (strlen($str))
- $ret = $this->_parse($str);
-
- return $ret;
- }
-
-
- // open and parse a css file
- function import_file($file)
- {
- $ret = FALSE;
-
- if (!is_file($file))
- return $ret;
-
- // for php version >= 4.3.0
- if (function_exists('file_get_contents'))
- $ret = $this->_parse(file_get_contents($file));
-
- // for order php versions
- else if ($fp = fopen($file, 'r'))
- {
- $ret = $this->_parse(fread($fp, filesize($file)));
- fclose($fp);
- }
-
- return $ret;
- }
-
-
- // copy all properties inherited from superior styles to a specific selector
- function copy_inherited_styles($selector)
- {
- // get inherited props from body and tag/class selectors
- $css_props = $this->_get_inherited_styles($selector);
-
- // write modified props back and clear goups array
- if (sizeof($css_props))
- {
- $this->css_data[$selector] = $css_props;
- $this->css_groups = array();
- }
- }
-
-
- // return css definition for embedding in HTML
- function show()
- {
- $out = '';
-
- // include external css files
- if (sizeof($this->include_files))
- foreach ($this->include_files as $file_arr)
- $out .= sprintf('<link rel="stylesheet" type="%s" href="%s"%s>'."\n",
- $this->content_type,
- $this->_get_file_path($file_arr['file']),
- $file_arr['media'] ? ' media="'.$file_arr['media'].'"' : '');
-
-
- // compose css string
- if (sizeof($this->css_data))
- $out .= sprintf("<style type=\"%s\">\n<!--\n\n%s-->\n</style>",
- $this->content_type,
- $this->to_string());
-
-
- return $out;
- }
-
-
- // return valid css code of the current styles grid
- function to_string($selector=NULL)
- {
- // return code for a single selector
- if ($selector)
- {
- $indent_str = $this->indent_chars;
- $this->indent_chars = '';
-
- $prop_arr = $this->to_array($selector);
- $out = $this->_style2string($prop_arr, TRUE);
-
- $this->indent_chars = $indent_str;
- }
-
- // compose css code for complete data grid
- else
- {
- $out = '';
- $css_data = $this->to_array();
-
- foreach ($css_data as $key => $prop_arr)
- $out .= sprintf("%s {\n%s}\n\n",
- $key,
- $this->_style2string($prop_arr, TRUE));
- }
-
- return $out;
- }
-
-
- // return a single-line string of a css definition
- function to_inline($selector)
- {
- if ($this->css_data[$selector])
- return str_replace('"', '\\"', $this->_style2string($this->css_data[$selector], FALSE));
- }
-
-
- // return an associative array with selector(s) as key and styles array as value
- function to_array($selector=NULL)
- {
- if (!$selector && $this->grouped_output)
- {
- // build groups if desired
- if (!sizeof($this->css_groups))
- $this->_build_groups();
-
- // modify group array to get an array(selector => properties)
- $out_arr = array();
- foreach ($this->css_groups as $group_arr)
- {
- $key = join(', ', $group_arr['selectors']);
- $out_arr[$key] = $group_arr['properties'];
- }
- }
- else
- $out_arr = $this->css_data;