You are here

sweaver.module in Sweaver 6

Same filename and directory in other branches
  1. 7 sweaver.module

Sweaver functions.

File

sweaver.module
View source
<?php

/**
 * @file
 * Sweaver functions.
 */

/**
 * Default paths to exclude the frontend editor from.
 */
define('SWEAVER_PATHS_EXCLUDE', "admin*\nnode/add*\nnode/*/edit\nnode/*/panel*\npanels/ajax*\nuser/*/edit\nbatch*\nwysiwyg_imageupload/*");

/**
 * Default selectors to exclude from selecting.
 */
define('SWEAVER_SELECTORS_EXCLUDE', "#sweaver-frontend\n#admin-menu\n.colorpicker\n#sweaver-messages\n#sweaver-popup\n#follow-link\n#l10n-client");

/**
 * Default classes to exclude from not being themed.
 */
define('SWEAVER_CLASSES_EXCLUDE', "clear\nclear-fix\nclear-block\nsweaver\nsweaver-hovered\nsweaver-clicked\nsweaver-clicked-temp");

/**
 * Sweaver default usable CSS properties extending the defaults from CTools.
 */
define('SWEAVER_CTOOLS_ALLOWED_PROPERTIES', "-moz-border-radius\n-webkit-border-radius\nborder-top-right-radius\nborder-bottom-right-radius\nborder-bottom-left-radius\nborder-top-left-radius\nwebkit-border-top-right-radius\nwebkit-border-bottom-right-radius\nwebkit-border-bottom-left-radius\nwebkit-border-top-left-radius\nmoz-border-radius-topright\nmoz-border-radius-bottomright\nmoz-border-radius-bottomleft\nmoz-border-radius-topleft\nposition\ntop\nleft\nbottom\nright\nborder-collapse\nborder-spacing\nlist-style-image\nlist-style-type\nmin-width\nmax-width\nmin-height\nmax-height\nvisibility");

/**
 * Default skin.
 */
define('SWEAVER_SKIN', "default");

/**
 * Require sweaver.inc
 */
require_once 'sweaver.inc';

/**
 * Implementation of hook_perm().
 */
function sweaver_perm() {
  return array(
    'configure sweaver',
    'use editor',
  );
}

/**
 * Implementation of hook_menu().
 */
function sweaver_menu() {
  module_load_include('inc', 'sweaver', 'sweaver.registry');
  return _sweaver_menu();
}

/**
 * Implementation of hook_theme().
 */
function sweaver_theme() {
  module_load_include('inc', 'sweaver', 'sweaver.registry');
  return _sweaver_theme();
}

/**
 * Implementation of hook_ctools_plugin_api().
 */
function sweaver_ctools_plugin_api($owner, $api) {
  if ($owner == 'sweaver' && ($api == 'sweaver' || $api == 'plugins')) {
    return array(
      'version' => 1,
    );
  }
}

/**
 * Implementation of hook_ctools_plugin_plugins().
 */
function sweaver_ctools_plugin_plugins() {
  return array(
    'cache' => TRUE,
    'use hooks' => TRUE,
  );
}

/**
 * Implementation of hook_default_sweaver_selector().
 */
function sweaver_default_sweaver_selector() {
  module_load_include('inc', 'sweaver', 'sweaver.registry');
  return _sweaver_default_sweaver_selector();
}

/**
 * Implementation of hook_default_sweaver_property().
 */
function sweaver_default_sweaver_property() {
  module_load_include('inc', 'sweaver', 'sweaver.registry');
  return _sweaver_default_sweaver_property();
}

/**
 * Implementation of hook_default_sweaver_type().
 */
function sweaver_default_sweaver_type() {
  module_load_include('inc', 'sweaver', 'sweaver.registry');
  return _sweaver_default_sweaver_type();
}

/**
 * Implementation of hook_sweaver_plugins().
 */
function sweaver_sweaver_plugins() {
  module_load_include('inc', 'sweaver', 'sweaver.registry');
  return _sweaver_sweaver_plugins();
}

/**
 * CTools selectors list callback for bulk export.
 */
