2 /* vim: set expandtab sw=4 ts=4 sts=4: */
12 require_once './libraries/Theme.class.php';
18 class PMA_Theme_Manager
21 * @var string path to theme folder
27 * @var array available themes
29 var $themes = array();
32 * @var string cookie name
34 var $cookie_name = 'pma_theme';
39 var $per_server = false;
42 * @var string name of active theme
44 var $active_theme = '';
47 * @var object PMA_Theme active theme
54 var $theme_default = 'original';
56 function __construct()
62 * sets path to folder containing the themes
64 * @param string $path path to themes folder
65 * @return boolean success
67 function setThemesPath($path)
69 if (! $this->_checkThemeFolder($path)) {
73 $this->_themes_path
= trim($path);
81 function getThemesPath()
83 return $this->_themes_path
;
87 * sets if there are different themes per server
89 * @param boolean $per_server
91 function setThemePerServer($per_server)
93 $this->per_server
= (bool) $per_server;
98 $this->themes
= array();
99 $this->theme_default
= 'original';
100 $this->active_theme
= '';
102 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
106 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
110 $this->theme
= new PMA_Theme
;
113 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
115 sprintf($GLOBALS['strThemeDefaultNotFound'],
116 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])),
118 $GLOBALS['cfg']['ThemeDefault'] = false;
121 $this->theme_default
= $GLOBALS['cfg']['ThemeDefault'];
123 // check if user have a theme cookie
124 if (! $this->getThemeCookie()
125 ||
! $this->setActiveTheme($this->getThemeCookie())) {
126 // otherwise use default theme
127 if ($GLOBALS['cfg']['ThemeDefault']) {
128 $this->setActiveTheme($GLOBALS['cfg']['ThemeDefault']);
131 $this->setActiveTheme('original');
136 function checkConfig()
138 if ($this->_themes_path
!= trim($GLOBALS['cfg']['ThemePath'])
139 ||
$this->theme_default
!= $GLOBALS['cfg']['ThemeDefault']) {
142 // at least the theme path needs to be checked every time for new
143 // themes, as there is no other way at the moment to keep track of
144 // new or removed themes
149 function setActiveTheme($theme = null)
151 if (! $this->checkTheme($theme)) {
153 sprintf($GLOBALS['strThemeNotFound'], 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
196 * @uses $GLOBALS['PMA_Config']->setCookie();
197 * @uses PMA_Theme_Manager::getThemeCookieName()
198 * @uses PMA_Theme_Manager::$theme
199 * @uses PMA_Theme_Manager::$theme_default
200 * @uses PMA_Theme::getId()
202 function setThemeCookie()
204 $GLOBALS['PMA_Config']->setCookie($this->getThemeCookieName(), $this->theme
->id
,
205 $this->theme_default
);
206 // force a change of a dummy session variable to avoid problems
207 // with the caching of phpmyadmin.css.php
208 $GLOBALS['PMA_Config']->set('theme-update', $this->theme
->id
);
214 * @param string $folder
217 /*private*/ function _checkThemeFolder($folder)
219 if (! is_dir($folder)) {
221 sprintf($GLOBALS['strThemePathNotFound'],
222 htmlspecialchars($folder)),
233 function loadThemes()
235 $this->themes
= array();
237 if ($handleThemes = opendir($this->getThemesPath())) {
238 // check for themes directory
239 while (false !== ($PMA_Theme = readdir($handleThemes))) {
240 if (array_key_exists($PMA_Theme, $this->themes
)) {
241 // this does nothing!
242 //$this->themes[$PMA_Theme] = $this->themes[$PMA_Theme];
245 $new_theme = PMA_Theme
::load($this->getThemesPath() . '/' . $PMA_Theme);
247 $new_theme->setId($PMA_Theme);
248 $this->themes
[$PMA_Theme] = $new_theme;
251 closedir($handleThemes);
254 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
257 } // end check for themes directory
259 ksort($this->themes
);
264 * checks if given theme name is a known theme
266 * @param string $theme name fo theme to check for
268 function checkTheme($theme)
270 if (! array_key_exists($theme, $this->themes
)) {
278 * returns HTML selectbox, with or without form enclosed
280 * @param boolean $form whether enclosed by from tags or not
282 function getHtmlSelectBox($form = true)
287 $select_box .= '<form name="setTheme" method="post" action="index.php"'
288 .' target="_parent">';
289 $select_box .= PMA_generate_common_hidden_inputs();
292 $theme_selected = FALSE;
293 $theme_preview_path= './themes.php';
294 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" onclick="'
295 . "window.open('" . $theme_preview_path . "','themes','left=10,top=20,width=510,height=350,scrollbars=yes,status=yes,resizable=yes');"
297 $select_box .= $theme_preview_href . $GLOBALS['strTheme'] . '</a>:' . "\n";
299 $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr"'
300 .' onchange="this.form.submit();" >';
301 foreach ($this->themes
as $each_theme_id => $each_theme) {
302 $select_box .= '<option value="' . $each_theme_id . '"';
303 if ($this->active_theme
=== $each_theme_id) {
304 $select_box .= ' selected="selected"';
306 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
308 $select_box .= '</select>';
311 $select_box .= '<noscript><input type="submit" value="' . $GLOBALS['strGo'] . '" /></noscript>';
312 $select_box .= '</form>';
319 * enables backward compatibility
323 $GLOBALS['theme'] = $this->theme
->getId();
324 $GLOBALS['pmaThemePath'] = $this->theme
->getPath();
325 $GLOBALS['pmaThemeImage'] = $this->theme
->getImgPath();
328 * load layout file if exists
330 if (@file_exists
($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
331 include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
338 * prints out preview for every theme
340 * @uses $this->themes
341 * @uses PMA_Theme::printPreview()
343 function printPreviews()
345 foreach ($this->themes
as $each_theme) {
346 $each_theme->printPreview();
347 } // end 'open themes'
351 * returns PMA_Theme object for fall back theme
352 * @return object PMA_Theme
354 function getFallBackTheme()
356 if (isset($this->themes
['original'])) {
357 return $this->themes
['original'];
366 function printCss($type)
368 if ($this->theme
->loadCss($type)) {
372 // if loading css for this theme failed, try default theme css
373 $fallback_theme = $this->getFallBackTheme();
374 if ($fallback_theme && $fallback_theme->loadCss($type)) {