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'); }