function sweaver_ctools_selectors_list() {
  return sweaver_ctools_object_list('selector');
}

/**
 * CTools properties list callback for bulk export.
 */
function sweaver_ctools_properties_list() {
  return sweaver_ctools_object_list('property');
}

/**
 * CTools types list callback for bulk export.
 */
function sweaver_ctools_types_list() {
  return sweaver_ctools_object_list('type');
}

/**
 * Helper function to return list for CTools.
 */
function sweaver_ctools_object_list($object_type, $export_type = 2, $parent_check = FALSE) {
  $list = array();
  $objects = sweaver_object_load(NULL, $object_type, 'enabled');
  foreach ($objects as $object) {
    if ($object->export_type != $export_type) {

      // Parent check.
      if ($parent_check) {
        if ($object->property_type == 'parent') {
          continue;
        }
        elseif (!empty($object->property_parent)) {
          $object->description = $objects[$object->property_parent]->description . ': ' . $object->description;
        }
      }
      $list[$object->name] = $object->description;
    }
  }
  return $list;
}

/**
 * CTools selector export function.
 */
function sweaver_export_sweaver_selector($object) {
  return sweaver_export_sweaver_object($object, 'selector');
}

/**
 * CTools property export function.
 */
function sweaver_export_sweaver_property($object) {
  return sweaver_export_sweaver_object($object, 'property');
}

/**
 * CTools property export function.
 */
function sweaver_export_sweaver_type($object) {
  return sweaver_export_sweaver_object($object, 'type');
}

/**
 * CTools object export function.
 */
function sweaver_export_sweaver_object($object, $object_type) {
  ctools_include('export');
  sweaver_export_check_serialized_keys($object);
  $output = ctools_export_object('sweaver_' . $object_type, $object, '  ');
  return $output;
}

/**
 * Check if we have a serialized key. If so convert it to an array.
 *
 * @param $object
 *   A sweaver object.
 */
function sweaver_export_check_serialized_keys($object) {
  if (isset($object->table)) {
    $object_type = str_replace('sweaver_', '', $object->table);
    $key = $object_type . '_options';
    if (isset($object->{$key}) && !empty($object->{$key}) && is_string($object->{$key})) {
      $object->{$key} = unserialize($object->{$key});
    }
  }
}

/**
 * Load one object or all objects.
 *
 * @param $name
 *   The machine name of the object.
 * @param $map
 *   Can be an array passed on by the load arguments of the menu or a string.
 *   In case of array, object_type will be index 5.
 * @param $status
 *   Whether to return all objects or only enabled.
 * @return
 *   One object or an array of objects.
 */
function sweaver_object_load($name = NULL, $map = NULL, $status = 'enabled') {
  static $run = FALSE;
  static $objects = NULL;
  $object_type = is_array($map) ? $map[5] : $map;
  if (!$run) {
    if ($objects_cache = cache_get('sweaver')) {
      $objects = $objects_cache->data;
    }
    else {
      ctools_include('export');
      $objects = new stdClass();
      foreach (array(
        'sweaver_selector',
        'sweaver_property',
        'sweaver_type',
      ) as $object_to_load) {
        $loaded_objects = ctools_export_load_object($object_to_load);
        foreach ($loaded_objects as $key => $object) {
          $object_type_key = str_replace('sweaver_', '', $object->table);
          if (!isset($object->disabled) || isset($object->disabled) && $object->disabled == FALSE) {
            $objects->{$object_type_key}->enabled[$key] = $object;
          }
          $objects->{$object_type_key}->all[$key] = $object;
        }
      }

      // Let sweaver plugins alter objects.
      $sweaver = Sweaver::get_instance();
      foreach (array_keys($sweaver
        ->get_plugins_registry(TRUE)) as $plugin_name) {
        $sweaver_plugin = $sweaver
          ->get_plugin($plugin_name);
        $sweaver_plugin
          ->sweaver_objects_alter($objects);
      }
      cache_set('sweaver', $objects);
    }
    $run = TRUE;
  }
  if ($name) {
    return isset($objects->{$object_type}->all[$name]) ? $objects->{$object_type}->all[$name] : FALSE;
  }
  else {
    return isset($objects->{$object_type}->{$status}) ? $objects->{$object_type}->{$status} : array();
  }
}

