summaryrefslogtreecommitdiffstats
path: root/superior_colors.module
blob: 63450a8094e02ef517247f8b78abfbaac469d492 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
 * @file
 * Module file for superior_colors.
 */

/**
 * Implements hook_permission().
 */
function superior_colors_permission() {
  return array(
    'administer site colors' => array(
      'title' => t('Administer site colors'),
      'description' => t('Lets the user have access to the color configuration page'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function superior_colors_menu() {
  $items['admin/config/media/colors'] = array(
    'title' => 'Superior Colors',
    'description' => 'Adjust site colors',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('superior_colors_colors_form'),
    'access callback' => 'user_access',
    'access arguments' => array('administer site colors'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_init().
 */
function superior_colors_init() {
  global $theme_key;
  if (variable_get('theme_default', '') == $theme_key) {
    // If file does not exist, create it.
    if (!file_exists(drupal_realpath('public://superior_colors.css'))) {
      $css2add = superior_colors_color_get_css() . "\n";
      file_save_data($css2add, 'public://superior_colors.css', FILE_EXISTS_REPLACE);
      watchdog('superior_colors', 'CSS file created');
    }
    // Add the css file.
    drupal_add_css(file_create_url('public://superior_colors.css'), array(
      'type' => 'external',
      'group' => CSS_THEME,
      'every_page' => TRUE,
      'weight' => 5000,
      )
    );
  }
}

/**
 * Implements a custom form submit handler.
 */
function superior_colors_colors_form_submit($form, &$form_state) {
  _superior_colors_clear_css();
  $color_keys = superior_colors_colors_get(array('keys_list' => TRUE));
  $color_list = array();
  foreach ($color_keys as $key) {
    if (isset($form_state['values'][$key])) {
      $color_list[$key] = $form_state['values'][$key];
    }
  }
  superior_colors_colors_set($color_list);
  drupal_set_message(t('Colors have been saved'), 'status');
}

/**
 * Merges arrays in many levels.
 *
 * Taken from http://www.php.net/manual/en/function.array-merge-recursive.php
 */
function superior_colors_mergearrays($arr1, $arr2) {
  foreach ($arr2 as $key => $value) {
    if (array_key_exists($key, $arr1) && is_array($value)) {
      $arr1[$key] = superior_colors_mergearrays($arr1[$key], $arr2[$key]);
    }
    else {
      $arr1[$key] = $value;
    }
  }
  return $arr1;
}

/**
 * Get the CSS that defines the colors.
 *
 * Sets the custom color CSS by getting custom colors and replacing the tags
 * in the color template css files. Unmatched tags are set to magenta.
 */
function superior_colors_color_get_css() {
  if ($css_cache = cache_get('superior_colors_generated_css', 'cache')) {
    $css2add = $css_cache->data;
  }
  else {
    global $theme_key;
    $info_arr = superior_colors_parse_theme_info($theme_key);
    // Skip if not supported by active theme.
    if (!isset($info_arr['superior_colors']['colors'])) {
      return;
    }
    $colors = superior_colors_colors_get(array('keys_values_list' => TRUE));

    $css2add = '';
    foreach ($info_arr['superior_colors']['color_css'] as $theme => $files) {
      foreach ($files as $file) {
        $filename = drupal_get_path('theme', $theme) . '/' . $file;
        if (file_exists($filename)) {
          $file_content = file_get_contents($filename);
          foreach ($colors as $key => $color) {
            $file_content = str_replace("[[$key]]", "#$color", $file_content);
          }
          $file_content = preg_replace('/(\[\[[A-Za-z0-9_-]+\]\])/', 'magenta', $file_content);
          $css2add .= $file_content;
        }
      }
    }
    cache_set('superior_colors_generated_css', $css2add, 'cache', CACHE_TEMPORARY);
  }
  return $css2add;
}

/**
 * Returns parsed info from the chosen theme and all its subthemes.
 */
function superior_colors_parse_theme_info($theme) {
  $infofile = drupal_get_path('theme', $theme) . '/' . $theme . '.info';
  $file_arr = drupal_parse_info_file($infofile);
  // Check for basetheme.
  if (isset($file_arr['base theme'])) {
    $base_arr = superior_colors_parse_theme_info($file_arr['base theme']);
    // Merge from basetheme info.
    $file_arr = superior_colors_mergearrays($base_arr, $file_arr);
  }

  // This could be improved by registering the order of the theme inheritance.
  $file_arr['superior_colors']['theme_order'][] = $theme;
  return $file_arr;
}

/**
 * Returns a list of color groups defined in the themes, as a key=>value array.
 */
function superior_colors_color_groups_get() {
  $theme = variable_get('theme_default', 'bartik');
  $info_arr = superior_colors_parse_theme_info($theme);
  $color_groups = $info_arr['superior_colors']['color_groups'];
  return $color_groups;
}

/**
 * Get color data.
 *
 * Returns a list of color definitions defined in the themes, and merged with
 * the customized colors stored in a variable. Can return various formats
 * depending on args:
 *
 * keys_list => TRUE:
 *   Returns a list of color keys
 *
 * keys_values_list => TRUE:
 *   Returns a list of color keys and values
 *
 * Default:
 *   Returns a structure
 */
function superior_colors_colors_get($args = NULL) {
  $theme = variable_get('theme_default', 'bartik');
  $info_arr = superior_colors_parse_theme_info($theme);

  // Skip if not supported by active theme.
  if (!isset($info_arr['superior_colors']['colors'])) {
    return array();
  }
  $colors = $info_arr['superior_colors']['colors'];

  // Returns list of keys.
  if ($args) {
    if (isset($args['keys_list'])) {
      $keys = array();
      foreach ($colors as $group) {
        foreach ($group as $key => $item) {
          $keys[] = $key;
        }
      }
      return $keys;
    }
  }

  // Getting customized colors.
  $colors_var = variable_get('superior_colors_colors', array());
  if (!is_array($colors_var)) {
    $colors_var = array();
  }
  foreach ($colors as $gkey => $group) {
    foreach ($group as $key => $item) {
      if (isset($colors_var[$key])) {
        $colors[$gkey][$key]['value'] = $colors_var[$key];
      }
    }
  }

  // Returns list of keys and values.
  if ($args) {
    if (isset($args['keys_values_list'])) {
      $list = array();
      foreach ($colors as $group) {
        foreach ($group as $key => $item) {
          $list[$key] = $item['value'];
        }
      }
      return $list;
    }
  }

  // Returns entire structure.
  return $colors;
}

/**
 * Saves the custom colors in a variable.
 */
function superior_colors_colors_set($color_list) {
  variable_set('superior_colors_colors', $color_list);
  cache_clear_all('superior_colors_generated_css', 'cache');
}

/**
 * Creates the admin form for editing the colors.
 */
function superior_colors_colors_form($form, &$form_state) {
  $colors = superior_colors_colors_get();
  $groups = superior_colors_color_groups_get();
  $fieldtype = module_exists('jquery_colorpicker') ? 'jquery_colorpicker' : 'textfield';

  $form = array();

  foreach ($groups as $gkey => $group) {
    $form[$gkey] = array(
      '#type' => 'fieldset',
      '#title' => check_plain(t($group)),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
  }

  foreach ($colors as $gkey => $group) {
    foreach ($group as $key => $item) {
      $form[$gkey][$key] = array(
        '#type' => $fieldtype,
        '#title' => check_plain(t($item['name'])),
        '#default_value' => $item['value'],
      );
    }
  }

  // Submit Button.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}

/**
 * Implements hook_flush_caches().
 */
function superior_colors_flush_caches() {
  _superior_colors_clear_css();
}

/**
 * Removes any generated colors CSS files.
 */
function _superior_colors_clear_css() {
  file_unmanaged_delete('public://superior_colors.css');
  watchdog('superior_colors', 'CSS file deleted');
  $css2add = superior_colors_color_get_css() . "\n";
  file_save_data($css2add, 'public://superior_colors.css', FILE_EXISTS_REPLACE);
  watchdog('superior_colors', 'CSS file created');
}