| 1 |
<?php
|
| 2 |
// $Id: conditional_styles.theme.inc,v 1.5 2009/05/25 14:33:03 johnalbin Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Allows themes to add conditional stylesheets.
|
| 7 |
*
|
| 8 |
* @see http://msdn.microsoft.com/en-us/library/ms537512.aspx
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Return paths for the theme and its base themes.
|
| 13 |
*
|
| 14 |
* @param $theme
|
| 15 |
* The name of the theme.
|
| 16 |
* @return
|
| 17 |
* An array of all the theme paths.
|
| 18 |
*/
|
| 19 |
function conditional_styles_paths_to_basetheme($theme) {
|
| 20 |
static $theme_paths;
|
| 21 |
if (empty($theme_paths[$theme])) {
|
| 22 |
$theme_paths[$theme] = array();
|
| 23 |
$themes = list_themes();
|
| 24 |
// Grab the paths from the base theme.
|
| 25 |
if (!empty($themes[$theme]->base_theme)) {
|
| 26 |
$theme_paths[$theme] = conditional_styles_paths_to_basetheme($themes[$theme]->base_theme);
|
| 27 |
}
|
| 28 |
$theme_paths[$theme][$theme] = dirname($themes[$theme]->filename);
|
| 29 |
}
|
| 30 |
return $theme_paths[$theme];
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* When the theme registry is rebuilt, we also build the conditional stylesheets.
|
| 35 |
*/
|
| 36 |
function _conditional_styles_theme($existing, $type, $theme, $path) {
|
| 37 |
// @TODO: For PHP 4 compatibility we use foreach (array_keys($array) AS $key).
|
| 38 |
// When PHP 5 becomes required (Drupal 7.x), use the following faster
|
| 39 |
// implementation: foreach ($array AS $key => &$value) {}
|
| 40 |
|
| 41 |
// Process the conditional stylesheets for every active theme.
|
| 42 |
global $language;
|
| 43 |
$themes = list_themes();
|
| 44 |
foreach (array_keys($themes) AS $theme) {
|
| 45 |
// We only need to process active themes.
|
| 46 |
if ($themes[$theme]->status || $GLOBALS['theme'] == $theme) {
|
| 47 |
$paths = conditional_styles_paths_to_basetheme($theme);
|
| 48 |
|
| 49 |
// Grab all the conditional stylesheets.
|
| 50 |
$stylesheets = array();
|
| 51 |
// Start with the base theme and travel up the chain to the active theme.
|
| 52 |
foreach ($paths AS $theme_name => $path) {
|
| 53 |
// Look at the conditional-stylesheets defined in the theme's .info file.
|
| 54 |
if (!empty($themes[$theme_name]->info['conditional-stylesheets'])) {
|
| 55 |
foreach ($themes[$theme_name]->info['conditional-stylesheets'] AS $condition => $css) {
|
| 56 |
// Allow the theme to override its base themes' styles.
|
| 57 |
foreach ($css AS $media => $files) {
|
| 58 |
foreach ($files AS $file) {
|
| 59 |
$stylesheets[$condition][$media][$file] = $path;
|
| 60 |
}
|
| 61 |
}
|
| 62 |
}
|
| 63 |
}
|
| 64 |
}
|
| 65 |
// Render the stylesheets to link elements.
|
| 66 |
$conditional_styles = '';
|
| 67 |
if (!empty($stylesheets)) {
|
| 68 |
$query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);
|
| 69 |
$base_path = base_path();
|
| 70 |
foreach ($stylesheets AS $condition => $css) {
|
| 71 |
// Each condition requires its own set of links.
|
| 72 |
$output = '';
|
| 73 |
foreach ($css AS $media => $files) {
|
| 74 |
foreach ($files AS $file => $path) {
|
| 75 |
// Don't allow non-existent stylesheets to clutter the logs with 404.
|
| 76 |
if (file_exists("./$path/$file")) {
|
| 77 |
$output .= "<link type=\"text/css\" rel=\"stylesheet\" media=\"$media\" href=\"$base_path$path/$file$query_string\" />\n";
|
| 78 |
if ($language->direction == LANGUAGE_RTL){
|
| 79 |
$file_rtl = str_replace('.css', '-rtl.css', $file);
|
| 80 |
if (file_exists("./$path/$file_rtl")) {
|
| 81 |
$output .= "<link type=\"text/css\" rel=\"stylesheet\" media=\"$media\" href=\"$base_path$path/$file_rtl$query_string\" />\n";
|
| 82 |
}
|
| 83 |
}
|
| 84 |
}
|
| 85 |
}
|
| 86 |
}
|
| 87 |
if ($output) {
|
| 88 |
$conditional_styles .= "<!--[$condition]>\n$output<![endif]-->\n";
|
| 89 |
}
|
| 90 |
}
|
| 91 |
}
|
| 92 |
// Save the stylesheets for later retrieval.
|
| 93 |
if ($conditional_styles) {
|
| 94 |
variable_set('conditional_styles_' . $theme, $conditional_styles);
|
| 95 |
}
|
| 96 |
else {
|
| 97 |
variable_del('conditional_styles_' . $theme);
|
| 98 |
}
|
| 99 |
}
|
| 100 |
}
|
| 101 |
|
| 102 |
// Return nothing.
|
| 103 |
return array();
|
| 104 |
}
|