You are here

function fivestar_color_form_submit in Fivestar 6

Same name and namespace in other branches
  1. 5 fivestar_color.inc \fivestar_color_form_submit()
  2. 6.2 includes/fivestar.color.inc \fivestar_color_form_submit()

Submit handler for color change form.

2 string references to 'fivestar_color_form_submit'
fivestar_color_form in ./fivestar_color.inc
Form callback. Returns the configuration form.
fivestar_settings in ./fivestar.module
Callback function for admin/settings/fivestar. Display the settings form.

File

./fivestar_color.inc, line 174
File containing functions relating to the color widget.

Code

function fivestar_color_form_submit($form, &$form_state) {
  if ($form_state['values']['matte'] == t('none')) {
    $form_state['values']['matte'] = '';
  }

  // Check if we're using a color-enabled set of stars.
  if (!array_key_exists($form_state['values']['fivestar_widget'], $form['widget']['fivestar_color_widget']['#options']) || $form_state['values']['fivestar_color_type'] == 'default') {
    return;
  }
  $widget = str_replace('.css', '', basename($form_state['values']['fivestar_widget']));
  $widget_css = $form_state['values']['fivestar_widget'];
  $widget_rtl_css = str_replace('.css', '-rtl.css', $widget_css);
  $upload_directory = file_directory_path() . '/fivestar';
  $widget_directory = $upload_directory . '/' . $widget;
  $current_widget = variable_get('fivestar_widget', 'default');

  // Delete the previous set of stars if any from the files directory.
  if (strpos($current_widget, $upload_directory) === 0) {
    $current_widget_directory = dirname($current_widget);
    file_scan_directory($current_widget_directory, '.*', array(
      '.',
      '..',
      'CVS',
    ), 'unlink');
    rmdir($current_widget_directory);
  }

  // Check the destination directory.
  file_check_directory($upload_directory, FILE_CREATE_DIRECTORY);
  file_check_directory($widget_directory, FILE_CREATE_DIRECTORY);

  // Create the new stars.
  $star = _fivestar_color_render(str_replace($widget . '.css', 'star-template.png', $form_state['values']['fivestar_widget']), $form_state['values']['fivestar_colors'], $form_state['values']['fivestar_color_type']);
  $cancel = _fivestar_color_render(str_replace($widget . '.css', 'cancel-template.png', $form_state['values']['fivestar_widget']), $form_state['values']['fivestar_colors'], $form_state['values']['fivestar_color_type']);
  imagepng($star, $widget_directory . '/star.png');
  imagepng($cancel, $widget_directory . '/cancel.png');

  // Copy over the stylesheet.
  file_copy($widget_css, $widget_directory . '/' . $widget . '.css', FILE_EXISTS_REPLACE);

  // Copy over RTL stylesheet.
  if (file_exists($widget_rtl_css)) {
    file_copy($widget_rtl_css, $widget_directory . '/' . $widget . '-rtl.css', FILE_EXISTS_REPLACE);
  }
  $form_state['values']['fivestar_widget'] = $widget_css;
  variable_set('fivestar_colors', $form_state['values']['fivestar_colors']);
  variable_set('fivestar_color_type', $form_state['values']['fivestar_color_type']);
  drupal_clear_css_cache();
  drupal_set_message(t('Custom %name stars generated. You may need to clear your browser cache before the new stars are visible.', array(
    '%name' => t($widget),
  )));
}