| 1 |
<?php |
<?php |
| 2 |
// $Id: conditional_styles.theme.inc,v 1.2 2008/09/14 09:57:17 johnalbin Exp $ |
// $Id: conditional_styles.theme.inc,v 1.3 2008/09/14 10:06:03 johnalbin Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 19 |
function conditional_styles_paths_to_basetheme($theme) { |
function conditional_styles_paths_to_basetheme($theme) { |
| 20 |
static $theme_paths; |
static $theme_paths; |
| 21 |
if (empty($theme_paths[$theme])) { |
if (empty($theme_paths[$theme])) { |
|
// Find all the base themes and their paths. |
|
|
$curr = $theme; |
|
|
$themes = list_themes(); |
|
| 22 |
$theme_paths[$theme] = array(); |
$theme_paths[$theme] = array(); |
| 23 |
do { |
$themes = list_themes(); |
| 24 |
// Save the theme name and the path. |
// Grab the paths from the base theme. |
| 25 |
$theme_paths[$theme][$curr] = dirname($themes[$curr]->filename); |
if (!empty($themes[$theme]->base_theme)) { |
| 26 |
} while (!empty($themes[$theme][$curr]->base_theme) && $curr = $themes[$theme][$curr]->base_theme); |
$theme_paths[$theme] = conditional_styles_paths_to_basetheme($themes[$theme]->base_theme); |
| 27 |
// Sort into base-theme-to-active-theme order. |
} |
| 28 |
$theme_paths[$theme] = array_reverse($theme_paths[$theme], TRUE); |
$theme_paths[$theme][$theme] = dirname($themes[$theme]->filename); |
| 29 |
} |
} |
| 30 |
return $theme_paths[$theme]; |
return $theme_paths[$theme]; |
| 31 |
} |
} |
| 32 |
|
|
| 34 |
* When the theme registry is rebuilt, we also build the conditional stylesheets. |
* When the theme registry is rebuilt, we also build the conditional stylesheets. |
| 35 |
*/ |
*/ |
| 36 |
function _conditional_styles_theme($existing, $type, $theme, $path) { |
function _conditional_styles_theme($existing, $type, $theme, $path) { |
| 37 |
// Process the conditional stylesheets. |
// @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 |
$themes = list_themes(); |
$themes = list_themes(); |
| 43 |
$paths = conditional_styles_paths_to_basetheme($theme); |
foreach (array_keys($themes) AS $theme) { |
| 44 |
|
// We only need to process active themes. |
| 45 |
|
if ($themes[$theme]->status) { |
| 46 |
|
$paths = conditional_styles_paths_to_basetheme($theme); |
| 47 |
|
|
| 48 |
// Grab all the conditional stylesheets. |
// Grab all the conditional stylesheets. |
| 49 |
$stylesheets = array(); |
$stylesheets = array(); |
| 50 |
// Start with the base theme and travel up the chain to the active theme. |
// Start with the base theme and travel up the chain to the active theme. |
| 51 |
foreach ($paths AS $theme_name => $path) { |
foreach ($paths AS $theme_name => $path) { |
| 52 |
// Look at the conditional-stylesheets defined in the theme's .info file. |
// Look at the conditional-stylesheets defined in the theme's .info file. |
| 53 |
if (!empty($themes[$theme_name]->info['conditional-stylesheets'])) { |
if (!empty($themes[$theme_name]->info['conditional-stylesheets'])) { |
| 54 |
foreach ($themes[$theme_name]->info['conditional-stylesheets'] AS $condition => $css) { |
foreach ($themes[$theme_name]->info['conditional-stylesheets'] AS $condition => $css) { |
| 55 |
// Allow the theme to override its base themes' styles. |
// Allow the theme to override its base themes' styles. |
| 56 |
foreach ($css AS $media => $files) { |
foreach ($css AS $media => $files) { |
| 57 |
foreach ($files AS $file) { |
foreach ($files AS $file) { |
| 58 |
$stylesheets[$condition][$media][$file] = $path; |
$stylesheets[$condition][$media][$file] = $path; |
| 59 |
|
} |
| 60 |
|
} |
| 61 |
} |
} |
| 62 |
} |
} |
| 63 |
} |
} |
| 64 |
} |
// Render the stylesheets to link elements. |
| 65 |
} |
$conditional_styles = ''; |
| 66 |
// Render the stylesheets to link elements. |
if (!empty($stylesheets)) { |
| 67 |
$conditional_styles = ''; |
$query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1); |
| 68 |
if (!empty($stylesheets)) { |
$base_path = base_path(); |
| 69 |
$query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1); |
foreach ($stylesheets AS $condition => $css) { |
| 70 |
$base_path = base_path(); |
// Each condition requires its own set of links. |
| 71 |
foreach ($stylesheets AS $condition => $css) { |
$output = ''; |
| 72 |
// Each condition requires its own set of links. |
foreach ($css AS $media => $files) { |
| 73 |
$output = ''; |
foreach ($files AS $file => $path) { |
| 74 |
foreach ($css AS $media => $files) { |
// Don't allow non-existent stylesheets to clutter the logs with 404. |
| 75 |
foreach ($files AS $file => $path) { |
if (file_exists("./$path/$file")) { |
| 76 |
// Don't allow non-existent stylesheets to clutter the logs with 404. |
$output .= "<link type=\"text/css\" rel=\"stylesheet\" media=\"$media\" href=\"$base_path$path/$file$query_string\" />\n"; |
| 77 |
if (file_exists("./$path/$file")) { |
} |
| 78 |
$output .= "<link type=\"text/css\" rel=\"stylesheet\" media=\"$media\" href=\"$base_path$path/$file$query_string\" />\n"; |
} |
| 79 |
|
} |
| 80 |
|
if ($output) { |
| 81 |
|
$conditional_styles .= "<!--[$condition]>\n$output<![endif]-->\n"; |
| 82 |
} |
} |
| 83 |
} |
} |
| 84 |
} |
} |
| 85 |
if ($output) { |
// Save the stylesheets for later retrieval. |
| 86 |
$conditional_styles .= "<!--[$condition]>\n$output<![endif]-->\n"; |
if ($conditional_styles) { |
| 87 |
|
variable_set('conditional_styles_' . $theme, $conditional_styles); |
| 88 |
|
} |
| 89 |
|
else { |
| 90 |
|
variable_del('conditional_styles_' . $theme); |
| 91 |
} |
} |
| 92 |
} |
} |
| 93 |
} |
} |
| 94 |
// Save the stylesheets for later retrieval. |
|
|
if ($conditional_styles) { |
|
|
variable_set('conditional_styles_' . $theme, $conditional_styles); |
|
|
} |
|
|
else { |
|
|
variable_del('conditional_styles_' . $theme); |
|
|
} |
|
| 95 |
// Return nothing. |
// Return nothing. |
| 96 |
return array(); |
return array(); |
| 97 |
} |
} |