-
Notifications
You must be signed in to change notification settings - Fork 1
/
color_rebuilder.module
211 lines (179 loc) · 6.27 KB
/
color_rebuilder.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Implements hook_menu().
*/
function color_rebuilder_menu() {
$items = array();
// if requested rebuild the colors cache of the current theme
$items['color_rebuilder/rebuild/%/%'] = array(
'title' => 'myPage',
'page callback' => 'color_rebuilder_rebuild',
'page arguments' => array(1,2),
'access arguments' => array('access color rebuilder'),
);
return $items;
}
function hook_permission() {
return array(
'access color rebuilder' => array(
'title' => t('Access Color Rebuilder Rebuild Page'),
'description' => t('Perform the cache clear of the passed theme color files.'),
),
);
}
/**
* Retrieve the colors of the color scheme and set them to palette #value
* @param array &$form $form array
* @param String $color_scheme The color scheme key to enable
* @return boolean FALSE is color scheme doesn't exist, TRUE otherwise
*/
function color_rebuilder_set_colors(&$form, $color_scheme) {
// the color scheme specified doesn't exist
if (empty($form['color']['info']['#value']['schemes'][$color_scheme])) {
return FALSE;
}
// array of colors: $color_field_id => $hex_color
$scheme_colors = $form['color']['info']['#value']['schemes'][$color_scheme]['colors'];
// array of colors fields $color_field_id => $color_field_label
$scheme_fields = $form['color']['info']['#value']['fields'];
foreach ($scheme_fields as $key => $value) {
$form['color']['palette'][$key]['#value'] = $scheme_colors[$key];
}
return TRUE;
}
/**
* Functions that rebuilds the color cache, based on a theme name and a color scheme
* @param string $theme_to_rebuild [description]
* @param string $color_scheme [description]
* @return [type] [description]
*/
function color_rebuilder_rebuild($theme_to_rebuild = '', $color_scheme = 'default') {
// Save error states and clear them.
$errors_before = drupal_static('form_set_error', array());
form_clear_error();
// See if the css file is used the theme.
$themes = list_themes();
// color is not active
if (!module_exists('color')) {
return $error = 1;
}
foreach ($themes as $theme_name => $theme_values) {
// rebuild only specified theme color files
if (!empty($theme_to_rebuild) && $theme_to_rebuild != $theme_name) {
continue;
}
// Get the form for this theme.
$router_item = menu_get_item('admin/appearance/settings/' . $theme_name);
if ($router_item['include_file']) {
require_once DRUPAL_ROOT . '/' . $router_item['include_file'];
}
$form = drupal_get_form('system_theme_settings', $theme_name);
// Get the form defaults.
$defaults = array();
// set defaults value in the form
color_rebuilder_defaults_from_form($defaults, $form);
// set the color scheme palette values
$color_scheme_exists = color_rebuilder_set_colors($form, $color_scheme);
// the color scheme doesn't exist, break and return $error
if (!$color_scheme_exists) {
return $error = 3;
}
// Build the palette value.
$palette = array();
if (isset($form['color'])) {
foreach (element_children($form['color']['palette']) as $key) {
$palette[$key] = $defaults[$key] = $form['color']['palette'][$key]['#value'];
}
}
$defaults['scheme'] = $color_scheme;
// Build the form state array.
$form_state = array(
'values' => array('palette' => $palette),
'build_info' => array('args' => array(0 => $theme_name)),
);
$form_state['values'] += $defaults;
if (isset($form['color'])) {
// Validate form.
color_scheme_form_validate($form, $form_state);
$errors = form_set_error();
if (empty($errors)) {
// Only submit if no errors.
color_scheme_form_submit($form, $form_state);
return $error = 0;
}
} else {
return $error = 2;
}
// Reset form errors.
form_clear_error();
}
// Save error states back.
$form_set_error = &drupal_static('form_set_error', array());
$form_set_error = $errors_before;
// theme doesn't exist
return $error = 4;
}
/**
* Given a form get the default values from it.
*
* @param array $defaults
* An empty array used to populate the default values.
* @param array $form
* The form returned from drupal_get_form().
* @param string $parent_key
* The key name of the parent.
*/
function color_rebuilder_defaults_from_form(array &$defaults, array $form, $parent_key = '') {
foreach (element_children($form) as $key) {
$values = $form[$key];
if (isset($values['#value'])) {
// Grab defaults at this level.
if (!isset($defaults[$key])) {
$defaults[$key] = $values['#value'];
}
else {
$defaults[$parent_key . '-' . $key] = $values['#value'];
}
}
elseif (isset($values['#default_value'])) {
// Grab defaults at this level.
if (!isset($defaults[$key])) {
$defaults[$key] = $values['#default_value'];
}
else {
$defaults[$parent_key . '-' . $key] = $values['#default_value'];
}
}
elseif (is_array($values)) {
// Go deeper if needed.
color_rebuilder_defaults_from_form($defaults, $values, $key);
}
}
}
function color_rebuilder_drush_color_rebuild($main_color = NULL) {
$theme_to_rebuild = drush_get_option('theme', 1);
$schema_color = drush_get_option('color', 1);
$error = color_rebuilder_rebuild($theme_to_rebuild, $schema_color);
switch ($error) {
// no error, the theme has been rebuilt
case 0:
drupal_set_message(t("The '@theme' color cache has been rebuilt with the scheme color '@scheme'", array('@theme' => $theme_to_rebuild, '@scheme' => $schema_color)));
break;
// error 1, color is not enabled
case 1:
drush_set_error(dt("The color module is not enabled"));
break;
// error 2, theme hasn't color
case 2:
drush_set_error(dt("The theme '@theme' hasn't the color-enabled", array('@theme' => $theme_to_rebuild)));
break;
// error 3, no color scheme
case 3:
drush_set_error(dt("The theme '@theme' hasn't the color scheme '@scheme' ", array('@theme' => $theme_to_rebuild, '@scheme' => $schema_color)));
break;
// theme doesn't exist
case 4:
drush_set_error(dt("The theme '@theme' doesn't exist", array('@theme' => $theme_to_rebuild)));
break;
}
}