You are here

function sweaver_drush_edit_style in Sweaver 6

Same name and namespace in other branches
  1. 7 drush/sweaver.drush.inc \sweaver_drush_edit_style()

Edit a style.

Parameters

$style: A style object from the Database.

1 call to sweaver_drush_edit_style()
sweaver_drush_style in drush/sweaver.drush.inc
Edit a style via drush.

File

drush/sweaver.drush.inc, line 85
Sweaver Drush functions.

Code

function sweaver_drush_edit_style($style) {
  $warning = variable_get('sweaver_drush_warning_edit', 'true');
  $editor = variable_get('sweaver_drush_editor', 'vi');

  // Display the warning text if needed.
  if ($warning == 'true') {
    $return = drush_confirm(sweaver_drush_warning_text());
    if (!$return) {

      // Bail out.
      drush_set_context("DRUSH_EXECUTION_COMPLETED", TRUE);
      drush_print(dt('Editing of style aborted.'));
      exit;
    }
  }

  // Prepare the css variable from the editor.
  $css = json_decode($style->css);
  $css = !empty($css) ? sweaver_drush_json_to_css($css) : '';

  // Create a static $data variable with CTools.
  $data =& ctools_static('sweaver_drush_style_data', array());
  $data = array(
    'style' => $style,
    'editor' => $editor,
    'ts_enabled' => FALSE,
  );

  // Get some plugins.
  $sweaver = Sweaver::get_instance();
  $sweaver_cs = $sweaver
    ->get_plugin('sweaver_plugin_customcss');
  $sweaver_ts = $sweaver
    ->get_plugin('sweaver_plugin_themesettings');

  // Create steps array.
  $steps = array(
    'css' => $css,
  );

  // Add custom css if enabled.
  if ($sweaver_cs) {
    $steps['customcss'] = $style->customcss;
  }

  // Add themesettings if enabled.
  if ($sweaver_ts) {
    $data['ts_enabled'] = TRUE;
    $steps['themesettings'] = sweaver_drush_array_to_string($style->themesettings);
  }
  $data['steps'] = $steps;

  // Step through.
  foreach ($steps as $step => $step_value) {

    // Ask confirmation first.
    $edit = drush_confirm(dt('Do you want to edit the @step ?', array(
      '@step' => $step,
    )));
    if ($edit) {
      $temp_file = drush_save_data_to_temp_file($step_value);
      $data['tempfiles'][$step] = $temp_file;
      $data['current_step'] = $step;
      $edit_command = $editor . ' ' . $temp_file;
      $pipes = array();

      // Opend the editor.
      proc_close(proc_open($edit_command, array(
        0 => STDIN,
        1 => STDOUT,
        2 => STDERR,
      ), $pipes));

      // We need to save the new content already here, because we lose the temporary data file.
      $step = $data['current_step'];
      $temp_file = $data['tempfiles'][$step];
      if ($content = file_exists($temp_file)) {
        switch ($step) {
          case 'css':
            $css = file_get_contents($temp_file);
            $style->write_css = $css;
            $style->css = sweaver_drush_css_to_json($css);
            break;
          case 'customcss':
            $data['style']->customcss = file_get_contents($temp_file);
            break;
          case 'themesettings':
            $data['style']->themesettings = serialize(sweaver_drush_array_lines_to_array_key_values(file($temp_file)));
            break;
        }
      }
    }
  }

  // Get the variables again.
  $style = $data['style'];
  $steps = $data['steps'];

  // Do we want to save ?
  $message = 'Do you want to save all changes ?';
  if (isset($style->active) && $style->active) {
    $message = 'Do you want to save all changes ? All changes will be visible for your visitors.';
  }
  $save = drush_confirm(dt($message));

  // No save.
  if (!$save) {
    drush_print(dt('Saving aborted, but was\'t this jolly good fun ? :)'));
  }
  else {

    // Get sweaver plugin styles.
    $sweaver_styles = $sweaver
      ->get_plugin('sweaver_plugin_styles');

    // Save into database.
    $update = array(
      'style_id',
    );

    // Create stylesheet.
    $stylesheet = $style->write_css;
    $stylesheet .= "\n" . $style->customcss;

    // Save draft version.
    if (!isset($style->active)) {
      drupal_write_record('sweaver_style_draft', $style, $update);
      $sweaver_styles
        ->sweaver_export_file($stylesheet, $style);
      drush_print(dt('Draft version has been saved.'));
    }

    // Save live version.
    if (isset($style->active)) {
      drupal_write_record('sweaver_style', $style, $update);
      drush_print(dt('Live version has been saved.'));
    }

    // Export css files and set themesettings.
    if (isset($style->active)) {
      $sweaver_styles
        ->sweaver_export_file($stylesheet, $style, 'live');
      if ($data['ts_enabled']) {
        $theme_settings_var = str_replace('/', '_', 'theme_' . $style->theme . '_settings');
        variable_set($theme_settings_var, unserialize($style->themesettings));
      }
    }

    // Flush cache.
    sweaver_clear_cache();
  }
}