2 /* vim: set expandtab sw=4 ts=4 sts=4: */
12 class PMA_Theme_Manager
15 * @var string path to theme folder
21 * @var array available themes
23 var $themes = array();
26 * @var string cookie name
28 var $cookie_name = 'pma_theme';
33 var $per_server = false;
36 * @var string name of active theme
38 var $active_theme = '';
41 * @var object PMA_Theme active theme
48 var $theme_default = 'original';
50 function __construct()
56 * sets path to folder containing the themes
58 * @param string $path path to themes folder
59 * @return boolean success
61 function setThemesPath($path)
63 if (! $this->_checkThemeFolder($path)) {
67 $this->_themes_path
= trim($path);
75 function getThemesPath()
77 return $this->_themes_path
;
81 * sets if there are different themes per server
83 * @param boolean $per_server
85 function setThemePerServer($per_server)
87 $this->per_server
= (bool) $per_server;
92 $this->themes
= array();
93 $this->theme_default
= 'original';
94 $this->active_theme
= '';
96 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
100 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
104 $this->theme
= new PMA_Theme
;
107 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
110 __('Default theme %s not found!'),
111 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])
115 $GLOBALS['cfg']['ThemeDefault'] = false;
118 $this->theme_default
= $GLOBALS['cfg']['ThemeDefault'];
120 // check if user have a theme cookie
121 if (! $this->getThemeCookie()
122 ||
! $this->setActiveTheme($this->getThemeCookie())) {
123 // otherwise use default theme
124 if ($GLOBALS['cfg']['ThemeDefault']) {
125 $this->setActiveTheme($GLOBALS['cfg']['ThemeDefault']);
128 $this->setActiveTheme('original');
133 function checkConfig()
135 if ($this->_themes_path
!= trim($GLOBALS['cfg']['ThemePath'])
136 ||
$this->theme_default
!= $GLOBALS['cfg']['ThemeDefault']) {
139 // at least the theme path needs to be checked every time for new
140 // themes, as there is no other way at the moment to keep track of
141 // new or removed themes
146 function setActiveTheme($theme = null)
148 if (! $this->checkTheme($theme)) {
151 __('Theme %s not found!'),
152 htmlspecialchars($theme)
158 $this->active_theme
= $theme;
159 $this->theme
= $this->themes
[$theme];
162 //$this->setThemeCookie();
168 * @return string cookie name
170 function getThemeCookieName()
172 // Allow different theme per server
173 if (isset($GLOBALS['server']) && $this->per_server
) {
174 return $this->cookie_name
. '-' . $GLOBALS['server'];
176 return $this->cookie_name
;
181 * returns name of theme stored in the cookie
182 * @return string theme name from cookie
184 function getThemeCookie()
186 if (isset($_COOKIE[$this->getThemeCookieName()])) {
187 return $_COOKIE[$this->getThemeCookieName()];
194 * save theme in cookie
197 function setThemeCookie()
199 $GLOBALS['PMA_Config']->setCookie($this->getThemeCookieName(), $this->theme
->id
,
200 $this->theme_default
);
201 // force a change of a dummy session variable to avoid problems
202 // with the caching of phpmyadmin.css.php
203 $GLOBALS['PMA_Config']->set('theme-update', $this->theme
->id
);
209 * @param string $folder
212 private function _checkThemeFolder($folder)
214 if (! is_dir($folder)) {
216 sprintf(__('Theme path not found for theme %s!'),
217 htmlspecialchars($folder)),
228 function loadThemes()
230 $this->themes
= array();
232 if ($handleThemes = opendir($this->getThemesPath())) {
233 // check for themes directory
234 while (false !== ($PMA_Theme = readdir($handleThemes))) {
235 if (array_key_exists($PMA_Theme, $this->themes
)) {
236 // this does nothing!
237 //$this->themes[$PMA_Theme] = $this->themes[$PMA_Theme];
240 $new_theme = PMA_Theme
::load($this->getThemesPath() . '/' . $PMA_Theme);
242 $new_theme->setId($PMA_Theme);
243 $this->themes
[$PMA_Theme] = $new_theme;
246 closedir($handleThemes);
249 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
252 } // end check for themes directory
254 ksort($this->themes
);
259 * checks if given theme name is a known theme
261 * @param string $theme name fo theme to check for
263 function checkTheme($theme)
265 if (! array_key_exists($theme, $this->themes
)) {
273 * returns HTML selectbox, with or without form enclosed
275 * @param boolean $form whether enclosed by from tags or not
277 function getHtmlSelectBox($form = true)
282 $select_box .= '<form name="setTheme" method="post" action="index.php"'
283 .' target="_parent">';
284 $select_box .= PMA_generate_common_hidden_inputs();
287 $theme_preview_path= './themes.php';
288 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" class="themeselect">';
289 $select_box .= $theme_preview_href . __('Theme') . '</a>:' . "\n";
291 $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr" class="autosubmit">';
292 foreach ($this->themes
as $each_theme_id => $each_theme) {
293 $select_box .= '<option value="' . $each_theme_id . '"';
294 if ($this->active_theme
=== $each_theme_id) {
295 $select_box .= ' selected="selected"';
297 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
299 $select_box .= '</select>';
302 $select_box .= '<noscript><input type="submit" value="' . __('Go') . '" /></noscript>';
303 $select_box .= '</form>';
310 * enables backward compatibility
314 $GLOBALS['theme'] = $this->theme
->getId();
315 $GLOBALS['pmaThemePath'] = $this->theme
->getPath();
316 $GLOBALS['pmaThemeImage'] = $this->theme
->getImgPath();
319 * load layout file if exists
321 if (@file_exists
($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
322 include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
329 * prints out preview for every theme
332 function printPreviews()
334 foreach ($this->themes
as $each_theme) {
335 $each_theme->printPreview();
336 } // end 'open themes'
340 * returns PMA_Theme object for fall back theme
341 * @return object PMA_Theme
343 function getFallBackTheme()
345 if (isset($this->themes
['original'])) {
346 return $this->themes
['original'];
355 function printCss($type)
357 if ($this->theme
->loadCss($type)) {
361 // if loading css for this theme failed, try default theme css
362 $fallback_theme = $this->getFallBackTheme();
363 if ($fallback_theme && $fallback_theme->loadCss($type)) {