You are here

class sweaver_plugin_palettes in Sweaver 6

Same name and namespace in other branches
  1. 7 plugins/sweaver_plugin_palettes/sweaver_plugin_palettes.inc \sweaver_plugin_palettes

@file Palettes plugin.

Hierarchy

Expanded class hierarchy of sweaver_plugin_palettes

1 string reference to 'sweaver_plugin_palettes'
_sweaver_sweaver_plugins in ./sweaver.registry.inc
Sweaver plugins.

File

plugins/sweaver_plugin_palettes/sweaver_plugin_palettes.inc, line 7
Palettes plugin.

View source
class sweaver_plugin_palettes extends sweaver_plugin {

  /**
   * Frontend css and js.
   */
  public function sweaver_form_css_js(&$inline_settings) {
    drupal_add_js(drupal_get_path('module', 'sweaver') . '/plugins/sweaver_plugin_palettes/sweaver_plugin_palettes.js', 'module');
  }

  /**
   * Preprocess page function.
   */
  function sweaver_preprocess_page(&$vars) {

    // Get active palette.
    $current_style = Sweaver::get_instance()
      ->get_current_style();
    $active_palette = isset($current_style->palette) ? $current_style->palette : '';

    // Add the correct stylesheet if an active palette is found.
    if (!empty($active_palette)) {
      $theme_key = Sweaver::get_instance()
        ->get_theme_key();
      $info = db_result(db_query("SELECT info FROM {system} WHERE name = '%s' AND type ='theme'", $theme_key));
      $theme_info = unserialize($info);
      if (isset($theme_info['sweaver']['palettes'][$active_palette])) {
        $css = drupal_get_path('theme', $theme_key) . '/' . $theme_info['sweaver']['palettes'][$active_palette]['file'];

        // Add the css.
        drupal_add_css($css, 'theme');

        // Update $variables['css'].
        $vars['css']['all']['theme'][$css] = TRUE;

        // Update $variables['styles'].
        $vars['styles'] = drupal_get_css();
      }
    }
  }

  /**
   * Frontend form.
   */
  public function sweaver_form() {
    $current_style = Sweaver::get_instance()
      ->get_current_style();
    $active_palette = isset($current_style->palette) ? $current_style->palette : '';
    global $base_path;
    $form = array();
    $containers = '';
    $theme_key = Sweaver::get_instance()
      ->get_theme_key();
    $theme_info = sweaver_get_theme_info($theme_key);
    if (isset($theme_info['sweaver']['palettes'])) {

      // Combine all properties in containers.
      $i = 0;
      $container_count = 1;
      $container_total = ceil(count($theme_info['sweaver']['palettes']) / 5);
      $containers .= '<div class="container-wrapper">';
      foreach ($theme_info['sweaver']['palettes'] as $key => $palette) {
        if ($i == 0) {

          // Add first/last classes to the containers.
          $container_class = '';
          if ($container_count == 1) {
            $container_class .= ' container-first';
          }
          if ($container_total == $container_count) {
            $container_class .= ' container-last';
          }
          $containers .= '<div class="container ' . $container_class . '"><div class="container-inner">';
        }

        // Add the actual content.
        $extra_class = $active_palette == $key ? ' active' : '';
        $containers .= '<div id="palette-' . $key . '" class="colors clear-block' . $extra_class . '">';
        foreach ($palette['colors'] as $color) {
          $containers .= '<div class="color" style="background-color:' . $color . ' !important">' . $color . '</div>';
        }
        $containers .= '<div class="name">' . $palette['name'] . '</div>';

        // Add a hidden field with the path to the css file.
        $containers .= '<div class="file" style="display: none;">' . $base_path . drupal_get_path('theme', $theme_key) . '/' . $palette['file'] . '</div>';

        // Add a hidden field with the key.
        $containers .= '<div class="key" style="display: none;">' . $key . '</div>';
        $containers .= '</div>';
        $i++;

        // close the container div if necessary.
        if ($i == 5) {
          $containers .= '</div></div>';
          $container_count++;
          $i = 0;
        }
      }
      if ($i != 0) {
        $containers .= '</div></div>';
      }
      $containers .= '</div>';

      // Add a hidden field that stores the active key to be saved.
      $form['sweaver_plugin_palette'] = array(
        '#type' => 'hidden',
        '#default_value' => $active_palette ? $active_palette : '',
      );
      $form['markup'] = array(
        '#type' => 'markup',
        '#value' => $containers,
      );
    }
    return $form;
  }

  /**
   * Frontend form submit handler.
   */
  function sweaver_form_submit($form, &$form_state) {
    $clicked_button = $form_state['clicked_button']['#value'];
    if (($clicked_button == t('Save and continue') || $clicked_button == t('Save and publish') || $clicked_button == t('Publish style')) && isset($form_state['style_id']) && isset($form_state['values']['sweaver_plugin_palette'])) {
      db_query("UPDATE {sweaver_style_draft} set palette = '%s' WHERE style_id = %d", $form_state['values']['sweaver_plugin_palette'], $form_state['style_id']);
      if ($form_state['publish']) {
        db_query("UPDATE {sweaver_style} set palette = '%s' WHERE style_id = %d", $form_state['values']['sweaver_plugin_palette'], $form_state['style_id']);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
sweaver_plugin::sweaver_dependencies public function Sweaver dependencies. 2
sweaver_plugin::sweaver_form_render public function Sweaver form render. 2
sweaver_plugin::sweaver_images_handler public function Images handler. 1
sweaver_plugin::sweaver_init public function Init function. 2
sweaver_plugin::sweaver_menu public function Menu registry. 6
sweaver_plugin::sweaver_menu_callback public function Menu callback. 5
sweaver_plugin::sweaver_menu_callback_submit public function Menu callback submit. 2
sweaver_plugin::sweaver_menu_callback_validate public function Menu callback validate.
sweaver_plugin::sweaver_objects_alter public function Sweaver objects alter. 1
sweaver_plugin::sweaver_popups_render protected function Helper function render the popups.
sweaver_plugin::sweaver_theme public function Theme registry. 1
sweaver_plugin_palettes::sweaver_form public function Frontend form. Overrides sweaver_plugin::sweaver_form
sweaver_plugin_palettes::sweaver_form_css_js public function Frontend css and js. Overrides sweaver_plugin::sweaver_form_css_js
sweaver_plugin_palettes::sweaver_form_submit function Frontend form submit handler. Overrides sweaver_plugin::sweaver_form_submit
sweaver_plugin_palettes::sweaver_preprocess_page function Preprocess page function. Overrides sweaver_plugin::sweaver_preprocess_page