You are here

color_rebuilder.module in Color Rebuilder 7

File

color_rebuilder.module
View source
<?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;
  }
}

Functions

Namesort descending Description
color_rebuilder_defaults_from_form Given a form get the default values from it.
color_rebuilder_drush_color_rebuild
color_rebuilder_menu Implements hook_menu().
color_rebuilder_rebuild Functions that rebuilds the color cache, based on a theme name and a color scheme
color_rebuilder_set_colors Retrieve the colors of the color scheme and set them to palette #value
hook_permission