3 // vim: expandtab sw=4 ts=4 sts=4:
5 require_once './libraries/Theme.class.php';
7 class PMA_Theme_Manager
{
10 * @var string path to theme folder
16 * @var array available themes
18 var $themes = array();
21 * @var string cookie name
23 var $cookie_name = 'pma_theme';
28 var $per_server = false;
31 * @var string name of active theme
33 var $active_theme = '';
36 * @var object PMA_Theme active theme
43 var $theme_default = 'original';
45 function __construct()
51 * sets path to folder containing the themes
53 * @param string $path path to themes folder
54 * @return boolean success
56 function setThemesPath($path)
58 if (! $this->_checkThemeFolder($path)) {
62 $this->_themes_path
= trim($path);
70 function getThemesPath()
72 return $this->_themes_path
;
76 * sets if there are different themes per server
78 * @param boolean $per_server
80 function setThemePerServer($per_server)
82 $this->per_server
= (bool) $per_server;
87 $this->themes
= array();
88 $this->theme_default
= 'original';
89 $this->active_theme
= '';
91 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
95 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
99 $this->theme
= new PMA_Theme
;
102 if ( ! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
103 $GLOBALS['PMA_errors'][] = sprintf( $GLOBALS['strThemeDefaultNotFound'],
104 htmlspecialchars($GLOBALS['cfg']['ThemeDefault']));
106 sprintf($GLOBALS['strThemeDefaultNotFound'],
107 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])),
109 $GLOBALS['cfg']['ThemeDefault'] = false;
112 $this->theme_default
= $GLOBALS['cfg']['ThemeDefault'];
114 // check if user have a theme cookie
115 if (! $this->getThemeCookie()
116 ||
! $this->setActiveTheme($this->getThemeCookie())) {
117 // otherwise use default theme
118 if ($GLOBALS['cfg']['ThemeDefault']) {
119 $this->setActiveTheme($GLOBALS['cfg']['ThemeDefault']);
122 $this->setActiveTheme('original');
127 function checkConfig()
129 if ($this->_themes_path
!= trim($GLOBALS['cfg']['ThemePath'])
130 ||
$this->theme_default
!= $GLOBALS['cfg']['ThemeDefault']) {
133 // at least the theme path needs to be checked every time for new
134 // themes, as there is no other way at the moment to keep track of
135 // new or removed themes
140 function setActiveTheme($theme = null)
142 if ( ! $this->checkTheme($theme)) {
143 $GLOBALS['PMA_errors'][] = sprintf($GLOBALS['strThemeNotFound'],
144 htmlspecialchars($theme));
146 sprintf($GLOBALS['strThemeNotFound'], htmlspecialchars($theme)),
151 $this->active_theme
= $theme;
152 $this->theme
= $this->themes
[$theme];
155 //$this->setThemeCookie();
161 * @return string cookie name
163 function getThemeCookieName()
165 // Allow different theme per server
166 if (isset($GLOBALS['server']) && $this->per_server
) {
167 return $this->cookie_name
. '-' . $GLOBALS['server'];
169 return $this->cookie_name
;
174 * returns name of theme stored in the cookie
175 * @return string theme name from cookie
177 function getThemeCookie()
179 if (isset($_COOKIE[$this->getThemeCookieName()])) {
180 return $_COOKIE[$this->getThemeCookieName()];
187 * save theme in cookie
189 * @uses PMA_setCookie();
190 * @uses PMA_Theme_Manager::getThemeCookieName()
191 * @uses PMA_Theme_Manager::$theme
192 * @uses PMA_Theme_Manager::$theme_default
193 * @uses PMA_Theme::getId()
195 function setThemeCookie()
197 PMA_setCookie($this->getThemeCookieName(), $this->theme
->id
,
198 $this->theme_default
);
203 * old PHP 4 constructor
205 function PMA_Theme_Manager()
207 $this->__construct();
212 * @param string $folder
215 /*private*/ function _checkThemeFolder($folder)
217 if (! is_dir($folder)) {
218 $GLOBALS['PMA_errors'][] =
219 sprintf($GLOBALS['strThemePathNotFound'],
220 htmlspecialchars($folder));
222 sprintf($GLOBALS['strThemePathNotFound'],
223 htmlspecialchars($folder)),
234 function loadThemes()
236 $this->themes
= array();
238 if ($handleThemes = opendir($this->getThemesPath())) {
239 // check for themes directory
240 while (false !== ($PMA_Theme = readdir($handleThemes))) {
241 if (array_key_exists($PMA_Theme, $this->themes
)) {
242 // this does nothing!
243 //$this->themes[$PMA_Theme] = $this->themes[$PMA_Theme];
246 $new_theme = PMA_Theme
::load($this->getThemesPath() . '/' . $PMA_Theme);
248 $new_theme->setId($PMA_Theme);
249 $this->themes
[$PMA_Theme] = $new_theme;
252 closedir($handleThemes);
255 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
258 } // end check for themes directory
260 ksort($this->themes
);
265 * checks if given theme name is a known theme
267 * @param string $theme name fo theme to check for
269 function checkTheme($theme)
271 if (! array_key_exists($theme, $this->themes
)) {
279 * returns HTML selectbox, with or without form enclsoed
281 * @param boolean $form wether enclosed by from tags or not
283 function getHtmlSelectBox($form = true)
288 $select_box .= '<form name="setTheme" method="post" action="index.php"'
289 .' target="_parent">';
290 $select_box .= PMA_generate_common_hidden_inputs();
293 $theme_selected = FALSE;
294 $theme_preview_path= './themes.php';
295 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" onclick="'
296 . "window.open('" . $theme_preview_path . "','themes','left=10,top=20,width=510,height=350,scrollbars=yes,status=yes,resizable=yes');"
298 $select_box .= $theme_preview_href . $GLOBALS['strTheme'] . '</a>:' . "\n";
300 $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr"'
301 .' onchange="this.form.submit();" >';
302 foreach ($this->themes
as $each_theme_id => $each_theme) {
303 $select_box .= '<option value="' . $each_theme_id . '"';
304 if ($this->active_theme
=== $each_theme_id) {
305 $select_box .= ' selected="selected"';
307 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
309 $select_box .= '</select>';
312 $select_box .= '<noscript><input type="submit" value="' . $GLOBALS['strGo'] . '" /></noscript>';
313 $select_box .= '</form>';
320 * enables backward compatibility
324 $GLOBALS['theme'] = $this->theme
->getId();
325 $GLOBALS['pmaThemePath'] = $this->theme
->getPath();
326 $GLOBALS['pmaThemeImage'] = $this->theme
->getImgPath();
329 * load layout file if exists
331 if (@file_exists
($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
332 include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
339 * prints out preview for every theme
341 * @uses $this->themes
342 * @uses PMA_Theme::printPreview()
344 function printPreviews()
346 foreach ($this->themes
as $each_theme) {
347 $each_theme->printPreview();
348 } // end 'open themes'
352 * returns PMA_Theme object for fall back theme
353 * @return object PMA_Theme
355 function getFallBackTheme()
357 if (isset($this->themes
['original'])) {
358 return $this->themes
['original'];
367 function printCss($type)
369 if ($this->theme
->loadCss($type)) {
373 // load css for this them failed, try default theme css
374 $fallback_theme = $this->getFallBackTheme();
375 if ($fallback_theme && $fallback_theme->loadCss($type)) {