/[drupal]/contributions/modules/style/style.module
ViewVC logotype

Contents of /contributions/modules/style/style.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.11 - (show annotations) (download) (as text)
Sat Dec 27 13:09:26 2008 UTC (11 months ago) by skiquel
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.10: +321 -1405 lines
File MIME type: text/x-php
updating these
1 <?php
2 // $ID:$
3 /**
4 * @file
5 * Style.module is a stylesheet override system for Drupal themes.
6 */
7
8 /**
9 * See if styles exist in a directory
10 *
11 * Checks for directories
12 *
13 * @param string $dir
14 * Directory to view
15 * @param boolean $refresh
16 * Bypass cache
17 * @return
18 * TRUE if styles exist. FALSE if not.
19 */
20 function style_exists($dir, $refresh = FALSE) {
21 static $exists;
22
23 if ($refresh === FALSE && isset($exists) && isset($exists[$dir])) {
24 return $exists[$dir];
25 }
26
27 if ($handle = opendir($dir)) {
28 while (FALSE !== ($file = readdir($handle))) {
29 if (is_dir($file)) {
30 // Directory (style) found.
31 return $exists[$dir] = TRUE;
32 }
33 }
34
35 // No directorys in styles
36 return $exists[$dir] = FALSE;
37
38 closedir($handle);
39 }
40 }
41
42 /**
43 * Scan for styles in a directory
44 *
45 * @param string $dir
46 * Directory of the theme or module styles
47 * @param boolean $refresh
48 Bypass cache
49 * @return array
50 * All styles in folder
51 */
52 function style_scan($dir, $refresh = FALSE) {
53 static $styles;
54
55 if ($refresh !== FALSE && isset($styles) && isset($styles[$dir])) {
56 return $styles[$dir];
57 }
58
59 if ($handle = opendir($dir)) {
60 $styles[$dir] = array();
61
62 while (FALSE !== ($file = readdir($handle))) {
63 if ($file != "." && $file != ".." && is_dir($dir . $file)) {
64 $styles[$dir][] = $file;
65 }
66 }
67
68 // No directorys in styles
69 closedir($handle);
70 }
71 return $styles[$dir];
72 }
73
74 /**
75 * Enclosure for style_scan_style_do
76 *
77 * @param string $dir
78 * Directory of style
79 * @param boolean $refresh
80 * Bypass cache
81 * @return array
82 * Recursive output of files.
83 */
84 function style_scan_style($dir, $refresh = FALSE) {
85 static $style;
86
87 if (isset($style[$dir]) && $refresh !== TRUE) {
88 return $style[$dir];
89 }
90
91 $style[$dir] = style_scan_style_do($dir);
92 return $style[$dir];
93 }
94
95 /**
96 * Get the structure of a style
97 *
98 * @param string $dir
99 * Directory of style
100 * @param boolean $show_files
101 * Scan files
102 * @return array
103 * Recursive output of files.
104 */
105 function style_scan_style_do($dir, $show_files = TRUE) {
106 $d = dir($dir); $x = array();
107 while (($r = $d->read()) !== FALSE) {
108 if ($r != "." && $r != ".." && (($show_files == FALSE && is_dir($dir.$r)) || $show_files == TRUE)) {
109 $x[$r] = (is_dir($dir.$r) ? array() : (is_file($dir.$r) ? TRUE : FALSE));
110
111
112 }
113 }
114 foreach ($x as $key => $value) {
115 if (is_dir($dir.$key."/")) {
116 $x[$key] = style_scan_style_do($dir.$key."/", $show_files);
117 }
118 }
119 ksort($x);
120 return $x;
121 }
122
123 /**
124 * Retrieve styles for theme/module/etc
125 *
126 * @param string $type
127 * 'module', 'theme'
128 * @param string name
129 * Name of the entity, i.e. 'garland'
130 * @return array
131 * Formatted array[full_path_to_style] = stylename
132 */
133 function style_get_styles($type, $name) {
134 $styles['local_dir'] = drupal_get_path($type, $name) . '/styles/';
135
136 $styles['files_dir'] = file_directory_path() . '/' . drupal_get_path($type, $name) . '/styles/';
137
138 $styles['list'] = array();
139 if (file_exists($styles['local_dir']) && style_exists($styles['local_dir'])) {
140 $style_scan = style_scan($styles['local_dir']);
141
142 foreach ($style_scan as $style) {
143 $styles['list'][$styles['local_dir'] . $style] = $style;
144 }
145 }
146
147 if (file_exists($styles['files_dir']) && style_exists($styles['files_dir'])) {
148 $style_scan = style_scan($styles['files_dir']);
149
150 foreach ($style_scan as $style) {
151 $styles['list'][$styles['files_dir'] . $style] = $style . " *";
152 }
153 }
154
155 return $styles;
156 }
157
158 function style_menu() {
159 $items['admin/build/styles'] = array(
160 'access arguments' => array('administer advanced forum'),
161 'description' => 'Configure Advanced Forum with these settings.',
162 'page arguments' => array('style_settings_page'),
163 'page callback' => 'drupal_get_form',
164 'title' => 'Styles',
165 );
166 return $items;
167 }
168
169 /**
170 * Form for our settings page
171 *
172 */
173 function style_settings_page() {
174 # Get a list of themes
175 $themes = array_keys(list_themes());
176
177 $form['diagnostic'] = array(
178 '#title' => t('Diagnostics'),
179 '#type' => 'fieldset',
180 '#description' => t('This module currently is set to detect themes stored
181 in /styles in your theme folders. It will also include
182 /files/themes/themename/styles.'),
183 );
184 foreach ($themes as $theme) {
185 $style_info = style_get_styles('theme', $theme);
186
187 $style_list = $style_info['list'];
188
189 if (isset($style_list) && count($style_list) > 0) {
190 $form['diagnostic'][$theme] = array(
191 '#type' => 'fieldset',
192 '#title' => t('style_scan(\'theme\', \'' . $theme . '\')'),
193 '#collapsible' => TRUE,
194 '#attributes' => array('class' => 'style-select-fieldset'),
195 );
196
197 drupal_add_css(drupal_get_path('module', 'style') .'/style.form.css', 'module', 'all', TRUE);
198
199 $form['diagnostic'][$theme]['theme_'.$theme.'_style'] = array(
200 '#type' => 'select',
201 '#title' => t('Style select box'),
202 '#description' => t('* generated style'),
203 '#options' => $style_list,
204 '#default_value' => variable_get('theme_'.$theme.'_style', 0),
205 );
206 }
207 }
208
209 // Send our form to Drupal to make a settings page
210 return system_settings_form($form);
211 }
212
213 function style_preprocess_page(&$vars) {
214 global $theme_key, $theme_info, $base_url;
215
216 if (dir(theme_get_setting('theme_'.$theme_key.'_style'))) {
217 $styles = array();
218 foreach (array_keys($vars['css']['all']['theme']) as $css) {
219 $styles[] = basename($css);
220 }
221
222 $newcss = style_scan_style(theme_get_setting('theme_'.$theme_key.'_style'));
223
224 $new_styles = array();
225 foreach (array_keys($newcss) as $css) {
226 $new_styles[] = theme_get_setting('theme_'.$theme_key.'_style') . '/' . $css;
227 }
228 $color_paths = $new_styles;
229 }
230 ///
231 /*********************************************************^^^^^^**| | *
232 * | D___| | *
233 * W A R N I N G W A R N I N G W A R N I N G | R_____| *
234 * /_ U| *
235 * <STYLESHEET>___O P|__<VOILA!>
236 * the machine | A| *
237 * CSS OVERRIDE IN PROGRESS BEEP BOOP BEEP BOOP | L| *
238 * |2008| *
239 *****************************************************************/
240 if (!empty($color_paths)) {
241 // Loop over theme CSS files and try to Rebuild CSS array with rewritten
242 // stylesheets. Keep the orginal order intact for CSS cascading.
243 $new_theme_css = array();
244
245 foreach ($vars['css']['all']['theme'] as $old_path => $old_preprocess) {
246 // Add the non-colored stylesheet first as we might not find a
247 // re-colored stylesheet for replacement later.
248 $new_theme_css[$old_path] = $old_preprocess;
249
250 // Loop over the path array with recolored CSS files to find matching
251 // paths which could replace the non-recolored paths.
252 foreach ($color_paths as $color_path) {
253 if (basename($old_path) == basename($color_path)) {
254 // Pull out the non-colored and add rewritten stylesheet.
255 unset($new_theme_css[$old_path]);
256 $new_theme_css[$color_path] = $old_preprocess;
257
258 // If the current language is RTL and the CSS file had an RTL variant,
259 // pull out the non-colored and add rewritten RTL stylesheet.
260 if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
261 $rtl_old_path = str_replace('.css', '-rtl.css', $old_path);
262 $rtl_color_path = str_replace('.css', '-rtl.css', $color_path);
263 if (file_exists($rtl_color_path)) {
264 unset($new_theme_css[$rtl_old_path]);
265 $new_theme_css[$rtl_color_path] = $old_preprocess;
266 }
267 }
268 break;
269 }
270 }
271 }
272 $vars['css']['all']['theme'] = $new_theme_css;
273 $vars['styles'] = drupal_get_css($vars['css']);
274 }
275
276 if (file_exists(theme_get_setting('theme_'.$theme_key.'_style') . '/logo.png')) {
277 $vars['logo'] = $base_url . '/' . theme_get_setting('theme_'.$theme_key.'_style') . '/logo.png';
278 }
279 }
280
281 /**
282 * Implementation of hook_form_alter().
283 */
284 function style_form_alter(&$form, $form_state, $form_id) {
285 global $base_url;
286 // Insert the color changer into the theme settings page.
287 if ($form_id == 'system_theme_settings' && arg(4)) {
288
289 if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) != FILE_DOWNLOADS_PUBLIC) {
290 // Disables the color changer when the private download method is used.
291 // TODO: This should be solved in a different way. See issue #181003.
292 drupal_set_message(t('The color picker only works if the <a href="@url">download method</a> is set to public.', array('@url' => url('admin/settings/file-system'))), 'warning');
293 }
294 else {
295 $theme = arg(4);
296
297 $style_info = style_get_styles('theme', $theme);
298
299 $style_list = $style_info['list'];
300
301 if (isset($style_list) && count($style_list) > 0 || count($form['style']['extensions']) > 0) {
302 drupal_add_css(drupal_get_path('module', 'style') .'/style_form.css', 'module', 'all', TRUE);
303
304 if (!isset($form['style'])) {
305 $form['style'] = array();
306 }
307 $form['style'] += array(
308 '#type' => 'fieldset',
309 '#title' => t('Style'),
310 '#description' => t('Change the look of your theme.'),
311 '#collapsible' => TRUE,
312 '#attributes' => array('class' => 'style-select-fieldset'),
313
314 '#weight' => -15,
315 );
316
317 // http://api.drupal.org/api/function/theme_get_settings/6
318 // We need to update theme_get_setting() in D7 to support per-theme
319 // caching in certain events.
320 $value = theme_get_settings($theme, 0);
321 $value = $value['theme_'.$theme.'_style'];
322
323 if (!isset($style_list) || count($style_list) == 0) {
324 $form['style']['theme_'.$theme.'_style'] = array(
325 '#type' => 'item',
326 '#value' => t('No styles available'),
327 '#default_value' => $value,
328 );
329 }
330 else {
331 $form['style']['theme_'.$theme.'_style'] = array(
332 '#type' => 'select',
333 '#title' => t('Default'),
334 '#description' => t('* generated style'),
335 '#options' => $style_list,
336 '#default_value' => $value,
337 //'#DANGEROUS_SKIP_CHECK' => TRUE
338
339 );
340 }
341 if (count($form['style']['extensions']) > 0) {
342 $form['style']['extensions'] += array(
343 '#type' => 'fieldset',
344 '#title' => t('Extensions'),
345 '#collapsible' => TRUE,
346 '#attributes' => array('class' => 'style-extensions-fieldset'),
347 '#weight' => 5,
348 );
349 }
350 }
351 }
352 }
353 }
354 ?>

  ViewVC Help
Powered by ViewVC 1.1.2