2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * phpMyAdmin theme manager
8 if (! defined('PHPMYADMIN')) {
13 * phpMyAdmin theme manager
17 class PMA_Theme_Manager
20 * @var string path to theme folder
23 private $_themes_path;
26 * @var array available themes
28 var $themes = array();
31 * @var string cookie name
33 var $cookie_name = 'pma_theme';
38 var $per_server = false;
41 * @var string name of active theme
43 var $active_theme = '';
46 * @var PMA_Theme PMA_Theme active theme
56 * @const string The name of the fallback theme
58 const FALLBACK_THEME
= 'pmahomme';
61 * Constructor for Theme Manager class
65 public function __construct()
71 * sets path to folder containing the themes
73 * @param string $path path to themes folder
76 * @return boolean success
78 public function setThemesPath($path)
80 if (! $this->_checkThemeFolder($path)) {
84 $this->_themes_path
= trim($path);
89 * Returns path to folder containing themes
92 * @return string theme path
94 public function getThemesPath()
96 return $this->_themes_path
;
100 * sets if there are different themes per server
102 * @param boolean $per_server Whether to enable per server flag
107 public function setThemePerServer($per_server)
109 $this->per_server
= (bool) $per_server;
113 * Initialise the class
118 public function init()
120 $this->themes
= array();
121 $this->theme_default
= self
::FALLBACK_THEME
;
122 $this->active_theme
= '';
124 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
128 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
132 $this->theme
= new PMA_Theme
;
134 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
137 __('Default theme %s not found!'),
138 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])
142 $GLOBALS['cfg']['ThemeDefault'] = false;
145 $this->theme_default
= $GLOBALS['cfg']['ThemeDefault'];
147 // check if user have a theme cookie
148 if (! $this->getThemeCookie()
149 ||
! $this->setActiveTheme($this->getThemeCookie())
151 if ($GLOBALS['cfg']['ThemeDefault']) {
152 // otherwise use default theme
153 $this->setActiveTheme($this->theme_default
);
156 $this->setActiveTheme(self
::FALLBACK_THEME
);
162 * Checks configuration
167 public function checkConfig()
169 if ($this->_themes_path
!= trim($GLOBALS['cfg']['ThemePath'])
170 ||
$this->theme_default
!= $GLOBALS['cfg']['ThemeDefault']
174 // at least the theme path needs to be checked every time for new
175 // themes, as there is no other way at the moment to keep track of
176 // new or removed themes
184 * @param string $theme theme name
187 * @return bool true on success
189 public function setActiveTheme($theme = null)
191 if (! $this->checkTheme($theme)) {
194 __('Theme %s not found!'),
195 htmlspecialchars($theme)
202 $this->active_theme
= $theme;
203 $this->theme
= $this->themes
[$theme];
206 //$this->setThemeCookie();
212 * Returns name for storing theme
214 * @return string cookie name
217 public function getThemeCookieName()
219 // Allow different theme per server
220 if (isset($GLOBALS['server']) && $this->per_server
) {
221 return $this->cookie_name
. '-' . $GLOBALS['server'];
223 return $this->cookie_name
;
228 * returns name of theme stored in the cookie
230 * @return string theme name from cookie
233 public function getThemeCookie()
235 if (isset($_COOKIE[$this->getThemeCookieName()])) {
236 return $_COOKIE[$this->getThemeCookieName()];
243 * save theme in cookie
248 public function setThemeCookie()
250 $GLOBALS['PMA_Config']->setCookie(
251 $this->getThemeCookieName(),
255 // force a change of a dummy session variable to avoid problems
256 // with the caching of phpmyadmin.css.php
257 $GLOBALS['PMA_Config']->set('theme-update', $this->theme
->id
);
262 * Checks whether folder is valid for storing themes
264 * @param string $folder Folder name to test
269 private function _checkThemeFolder($folder)
271 if (! is_dir($folder)) {
274 __('Theme path not found for theme %s!'),
275 htmlspecialchars($folder)
291 public function loadThemes()
293 $this->themes
= array();
295 if (false === ($handleThemes = opendir($this->getThemesPath()))) {
297 'phpMyAdmin-ERROR: cannot open themes folder: '
298 . $this->getThemesPath(),
304 // check for themes directory
305 while (false !== ($PMA_Theme = readdir($handleThemes))) {
306 // Skip non dirs, . and ..
307 if ($PMA_Theme == '.'
308 ||
$PMA_Theme == '..'
309 ||
! is_dir($this->getThemesPath() . '/' . $PMA_Theme)
313 if (array_key_exists($PMA_Theme, $this->themes
)) {
316 $new_theme = PMA_Theme
::load(
317 $this->getThemesPath() . '/' . $PMA_Theme
320 $new_theme->setId($PMA_Theme);
321 $this->themes
[$PMA_Theme] = $new_theme;
324 closedir($handleThemes);
326 ksort($this->themes
);
331 * checks if given theme name is a known theme
333 * @param string $theme name fo theme to check for
338 public function checkTheme($theme)
340 if (! array_key_exists($theme, $this->themes
)) {
348 * returns HTML selectbox, with or without form enclosed
350 * @param boolean $form whether enclosed by from tags or not
355 public function getHtmlSelectBox($form = true)
360 $select_box .= '<form name="setTheme" method="get"';
361 $select_box .= ' action="index.php" class="disableAjax">';
362 $select_box .= PMA_URL_getHiddenInputs();
365 $theme_preview_path= './themes.php';
366 $theme_preview_href = '<a href="'
367 . $theme_preview_path . '" target="themes" class="themeselect">';
368 $select_box .= $theme_preview_href . __('Theme:') . '</a>' . "\n";
370 $select_box .= '<select name="set_theme" lang="en" dir="ltr"'
371 . ' class="autosubmit">';
372 foreach ($this->themes
as $each_theme_id => $each_theme) {
373 $select_box .= '<option value="' . $each_theme_id . '"';
374 if ($this->active_theme
=== $each_theme_id) {
375 $select_box .= ' selected="selected"';
377 $select_box .= '>' . htmlspecialchars($each_theme->getName())
380 $select_box .= '</select>';
383 $select_box .= '</form>';
390 * enables backward compatibility
395 public function makeBc()
397 $GLOBALS['theme'] = $this->theme
->getId();
398 $GLOBALS['pmaThemePath'] = $this->theme
->getPath();
399 $GLOBALS['pmaThemeImage'] = $this->theme
->getImgPath();
402 * load layout file if exists
404 if (file_exists($this->theme
->getLayoutFile())) {
405 include $this->theme
->getLayoutFile();
410 * Renders the previews for all themes
415 public function getPrintPreviews()
418 foreach ($this->themes
as $each_theme) {
419 $retval .= $each_theme->getPrintPreview();
420 } // end 'open themes'
425 * returns PMA_Theme object for fall back theme
427 * @return PMA_Theme fall back theme
430 public function getFallBackTheme()
432 if (isset($this->themes
[self
::FALLBACK_THEME
])) {
433 return $this->themes
[self
::FALLBACK_THEME
];
445 public function printCss()
447 if ($this->theme
->loadCss()) {
451 // if loading css for this theme failed, try default theme css
452 $fallback_theme = $this->getFallBackTheme();
453 if ($fallback_theme && $fallback_theme->loadCss()) {