You are here

public function sweaver_plugin_styles::sweaver_form_submit in Sweaver 7

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

Frontend form submit.

Overrides sweaver_plugin::sweaver_form_submit

File

plugins/sweaver_plugin_styles/sweaver_plugin_styles.inc, line 286
Styles plugin.

Class

sweaver_plugin_styles

Code

public function sweaver_form_submit($form, &$form_state) {
  $sweaver_directory = 'public://sweaver';
  file_prepare_directory($sweaver_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  $styles_form = $form['sweaver_plugin_styles']['form'];
  $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')) {

    // Reset session.
    $this
      ->sweaver_reset_style_session();
    $update = array();
    $style = new stdClass();
    $theme_key = $form['#current_theme'];
    $form_state['publish'] = TRUE;

    // Submit type.
    if ($clicked_button == t('Save and publish')) {
      $submit_type = SWEAVER_STYLE_SAVE_PUBLISH;
    }
    elseif ($clicked_button == t('Publish style')) {
      $submit_type = SWEAVER_STYLE_PUBLISH;
    }
    else {
      $form_state['publish'] = FALSE;
      $submit_type = SWEAVER_STYLE_SAVE;
    }

    // Create variables from form_state, easier to re-use them in the flow.
    $style_name = $form_state['values']['save_style'];
    $keep_working = $submit_type == SWEAVER_STYLE_PUBLISH ? FALSE : TRUE;
    $save_type = $submit_type == SWEAVER_STYLE_PUBLISH ? TRUE : $form_state['values']['save_type'];
    $style_existing_id = $submit_type == SWEAVER_STYLE_PUBLISH ? $form_state['values']['publish_id'] : $form_state['values']['style_existing_id'];
    if ($save_type) {
      $update = array(
        'style_id',
      );
      $style->style_id = $style_existing_id;
      $style->style = $styles_form['save_style_popup']['style_existing_id']['#options'][$style->style_id];
    }
    else {
      $style->style = $style_name;
      if (empty($style->style)) {
        $style->style = t('Undefined');
      }

      // Check if this name is already used
      $styles = db_select('sweaver_style_draft', 's')
        ->fields('s', array(
        'style',
      ))
        ->execute()
        ->fetchAll();
      $i = '';
      do {
        $exist = FALSE;
        if ($i == '') {
          $name = $style->style;
        }
        else {
          $name = $style->style . ' ' . $i;
        }
        foreach ($styles as $db_style) {
          if ($name == $db_style->style) {
            $exist = TRUE;
          }
        }
        $i = $i == '' ? 2 : $i + 1;
      } while ($exist);
      $style->style = $name;
    }

    // Always save the draft version.
    $style->theme = $theme_key;
    $sweaver = Sweaver::get_instance();
    $working_style = $sweaver
      ->get_current_style();
    $sweaver_css = isset($form_state['values']['sweaver-css']) ? $form_state['values']['sweaver-css'] : $working_style->css;
    $css_rendered = isset($form_state['values']['css-rendered']) ? $form_state['values']['css-rendered'] : '';
    $style->css = $sweaver_css;
    drupal_write_record('sweaver_style_draft', $style, $update);
    $this
      ->sweaver_export_file($css_rendered, $style);
    $form_state['style_id'] = $style->style_id;

    // Publish style.
    if ($submit_type == SWEAVER_STYLE_PUBLISH || $submit_type == SWEAVER_STYLE_SAVE_PUBLISH) {
      $style->active = 1;
      $this
        ->sweaver_export_file($css_rendered, $style, 'live');
      $message = 'The style @style has been published.';
      db_query("UPDATE {sweaver_style} set active = 0 WHERE theme = :theme", array(
        ':theme' => $style->theme,
      ));

      // Find out first if this style already exists or not.
      if (db_query("SELECT style_id FROM {sweaver_style} WHERE style_id = :style_id", array(
        ':style_id' => $style->style_id,
      ))
        ->fetchField() === FALSE) {
        $update = array();
      }
      drupal_write_record('sweaver_style', $style, $update);
    }

    // Draft only save.
    if ($submit_type != SWEAVER_STYLE_PUBLISH) {
      sweaver_session(TRUE, 'draft_mode');
      sweaver_session($style->style_id, 'loaded_style');
      sweaver_session('draft', 'loaded_table');
      $message = 'The style @style has been saved. You can keep working on your style.';
    }

    // Draft & published save.
    if ($submit_type == SWEAVER_STYLE_SAVE_PUBLISH) {
      $message = 'The style @style has been saved and published. You can keep working on your style.';
    }
    sweaver_session(t($message, array(
      '@style' => $style->style,
    )));
  }

  // Load style.
  if ($clicked_button == t('Load style')) {

    // Reset session.
    $this
      ->sweaver_reset_style_session();

    // Build message and session variables.
    $theme_key = $form['#current_theme'];
    $style_id = $form_state['values']['load_style'];
    $style_name = $styles_form['load_style_popup']['load_style']['#options'][$style_id];

    // Load from draft or live table ?
    $table = 'draft';
    $pos = strpos($style_id, '_live_table');
    if ($pos !== FALSE) {
      $table = 'live';
      $style_id = str_replace('_live_table', '', $style_id);
    }
    sweaver_session(TRUE, 'draft_mode');
    sweaver_session($style_id, 'loaded_style');
    sweaver_session($table, 'loaded_table');
    sweaver_session(t('The style @style has been loaded. It is only visible for you.', array(
      '@style' => $style_name,
    )));
  }

  // Delete style.
  if ($clicked_button == t('Delete style')) {

    // Reset session.
    $this
      ->sweaver_reset_style_session();

    // Get info from db and store in form_state so other modules can profit from it.
    $style_id = $form_state['values']['delete_style'];
    $style = db_query("SELECT * FROM {sweaver_style_draft} WHERE style_id = :style_id", array(
      ':style_id' => $style_id,
    ))
      ->fetchObject();
    $style_live = db_query("SELECT * FROM {sweaver_style} WHERE style_id = :style_id", array(
      ':style_id' => $style_id,
    ))
      ->fetchObject();
    $form_state['style_active'] = isset($style_live->active) ? $style_live->active : FALSE;
    $form_state['style_to_delete'] = $style;

    // Delete entries from tables, files.
    $this
      ->sweaver_delete_style($style);

    // Message.
    $style_name = $styles_form['delete_style_popup']['question']['delete_style']['#options'][$style_id];
    sweaver_session(t('The style @style has been deleted.', array(
      '@style' => $style_name,
    )));
  }

  // Clear page & block cache and page requisites.
  sweaver_clear_cache();
}