2 /* vim: set expandtab sw=4 ts=4 sts=4: */
7 if (! defined('PHPMYADMIN')) {
15 class PMA_Theme_Manager
18 * @var string path to theme folder
21 private $_themes_path;
24 * @var array available themes
26 var $themes = array();
29 * @var string cookie name
31 var $cookie_name = 'pma_theme';
36 var $per_server = false;
39 * @var string name of active theme
41 var $active_theme = '';
44 * @var PMA_Theme PMA_Theme active theme
54 * @const string The name of the fallback theme
56 const FALLBACK_THEME
= 'pmahomme';
59 * Constructor for Theme Manager class
64 public function __construct()
70 * sets path to folder containing the themes
72 * @param string $path path to themes folder
75 * @return boolean success
77 public function setThemesPath($path)
79 if (! $this->_checkThemeFolder($path)) {
83 $this->_themes_path
= trim($path);
88 * Returns path to folder containing themes
91 * @return string theme path
93 public function getThemesPath()
95 return $this->_themes_path
;
99 * sets if there are different themes per server
101 * @param boolean $per_server
106 public function setThemePerServer($per_server)
108 $this->per_server
= (bool) $per_server;
112 * Initialise the class
117 public function init()
119 $this->themes
= array();
120 $this->theme_default
= self
::FALLBACK_THEME
;
121 $this->active_theme
= '';
123 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
127 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
131 $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();
213 * @return string cookie name
216 public function getThemeCookieName()
218 // Allow different theme per server
219 if (isset($GLOBALS['server']) && $this->per_server
) {
220 return $this->cookie_name
. '-' . $GLOBALS['server'];
222 return $this->cookie_name
;
227 * returns name of theme stored in the cookie
229 * @return string theme name from cookie
232 public function getThemeCookie()
234 if (isset($_COOKIE[$this->getThemeCookieName()])) {
235 return $_COOKIE[$this->getThemeCookieName()];
242 * save theme in cookie
247 public function setThemeCookie()
249 $GLOBALS['PMA_Config']->setCookie(
250 $this->getThemeCookieName(),
254 // force a change of a dummy session variable to avoid problems
255 // with the caching of phpmyadmin.css.php
256 $GLOBALS['PMA_Config']->set('theme-update', $this->theme
->id
);
261 * @param string $folder
266 private function _checkThemeFolder($folder)
268 if (! is_dir($folder)) {
271 __('Theme path not found for theme %s!'),
272 htmlspecialchars($folder)
288 public function loadThemes()
290 $this->themes
= array();
292 if ($handleThemes = opendir($this->getThemesPath())) {
293 // check for themes directory
294 while (false !== ($PMA_Theme = readdir($handleThemes))) {
295 // Skip non dirs, . and ..
296 if ($PMA_Theme == '.'
297 ||
$PMA_Theme == '..'
298 ||
! is_dir($this->getThemesPath() . '/' . $PMA_Theme)
302 if (array_key_exists($PMA_Theme, $this->themes
)) {
305 $new_theme = PMA_Theme
::load(
306 $this->getThemesPath() . '/' . $PMA_Theme
309 $new_theme->setId($PMA_Theme);
310 $this->themes
[$PMA_Theme] = $new_theme;
313 closedir($handleThemes);
316 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
320 } // end check for themes directory
322 ksort($this->themes
);
327 * checks if given theme name is a known theme
329 * @param string $theme name fo theme to check for
334 public function checkTheme($theme)
336 if (! array_key_exists($theme, $this->themes
)) {
344 * returns HTML selectbox, with or without form enclosed
346 * @param boolean $form whether enclosed by from tags or not
351 public function getHtmlSelectBox($form = true)
356 $select_box .= '<form name="setTheme" method="get"';
357 $select_box .= ' action="index.php" class="disableAjax">';
358 $select_box .= PMA_generate_common_hidden_inputs();
361 $theme_preview_path= './themes.php';
362 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" class="themeselect">';
363 $select_box .= $theme_preview_href . __('Theme') . '</a>:' . "\n";
365 $select_box .= '<select name="set_theme" lang="en" dir="ltr" class="autosubmit">';
366 foreach ($this->themes
as $each_theme_id => $each_theme) {
367 $select_box .= '<option value="' . $each_theme_id . '"';
368 if ($this->active_theme
=== $each_theme_id) {
369 $select_box .= ' selected="selected"';
371 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
373 $select_box .= '</select>';
376 $select_box .= '</form>';
383 * enables backward compatibility
388 public function makeBc()
390 $GLOBALS['theme'] = $this->theme
->getId();
391 $GLOBALS['pmaThemePath'] = $this->theme
->getPath();
392 $GLOBALS['pmaThemeImage'] = $this->theme
->getImgPath();
395 * load layout file if exists
397 if (file_exists($this->theme
->getLayoutFile())) {
398 include $this->theme
->getLayoutFile();
403 * Renders the previews for all themes
408 public function getPrintPreviews()
411 foreach ($this->themes
as $each_theme) {
412 $retval .= $each_theme->getPrintPreview();
413 } // end 'open themes'
418 * returns PMA_Theme object for fall back theme
420 * @return object PMA_Theme
423 public function getFallBackTheme()
425 if (isset($this->themes
[self
::FALLBACK_THEME
])) {
426 return $this->themes
[self
::FALLBACK_THEME
];
438 public function printCss()
440 if ($this->theme
->loadCss()) {
444 // if loading css for this theme failed, try default theme css
445 $fallback_theme = $this->getFallBackTheme();
446 if ($fallback_theme && $fallback_theme->loadCss()) {