/**
 * Get all plugins or a plugin handler.
 *
 * @deprecated Only here for backward compatibility.
 *
 * @param $plugin_name
 *   The name of the plugin.
 * @param $enabled
 *   Whether the plugin is enabled or not.
 * @return
 *   Either a list of (enabled) plugins or a(n) (enabled) loaded plugin class.
 */
function sweaver_get_plugin($plugin_name = NULL, $enabled = FALSE) {
  $sweaver = Sweaver::get_instance();
  if ($plugin_name == NULL) {
    return $sweaver
      ->get_plugins_registry($enabled);
  }
  else {
    return $sweaver
      ->get_plugin($plugin_name, $enabled);
  }
}

/**
 * Implementation of hook_init().
 */
function sweaver_init() {

  // We use the ctools_static here instead of a session, because
  // pressflow doesn't allow us to start a session without breaking
  // the page cache.
  $load_style =& ctools_static('load_style', TRUE);
  if (sweaver_show_editor()) {
    $sweaver = Sweaver::get_instance();
    $load_style = FALSE;
    $inline_js_settings = array(
      'sweaver' => array(
        'invokes' => array(),
      ),
    );
    $skin = variable_get('sweaver_skin', SWEAVER_SKIN);
    drupal_add_js(drupal_get_path('module', 'sweaver') . '/sweaver_plugin.js', 'module');
    drupal_add_css(drupal_get_path('module', 'sweaver') . '/skins/' . $skin . '/sweaver-' . $skin . '.css', 'module');
    foreach (array_keys($sweaver
      ->get_plugins_registry(TRUE)) as $plugin_name) {
      $sweaver_plugin = $sweaver
        ->get_plugin($plugin_name);

      // Fire init.
      $sweaver_plugin
        ->sweaver_init();

      // CSS and JS.
      $sweaver_plugin
        ->sweaver_form_css_js($inline_js_settings);
    }

    // JS inline settings.
    drupal_add_js($inline_js_settings, 'setting');
  }
}

/**
 * Implementation of hook_preprocess_page().
 */
function sweaver_preprocess_page(&$vars) {

  // Load css for this theme if available.
  if (ctools_static('load_style')) {
    $sweaver = Sweaver::get_instance();

    // Extra check for the theme if we need to load css files.
    if (ctools_static('load_style')) {
      if (!empty($GLOBALS['custom_theme'])) {
        $sweaver
          ->set_current_style($GLOBALS['custom_theme']);
      }
    }
    $sweaver_style = $sweaver
      ->get_current_style();
    if (isset($sweaver_style->css)) {
      $css = $vars['css'];
      $css_path = file_directory_path() . '/sweaver/sweaver_' . $sweaver
        ->get_theme_key() . '_' . $sweaver_style->style_id . '_' . $sweaver_style->type . '.css';
      drupal_add_css($css_path, 'theme');
      $vars['css']['all']['theme'][$css_path] = TRUE;
      $vars['styles'] = drupal_get_css();
      foreach (array_keys($sweaver
        ->get_plugins_registry(TRUE)) as $plugin_name) {
        $sweaver_plugin = $sweaver
          ->get_plugin($plugin_name);

        // Fire preprocess.
        $sweaver_plugin
          ->sweaver_preprocess_page($vars);
      }
    }
  }
}

/**
 * Implementation of hook_footer().
 */
function sweaver_footer($main = 0) {

  // We return the complete form in the footer.
  if (sweaver_show_editor()) {
    return drupal_get_form('sweaver_frontend') . '<div id="sweaver-messages"><div class="close">x</div><div class="message"></div></div>';
  }
}

/**
 * Sweaver editor containers.
 */
function sweaver_containers() {
  return array(
    'one' => t('Font'),
    'two' => t('Background'),
    'three' => t('Spacing'),
    'four' => t('Empty'),
  );
}

