Fixed whitespace
[project/superior_colors.git] / superior_colors.module
1 <?php
2 /**
3 * @file
4 * Module file for superior_colors.
5 */
6
7 /**
8 * Implements hook_permission().
9 */
10 function superior_colors_permission() {
11 return array(
12 'administer site colors' => array(
13 'title' => t('Administer site colors'),
14 'description' => t('Lets the user have access to the color configuration page'),
15 ),
16 );
17 }
18
19 /**
20 * Implements hook_menu().
21 */
22 function superior_colors_menu() {
23 $items['admin/config/media/colors'] = array(
24 'title' => 'Superior Colors',
25 'description' => 'Adjust site colors',
26 'page callback' => 'drupal_get_form',
27 'page arguments' => array('superior_colors_colors_form'),
28 'access callback' => 'user_access',
29 'access arguments' => array('administer site colors'),
30 'type' => MENU_NORMAL_ITEM,
31 );
32 return $items;
33 }
34
35 /**
36 * Implements hook_init().
37 */
38 function superior_colors_init() {
39 global $theme_key;
40 if (variable_get('theme_default', '') == $theme_key) {
41 // If file does not exist, create it.
42 if (!file_exists(drupal_realpath('public://superior_colors.css'))) {
43 $css2add = superior_colors_color_get_css() . "\n";
44 file_save_data($css2add, 'public://superior_colors.css', FILE_EXISTS_REPLACE);
45 watchdog('superior_colors', 'CSS file created');
46 }
47 // Add the css file.
48 drupal_add_css(file_create_url('public://superior_colors.css'), array(
49 'type' => 'external',
50 'group' => CSS_THEME,
51 'every_page' => TRUE,
52 'weight' => 5000,
53 )
54 );
55 }
56 }
57
58 /**
59 * Implements a custom form submit handler.
60 */
61 function superior_colors_colors_form_submit($form, &$form_state) {
62 _superior_colors_clear_css();
63 $color_keys = superior_colors_colors_get(array('keys_list' => TRUE));
64 $color_list = array();
65 foreach ($color_keys as $key) {
66 if (isset($form_state['values'][$key])) {
67 $color_list[$key] = $form_state['values'][$key];
68 }
69 }
70 superior_colors_colors_set($color_list);
71 drupal_set_message(t('Colors have been saved'), 'status');
72 }
73
74 /**
75 * Merges arrays in many levels.
76 *
77 * Taken from http://www.php.net/manual/en/function.array-merge-recursive.php
78 */
79 function superior_colors_mergearrays($arr1, $arr2) {
80 foreach ($arr2 as $key => $value) {
81 if (array_key_exists($key, $arr1) && is_array($value)) {
82 $arr1[$key] = superior_colors_mergearrays($arr1[$key], $arr2[$key]);
83 }
84 else {
85 $arr1[$key] = $value;
86 }
87 }
88 return $arr1;
89 }
90
91 /**
92 * Get the CSS that defines the colors.
93 *
94 * Sets the custom color CSS by getting custom colors and replacing the tags
95 * in the color template css files. Unmatched tags are set to magenta.
96 */
97 function superior_colors_color_get_css() {
98 if ($css_cache = cache_get('superior_colors_generated_css', 'cache')) {
99 $css2add = $css_cache->data;
100 }
101 else {
102 global $theme_key;
103 $info_arr = superior_colors_parse_theme_info($theme_key);
104 // Skip if not supported by active theme.
105 if (!isset($info_arr['superior_colors']['colors'])) {
106 return;
107 }
108 $colors = superior_colors_colors_get(array('keys_values_list' => TRUE));
109
110 $css2add = '';
111 foreach ($info_arr['superior_colors']['color_css'] as $theme => $files) {
112 foreach ($files as $file) {
113 $filename = drupal_get_path('theme', $theme) . '/' . $file;
114 if (file_exists($filename)) {
115 $file_content = file_get_contents($filename);
116 foreach ($colors as $key => $color) {
117 $file_content = str_replace("[[$key]]", "#$color", $file_content);
118 }
119 $file_content = preg_replace('/(\[\[[A-Za-z0-9_-]+\]\])/', 'magenta', $file_content);
120 $css2add .= $file_content;
121 }
122 }
123 }
124 cache_set('superior_colors_generated_css', $css2add, 'cache', CACHE_TEMPORARY);
125 }
126 return $css2add;
127 }
128
129 /**
130 * Returns parsed info from the chosen theme and all its subthemes.
131 */
132 function superior_colors_parse_theme_info($theme) {
133 $infofile = drupal_get_path('theme', $theme) . '/' . $theme . '.info';
134 $file_arr = drupal_parse_info_file($infofile);
135 // Check for basetheme.
136 if (isset($file_arr['base theme'])) {
137 $base_arr = superior_colors_parse_theme_info($file_arr['base theme']);
138 // Merge from basetheme info.
139 $file_arr = superior_colors_mergearrays($base_arr, $file_arr);
140 }
141
142 // This could be improved by registering the order of the theme inheritance.
143 $file_arr['superior_colors']['theme_order'][] = $theme;
144 return $file_arr;
145 }
146
147 /**
148 * Returns a list of color groups defined in the themes, as a key=>value array.
149 */
150 function superior_colors_color_groups_get() {
151 $theme = variable_get('theme_default', 'bartik');
152 $info_arr = superior_colors_parse_theme_info($theme);
153 $color_groups = $info_arr['superior_colors']['color_groups'];
154 return $color_groups;
155 }
156
157 /**
158 * Get color data.
159 *
160 * Returns a list of color definitions defined in the themes, and merged with
161 * the customized colors stored in a variable. Can return various formats
162 * depending on args:
163 *
164 * keys_list => TRUE:
165 * Returns a list of color keys
166 *
167 * keys_values_list => TRUE:
168 * Returns a list of color keys and values
169 *
170 * Default:
171 * Returns a structure
172 */
173 function superior_colors_colors_get($args = NULL) {
174 $theme = variable_get('theme_default', 'bartik');
175 $info_arr = superior_colors_parse_theme_info($theme);
176
177 // Skip if not supported by active theme.
178 if (!isset($info_arr['superior_colors']['colors'])) {
179 return array();
180 }
181 $colors = $info_arr['superior_colors']['colors'];
182
183 // Returns list of keys.
184 if ($args) {
185 if (isset($args['keys_list'])) {
186 $keys = array();
187 foreach ($colors as $group) {
188 foreach ($group as $key => $item) {
189 $keys[] = $key;
190 }
191 }
192 return $keys;
193 }
194 }
195
196 // Getting customized colors.
197 $colors_var = variable_get('superior_colors_colors', array());
198 if (!is_array($colors_var)) {
199 $colors_var = array();
200 }
201 foreach ($colors as $gkey => $group) {
202 foreach ($group as $key => $item) {
203 if (isset($colors_var[$key])) {
204 $colors[$gkey][$key]['value'] = $colors_var[$key];
205 }
206 }
207 }
208
209 // Returns list of keys and values.
210 if ($args) {
211 if (isset($args['keys_values_list'])) {
212 $list = array();
213 foreach ($colors as $group) {
214 foreach ($group as $key => $item) {
215 $list[$key] = $item['value'];
216 }
217 }
218 return $list;
219 }
220 }
221
222 // Returns entire structure.
223 return $colors;
224 }
225
226 /**
227 * Saves the custom colors in a variable.
228 */
229 function superior_colors_colors_set($color_list) {
230 variable_set('superior_colors_colors', $color_list);
231 cache_clear_all('superior_colors_generated_css', 'cache');
232 }
233
234 /**
235 * Creates the admin form for editing the colors.
236 */
237 function superior_colors_colors_form($form, &$form_state) {
238 $colors = superior_colors_colors_get();
239 $groups = superior_colors_color_groups_get();
240 $fieldtype = module_exists('jquery_colorpicker') ? 'jquery_colorpicker' : 'textfield';
241
242 $form = array();
243
244 foreach ($groups as $gkey => $group) {
245 $form[$gkey] = array(
246 '#type' => 'fieldset',
247 '#title' => check_plain(t($group)),
248 '#collapsible' => TRUE,
249 '#collapsed' => TRUE,
250 );
251 }
252
253 foreach ($colors as $gkey => $group) {
254 foreach ($group as $key => $item) {
255 $form[$gkey][$key] = array(
256 '#type' => $fieldtype,
257 '#title' => check_plain(t($item['name'])),
258 '#default_value' => $item['value'],
259 );
260 }
261 }
262
263 // Submit Button.
264 $form['submit'] = array(
265 '#type' => 'submit',
266 '#value' => t('Save'),
267 );
268 return $form;
269 }
270
271 /**
272 * Implements hook_flush_caches().
273 */
274 function superior_colors_flush_caches() {
275 _superior_colors_clear_css();
276 }
277
278 /**
279 * Removes any generated colors CSS files.
280 */
281 function _superior_colors_clear_css() {
282 file_unmanaged_delete('public://superior_colors.css');
283 watchdog('superior_colors', 'CSS file deleted');
284 $css2add = superior_colors_color_get_css() . "\n";
285 file_save_data($css2add, 'public://superior_colors.css', FILE_EXISTS_REPLACE);
286 watchdog('superior_colors', 'CSS file created');
287 }