You are here

public function sweaver_plugin_styles::sweaver_form 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()

Frontend form: add styles form.

Overrides sweaver_plugin::sweaver_form

File

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

Class

sweaver_plugin_styles

Code

public function sweaver_form() {
  $form = array();
  $form['#popups'] = array();

  // Current style.
  $sweaver = Sweaver::get_instance();
  $current_style = $sweaver
    ->get_current_style();

  // Saved styles in database.
  $existing_styles = FALSE;
  $existing_styles_options = array();

  // Draft versions.
  $drafts = db_query("SELECT style_id, style FROM {sweaver_style_draft} where theme = :theme", array(
    ':theme' => $sweaver
      ->get_theme_key(),
  ))
    ->fetchAll();
  foreach ($drafts as $draft) {
    $existing_styles_options[$draft->style_id] = $draft->style;
  }
  if (count($existing_styles_options) > 0) {
    $existing_styles = TRUE;
  }

  // Live versions.
  $lives = db_query("SELECT style_id, style FROM {sweaver_style} where theme = :theme AND active = 1", array(
    ':theme' => $sweaver
      ->get_theme_key(),
  ))
    ->fetchAll();
  foreach ($lives as $live) {
    $existing_styles_options[$live->style_id] = $live->style;
  }

  // Save popup.
  $form['save_style_popup']['save_style'] = array(
    '#type' => 'textfield',
    '#size' => 50,
    '#weight' => 1,
  );
  if ($existing_styles) {

    // Hide new style name by default.
    $form['save_style_popup']['save_style']['#attributes'] = array(
      'style' => 'display:none',
    );
    $form['save_style_popup']['save_type'] = array(
      '#type' => 'select',
      '#options' => array(
        0 => t('New style'),
        1 => t('Existing style'),
      ),
      '#default_value' => 1,
      '#attributes' => array(
        'class' => array(
          'radio-style-save-type',
        ),
      ),
      '#weight' => 0,
    );
    $form['save_style_popup']['style_existing_id'] = array(
      '#type' => 'select',
      '#options' => $existing_styles_options,
      '#default_value' => isset($current_style->style_id) ? $current_style->style_id : 0,
      '#weight' => 2,
    );
  }
  else {

    // If no existing styles, set style save type to new.
    $form['save_style_popup']['save_type'] = array(
      '#type' => 'value',
      '#value' => 0,
    );
    $form['save_style_popup']['style_existing_id'] = array(
      '#type' => 'value',
      '#value' => 0,
    );
  }

  // Save buttons for save popup.
  $form['save_style_popup']['save']['save_continue'] = array(
    '#type' => 'submit',
    '#value' => t('Save and continue'),
    '#weight' => 4,
  );
  $form['save_style_popup']['save']['save_publish'] = array(
    '#type' => 'submit',
    '#value' => t('Save and publish'),
    '#weight' => 5,
  );
  $form['save_style_popup']['save']['#weight'] = 10;
  $form['save_style_popup']['save']['#prefix'] = '<div class="save-publish-buttons clearfix">';
  $form['save_style_popup']['save']['#suffix'] = '</div>';

  // Load popup.
  if ($existing_styles) {
    $load_style_options = $existing_styles_options;

    // Add the active versions of a style.
    $active_results = db_query("SELECT style_id, style FROM {sweaver_style} WHERE active = 1")
      ->fetchAll();
    foreach ($active_results as $active) {
      $load_style_options[$active->style_id . '_live_table'] = $active->style . ' (' . t('live') . ')';
    }

    // Fresh start.
    $load_style_options[0] = t('Fresh style');
    $form['load_style_popup']['load_style'] = array(
      '#type' => 'select',
      '#options' => $load_style_options,
    );
    $form['load_style_popup']['load_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Load style'),
    );
  }

  // Publish popup. Make sure we don't save the temporary one.
  if (isset($current_style->style_id) && !empty($current_style->style_id)) {
    $form['publish_style_popup']['publish_style'] = array(
      '#markup' => t('Set style %stylename visible for your visitors. Any unsaved changes will also be saved when publishing.', array(
        '%stylename' => $current_style->style,
      )),
    );
    $form['publish_style_popup']['publish_id'] = array(
      '#type' => 'hidden',
      '#value' => $current_style->style_id,
    );
    $form['publish_style_popup']['publish_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Publish style'),
    );
  }

  // Delete popup.
  if ($existing_styles && variable_get('sweaver_styles_delete_tab', FALSE)) {

    // Question.
    $form['delete_style_popup']['question']['delete_style'] = array(
      '#type' => 'select',
      '#options' => $existing_styles_options,
    );
    $form['delete_style_popup']['question']['delete_confirm'] = array(
      '#type' => 'button',
      '#value' => t('Delete'),
    );
    $form['delete_style_popup']['question']['#prefix'] = '<div class="delete-style-confirm">';
    $form['delete_style_popup']['question']['#suffix'] = '</div>';

    // Confirmation.
    $form['delete_style_popup']['warning']['delete_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Delete style'),
    );
    $form['delete_style_popup']['warning']['delete_cancel'] = array(
      '#type' => 'button',
      '#value' => t('Cancel'),
    );
    $form['delete_style_popup']['warning']['#prefix'] = '<div class="delete-style-question clearfix" style="display:none">';
    $form['delete_style_popup']['warning']['#prefix'] .= '<div>' . t('Are you sure you want to delete the style ? All changes and files will be lost. If the style is also active, your visitors will see the default theming.') . '</div>';
    $form['delete_style_popup']['warning']['#suffix'] = '</div>';
  }

  // Build the popup links & content.
  $i = 0;
  $form['#popups_links'] = '';
  $form['#popups_styles'] = array(
    'save_style_popup' => array(
      'title' => t('Save'),
      'description' => 'Save and keep working on your style. You can also publish it immediately for your visitors.',
    ),
    'load_style_popup' => array(
      'title' => t('Load'),
      'description' => t('Load a style to continue working on it. It will only be visible for your visitors after publishing.'),
    ),
    'publish_style_popup' => array(
      'title' => t('Publish'),
      'description' => '',
    ),
    'delete_style_popup' => array(
      'title' => t('Delete'),
      'description' => t('Delete a style which you do not want to use anymore.'),
    ),
  );
  foreach ($form['#popups_styles'] as $key => $action) {
    if (isset($form[$key])) {
      ++$i;
      $form['#popups'][] = $key;
      $form['#popups_links'] .= '<div class="style-actions-link"><a href="#" id="style-actions-link-' . $i . '">' . $action['title'] . '</a></div>';
      $form[$key]['#prefix'] = '<div style="display: none;" class="' . str_replace('_', '-', $key) . '" id="style-actions-data-' . $i . '"><h2>' . t('@action style', array(
        '@action' => $action['title'],
      )) . '</h2><p>' . $action['description'] . '</p>';
      $form[$key]['#suffix'] = '</div>';
    }
  }
  return $form;
}