/**
 * Return all enabled themes.
 */
function sweaver_get_all_themes() {
  $themes =& ctools_static('all_themes', array());
  if (empty($themes)) {
    $themes = array();
    $result = db_query("SELECT filename, name, status, info FROM {system} WHERE type = 'theme' AND status = '1' ORDER BY name ASC");
    while ($theme = db_fetch_object($result)) {
      if ($theme->status) {
        $theme->info = unserialize($theme->info);
        $theme->prefix = isset($theme->info['engine']) ? $theme->info['engine'] : NULL;
        $themes[$theme->name] = $theme;
      }
    }
  }
  return $themes;
}

/**
 * Get theme info.
 *
 * @param $theme
 *   The machine name for the theme.
 * @return $theme_info
 *   The info about a theme.
 */
function sweaver_get_theme_info($theme) {
  $themes = sweaver_get_all_themes();
  return $themes[$theme]->info;
}

/**
 * Theme an image. Mimics theme('image') but uses the alias => TRUE option.
 *
 * @return $image
 *   A themed image.
 */
function sweaver_theme_image($path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
  if (!$getsize || is_file($path) && (list($width, $height, $type, $image_attributes) = @getimagesize($path))) {
    $attributes = drupal_attributes($attributes);
    $url = url($path, array(
      'alias' => TRUE,
    )) == $path ? $path : base_path() . $path;
    return '<img src="' . check_url($url) . '" alt="' . check_plain($alt) . '" title="' . check_plain($title) . '" ' . (isset($image_attributes) ? $image_attributes : '') . $attributes . ' />';
  }
}

/**
 * Session handler for sweaver.
 *
 * @param $value
 *   The value hold by the session key.
 * @param $session_key
 *   Name of the session key.
 * @param $action
 *   Action to perform. TRUE will remove the session key.
 * @param $return_string
 *   Whether to return the value of the session key as string
 *   or simply return the value.
 * @return
 *   Either FALSE, a value from the key or nothing.
 */
function sweaver_session($value = NULL, $session_key = 'sweaver_editor_messages', $remove = FALSE, $return_string = TRUE) {

  // Add value to session key.
  if (isset($session_key) && isset($value) && !$remove) {
    if (!isset($_SESSION[$session_key])) {
      $_SESSION[$session_key] = array();
    }
    $_SESSION[$session_key][] = $value;
  }
  elseif (isset($_SESSION[$session_key]) && !isset($value) && $remove) {
    if (isset($_SESSION[$session_key])) {
      unset($_SESSION[$session_key]);
    }
  }
  elseif (isset($_SESSION[$session_key])) {
    if ($return_string) {
      $return = FALSE;
      $values = $_SESSION[$session_key];
      if (count($values) > 1) {
        foreach ($values as $key => $value) {
          $return .= $value . "\n";
        }
      }
      elseif (isset($values[0])) {
        $return = $values[0];
      }
      return $return;
    }
    else {
      return $_SESSION[$session_key];
    }
  }
}

/**
 * Function to check if we are going to show the editor.
 */
function sweaver_show_editor() {
  static $run = FALSE;
  static $return = FALSE;
  if (!$run) {
    $run = TRUE;

    // Let's do the check for the editor early.
    if (variable_get('sweaver_enabled', TRUE)) {

      // Path visibility.
      $path = drupal_get_path_alias($_GET['q']);
      $page_match = drupal_match_path($path, variable_get('sweaver_paths', SWEAVER_PATHS_EXCLUDE));
      if ($path != $_GET['q']) {
        $page_match = $page_match || drupal_match_path($_GET['q'], variable_get('sweaver_paths', SWEAVER_PATHS_EXCLUDE));
      }
      $page_match = !(0 xor $page_match);

      // Compare all variables.
      if (user_access('use editor') && $page_match) {
        $return = TRUE;
      }
    }
  }
  return $return;
}

/**
 * Rock 'n' roll: the sweaver editor.
 */
