2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * phpMyAdmin theme manager
14 * phpMyAdmin theme manager
21 * ThemeManager instance
27 private static $_instance;
30 * @var string path to theme folder
33 private $_themes_path = './themes/';
36 * @var array available themes
38 var $themes = array();
41 * @var string cookie name
43 var $cookie_name = 'pma_theme';
48 var $per_server = false;
51 * @var string name of active theme
53 var $active_theme = '';
56 * @var Theme Theme active theme
66 * @const string The name of the fallback theme
68 const FALLBACK_THEME
= 'pmahomme';
71 * Constructor for Theme Manager class
75 public function __construct()
77 $this->themes
= array();
78 $this->theme_default
= self
::FALLBACK_THEME
;
79 $this->active_theme
= '';
81 if (! $this->setThemesPath('./themes/')) {
85 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
89 $this->theme
= new Theme
;
91 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
94 __('Default theme %s not found!'),
95 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])
99 $GLOBALS['cfg']['ThemeDefault'] = false;
102 $this->theme_default
= $GLOBALS['cfg']['ThemeDefault'];
104 // check if user have a theme cookie
105 $cookie_theme = $this->getThemeCookie();
106 if (! $cookie_theme ||
! $this->setActiveTheme($cookie_theme)) {
107 if ($GLOBALS['cfg']['ThemeDefault']) {
108 // otherwise use default theme
109 $this->setActiveTheme($this->theme_default
);
112 $this->setActiveTheme(self
::FALLBACK_THEME
);
118 * Returns the singleton Response object
120 * @return Response object
122 public static function getInstance()
124 if (empty(self
::$_instance)) {
125 self
::$_instance = new ThemeManager();
127 return self
::$_instance;
131 * sets path to folder containing the themes
133 * @param string $path path to themes folder
136 * @return boolean success
138 public function setThemesPath($path)
140 if (! $this->_checkThemeFolder($path)) {
144 $this->_themes_path
= trim($path);
149 * sets if there are different themes per server
151 * @param boolean $per_server Whether to enable per server flag
156 public function setThemePerServer($per_server)
158 $this->per_server
= (bool) $per_server;
164 * @param string $theme theme name
167 * @return bool true on success
169 public function setActiveTheme($theme = null)
171 if (! $this->checkTheme($theme)) {
174 __('Theme %s not found!'),
175 htmlspecialchars($theme)
182 $this->active_theme
= $theme;
183 $this->theme
= $this->themes
[$theme];
186 //$this->setThemeCookie();
192 * Returns name for storing theme
194 * @return string cookie name
197 public function getThemeCookieName()
199 // Allow different theme per server
200 if (isset($GLOBALS['server']) && $this->per_server
) {
201 return $this->cookie_name
. '-' . $GLOBALS['server'];
204 return $this->cookie_name
;
208 * returns name of theme stored in the cookie
210 * @return string theme name from cookie
213 public function getThemeCookie()
215 /** @var Config $PMA_Config */
218 $name = $this->getThemeCookieName();
219 if ($PMA_Config->issetCookie($name)) {
220 return $PMA_Config->getCookie($name);
227 * save theme in cookie
232 public function setThemeCookie()
234 $GLOBALS['PMA_Config']->setCookie(
235 $this->getThemeCookieName(),
239 // force a change of a dummy session variable to avoid problems
240 // with the caching of phpmyadmin.css.php
241 $GLOBALS['PMA_Config']->set('theme-update', $this->theme
->id
);
246 * Checks whether folder is valid for storing themes
248 * @param string $folder Folder name to test
253 private function _checkThemeFolder($folder)
255 if (! is_dir($folder)) {
258 __('Theme path not found for theme %s!'),
259 htmlspecialchars($folder)
275 public function loadThemes()
277 $this->themes
= array();
279 if (false === ($handleThemes = opendir($this->_themes_path
))) {
281 'phpMyAdmin-ERROR: cannot open themes folder: '
282 . $this->_themes_path
,
288 // check for themes directory
289 while (false !== ($PMA_Theme = readdir($handleThemes))) {
290 // Skip non dirs, . and ..
291 if ($PMA_Theme == '.'
292 ||
$PMA_Theme == '..'
293 ||
! @is_dir
($this->_themes_path
. $PMA_Theme)
297 if (array_key_exists($PMA_Theme, $this->themes
)) {
300 $new_theme = Theme
::load(
301 $this->_themes_path
. $PMA_Theme
304 $new_theme->setId($PMA_Theme);
305 $this->themes
[$PMA_Theme] = $new_theme;
308 closedir($handleThemes);
310 ksort($this->themes
);
315 * checks if given theme name is a known theme
317 * @param string $theme name fo theme to check for
322 public function checkTheme($theme)
324 return array_key_exists($theme, $this->themes
);
328 * returns HTML selectbox, with or without form enclosed
330 * @param boolean $form whether enclosed by from tags or not
335 public function getHtmlSelectBox($form = true)
340 $select_box .= '<form name="setTheme" method="post"';
341 $select_box .= ' action="index.php" class="disableAjax">';
342 $select_box .= Url
::getHiddenInputs();
345 $theme_preview_path= './themes.php';
346 $theme_preview_href = '<a href="'
347 . $theme_preview_path . '" target="themes" class="themeselect">';
348 $select_box .= $theme_preview_href . __('Theme:') . '</a>' . "\n";
350 $select_box .= '<select name="set_theme" lang="en" dir="ltr"'
351 . ' class="autosubmit">';
352 foreach ($this->themes
as $each_theme_id => $each_theme) {
353 $select_box .= '<option value="' . $each_theme_id . '"';
354 if ($this->active_theme
=== $each_theme_id) {
355 $select_box .= ' selected="selected"';
357 $select_box .= '>' . htmlspecialchars($each_theme->getName())
360 $select_box .= '</select>';
363 $select_box .= '</form>';
370 * Renders the previews for all themes
375 public function getPrintPreviews()
378 foreach ($this->themes
as $each_theme) {
379 $retval .= $each_theme->getPrintPreview();
380 } // end 'open themes'
385 * returns Theme object for fall back theme
387 * @return Theme fall back theme
390 public function getFallBackTheme()
392 if (isset($this->themes
[self
::FALLBACK_THEME
])) {
393 return $this->themes
[self
::FALLBACK_THEME
];
405 public function printCss()
407 if ($this->theme
->loadCss()) {
411 // if loading css for this theme failed, try default theme css
412 $fallback_theme = $this->getFallBackTheme();
413 if ($fallback_theme && $fallback_theme->loadCss()) {
421 * Theme initialization
426 public static function initializeTheme()
428 $tmanager = self
::getInstance();
433 * @global Theme $GLOBALS['PMA_Theme']
435 $GLOBALS['PMA_Theme'] = $tmanager->theme
;
440 * @global string $GLOBALS['pmaThemePath']
442 $GLOBALS['pmaThemePath'] = $GLOBALS['PMA_Theme']->getPath();
444 * the theme image path
445 * @global string $GLOBALS['pmaThemeImage']
447 $GLOBALS['pmaThemeImage'] = $GLOBALS['PMA_Theme']->getImgPath();
450 * load layout file if exists
452 if (@file_exists
($GLOBALS['PMA_Theme']->getLayoutFile())) {
453 include $GLOBALS['PMA_Theme']->getLayoutFile();