You are here

styleswitcher.install in Style Switcher 7.2

Installation tasks.

File

styleswitcher.install
View source
<?php

/**
 * @file
 * Installation tasks.
 */

/**
 * Implements hook_uninstall().
 */
function styleswitcher_uninstall() {
  variable_del('styleswitcher_custom_styles');
  variable_del('styleswitcher_styles_settings');
  variable_del('styleswitcher_enable_overlay');
  variable_del('styleswitcher_7206_theme_default');
}

/**
 * Remove hardcoded numeric delta from block.
 */
function styleswitcher_update_7000(&$sandbox) {

  // This operation is safe for 7.x-1.x to 7.x-2.x update because there will be
  // no update if there's no block with '0' delta.
  $renamed_deltas['styleswitcher'] = array(
    '0' => 'styleswitcher',
  );
  update_fix_d7_block_deltas($sandbox, $renamed_deltas, array());
}

/**
 * Ensure default (blank) style is in place.
 */
function styleswitcher_update_7201() {
  $styles = variable_get('styleswitcher_styles');
  if (isset($styles)) {
    $blank_is_in_place = FALSE;
    $blank_css_path = drupal_get_path('module', 'styleswitcher') . '/styleswitcher.css';
    $min_weight = 0;
    foreach ($styles as $name => $style) {
      if ($style['path'] == $blank_css_path) {

        // No need to add anything.
        $blank_is_in_place = TRUE;
        break;
      }
      $min_weight = min($min_weight, $style['weight']);
    }
    if (!$blank_is_in_place) {
      $name = 'default';
      if (isset($styles[$name])) {
        $i = 0;
        do {
          ++$i;
          $name = 'default_' . $i;
        } while (isset($styles[$name]));
      }
      $styles[$name] = array(
        'name' => $name,
        'label' => 'Default',
        'path' => $blank_css_path,
        'weight' => $min_weight - 1,
        'custom' => TRUE,
        // Add it disabled to not mess established things.
        'status' => FALSE,
        'is_default' => FALSE,
      );
      if (count($styles) > 1) {
        uasort($styles, 'drupal_sort_weight');
      }
      variable_set('styleswitcher_styles', $styles);
      return 'Style Switcher module has been updated. You might want to ' . l('configure it', 'admin/config/user-interface/styleswitcher') . '.';
    }
  }
}

/**
 * Remove dependency on blank CSS file.
 */
function styleswitcher_update_7202() {
  $styles = variable_get('styleswitcher_styles');
  if (isset($styles)) {
    $blank_css_path = drupal_get_path('module', 'styleswitcher') . '/styleswitcher.css';
    foreach ($styles as $name => $style) {
      if ($style['path'] == $blank_css_path) {
        $styles[$name]['path'] = NULL;
        break;
      }
    }
    variable_set('styleswitcher_styles', $styles);
  }
}

/**
 * Prefix style keys in persistent variable.
 */
function styleswitcher_update_7203() {
  if ($styles = variable_get('styleswitcher_styles')) {
    $updated_styles = array();
    foreach ($styles as $key => $style) {
      if (strpos($key, 'theme/') !== 0 && strpos($key, 'custom/') !== 0) {
        $prefix = empty($style['custom']) ? 'theme/' : 'custom/';
        $key = $style['name'] = $prefix . $key;
      }
      $updated_styles[$key] = $style;
    }
    variable_set('styleswitcher_styles', $updated_styles);
  }
}

/**
 * Split styles storage into separate variables for settings and properties.
 */
function styleswitcher_update_7204() {
  $styles = variable_get('styleswitcher_styles');
  $settings = variable_get('styleswitcher_styles_settings');
  if ($styles && !isset($settings)) {
    $settings = array();
    $properties = array();
    foreach ($styles as $key => $style) {
      $settings[$key] = array(
        'weight' => $style['weight'],
        'status' => $style['status'],
        'is_default' => !empty($style['is_default']),
      );
      $properties[$key] = array(
        'name' => $style['name'],
        'label' => $style['label'],
        'path' => $style['path'],
      );
    }
    variable_set('styleswitcher_styles_settings', $settings);
    variable_set('styleswitcher_styles', $properties);
  }
}

/**
 * Filter out styles: delete theme's ones - they will be loaded alive.
 */
function styleswitcher_update_7205() {
  if ($styles = variable_get('styleswitcher_styles')) {
    $custom = array();
    foreach ($styles as $key => $style) {
      if (strpos($key, 'custom/') === 0) {
        $custom[$key] = $style;
      }
    }
    variable_set('styleswitcher_custom_styles', $custom);
  }

  // A var can be null but still may use a row in DB so delete it outside of the
  // above checking.
  variable_del('styleswitcher_styles');
}

/**
 * Update styles settings data model to have separate settings per theme.
 */
function styleswitcher_update_7206() {

  // Set a variable to know what theme was previously used as a host of styles
  // to switch - the default site theme. So when a user with a plain
  // styleswitcher cookie comes to the site we know what theme that style
  // belongs to and we can convert user's cookie to the new array format
  // preserving user's preference.
  $theme_default = variable_get('theme_default', 'bartik');
  variable_set('styleswitcher_7206_theme_default', $theme_default);
  if ($settings = variable_get('styleswitcher_styles_settings')) {

    // Save all existing styles settings as the default theme's styles settings.
    // Check that settings still have an old structure by analyzing its first
    // key: theme machine names can't contain a slash.
    if (strpos(key($settings), '/') !== FALSE) {
      variable_set('styleswitcher_styles_settings', array(
        $theme_default => $settings,
      ));
    }
  }
}

Functions

Namesort descending Description
styleswitcher_uninstall Implements hook_uninstall().
styleswitcher_update_7000 Remove hardcoded numeric delta from block.
styleswitcher_update_7201 Ensure default (blank) style is in place.
styleswitcher_update_7202 Remove dependency on blank CSS file.
styleswitcher_update_7203 Prefix style keys in persistent variable.
styleswitcher_update_7204 Split styles storage into separate variables for settings and properties.
styleswitcher_update_7205 Filter out styles: delete theme's ones - they will be loaded alive.
styleswitcher_update_7206 Update styles settings data model to have separate settings per theme.