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
116 * @return boolean|void
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
;
135 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
138 __('Default theme %s not found!'),
139 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])
143 $GLOBALS['cfg']['ThemeDefault'] = false;
146 $this->theme_default
= $GLOBALS['cfg']['ThemeDefault'];
148 // check if user have a theme cookie
149 if (! $this->getThemeCookie()
150 ||
! $this->setActiveTheme($this->getThemeCookie())
152 if ($GLOBALS['cfg']['ThemeDefault']) {
153 // otherwise use default theme
154 $this->setActiveTheme($this->theme_default
);
157 $this->setActiveTheme(self
::FALLBACK_THEME
);
163 * Checks configuration
168 public function checkConfig()
170 if ($this->_themes_path
!= trim($GLOBALS['cfg']['ThemePath'])
171 ||
$this->theme_default
!= $GLOBALS['cfg']['ThemeDefault']
175 // at least the theme path needs to be checked every time for new
176 // themes, as there is no other way at the moment to keep track of
177 // new or removed themes
185 * @param string $theme theme name
188 * @return bool true on success
190 public function setActiveTheme($theme = null)
192 if (! $this->checkTheme($theme)) {
195 __('Theme %s not found!'),
196 htmlspecialchars($theme)
203 $this->active_theme
= $theme;
204 $this->theme
= $this->themes
[$theme];
207 //$this->setThemeCookie();
213 * Returns name for storing theme
215 * @return string cookie name
218 public function getThemeCookieName()
220 // Allow different theme per server
221 if (isset($GLOBALS['server']) && $this->per_server
) {
222 return $this->cookie_name
. '-' . $GLOBALS['server'];
224 return $this->cookie_name
;
229 * returns name of theme stored in the cookie
231 * @return string theme name from cookie
234 public function getThemeCookie()
236 if (isset($_COOKIE[$this->getThemeCookieName()])) {
237 return $_COOKIE[$this->getThemeCookieName()];
244 * save theme in cookie
249 public function setThemeCookie()
251 $GLOBALS['PMA_Config']->setCookie(
252 $this->getThemeCookieName(),
256 // force a change of a dummy session variable to avoid problems
257 // with the caching of phpmyadmin.css.php
258 $GLOBALS['PMA_Config']->set('theme-update', $this->theme
->id
);
263 * Checks whether folder is valid for storing themes
265 * @param string $folder Folder name to test
270 private function _checkThemeFolder($folder)
272 if (! is_dir($folder)) {
275 __('Theme path not found for theme %s!'),
276 htmlspecialchars($folder)
292 public function loadThemes()
294 $this->themes
= array();
296 if ($handleThemes = opendir($this->getThemesPath())) {
297 // check for themes directory
298 while (false !== ($PMA_Theme = readdir($handleThemes))) {
299 // Skip non dirs, . and ..
300 if ($PMA_Theme == '.'
301 ||
$PMA_Theme == '..'
302 ||
! is_dir($this->getThemesPath() . '/' . $PMA_Theme)
306 if (array_key_exists($PMA_Theme, $this->themes
)) {
309 $new_theme = PMA_Theme
::load(
310 $this->getThemesPath() . '/' . $PMA_Theme
313 $new_theme->setId($PMA_Theme);
314 $this->themes
[$PMA_Theme] = $new_theme;
317 closedir($handleThemes);
320 'phpMyAdmin-ERROR: cannot open themes folder: '
321 . $this->getThemesPath(),
325 } // end check for themes directory
327 ksort($this->themes
);
332 * checks if given theme name is a known theme
334 * @param string $theme name fo theme to check for
339 public function checkTheme($theme)
341 if (! array_key_exists($theme, $this->themes
)) {
349 * returns HTML selectbox, with or without form enclosed
351 * @param boolean $form whether enclosed by from tags or not
356 public function getHtmlSelectBox($form = true)
361 $select_box .= '<form name="setTheme" method="get"';
362 $select_box .= ' action="index.php" class="disableAjax">';
363 $select_box .= PMA_URL_getHiddenInputs();
366 $theme_preview_path= './themes.php';
367 $theme_preview_href = '<a href="'
368 . $theme_preview_path . '" target="themes" class="themeselect">';
369 $select_box .= $theme_preview_href . __('Theme:') . '</a>' . "\n";
371 $select_box .= '<select name="set_theme" lang="en" dir="ltr"'
372 . ' class="autosubmit">';
373 foreach ($this->themes
as $each_theme_id => $each_theme) {
374 $select_box .= '<option value="' . $each_theme_id . '"';
375 if ($this->active_theme
=== $each_theme_id) {
376 $select_box .= ' selected="selected"';
378 $select_box .= '>' . htmlspecialchars($each_theme->getName())
381 $select_box .= '</select>';
384 $select_box .= '</form>';
391 * enables backward compatibility
396 public function makeBc()
398 $GLOBALS['theme'] = $this->theme
->getId();
399 $GLOBALS['pmaThemePath'] = $this->theme
->getPath();
400 $GLOBALS['pmaThemeImage'] = $this->theme
->getImgPath();
403 * load layout file if exists
405 if (file_exists($this->theme
->getLayoutFile())) {
406 include $this->theme
->getLayoutFile();
411 * Renders the previews for all themes
416 public function getPrintPreviews()
419 foreach ($this->themes
as $each_theme) {
420 $retval .= $each_theme->getPrintPreview();
421 } // end 'open themes'
426 * returns PMA_Theme object for fall back theme
428 * @return PMA_Theme fall back theme
431 public function getFallBackTheme()
433 if (isset($this->themes
[self
::FALLBACK_THEME
])) {
434 return $this->themes
[self
::FALLBACK_THEME
];
446 public function printCss()
448 if ($this->theme
->loadCss()) {
452 // if loading css for this theme failed, try default theme css
453 $fallback_theme = $this->getFallBackTheme();
454 if ($fallback_theme && $fallback_theme->loadCss()) {