Issues (1839)

zen/zen-internals/template.conditional-styles.inc (3 issues)

1
<?php
2
// The code for conditional stylesheets started out as a patch for Zen. Now that
3
// it has been spun out to its own separate module, It would be nice to prevent
4
// code drift between the Zen implementation and the conditional_styles.module,
5
// so Zen now includes an exact copy of conditonal_style module's
6
// conditional_styles.theme.inc file.
7
8
/**
9
 * @file
10
 * Allows themes to add conditional stylesheets.
11
 *
12
 * @see http://msdn.microsoft.com/en-us/library/ms537512.aspx
13
 */
14
15
/**
16
 * Return paths for the theme and its base themes.
17
 *
18
 * @param $theme
19
 *   The name of the theme.
20
 * @return
21
 *   An array of all the theme paths.
22
 */
23
function conditional_styles_paths_to_basetheme($theme) {
24
  static $theme_paths;
25
  if (empty($theme_paths[$theme])) {
26
    $theme_paths[$theme] = array();
27
    $themes = list_themes();
28
    // Grab the paths from the base theme.
29
    if (!empty($themes[$theme]->base_theme)) {
30
      $theme_paths[$theme] = conditional_styles_paths_to_basetheme($themes[$theme]->base_theme);
31
    }
32
    $theme_paths[$theme][$theme] = dirname($themes[$theme]->filename);
33
  }
34
  return $theme_paths[$theme];
35
}
36
37
/**
38
 * When the theme registry is rebuilt, we also build the conditional stylesheets.
39
 */
40
function _conditional_styles_theme($existing, $type, $theme, $path) {
41
  // Process the conditional stylesheets for every active theme.
42
  $themes = list_themes();
43
  foreach (array_keys($themes) AS $theme) {
0 ignored issues
show
$theme is overwriting one of the parameters of this function.
Loading history...
44
    // We only need to process active themes.
45
    if ($themes[$theme]->status || $GLOBALS['theme'] == $theme) {
46
      $paths = conditional_styles_paths_to_basetheme($theme);
47
48
      // Grab all the conditional stylesheets.
49
      $stylesheets = array();
50
      // Start with the base theme and travel up the chain to the active theme.
51
      foreach ($paths AS $theme_name => $path) {
0 ignored issues
show
$path is overwriting one of the parameters of this function.
Loading history...
52
        // Look at the conditional-stylesheets defined in the theme's .info file.
53
        if (!empty($themes[$theme_name]->info['conditional-stylesheets'])) {
54
          foreach ($themes[$theme_name]->info['conditional-stylesheets'] AS $condition => $css) {
55
            // Allow the theme to override its base themes' styles.
56
            foreach ($css AS $media => $files) {
57
              foreach ($files AS $file) {
58
                $stylesheets[$condition][$media][$file] = $path;
59
              }
60
            }
61
          }
62
        }
63
      }
64
      // Render the stylesheets to link elements.
65
      $conditional_styles = $conditional_styles_rtl = '';
66
      if (!empty($stylesheets)) {
67
        $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);
68
        $base_path = base_path();
69
        foreach ($stylesheets AS $condition => $css) {
70
          // Each condition requires its own set of links.
71
          $output = $output_rtl = '';
72
          foreach ($css AS $media => $files) {
73
            foreach ($files AS $file => $path) {
0 ignored issues
show
$path is overwriting one of the parameters of this function.
Loading history...
74
              // Don't allow non-existent stylesheets to clutter the logs with 404.
75
              if (file_exists("./$path/$file")) {
76
                $link = "<link type=\"text/css\" rel=\"stylesheet\" media=\"$media\" href=\"$base_path$path/$file$query_string\" />\n";
77
                $output .= $link;
78
                $output_rtl .= $link;
79
                $file_rtl = str_replace('.css', '-rtl.css', $file);
80
                if (file_exists("./$path/$file_rtl")) {
81
                  $output_rtl .= "<link type=\"text/css\" rel=\"stylesheet\" media=\"$media\" href=\"$base_path$path/$file_rtl$query_string\" />\n";
82
                }
83
              }
84
            }
85
          }
86
          if ($output) {
87
            $conditional_styles .= "<!--[$condition]>\n$output<![endif]-->\n";
88
            $conditional_styles_rtl .= "<!--[$condition]>\n$output_rtl<![endif]-->\n";
89
          }
90
        }
91
      }
92
      // Save the stylesheets for later retrieval.
93
      if ($conditional_styles) {
94
        if (db_is_active()) {
95
          variable_set('conditional_styles_' . $theme, $conditional_styles);
96
          variable_set('conditional_styles_' . $theme . '_rtl', $conditional_styles_rtl);
97
        }
98
        else {
99
          $GLOBALS['conf']['conditional_styles_' . $theme] = $conditional_styles;
100
          $GLOBALS['conf']['conditional_styles_' . $theme . '_rtl'] = $conditional_styles_rtl;
101
        }
102
      }
103
      elseif (db_is_active()) {
104
        variable_del('conditional_styles_' . $theme);
105
        variable_del('conditional_styles_' . $theme . '_rtl');
106
      }
107
    }
108
  }
109
110
  // Return nothing.
111
  return array();
112
}
113