/[drupal]/contributions/modules/color_soc08/color.module
ViewVC logotype

Contents of /contributions/modules/color_soc08/color.module

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


Revision 1.25 - (show annotations) (download) (as text)
Fri Aug 22 10:21:56 2008 UTC (15 months ago) by skiquel
Branch: MAIN
CVS Tags: HEAD
Changes since 1.24: +553 -681 lines
File MIME type: text/x-php
head commit
1 <?php
2 // $Id: color.module,v 1.18.2.6 2008/08/22 09:57:31 skiquel Exp $
3 /**
4 * @file
5 * Allows users and webmasters to change colors schemes in enabled themes
6 */
7
8 /**
9 * Implementation of hook_help().
10 */
11 function color_help($path, $arg) {
12 switch ($path) {
13 case 'admin/help#color':
14 $output = '<p>'. t('The color module allows a site administrator to quickly and easily change the color scheme of certain themes. Although not all themes support color module, both Garland (the default theme) and Minnelli were designed to take advantage of its features. By using color module with a compatible theme, you can easily change the color of links, backgrounds, text, and other theme elements. Color module requires that your <a href="@url">file download method</a> be set to public.', array('@url' => url('admin/settings/file-system'))) .'</p>';
15 $output .= '<p>'. t("It is important to remember that color module saves a modified copy of the theme's specified stylesheets in the files directory. This means that if you make any manual changes to your theme's stylesheet, you must save your color settings again, even if they haven't changed. This causes the color module generated version of the stylesheets in the files directory to be recreated using the new version of the original file.") .'</p>';
16 $output .= '<p>'. t('To change the color settings for a compatible theme, select the "configure" link for the theme on the <a href="@themes">themes administration page</a>.', array('@themes' => url('admin/build/themes'))) .'</p>';
17 $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@color">Color module</a>.', array('@color' => 'http://drupal.org/handbook/modules/color/')) .'</p>';
18 return $output;
19 }
20 }
21
22 /**
23 * Implementation of hook_theme().
24 */
25 function color_theme() {
26 return array(
27 'color_scheme_form' => array(
28 'arguments' => array('form' => NULL),
29 ),
30 'color_edit_scheme_form' => array(
31 'arguments' => array('form' => NULL),
32 ),
33 'scheme-selectbox-wrapper' => array(
34 'arguments' => array('form' => NULL),
35 ),
36 );
37 }
38
39 /**
40 * Implementation of hook_menu()
41 */
42 function color_menu() {
43 $items['color/js'] = array(
44 'page callback' => 'color_js',
45 'access arguments' => array('choose color scheme'),
46 'type' => MENU_CALLBACK,
47 );
48 foreach (list_themes() as $theme) {
49 $items['admin/build/themes/settings/'. $theme->name .'/%'] = array(
50 'page callback' => 'drupal_get_form',
51 'title callback' => 'color_get_title',
52 'title arguments' => array(5),
53 'page arguments' => array('color_edit_scheme_form', 4, 5),
54 'type' => MENU_CALLBACK,
55 // 'access arguments' => array('administer access control'),
56 'access arguments' => array('access administration pages'),
57 );
58 }
59 return $items;
60 }
61
62 /**
63 * Implementation of hook_perm().
64 */
65 function color_perm() {
66 return array('choose color scheme');
67 }
68
69 /**
70 * Callback for the theme to alter the resources used.
71 */
72 function _color_page_alter(&$vars) {
73 global $language, $theme_key, $user;
74 $uid = $user->uid;
75 //drupal_set_message(kprint_r(gd_info(), TRUE));
76 /**
77 * If logged in and has a SESSION var from anonymous use, delete.
78 */
79 if ($uid != 0 && isset($_SESSION['scheme_id'])) {
80 unset($_SESSION['scheme_id']);
81 }
82 if (isset($theme_key)) {
83 $theme = $theme_key;
84 }
85 else {
86 $theme = empty($vars['user']->theme) ? variable_get('theme_default', 'garland') : $vars['user']->theme;
87 }
88
89 module_load_include('inc', 'color', 'color.database');
90 module_load_include('inc', 'color', 'color.misc');
91
92 $scheme = color_find_scheme_by_UID($uid, $theme, 'extra_attr');
93 if (!color_scheme_id_valid($scheme['page_id'], $theme)) {
94 return;
95 }
96 $extra_attr = unserialize($scheme['extra_attr']);
97 $color_paths = $extra_attr['stylesheets'];
98 if ($extra_attr['logo']) {
99 $logo = $extra_attr['logo'];
100 }
101
102 // Override stylesheets.
103 if (!empty($color_paths)) {
104 // Loop over theme CSS files and try to Rebuild CSS array with rewritten
105 // stylesheets. Keep the orginal order intact for CSS cascading.
106 $new_theme_css = array();
107
108 foreach ($vars['css']['all']['theme'] as $old_path => $old_preprocess) {
109 // Add the non-colored stylesheet first as we might not find a
110 // re-colored stylesheet for replacement later.
111 $new_theme_css[$old_path] = $old_preprocess;
112
113 // Loop over the path array with recolored CSS files to find matching
114 // paths which could replace the non-recolored paths.
115 foreach ($color_paths as $color_path) {
116 // Color module currently requires unique file names to be used,
117 // which allows us to compare different file paths.
118 if (basename($old_path) == basename($color_path)) {
119 // Pull out the non-colored and add rewritten stylesheet.
120 unset($new_theme_css[$old_path]);
121 $new_theme_css[$color_path] = $old_preprocess;
122
123 // If the current language is RTL and the CSS file had an RTL variant,
124 // pull out the non-colored and add rewritten RTL stylesheet.
125 if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
126 $rtl_old_path = str_replace('.css', '-rtl.css', $old_path);
127 $rtl_color_path = str_replace('.css', '-rtl.css', $color_path);
128 if (file_exists($rtl_color_path)) {
129 unset($new_theme_css[$rtl_old_path]);
130 $new_theme_css[$rtl_color_path] = $old_preprocess;
131 }
132 }
133 break;
134 }
135 }
136 }
137 $vars['css']['all']['theme'] = $new_theme_css;
138 $vars['styles'] = drupal_get_css($vars['css']);
139 }
140
141 // Override logo.
142 if ($logo && $vars['logo'] && preg_match('!'. $theme_key .'/logo.png$!', $vars['logo'])) {
143 if (file_exists($_SERVER["DOCUMENT_ROOT"] . base_path() . $logo)) {
144 $vars['logo'] = base_path() . $logo;
145 }
146 else {
147 //$vars['logo'] = NULL;
148 }
149 }
150 }
151
152 /**
153 * Implementation of hook_block()
154 */
155 function color_block($op='list', $delta=0) {
156 global $user;
157
158 module_load_include('inc', 'color', 'color.misc');
159 module_load_include('inc', 'color', 'color.database');
160
161 $theme = (!isset($user->theme) || empty($user->theme)) ? variable_get('theme_default', 'garland'): $user->theme;
162
163 if ($op == 'list') {
164 $blocks[0]["info"] = t('Pick color scheme');
165 $blocks[0]["description"] = t('User color scheme selection');
166 return $blocks;
167 }
168 else if ($op == 'view' && user_access('choose color scheme') && !(($user->uid == 0) && (variable_get('page_compression', 0) != 0) || (variable_get('cache', 0) != 0)) && color_compatible($theme) && color_theme_info_generated($theme) && color_theme_scheme_generated($theme)) {
169
170 if (!((variable_get('page_compression', 1) == 0) || (variable_get('cache', 0) == 0)) && in_array('anonymous user', $user->roles)) {
171 drupal_set_message("Anonymous users cannot change schemes w/ Page Compression or Caching on!", "info", FALSE);
172 }
173
174 $block['content'] = "";
175 switch ($delta) {
176 case 0:
177 $block['content'] .= drupal_get_form('color_block_form');
178 break;
179 }
180
181 $block['subject'] = t('Choose scheme');
182
183 return $block;
184 }
185 }
186
187 function color_block_form() {
188 global $user;
189 $uid = $user->uid;
190
191 $theme = (!isset($user->theme) || empty($user->theme)) ? variable_get('theme_default', 'garland'): $user->theme;
192
193 module_load_include('inc', 'color', 'color.database');
194
195 $form['color']['#tree'] = TRUE;
196
197 $info = color_get_theme_info($theme, 'sql');
198 $scheme = color_find_scheme_by_UID($uid, $theme);
199 $current = $scheme['id'];
200
201 $info['scheme_ids'] = array('<Official site scheme>' => 0)+$info['scheme_ids'];
202 $form['color']['colorselect'] = array(
203 '#type' => 'select',
204 '#options' => array_flip($info['scheme_ids']),
205 '#default_value' => $current,
206 );
207
208 $form['currenttheme'] = array(
209 '#type' => 'value',
210 '#value' => $theme,
211 );
212
213 $form['op'] = array('#type' => 'submit', '#value' => t('Select'), '#weight' => 3);
214
215 // if ($user->uid == 1) {
216 if ($user->uid == 1 || user_access('access administration pages')) {
217 $form['edit_scheme'] = array('#type' => 'submit', '#value' => t('Edit'), '#weight' => 3);
218 }
219
220 $form['#submit'][] = 'color_theme_select_submit';
221
222 return $form;
223 }
224
225
226 /**
227 * Add color selection for Users
228 */
229 function color_user($op, &$edit, &$account, $category) {
230 global $user;
231
232 $theme = !empty($edit['theme']) ? $edit['theme'] : variable_get('theme_default', 'garland');
233
234 if ($op == 'form' && $category == 'account' && user_access('choose color scheme')) {
235
236 module_load_include('inc', 'color', 'color.database');
237 module_load_include('inc', 'color', 'color.misc');
238
239 if (color_compatible($theme) && color_theme_info_generated($theme) && color_theme_scheme_generated($theme)) {
240 $uid = $user->uid;
241 if (color_compatible($theme)) {
242 $info = color_get_theme_info($theme);
243
244 $form['color'] = array(
245 '#type' => 'fieldset',
246 '#title' => t('Color scheme'),
247 '#description' => t('Your selected theme (@theme) has color schemes!', array('@theme' => $theme)),
248 '#weight' => 3,
249 '#collapsible' => TRUE
250 );
251
252 $form['color']['#tree'] = TRUE;
253
254 $scheme = color_find_scheme_by_UID($uid, $theme, 'id');
255 $current = $scheme['id'];
256 $info['scheme_ids'] = array('<Official site scheme>' => 0)+$info['scheme_ids'];
257
258 $form['color']['colorselect'] = array(
259 '#type' => 'select',
260 '#title' => t('Scheme'),
261 '#options' => array_flip($info['scheme_ids']),
262 '#default_value' => $current,
263 );
264
265 $form['currenttheme'] = array(
266 '#type' => 'value',
267 '#value' => $theme,
268 );
269
270 }
271 return $form;
272 }
273 }
274 }
275
276 /**
277 * Processes forms from color_block_form() and color_user().
278 * Sets user scheme.
279 */
280 function color_theme_select_submit($form, &$form_state) {
281 global $user;
282 $uid = $user->uid;
283 $theme = empty($form['currenttheme']['#value']) ? variable_get('theme_default', 'garland') : $form['currenttheme']['#value'];
284 $scheme_id = $form['#post']['color']['colorselect'];
285
286 if ($form_state['clicked_button']['#value'] == "Edit") {
287 if ($scheme_id != 0) {
288 drupal_goto('admin/build/themes/settings/' . $theme . '/' . $scheme_id);
289 }
290 return;
291 }
292 else {
293 module_load_include('inc', 'color', 'color.database');
294 // Check for user scheme in table. Place in $userscheme array if exists
295 $results = db_fetch_array(db_query("SELECT COUNT(*) AS count FROM {color_users} WHERE uid = '%s'", $uid));
296 $userrows = $results['count'];
297
298 // If theme has not be changed
299 if ($uid != 0 && $userrows > 0) {
300 color_set_user_scheme($scheme_id, $uid, 'update');
301 }
302 elseif ($uid != 0 && !($scheme = color_get_scheme('uid', $uid, 'scheme_id'))) {
303 color_set_user_scheme($scheme_id, $uid, 'insert');
304 }
305 elseif (!color_scheme_id_valid($scheme_id, $theme) && $uid != 0) {
306 color_set_user_scheme($scheme_id, $uid, 'delete');
307 }
308 elseif ($uid == 0 && user_access('choose color scheme') && (variable_get("page_compression", 1) == 0) && variable_get('cache', 0) == 0) {
309 color_set_user_scheme($scheme_id, $uid);
310 }
311 }
312 }
313
314
315 /**
316 * Implementation of hook_form_alter().
317 */
318 function color_form_alter(&$form, $form_state, $form_id) {
319
320 module_load_include('inc', 'color', 'color.misc');
321 // Insert the color changer into the theme settings page.
322 if ($form_id == 'system_theme_settings' && arg(4) && color_compatible(arg(4)) && function_exists('gd_info')) {
323
324 module_load_include('inc', 'color', 'color.database');
325 color_check_themes(arg(4));
326
327 if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) != FILE_DOWNLOADS_PUBLIC) {
328 // Disables the color changer when the private download method is used.
329 // TODO: This should be solved in a different way. See issue #181003.
330 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');
331 }
332 else {
333 $form['color'] = array(
334 '#type' => 'fieldset',
335 '#title' => t('Color scheme'),
336 '#weight' => -1,
337 '#attributes' => array('id' => 'color_scheme_form'),
338 '#theme' => 'color_scheme_form',
339 );
340
341 $form['color'] += color_scheme_form($form_state, arg(4));
342 $form['#submit'][] = 'color_scheme_form_submit';
343 }
344 }
345
346 if ($form_id == 'system_themes_form' || isset($form['theme_select']['themes'])) {
347 $themes = color_list_colorable_themes();
348 }
349
350 // For admin/build/themes
351 if ($form_id == 'system_themes_form') {
352 foreach ($themes as $theme) {
353 $screenshot = variable_get('color_'. $theme .'_screenshot', NULL);
354
355 $form[$theme]['info']['#value']['description'] .= '<br /><strong>Color-enabled </strong>';
356 if (($screenshot = variable_get('color_'. $theme .'_screenshot', NULL)) && isset($form[$theme]['screenshot'])) {
357 $form[$theme]['screenshot']['#value'] = theme('image', $screenshot, '', '', array('class' => 'screenshot'), FALSE);
358 }
359 }
360 }
361
362 // For user "Theme configuration" at /user/{uid}/edit
363 if (isset($form['theme_select']['themes'])) {
364 global $user;
365 foreach ($themes as $theme) {
366 // Due to the structure of the arrays showing default theme blank.
367
368 $theme_key = ((isset($form['theme_select']['themes']['']['description']['#title'])) && (variable_get('theme_default', 'garland') == $theme)) ? NULL : $theme;
369
370 module_load_include('inc', 'color', 'color.database');
371 $screenshot = NULL;
372 if (($scheme = color_get_scheme('uid', $user->uid, array('extra_attr', 'theme'))) && $theme_key == $scheme['theme']) {
373
374 $extra_attr = unserialize($scheme['extra_attr']);
375 $screenshot = isset($extra_attr['screenshot']) ? $extra_attr['screenshot'] : NULL;
376 }
377
378 // If: Make sure only enabled themes show color description.
379 if ($form['theme_select']['themes'][$theme_key]) {
380 $form['theme_select']['themes'][$theme_key]['description']['#value'] .= "<br /><strong>Color schemes available</strong>";
381 }
382
383 if (isset($form['theme_select']['themes'][$theme_key]['screenshot']) && isset($screenshot)) {
384 $form['theme_select']['themes'][$theme_key]['screenshot']['#value'] = theme('image', $screenshot, '', '', array('class' => 'screenshot'), FALSE);
385 }
386 }
387
388 $form['#submit'][] = 'color_theme_select_submit';
389 }
390 }
391
392 /**
393 * Form callback. Returns the configuration form.
394 */
395 function color_scheme_form(&$form_state, $theme) {
396 module_load_include('inc', 'color', 'color.misc');
397 module_load_include('inc', 'color', 'color.database');
398
399 $info = color_get_theme_info($theme);
400
401 // See if we're using a predefined scheme
402 $current = $info['default_scheme'];
403
404 $form = array(
405 '#prefix' => '<div id="color-scheme-wrapper">',
406 '#suffix' => '</div>',
407 );
408
409 $form['default'] = array(
410 '#type' => 'fieldset',
411 '#title' => t('Default scheme'),
412 '#prefix' => '<div id="default-scheme-wrapper">',
413 '#suffix' => '</div>',
414 '#weight' => -5,
415 '#description' => t('Select the default scheme for this theme.'),
416 );
417
418 $form['default']['default_scheme'] = array(
419 '#type' => 'select',
420 '#options' => array_flip($info['scheme_ids']),
421 '#default_value' => $current,
422 );
423
424 $form['default']['button'] = array(
425 '#type' => 'submit',
426 '#value' => t('Set scheme'),
427 );
428
429 $form['new-scheme-wrapper'] = array(
430 '#tree' => TRUE,
431 '#prefix' => '<div id="new-scheme-wrapper">',
432 '#suffix' => '</div>',
433 '#type' => 'fieldset',
434 '#title' => t('Create a scheme'),
435 '#description' => t('Type in the name of your new creation.'),
436 );
437
438 $form['new-scheme-wrapper']['textfield'] = array(
439 '#type' => 'textfield',
440 // '#title' => 'New scheme',
441 '#size' => 12
442 );
443
444 $form['new-scheme-wrapper']['button'] = array(
445 '#type' => 'submit',
446 '#value' => t('Add scheme'),
447 );
448
449 $form['edit'] = array(
450 '#prefix' => '<div id="edit-scheme-wrapper">',
451 '#suffix' => '</div>',
452 '#type' => 'fieldset',
453 '#title' => t('Edit a scheme'),
454 '#description' => t('Modify a scheme.'),
455 '#tree' => TRUE,
456 );
457
458 $form['edit']['scheme'] = array(
459 '#type' => 'select',
460 '#options' => array_flip($info['scheme_ids']),
461 '#default_value' => $current,
462 );
463
464 $form['edit']['button'] = array(
465 '#type' => 'submit',
466 '#value' => t('Edit this scheme'),
467 );
468
469 $form['rebuild-scheme-wrapper'] = array(
470 '#type' => 'fieldset',
471 '#prefix' => '<div id="rebuild-scheme-wrapper">',
472 '#suffix' => '</div>',
473 '#weight' => -5,
474 '#title' => 'Rebuild schemes',
475 '#description' => t('Rebuild your entire scheme database.'),
476 );
477
478 $form['rebuild-scheme-wrapper']['button'] = array(
479 '#type' => 'submit',
480 '#value' => t('Rebuild schemes'),
481 );
482
483 $path = drupal_get_path('theme', $theme);
484 $file = $path .'/color/color.inc';
485 if ($path && file_exists($file)) {
486 $color_inc = file_get_contents($file);
487 }
488
489 $form['color-inc-scheme-wrapper'] = array(
490 '#type' => 'fieldset',
491 '#prefix' => '<div id="color-inc-wrapper">',
492 '#suffix' => '</div>',
493 '#weight' => -5,
494 '#title' => 'Color.inc viewer (for theme developers)',
495 '#description' => t('Allows demo users to see the theme\'s color.inc.'),
496 );
497
498 $form['color-inc-scheme-wrapper']['textfield'] = array(
499 '#type' => 'textarea',
500 '#default_value' => $color_inc,
501 );
502
503 $form['scheme_ids'] = array(
504 '#type' => 'hidden',
505 '#value' => serialize($info['scheme_ids']),
506 );
507 $form['schemes'] = array(
508 '#type' => 'hidden',
509 '#value' => serialize($info['schemes']),
510 );
511
512 $form['theme'] = array('#type' => 'hidden', '#value' => arg(4));
513 $form['info'] = array('#type' => 'value', '#value' => $info);
514
515 return $form;
516 }
517
518 /**
519 * Theme color form.
520 *
521 * @ingroup @themeable
522 */
523 function theme_color_scheme_form($form) {
524
525 $base = drupal_get_path('module', 'color');
526 drupal_add_css($base .'/color.css', 'module', 'all', TRUE);
527
528 // Include stylesheet
529 $theme = $form['theme']['#value'];
530 $info = $form['info']['#value'];
531 $path = drupal_get_path('theme', $theme) .'/';
532 $output = '';
533
534 $output .= '<div class="clear-block">';
535 $output .= drupal_render($form['default']);
536 $output .= drupal_render($form['rebuild-scheme-wrapper']);
537 $output .= "</div>";
538
539 $output .= '<div class="clear-block">';
540 $output .= drupal_render($form['edit']);
541 $output .= drupal_render($form['new-scheme-wrapper']);
542 $output .= "</div>";
543
544 $output .= drupal_render($form);
545 return $output;
546 }
547
548 /**
549 * Submit handler for color change form.
550 */
551 function color_scheme_form_submit($form, &$form_state) {
552 if (!isset($form_state['values']['info'])) {
553 return;
554 }
555 module_load_include('inc', 'color', 'color.database');
556
557 $theme = $form_state['values']['theme'];
558 $info = $form_state['values']['info'];
559 $palette = $form_state['values']['palette'];
560 $scheme_id = $form_state['clicked_button']['#post']['edit']['scheme'];
561 $default_scheme_id = $form_state['clicked_button']['#post']['default_scheme'];
562
563 if ($form_state['clicked_button']['#value'] == "Rebuild schemes") {
564 color_store_reset($theme);
565 return;
566 }
567 elseif ($form['#post']['op'] == "Edit this scheme") {
568 drupal_goto('admin/build/themes/settings/' . $theme . '/' . $scheme_id);
569 return;
570 }
571 elseif ($form_state['clicked_button']['#value'] == "Set scheme") {
572 db_query('UPDATE {color} SET value = \'%s\' WHERE theme = \'%s\' AND name = \'%s\'', $default_scheme_id, $theme, 'default_scheme');
573 }
574 elseif ($form['#post']['op'] == 'Add scheme' || strlen($_POST['add']['textfield']) > 0) {
575 if ($scheme = color_get_scheme('scheme_id', $default_scheme_id)) {
576 $hex = array_combine($info['fields'], unserialize($scheme['hex']));
577 }
578 $vars = array(
579 'name' => $_POST['new-scheme-wrapper']['textfield'],
580 'theme' => $theme,
581 'hex' => $hex,
582 );
583
584 $last_id = color_set_scheme(NULL, $vars, 'insert');
585
586 drupal_goto('admin/build/themes/settings/' . $theme . '/' . $last_id);
587 }
588 }
589
590 function color_edit_scheme_form() {
591 global $user;
592
593 $theme = arg(4);
594 $scheme_id = arg(5);
595 module_load_include('inc', 'color', 'color.database');
596
597 $base = drupal_get_path('module', 'color');
598 $info = color_get_theme_info($theme);
599
600 // Add Farbtastic color picker
601 drupal_add_css('misc/farbtastic/farbtastic.css', 'module', 'all', FALSE);
602 drupal_add_js($base .'/farbtastic.js');
603
604 // Add custom CSS/JS
605 drupal_add_css($base .'/color-edit.css', 'module', 'all', FALSE);
606 drupal_add_js($base .'/color.js');
607
608 $reference = array_combine($info['fields'], $info['reference_scheme']);
609
610 // See if we're using a predefined scheme
611 if ($schemeinfo = color_get_scheme('scheme_id', $scheme_id, array('hex', 'name'))) {
612 $palette = unserialize($schemeinfo['hex']);
613 $palette = array_combine($info['fields'], $palette);
614 $schemename = $schemeinfo['name'];
615 }
616
617 // For future reference! By changing this option, we change the way the 'lock' functionality works
618 // 'reference' => $palette will retain hue, modifying saturation and lightness (SL)
619 // 'reference' => $reference will switch all to the same hue, apparently only affecting lightness.
620 drupal_add_js(array('color' => array(
621 'reference' => $palette,
622 )), 'setting');
623
624 $breadcrumb = drupal_get_breadcrumb();
625 $breadcrumb[] = l($theme, 'admin/build/themes/settings/' . $theme);
626 $breadcrumb[] = $schemename . " (Scheme)";
627 drupal_set_breadcrumb($breadcrumb);
628
629 $current = $info['default_scheme'];
630
631 $form = array(
632 '#weight' => -1,
633 '#prefix' => '<div id="color-scheme-wrapper">',
634 '#suffix' => '</div>',
635 '#tree' => TRUE,
636 // '#attributes' => array('id' => 'color_scheme_form'),
637 '#theme' => 'color_edit_scheme_form',
638 );
639
640 $alter_links = "<a href=\"#\" id=\"darken\">darken</a> | <a href=\"#\" id=\"lighten\">lighten</a> | <a href=\"#\" id=\"randomize\">randomize</a>";
641
642 $path = drupal_get_path('theme', $theme) .'/';
643
644 // Preview
645 if ($path && file_exists($path .'color/preview.inc')) {
646 $preview = file_get_contents($file);
647 }
648 elseif ((isset($info['preview']['css']) && file_exists($path . $info['preview']['css'])) || file_exists($path . 'color/preview.css')) {
649 $preview_image = isset($info['preview']['image']) ? '<div id="img" style="background-image: url(' . base_path() . $path . $info['preview']['image'] . ')"></div>' : '';
650 $preview .= '<div id="preview"><div id="text"><h2>Lorem ipsum dolor</h2><p>Sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud <a href="#">exercitation ullamco</a> laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p></div>'.$preview_image.'</div>';
651 }
652 if (isset($preview)) {
653 $form['preview'] = array(
654 '#type' => 'fieldset',
655 '#title'=> 'Preview',
656 '#value' => $preview,
657 '#prefix' => '<div id="preview-scheme-wrapper">',
658 '#suffix' => '</div>',
659 '#collapsible' => TRUE,
660 '#collapsed' => TRUE,
661 );
662 }
663
664 $form['scheme'] = array(
665 '#type' => 'textfield',
666 '#title' => t('Scheme'),
667 '#default_value' => $schemename,
668 '#size' => 12
669 );
670 $form['alter_input'] = array(
671 '#value' => $alter_links,
672 '#type' => "item",
673 );
674 $form['color_scheme_output'] = array(
675 '#title' => 'Color.inc input (Theme developers)',
676 '#type' => 'textfield',
677 '#value' => "'" . $schemename. "' => '" . implode(',', array_values($palette)) . "',",
678 );
679
680 $form['options'] = array(
681 '#tree' => TRUE,
682 '#prefix' => '<div id="options-scheme-wrapper">',
683 '#suffix' => '</div>',
684 );
685 $form['options']['save'] = array(
686 '#tree' => TRUE,
687 '#prefix' => '<div id="save-scheme-wrapper">',
688 '#suffix' => '</div>',
689 );
690 $form['options']['save']['button'] = array(
691 '#type' => 'submit',
692 '#value' => t('Save scheme'),
693 );
694
695 $form['options']['del'] = array(
696 '#tree' => TRUE,
697 '#prefix' => '<div id="del-scheme-wrapper">',
698 '#suffix' => '</div>',
699 );
700 $form['options']['del']['button'] = array(
701 '#type' => 'submit',
702 '#value' => t('Delete scheme'),
703 );
704
705 if ($current != $scheme_id) {
706 $form['options']['default'] = array(
707 '#tree' => TRUE,
708 '#prefix' => '<div id="default-scheme-wrapper">',
709 '#suffix' => '</div>',
710 );
711 $form['options']['default']['button'] = array(
712 '#type' => 'submit',
713 '#value' => t('Make default'),
714 );
715 }
716
717 $form['back'] = array(
718 '#type' => 'item',
719 '#value' => l('Back to theme configuration', 'admin/build/themes/settings/' . $theme) . '.',
720 );
721
722 // Find colors
723 $names = array();
724 foreach ($palette as $name => $hex) {
725 $names[$name] = $name;
726 $palette[$name] = $hex;
727 }
728
729 $form['palette'] = array(
730 '#prefix' => '<div id="palette">',
731 '#suffix' => '</div>'
732 );
733 $form['palette']['#tree'] = TRUE;
734 foreach ($palette as $name => $value) {
735 if (!$value) {
736 drupal_set_message('The colors set in the scheme in the color.inc file doesn\'t correspond with the number of fields.', 'status');
737 }
738 $form['palette'][$name] = array(
739 '#type' => 'textfield',
740 '#title' => $names[$name],
741 '#default_value' => $value,
742 '#size' => 8,
743 );
744
745 }
746
747 $form['scheme_id'] = array('#type' => 'hidden', '#value' => arg(5));
748 $form['theme'] = array('#type' => 'hidden', '#value' => arg(4));
749 $form['info'] = array('#type' => 'value', '#value' => $info);
750
751 return $form;
752 }
753
754
755 function color_edit_scheme_form_submit($form, &$form_state) {
756 if (!isset($form_state['values']['info']) || !isset($form['#post']['op'])) {
757 return;
758 }
759 $theme = $form['theme']['#value'];
760 $scheme_id = $form['#post']['scheme_id'];
761
762 if ($form['#post']['op'] == "Save scheme" || $form['#post']['op'] == "Save configuration") {
763
764 $info = $form_state['values']['info'];
765 $palette = $form_state['values']['palette'];
766 $schemename = $form_state['values']['scheme'];
767
768 // Resolve palette
769 if (empty($hex)) {
770 foreach ($palette as $k => $color) {
771 $hex[] = $color;
772 }
773 }
774
775 module_load_include('inc', 'color', 'color.database');
776
777 if ($query = color_get_scheme('scheme_id', $scheme_id, 'name')) {
778 $scheme = $query['name'];
779 }
780
781 // Make sure enough memory is available, if PHP's memory limit is compiled in.
782 if (function_exists('memory_get_usage') && isset($info['img'])) {
783 // Fetch source image dimensions.
784 foreach ($info['img'] as $img) {
785 $source = drupal_get_path('theme', $theme) .'/'. $img['file'];
786 list($width, $height) = getimagesize($source);
787
788 // We need at least a copy of the source and a target buffer of the same
789 // size (both at 32bpp).
790 $required = $width * $height * 8;
791 $usage = memory_get_usage();
792 $limit = parse_size(ini_get('memory_limit'));
793 if ($usage + $required > $limit) {
794 drupal_set_message(t('There is not enough memory available to PHP to change this theme\'s color scheme. You need at least %size more. Check the <a href="@url">PHP documentation</a> for more information.', array('%size' => format_size($usage + $required - $limit), '@url' => 'http://www.php.net/manual/en/ini.core.php#ini.sect.resource-limits')), 'error');
795 return;
796 }
797 }
798 }
799 color_add_scheme($theme, $hex, $scheme_id, $schemename);
800 }
801
802 elseif ($form_state['clicked_button']['#value'] == "Make default") {
803 db_query('UPDATE {color} SET value = \'%s\' WHERE theme = \'%s\' AND name = \'%s\'', $scheme_id, $theme, 'default_scheme');
804 drupal_set_message("{$schemename} default scheme.");
805 }
806 elseif ($form['#post']['op'] == 'Delete scheme') {
807 $returnstatement = color_set_scheme($scheme_id, NULL, 'delete');
808 drupal_goto('admin/build/themes/settings/' . $theme);
809 return;
810 }
811 }
812
813 /**
814 * Theme color form.
815 *
816 * @ingroup @themeable
817 */
818 function theme_color_edit_scheme_form($form) {
819 // Include stylesheet
820 $theme = $form['theme']['#value'];
821 $info = $form['info']['#value'];
822 $path = drupal_get_path('theme', $theme) .'/';
823 drupal_add_css($path . $info['preview']['css']);
824 $output = '';
825
826 $output .= '<div class="color-form clear-block">';
827 $output .= drupal_render($form['scheme']);
828 $output .= drupal_render($form['palette']);
829 $output .= drupal_render($form['alter_input']);
830 $output .= '
831 <div style="clear: both">
832 <div style="width: 250px; float: left">
833 <fieldset> <legend>Compatible colors</legend>
834 <select id="selectsuggestedcolors">
835 <option value="analogous">Analogous</option>
836 <option value="complementary">Complementary</option>
837 <option value="triadic">Triadic</option>
838 <option value="tetradic">Tetradic</option>
839 <option value="neutral">Neutral</option>
840 <option value="split-complementary">Split-complementary</option>
841 <option value="clash">Clash</option>
842 <option value="four-tone">Four-tone</option>
843 <option value="five-tone">Five-tone</option>
844 <option value="six-tone">Six-tone</option>
845
846 </select>
847 <div id="suggestedcolors"></div>
848 <div class="description" style="font-size: 75%">Whip out some <a href="http://www.colormatters.com/colortheory.html">color theory</a> kung-fu.</div>
849 </fieldset>
850 </div>
851
852 <div style="width: 250px; float: left">
853 <fieldset>
854 <legend>Color History</legend>
855 <div id="prevcolors"></div>
856 <div class="description" style="font-size: 75%">Tip: You can also select an input field and hold down Ctrl-Z or Command-Z to undo. Likewise for redo.</div>
857 </fieldset>
858 </div>
859 </div>
860 <div style="clear: both"></div>';
861 $output .= drupal_render($form['preview']);
862 $output .= drupal_render($form['options']);
863
864 $output .= '</div>';
865
866 $output .= drupal_render($form);
867
868 return $output;
869 }
870 /**
871 * Generate a new scheme, including stylesheet(s) and image(s).
872 * Also updates {color_scheme} row generated earlier (immediately after
873 * fresh theme added, or modifying new scheme). If fresh, will populate
874 * the scheme_id's 'extra_attr' column, if modifying current, will update.
875 * Will change the name of current scheme.
876 *
877 * @param $theme
878 * String of theme name.
879 * @param $hex
880 * Array of hex codes for scheme to be added. array('#ffffff', '#000000')
881 * @param $scheme_id
882 * Integer of scheme id
883 * @param $schemename
884 * Optional string, for modifying scheme name.
885 */
886 function color_add_scheme($theme, $hex, $scheme_id, $schemename = NULL) {
887 module_load_include('inc', 'color', 'color.misc');
888 module_load_include('inc', 'color', 'color.database');
889 module_load_include('inc', 'color', 'color.stylesheet');
890
891 $info = color_get_theme_info($theme, 'sql');
892 $reference = array_combine($info['fields'], $info['reference_scheme']);
893
894 $palette = array_combine($info['fields'], $hex);
895
896 // Grab our current info!
897 // On store theme, use a $scheme key-array to handle this stuff, so we don't need to use name_to_scheme
898 if ($current_scheme = color_get_scheme('scheme_id', $scheme_id, array('extra_attr', 'name'))) {
899 if ($current_scheme['extra_attr'] != NULL) {
900 $extra_attr = unserialize($current_scheme['extra_attr']);
901 foreach ($extra_attr['files'] as $file) {
902 @unlink($file);
903 }
904 if (isset($file) && $file = dirname($file)) {
905 @rmdir($file);
906 }
907 }
908 }
909
910 // Prepare target locations for generated files.
911 $id = $theme .'-'. $schemename .'-'. substr(md5(serialize($palette) . microtime()), 0, 8);
912 // Normalize $id
913 $id = color_normalize($id);
914
915 $paths['color'] = file_directory_path() .'/color';
916 $paths['target'] = $paths['color'] .'/'. $id;
917
918 foreach ($paths as $path) {
919 file_check_directory($path, FILE_CREATE_DIRECTORY);
920 }
921 $paths['target'] = $paths['target'] .'/';
922 $paths['id'] = $id;
923 $paths['source'] = drupal_get_path('theme', $theme) .'/';
924 $paths['files'] = $paths['map'] = array();
925
926 // Copy over neutral images.
927 if (isset($info['copy'])) {
928 foreach ($info['copy'] as $file) {
929 $base = basename($file);
930 $source = $paths['source'] . $file;
931
932 file_copy($source, $paths['target'] . $base);
933 $paths['map'][$file] = $base;
934 $paths['files'][] = $paths['target'] . $base;
935 }
936 }
937
938 // Render new images, if image provided.
939 if (isset($info['img'])) {
940 module_load_include('inc', 'color', 'color.image');
941 $paths += _color_render_images($theme, $info, $paths, $palette);
942 }
943
944 // Rewrite theme stylesheets.
945 $css = array();
946 foreach ($info['css'] as $stylesheet) {
947 // Build a temporary array with LTR and RTL files.
948 $files = array();
949 if (file_exists($paths['source'] . $stylesheet)) {
950 $files[] = $stylesheet;
951
952 $rtl_file = str_replace('.css', '-rtl.css', $stylesheet);
953 if (file_exists($paths['source'] . $rtl_file)) {
954 $files[] = $rtl_file;
955 }
956 }
957
958 foreach ($files as $file) {
959 // Aggregate @imports recursively for each configured top level CSS file
960 // without optimization. Aggregation and optimization will be
961 // handled by drupal_build_css_cache() only.
962 $style = drupal_load_stylesheet($paths['source'] . $file, FALSE);
963
964 // Return the path to where this CSS file originated from, stripping
965 // off the name of the file at the end of the path.
966 // Also strip off the color/ dir. if it's there.
967 $file = str_replace("color/", NULL, $file);
968 $base = base_path() . dirname($paths['source'] . $file) .'/';
969 _drupal_build_css_path(NULL, $base);
970
971 // Prefix all paths within this CSS file, ignoring absolute paths.
972 $style = preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_path', $style);
973
974 // Rewrite stylesheet with new colors.
975 module_load_include('inc', 'color', 'color.stylesheet');
976 $style = _color_rewrite_stylesheet($theme, $info, $paths, $palette, $style);
977 $base_file = basename($file);
978 $css[] = $paths['target'] . $base_file;
979 _color_save_stylesheet($paths['target'] . $base_file, $style, $paths);
980 }
981 }
982
983 // Update the db!
984 $extra_attr['palette'] = $reference;
985 $extra_attr['id'] = $id;
986 $extra_attr['target'] = $paths['target'];
987
988 // Find screenshot.png slice/copy
989 foreach ($paths['map'] as $img => $val) {
990 if (strpos('screenshot.png', $val) !== FALSE) {
991 $screenshot = $val;
992 break;
993 }
994 }
995
996 if (isset($logo)) {
997 $extra_attr['screenshot'] = $paths['target'] . $screenshot;
998 }
999 else {
1000 $extra_attr['screenshot'] = $paths['source'] . "screenshot.png";
1001 }
1002
1003 foreach ($paths['map'] as $img => $val) {
1004 if (strpos('logo.png', $val) !== FALSE) {
1005 $logo = $val;
1006 break;
1007 }
1008 }
1009
1010 // Find logo.png slice/copy
1011 if (isset($logo)) {
1012 $extra_attr['logo'] = $paths['target'] . $logo;
1013 }
1014 else {
1015 $extra_attr['logo'] = $paths['source'] . "logo.png";
1016 }
1017
1018 $extra_attr['stylesheets'] = $css;
1019 $extra_attr['files'] = $paths['files'];
1020
1021 $vars['extra_attr'] = $extra_attr;
1022 $vars['hex'] = $hex;
1023 $vars['schemename'] = $schemename;
1024
1025 color_set_scheme($scheme_id, $vars, 'update');
1026 }
1027
1028 /**
1029 * Retreive name of scheme
1030 *
1031 * @param $id
1032 * Integer of scheme id
1033 * @return
1034 * String of scheme name
1035 */
1036 function color_get_title($id) {
1037 module_load_include('inc', 'color', 'color.database');
1038
1039 $scheme = color_get_scheme('scheme_id', $id, 'name');
1040
1041 return $scheme['name'];
1042 }

  ViewVC Help
Powered by ViewVC 1.1.2