function sweaver_frontend(&$form_state) {
  $form = array();
  $weight = 1;
  $form['#plugins'] = array();
  $form['#theme'] = 'sweaver_plugin';
  $form['#attributes'] = array(
    'enctype' => 'multipart/form-data',
  );
  $plugins_order = variable_get('sweaver_plugins_weight', array());
  $sweaver = Sweaver::get_instance();
  foreach ($sweaver
    ->get_plugins_registry(TRUE) as $plugin_name => $plugin) {
    $sweaver_plugin = $sweaver
      ->get_plugin($plugin_name);

    // Calculate weight.
    $default_weight = isset($plugins_order[$plugin_name]) ? $plugins_order[$plugin_name] : $weight++;
    if ($plugin_name == 'sweaver_plugin') {
      $default_weight = -100;
    }
    $form['#plugins_order'][$plugin_name] = $default_weight;
    $form['#plugins_full'][$plugin_name] = $plugin;

    // Form.
    $plugin_form = $sweaver_plugin
      ->sweaver_form();
    if (!empty($plugin_form)) {
      $form[$plugin['name']]['form'] = $plugin_form;
      if (isset($plugin['handler']['tab'])) {
        $form[$plugin['name']]['#tab_name'] = isset($plugin['handler']['tab']) ? $plugin['handler']['tab'] : drupal_ucfirst($plugin_name);
        $form[$plugin['name']]['#tab_description'] = isset($plugin['handler']['tab_description']) ? $plugin['handler']['tab_description'] : '';
      }
    }
  }

  // Editor messages.
  $messages = sweaver_session();
  sweaver_session(NULL, 'sweaver_editor_messages', TRUE);
  $form['sweaver-editor-messages'] = array(
    '#type' => 'hidden',
    '#value' => trim($messages),
  );
  $form['#current_theme'] = $sweaver
    ->get_theme_key();
  $form['destination'] = array(
    '#type' => 'hidden',
    '#value' => $_GET['q'],
  );
  return $form;
}

/**
 * Clear cache and page requisites.
 */
function sweaver_clear_cache() {
  cache_clear_all();
  drupal_clear_css_cache();
}

/**
 * Sweaver frontend submit.
 */
function sweaver_frontend_submit($form, &$form_state) {
  $sweaver = Sweaver::get_instance();
  foreach (array_keys($sweaver
    ->get_plugins_registry(TRUE)) as $plugin_name) {
    $sweaver_plugin = $sweaver
      ->get_plugin($plugin_name);

    // In the first version, we'll let everyone who has submit methods
    // simply do their stuff with the submitted values.
    $sweaver_plugin
      ->sweaver_form_submit($form, $form_state);
  }

  // Redirect to same path. We issue this function so things like
  // session, current style and possible others are properly set.
  drupal_goto();
}

/**
 * Sweaver menu callback.
 *
 * @param $settings
 *   A collection of page arguments from the menu callback.
 * @return
 *   The rendered page - can be form or anything else.
 */
function sweaver_menu_callback($settings) {
  $plugin_name = $settings['plugin'];
  $callback_method = isset($settings['callback_method']) ? $settings['callback_method'] : 'sweaver_menu_callback';
  $return_method = isset($settings['return_method']) ? $settings['return_method'] : 'drupal_get_form';
  $js = isset($settings['js']) ? TRUE : FALSE;
  if ($sweaver_plugin = Sweaver::get_instance()
    ->get_plugin($plugin_name, TRUE)) {
    $output = $sweaver_plugin
      ->{$callback_method}();
    if (!empty($output)) {
      if ($return_method == 'drupal_get_form') {
        return drupal_get_form('sweaver_menu_callback_form', $output, $plugin_name, $callback_method);
      }
      elseif (isset($js)) {
        exit(drupal_to_js($output));
      }
      elseif (!empty($return_method) && function_exists($return_method)) {
        return $return_method($output);
      }
      else {
        return $output;
      }
    }
  }
  return t('No page found or you do not have sufficient permissions to access the page.');
}

/**
 * Helper function to return the form passed by sweaver_menu_callback.
 *
 * @param $plugin_name
 *   Name of the plugin.
 * @param $callback_method
 *   Name of the method to call.
 * @return $form
 *   The rendered form.
 */
