You are here

function styleswitcher_active_style_path in Style Switcher 6.2

Same name and namespace in other branches
  1. 7.2 styleswitcher.module \styleswitcher_active_style_path()

Finds the style active for current user and returns its path.

This function is called at every page request before styleswitcher_switch() or JS' Drupal.styleSwitcher.switchStyle() so we can update old user cookies here once and not bother about it in other places.

Parameters

string $theme: Name of the theme to find the active style for.

Return value

string|null The path property of active style. It can be NULL if active style is the blank one.

See also

styleswitcher_switch()

Drupal.styleSwitcher.switchStyle()

1 call to styleswitcher_active_style_path()
styleswitcher_css in ./styleswitcher.module
Page callback: Redirects to CSS file of currently active style.

File

./styleswitcher.module, line 680
Module's hooks implementations and helper functions.

Code

function styleswitcher_active_style_path($theme) {
  if (isset($_COOKIE['styleswitcher'])) {
    $cookie = $_COOKIE['styleswitcher'];
    if (isset($cookie[$theme])) {
      $active = styleswitcher_style_load($cookie[$theme], $theme);
    }
  }
  elseif (isset($_COOKIE['styleSwitcher'])) {
    $name = 'theme/' . _styleswitcher_style_name($_COOKIE['styleSwitcher']);

    // Remove this old cookie.
    setcookie('styleSwitcher', '', 0, base_path());

    // We actually do not know what theme was used (it was a global $theme) when
    // user switched to this style. So let us just set this style as active for
    // every theme which has a style with this name.
    foreach (array_keys(list_themes()) as $style_theme) {
      if ($style = styleswitcher_style_load($name, $style_theme)) {
        styleswitcher_save_user_preference($style_theme, $name);
        if ($theme == $style_theme) {
          $active = $style;
        }
      }
    }
  }
  if (empty($active)) {
    $active = styleswitcher_style_load(styleswitcher_default_style_key($theme), $theme);
  }
  return $active['path'];
}