2 /* vim: set expandtab sw=4 ts=4 sts=4: */
11 require_once './libraries/Theme.class.php';
17 class PMA_Theme_Manager
20 * @var string path to theme folder
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 object PMA_Theme active theme
53 var $theme_default = 'original';
55 function __construct()
61 * sets path to folder containing the themes
63 * @param string $path path to themes folder
64 * @return boolean success
66 function setThemesPath($path)
68 if (! $this->_checkThemeFolder($path)) {
72 $this->_themes_path
= trim($path);
80 function getThemesPath()
82 return $this->_themes_path
;
86 * sets if there are different themes per server
88 * @param boolean $per_server
90 function setThemePerServer($per_server)
92 $this->per_server
= (bool) $per_server;
97 $this->themes
= array();
98 $this->theme_default
= 'original';
99 $this->active_theme
= '';
101 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
105 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
109 $this->theme
= new PMA_Theme
;
112 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
114 sprintf(__('Default theme %s not found!'),
115 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])),
117 $GLOBALS['cfg']['ThemeDefault'] = false;
120 $this->theme_default
= $GLOBALS['cfg']['ThemeDefault'];
122 // check if user have a theme cookie
123 if (! $this->getThemeCookie()
124 ||
! $this->setActiveTheme($this->getThemeCookie())) {
125 // otherwise use default theme
126 if ($GLOBALS['cfg']['ThemeDefault']) {
127 $this->setActiveTheme($GLOBALS['cfg']['ThemeDefault']);
130 $this->setActiveTheme('original');
135 function checkConfig()
137 if ($this->_themes_path
!= trim($GLOBALS['cfg']['ThemePath'])
138 ||
$this->theme_default
!= $GLOBALS['cfg']['ThemeDefault']) {
141 // at least the theme path needs to be checked every time for new
142 // themes, as there is no other way at the moment to keep track of
143 // new or removed themes
148 function setActiveTheme($theme = null)
150 if (! $this->checkTheme($theme)) {
152 sprintf(__('Theme %s not found!'), htmlspecialchars($theme)),
157 $this->active_theme
= $theme;
158 $this->theme
= $this->themes
[$theme];
161 //$this->setThemeCookie();
167 * @return string cookie name
169 function getThemeCookieName()
171 // Allow different theme per server
172 if (isset($GLOBALS['server']) && $this->per_server
) {
173 return $this->cookie_name
. '-' . $GLOBALS['server'];
175 return $this->cookie_name
;
180 * returns name of theme stored in the cookie
181 * @return string theme name from cookie
183 function getThemeCookie()
185 if (isset($_COOKIE[$this->getThemeCookieName()])) {
186 return $_COOKIE[$this->getThemeCookieName()];
193 * save theme in cookie
195 * @uses $GLOBALS['PMA_Config']->setCookie();
196 * @uses PMA_Theme_Manager::getThemeCookieName()
197 * @uses PMA_Theme_Manager::$theme
198 * @uses PMA_Theme_Manager::$theme_default
199 * @uses PMA_Theme::getId()
201 function setThemeCookie()
203 $GLOBALS['PMA_Config']->setCookie($this->getThemeCookieName(), $this->theme
->id
,
204 $this->theme_default
);
205 // force a change of a dummy session variable to avoid problems
206 // with the caching of phpmyadmin.css.php
207 $GLOBALS['PMA_Config']->set('theme-update', $this->theme
->id
);
213 * @param string $folder
216 /*private*/ function _checkThemeFolder($folder)
218 if (! is_dir($folder)) {
220 sprintf(__('Theme path not found for theme %s!'),
221 htmlspecialchars($folder)),
232 function loadThemes()
234 $this->themes
= array();
236 if ($handleThemes = opendir($this->getThemesPath())) {
237 // check for themes directory
238 while (false !== ($PMA_Theme = readdir($handleThemes))) {
239 if (array_key_exists($PMA_Theme, $this->themes
)) {
240 // this does nothing!
241 //$this->themes[$PMA_Theme] = $this->themes[$PMA_Theme];
244 $new_theme = PMA_Theme
::load($this->getThemesPath() . '/' . $PMA_Theme);
246 $new_theme->setId($PMA_Theme);
247 $this->themes
[$PMA_Theme] = $new_theme;
250 closedir($handleThemes);
253 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
256 } // end check for themes directory
258 ksort($this->themes
);
263 * checks if given theme name is a known theme
265 * @param string $theme name fo theme to check for
267 function checkTheme($theme)
269 if (! array_key_exists($theme, $this->themes
)) {
277 * returns HTML selectbox, with or without form enclosed
279 * @param boolean $form whether enclosed by from tags or not
281 function getHtmlSelectBox($form = true)
286 $select_box .= '<form name="setTheme" method="post" action="index.php"'
287 .' target="_parent">';
288 $select_box .= PMA_generate_common_hidden_inputs();
291 $theme_selected = FALSE;
292 $theme_preview_path= './themes.php';
293 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" onclick="'
294 . "window.open('" . $theme_preview_path . "','themes','left=10,top=20,width=510,height=350,scrollbars=yes,status=yes,resizable=yes');"
296 $select_box .= $theme_preview_href . __('Theme / Style') . '</a>:' . "\n";
298 $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr"'
299 .' onchange="this.form.submit();" >';
300 foreach ($this->themes
as $each_theme_id => $each_theme) {
301 $select_box .= '<option value="' . $each_theme_id . '"';
302 if ($this->active_theme
=== $each_theme_id) {
303 $select_box .= ' selected="selected"';
305 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
307 $select_box .= '</select>';
310 $select_box .= '<noscript><input type="submit" value="' . __('Go') . '" /></noscript>';
311 $select_box .= '</form>';
318 * enables backward compatibility
322 $GLOBALS['theme'] = $this->theme
->getId();
323 $GLOBALS['pmaThemePath'] = $this->theme
->getPath();
324 $GLOBALS['pmaThemeImage'] = $this->theme
->getImgPath();
327 * load layout file if exists
329 if (@file_exists
($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
330 include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
337 * prints out preview for every theme
339 * @uses $this->themes
340 * @uses PMA_Theme::printPreview()
342 function printPreviews()
344 foreach ($this->themes
as $each_theme) {
345 $each_theme->printPreview();
346 } // end 'open themes'
350 * returns PMA_Theme object for fall back theme
351 * @return object PMA_Theme
353 function getFallBackTheme()
355 if (isset($this->themes
['original'])) {
356 return $this->themes
['original'];
365 function printCss($type)
367 if ($this->theme
->loadCss($type)) {
371 // if loading css for this theme failed, try default theme css
372 $fallback_theme = $this->getFallBackTheme();
373 if ($fallback_theme && $fallback_theme->loadCss($type)) {