You are here

function color_scheme_form_submit in Drupal 5

Same name and namespace in other branches
  1. 8 core/modules/color/color.module \color_scheme_form_submit()
  2. 6 modules/color/color.module \color_scheme_form_submit()
  3. 7 modules/color/color.module \color_scheme_form_submit()
  4. 9 core/modules/color/color.module \color_scheme_form_submit()

Submit handler for color change form.

File

modules/color/color.module, line 193

Code

function color_scheme_form_submit($form_id, $values) {

  // Get theme coloring info
  if (!isset($values['info'])) {
    return;
  }
  $theme = $values['theme'];
  $info = $values['info'];

  // Resolve palette
  $palette = $values['palette'];
  if ($values['scheme'] != '') {
    $scheme = explode(',', $values['scheme']);
    foreach ($palette as $k => $color) {
      $palette[$k] = array_shift($scheme);
    }
  }

  // Make sure enough memory is available, if PHP's memory limit is compiled in.
  if (function_exists('memory_get_usage')) {

    // Fetch source image dimensions.
    $source = drupal_get_path('theme', $theme) . '/' . $info['base_image'];
    list($width, $height) = getimagesize($source);

    // We need at least a copy of the source and a target buffer of the same
    // size (both at 32bpp).
    $required = $width * $height * 8;
    $usage = memory_get_usage();
    $limit = parse_size(ini_get('memory_limit'));
    if ($usage + $required > $limit) {
      drupal_set_message(t('There is not enough memory available to PHP to change this theme\'s color scheme. You need at least %size more. Check the <a href="@url">PHP documentation</a> for more information.', array(
        '%size' => format_size($usage + $required - $limit),
        '@url' => 'http://www.php.net/manual/en/ini.core.php#ini.sect.resource-limits',
      )), 'error');
      return;
    }
  }

  // Delete old files
  foreach (variable_get('color_' . $theme . '_files', array()) as $file) {
    @unlink($file);
  }
  if ($file = dirname($file)) {
    @rmdir($file);
  }

  // Don't render the default colorscheme, use the standard theme instead.
  if (implode(',', color_get_palette($theme, true)) == implode(',', $palette) || $values['op'] == t('Reset to defaults')) {
    variable_del('color_' . $theme . '_palette');
    variable_del('color_' . $theme . '_stylesheet');
    variable_del('color_' . $theme . '_logo');
    variable_del('color_' . $theme . '_files');
    variable_del('color_' . $theme . '_screenshot');
    return;
  }

  // Prepare target locations for generated files
  $id = $theme . '-' . substr(md5(serialize($palette) . microtime()), 0, 8);
  $paths['color'] = file_directory_path() . '/color';
  $paths['target'] = $paths['color'] . '/' . $id;
  foreach ($paths as $path) {
    file_check_directory($path, FILE_CREATE_DIRECTORY);
  }
  $paths['target'] = $paths['target'] . '/';
  $paths['id'] = $id;
  $paths['source'] = drupal_get_path('theme', $theme) . '/';
  $paths['stylesheet'] = $paths['target'] . 'style.css';
  $paths['files'] = $paths['map'] = array();

  // Save palette and stylesheet location
  variable_set('color_' . $theme . '_palette', $palette);
  variable_set('color_' . $theme . '_stylesheet', $paths['stylesheet']);
  variable_set('color_' . $theme . '_logo', $paths['target'] . 'logo.png');

  // Copy over neutral images
  foreach ($info['copy'] as $file) {
    $base = basename($file);
    $source = $paths['source'] . $file;
    file_copy($source, $paths['target'] . $base);
    $paths['map'][$file] = $base;
    $paths['files'][] = $paths['target'] . $base;
  }

  // Render new images, if base image exists
  if ($info['base_image']) {
    _color_render_images($theme, $info, $paths, $palette);
  }

  // Rewrite stylesheet
  _color_rewrite_stylesheet($theme, $info, $paths, $palette);

  // Maintain list of files
  variable_set('color_' . $theme . '_files', $paths['files']);
}