You are here

public function sweaver_plugin_themesettings::sweaver_form_submit in Sweaver 7

Same name and namespace in other branches
  1. 6 plugins/sweaver_plugin_themesettings/sweaver_plugin_themesettings.inc \sweaver_plugin_themesettings::sweaver_form_submit()

Frontend form submit.

Overrides sweaver_plugin::sweaver_form_submit

File

plugins/sweaver_plugin_themesettings/sweaver_plugin_themesettings.inc, line 110
Theme settings plugin.

Class

sweaver_plugin_themesettings
@file Theme settings plugin.

Code

public function sweaver_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $theme_key = $values['var'];
  $clicked_button = $form_state['clicked_button']['#value'];

  // Save style.
  if ($clicked_button == t('Save and continue') || $clicked_button == t('Save and publish') || $clicked_button == t('Publish style') || $clicked_button == t('Apply theme settings')) {

    // Style id is not always set.
    $style_id = isset($form_state['style_id']) ? $form_state['style_id'] : 'temp';

    // General theme settings.
    $theme_values = array();
    $theme_settings_form = $form['sweaver_plugin_themesettings']['form']['theme_settings'];
    $theme_form_keys = element_children($theme_settings_form);

    // Logo and favicon.
    if (isset($form['sweaver_plugin_themesettings']['form']['logo'])) {
      $theme_form_keys = array_merge($theme_form_keys, array(
        'default_logo',
        'logo_path',
        'logo_upload',
      ));
    }
    if (isset($form['sweaver_plugin_themesettings']['form']['favicon'])) {
      $theme_form_keys = array_merge($theme_form_keys, array(
        'default_favicon',
        'favicon_path',
        'favicon_upload',
      ));
    }

    // Theme specific settings.
    if (isset($form['sweaver_plugin_themesettings']['form']['theme_specific'])) {
      $theme_specific_keys = array();
      $fapi_types = array(
        'select',
        'checkbox',
        'checkboxes',
        'textfield',
        'textarea',
        'value',
        'hidden',
        'radio',
        'weight',
        'radios',
      );
      $this
        ->sweaver_get_theme_specific_keys($form['sweaver_plugin_themesettings']['form']['theme_specific'], $theme_specific_keys, $fapi_types);
      $theme_form_keys = array_merge($theme_form_keys, $theme_specific_keys);
    }

    // Other variables which are set by other form alters.
    // All keys must be put in a $form['sweaver_plugin_themesettings']['form']['other_theme_settings'];
    $other_theme_settings = explode(',', $values['other_theme_settings']);
    $theme_form_keys = array_merge($theme_form_keys, $other_theme_settings);

    // Only save the necessary theme values.
    foreach ($values as $fkey => $fvalue) {
      if (in_array($fkey, $theme_form_keys)) {
        $theme_values[$fkey] = $fvalue;
      }
    }

    // Rename logo / favicon when needed.
    foreach (array(
      'logo_path',
      'favicon_path',
    ) as $file) {

      // Create the default variable for this file.
      $default = 'default_' . str_replace('_path', '', $file);
      $theme_name = $form['#current_theme'];

      // Rename when just uploaded.
      if (isset($form['sweaver_plugin_themesettings']['form']['#' . $file])) {
        $type = str_replace('_path', '', $file);
        $source_path = $form['sweaver_plugin_themesettings']['form']['#' . $file];
        $dest_draft = 'public://sweaver/' . $type . '_' . $theme_name . '_' . $style_id . '_draft.' . $form['sweaver_plugin_themesettings']['form']['#' . $file . '_extension'];
        file_unmanaged_move($source_path, $dest_draft, FILE_EXISTS_REPLACE);
        $theme_values[$file] = $dest_draft;
        $theme_values[$default] = 0;
      }

      // Move temporary if needed.
      if ($style_id != 'temp' && !$theme_values[$default] && !empty($theme_values[$file]) && strpos($theme_values[$file], 'temp_draft') !== FALSE) {
        $type = str_replace('_path', '', $file);
        $parts = pathinfo($theme_values[$file]);
        $temp_path = $theme_values[$file];
        $dest_draft = 'public://sweaver/' . $type . '_' . $theme_name . '_' . $style_id . '_draft.' . $parts['extension'];
        file_unmanaged_move($temp_path, $dest_draft, FILE_EXISTS_REPLACE);
        $theme_values[$file] = $dest_draft;
      }

      // Copy to live if save_live is found. Can either be directly after the upload
      // or when setting a style live after the uploads were done already.
      if (isset($form_state['publish']) && $form_state['publish'] && !$theme_values[$default] && !empty($theme_values[$file])) {
        $source_path = $theme_values[$file];
        $dest_live = str_replace('_draft', '_live', $theme_values[$file]);
        file_unmanaged_copy($source_path, $dest_live, FILE_EXISTS_REPLACE);
        $theme_values[$file] = $dest_live;
      }
    }

    // Save when style_id is found. Always save the draft version. If the
    // save_live key is found also update the live table.
    if ($style_id != 'temp') {
      $theme_values_serialized = serialize($theme_values);
      db_query("UPDATE {sweaver_style_draft} set themesettings = :themesettings WHERE style_id = :style_id", array(
        ':themesettings' => $theme_values_serialized,
        ':style_id' => $style_id,
      ));
      if ($form_state['publish']) {
        $old_settings = variable_get($theme_key);
        $new_settings = array_merge($old_settings, $theme_values);
        variable_set($theme_key, $new_settings);
        db_query("UPDATE {sweaver_style} set themesettings = :themesettings WHERE style_id = :style_id", array(
          ':themesettings' => $theme_values_serialized,
          ':style_id' => $style_id,
        ));
      }

      // Fire other submit functions on the theme settings form.
      $submit_handlers = isset($form['sweaver_plugin_themesettings']['form']['#submit']) ? $form['sweaver_plugin_themesettings']['form']['#submit'] : array();
      foreach ($submit_handlers as $key => $function) {
        if (function_exists($function)) {
          $function($form['sweaver_plugin_themesettings']['form'], $form_state);
        }
      }
    }
    else {

      // Save to CTools object cache.
      $sweaver = Sweaver::get_instance();
      ctools_include('object-cache');
      $style = $sweaver
        ->get_current_style();
      if (!isset($style->style_id)) {
        $style = new stdClass();
        $style->style_id = 0;
        $style->style = t('Temporary');
        $style->css = '';
        $style->customcss = '';
        $style->palette = '';
      }
      $style->themesettings = serialize($theme_values);
      ctools_object_cache_set('sweaver-styling', 'sweaver-styling', $style);
      sweaver_session(t('The theme settings have been applied.'));
      sweaver_session(TRUE, 'sweaver_temp');
    }
  }

  // Delete style.
  if (isset($form_state['style_to_delete'])) {
    $style = $form_state['style_to_delete'];

    // Reset theme settings when active.
    if ($form_state['style_active']) {
      variable_del($theme_key);
    }
  }
}