function sweaver_menu_callback_form($form_state, $form, $plugin_name, $callback_method) {
  $form['#plugin_name'] = $plugin_name;
  $form['#callback_method'] = $callback_method;
  return $form;
}

/**
 * Sweaver menu callback validate.
 */
function sweaver_menu_callback_form_validate($form, &$form_state) {
  $sweaver_plugin = Sweaver::get_instance()
    ->get_plugin($form['#plugin_name']);
  $callback_validate_function = $form['#callback_method'] . '_validate';
  if (method_exists($sweaver_plugin, $callback_validate_function)) {
    $sweaver_plugin
      ->{$callback_validate_function}($form, $form_state);
  }
}

/**
 * Sweaver menu callback form submit.
 */
function sweaver_menu_callback_form_submit($form, &$form_state) {
  $sweaver_plugin = Sweaver::get_instance()
    ->get_plugin($form['#plugin_name']);
  $callback_submit_function = $form['#callback_method'] . '_submit';
  if (method_exists($sweaver_plugin, $callback_submit_function)) {
    $sweaver_plugin
      ->{$callback_submit_function}($form, $form_state);
  }
}

Functions

Namesort descending Description
sweaver_clear_cache Clear cache and page requisites.
sweaver_containers Sweaver editor containers.
sweaver_ctools_object_list Helper function to return list for CTools.
sweaver_ctools_plugin_api Implementation of hook_ctools_plugin_api().
sweaver_ctools_plugin_plugins Implementation of hook_ctools_plugin_plugins().
sweaver_ctools_properties_list CTools properties list callback for bulk export.
sweaver_ctools_selectors_list CTools selectors list callback for bulk export.
sweaver_ctools_types_list CTools types list callback for bulk export.
sweaver_default_sweaver_property Implementation of hook_default_sweaver_property().
sweaver_default_sweaver_selector Implementation of hook_default_sweaver_selector().
sweaver_default_sweaver_type Implementation of hook_default_sweaver_type().
sweaver_export_check_serialized_keys Check if we have a serialized key. If so convert it to an array.
sweaver_export_sweaver_object CTools object export function.
sweaver_export_sweaver_property CTools property export function.
sweaver_export_sweaver_selector CTools selector export function.
sweaver_export_sweaver_type CTools property export function.
sweaver_footer Implementation of hook_footer().
sweaver_frontend Rock 'n' roll: the sweaver editor.
sweaver_frontend_submit Sweaver frontend submit.
sweaver_get_all_themes Return all enabled themes.
sweaver_get_plugin Deprecated Get all plugins or a plugin handler.
sweaver_get_theme_info Get theme info.
sweaver_init Implementation of hook_init().
sweaver_menu Implementation of hook_menu().
sweaver_menu_callback Sweaver menu callback.
sweaver_menu_callback_form Helper function to return the form passed by sweaver_menu_callback.
sweaver_menu_callback_form_submit Sweaver menu callback form submit.
sweaver_menu_callback_form_validate Sweaver menu callback validate.
sweaver_object_load Load one object or all objects.
sweaver_perm Implementation of hook_perm().
sweaver_preprocess_page Implementation of hook_preprocess_page().
sweaver_session Session handler for sweaver.
sweaver_show_editor Function to check if we are going to show the editor.
sweaver_sweaver_plugins Implementation of hook_sweaver_plugins().
sweaver_theme Implementation of hook_theme().
sweaver_theme_image Theme an image. Mimics theme('image') but uses the alias => TRUE option.

Constants

Namesort descending Description
SWEAVER_CLASSES_EXCLUDE Default classes to exclude from not being themed.
SWEAVER_CTOOLS_ALLOWED_PROPERTIES Sweaver default usable CSS properties extending the defaults from CTools.
SWEAVER_PATHS_EXCLUDE Default paths to exclude the frontend editor from.
SWEAVER_SELECTORS_EXCLUDE Default selectors to exclude from selecting.
SWEAVER_SKIN Default skin.