2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * phpMyAdmin theme manager
8 namespace PMA\libraries
;
10 use PMA\libraries\URL
;
13 * phpMyAdmin theme manager
20 * ThemeManager instance
26 private static $_instance;
29 * @var string path to theme folder
32 private $_themes_path = './themes/';
35 * @var array available themes
37 var $themes = array();
40 * @var string cookie name
42 var $cookie_name = 'pma_theme';
47 var $per_server = false;
50 * @var string name of active theme
52 var $active_theme = '';
55 * @var Theme Theme active theme
65 * @const string The name of the fallback theme
67 const FALLBACK_THEME
= 'pmahomme';
70 * Constructor for Theme Manager class
74 public function __construct()
80 * Returns the singleton Response object
82 * @return Response object
84 public static function getInstance()
86 if (empty(self
::$_instance)) {
87 self
::$_instance = new ThemeManager();
89 return self
::$_instance;
93 * sets path to folder containing the themes
95 * @param string $path path to themes folder
98 * @return boolean success
100 public function setThemesPath($path)
102 if (! $this->_checkThemeFolder($path)) {
106 $this->_themes_path
= trim($path);
111 * sets if there are different themes per server
113 * @param boolean $per_server Whether to enable per server flag
118 public function setThemePerServer($per_server)
120 $this->per_server
= (bool) $per_server;
124 * Initialise the class
129 public function init()
131 $this->themes
= array();
132 $this->theme_default
= self
::FALLBACK_THEME
;
133 $this->active_theme
= '';
135 if (! $this->setThemesPath('./themes/')) {
139 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
143 $this->theme
= new Theme
;
145 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
148 __('Default theme %s not found!'),
149 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])
153 $GLOBALS['cfg']['ThemeDefault'] = false;
156 $this->theme_default
= $GLOBALS['cfg']['ThemeDefault'];
158 // check if user have a theme cookie
159 if (! $this->getThemeCookie()
160 ||
! $this->setActiveTheme($this->getThemeCookie())
162 if ($GLOBALS['cfg']['ThemeDefault']) {
163 // otherwise use default theme
164 $this->setActiveTheme($this->theme_default
);
167 $this->setActiveTheme(self
::FALLBACK_THEME
);
173 * Checks configuration
178 public function checkConfig()
180 if ($this->theme_default
!= $GLOBALS['cfg']['ThemeDefault']
184 // at least the theme path needs to be checked every time for new
185 // themes, as there is no other way at the moment to keep track of
186 // new or removed themes
194 * @param string $theme theme name
197 * @return bool true on success
199 public function setActiveTheme($theme = null)
201 if (! $this->checkTheme($theme)) {
204 __('Theme %s not found!'),
205 htmlspecialchars($theme)
212 $this->active_theme
= $theme;
213 $this->theme
= $this->themes
[$theme];
216 //$this->setThemeCookie();
222 * Returns name for storing theme
224 * @return string cookie name
227 public function getThemeCookieName()
229 // Allow different theme per server
230 if (isset($GLOBALS['server']) && $this->per_server
) {
231 return $this->cookie_name
. '-' . $GLOBALS['server'];
233 return $this->cookie_name
;
238 * returns name of theme stored in the cookie
240 * @return string theme name from cookie
243 public function getThemeCookie()
245 if (isset($_COOKIE[$this->getThemeCookieName()])) {
246 return $_COOKIE[$this->getThemeCookieName()];
253 * save theme in cookie
258 public function setThemeCookie()
260 $GLOBALS['PMA_Config']->setCookie(
261 $this->getThemeCookieName(),
265 // force a change of a dummy session variable to avoid problems
266 // with the caching of phpmyadmin.css.php
267 $GLOBALS['PMA_Config']->set('theme-update', $this->theme
->id
);
272 * Checks whether folder is valid for storing themes
274 * @param string $folder Folder name to test
279 private function _checkThemeFolder($folder)
281 if (! is_dir($folder)) {
284 __('Theme path not found for theme %s!'),
285 htmlspecialchars($folder)
301 public function loadThemes()
303 $this->themes
= array();
305 if (false === ($handleThemes = opendir($this->_themes_path
))) {
307 'phpMyAdmin-ERROR: cannot open themes folder: '
308 . $this->_themes_path
,
314 // check for themes directory
315 while (false !== ($PMA_Theme = readdir($handleThemes))) {
316 // Skip non dirs, . and ..
317 if ($PMA_Theme == '.'
318 ||
$PMA_Theme == '..'
319 ||
! is_dir($this->_themes_path
. $PMA_Theme)
323 if (array_key_exists($PMA_Theme, $this->themes
)) {
326 $new_theme = Theme
::load(
327 $this->_themes_path
. $PMA_Theme
330 $new_theme->setId($PMA_Theme);
331 $this->themes
[$PMA_Theme] = $new_theme;
334 closedir($handleThemes);
336 ksort($this->themes
);
341 * checks if given theme name is a known theme
343 * @param string $theme name fo theme to check for
348 public function checkTheme($theme)
350 if (! array_key_exists($theme, $this->themes
)) {
358 * returns HTML selectbox, with or without form enclosed
360 * @param boolean $form whether enclosed by from tags or not
365 public function getHtmlSelectBox($form = true)
370 $select_box .= '<form name="setTheme" method="get"';
371 $select_box .= ' action="index.php" class="disableAjax">';
372 $select_box .= URL
::getHiddenInputs();
375 $theme_preview_path= './themes.php';
376 $theme_preview_href = '<a href="'
377 . $theme_preview_path . '" target="themes" class="themeselect">';
378 $select_box .= $theme_preview_href . __('Theme:') . '</a>' . "\n";
380 $select_box .= '<select name="set_theme" lang="en" dir="ltr"'
381 . ' class="autosubmit">';
382 foreach ($this->themes
as $each_theme_id => $each_theme) {
383 $select_box .= '<option value="' . $each_theme_id . '"';
384 if ($this->active_theme
=== $each_theme_id) {
385 $select_box .= ' selected="selected"';
387 $select_box .= '>' . htmlspecialchars($each_theme->getName())
390 $select_box .= '</select>';
393 $select_box .= '</form>';
400 * enables backward compatibility
405 public function makeBc()
407 $GLOBALS['theme'] = $this->theme
->getId();
408 $GLOBALS['pmaThemePath'] = $this->theme
->getPath();
409 $GLOBALS['pmaThemeImage'] = $this->theme
->getImgPath();
412 * load layout file if exists
414 if (file_exists($this->theme
->getLayoutFile())) {
415 include $this->theme
->getLayoutFile();
420 * Renders the previews for all themes
425 public function getPrintPreviews()
428 foreach ($this->themes
as $each_theme) {
429 $retval .= $each_theme->getPrintPreview();
430 } // end 'open themes'
435 * returns Theme object for fall back theme
437 * @return Theme fall back theme
440 public function getFallBackTheme()
442 if (isset($this->themes
[self
::FALLBACK_THEME
])) {
443 return $this->themes
[self
::FALLBACK_THEME
];
455 public function printCss()
457 if ($this->theme
->loadCss()) {
461 // if loading css for this theme failed, try default theme css
462 $fallback_theme = $this->getFallBackTheme();
463 if ($fallback_theme && $fallback_theme->loadCss()) {
471 * Theme initialization
476 public static function initializeTheme()
478 $tmanager = self
::getInstance();
480 // for the theme per server feature
481 if (isset($_REQUEST['server']) && ! isset($_REQUEST['set_theme'])) {
482 $GLOBALS['server'] = $_REQUEST['server'];
483 $tmp = $tmanager->getThemeCookie();
485 $tmp = $tmanager->theme_default
;
487 $tmanager->setActiveTheme($tmp);
490 * @todo move into ThemeManager::__wakeup()
492 if (isset($_REQUEST['set_theme'])) {
493 // if user selected a theme
494 $tmanager->setActiveTheme($_REQUEST['set_theme']);
500 * @global Theme $_SESSION['PMA_Theme']
502 $_SESSION['PMA_Theme'] = $tmanager->theme
;
507 * @global string $GLOBALS['theme']
509 $GLOBALS['theme'] = $_SESSION['PMA_Theme']->getName();
512 * @global string $GLOBALS['pmaThemePath']
514 $GLOBALS['pmaThemePath'] = $_SESSION['PMA_Theme']->getPath();
516 * the theme image path
517 * @global string $GLOBALS['pmaThemeImage']
519 $GLOBALS['pmaThemeImage'] = $_SESSION['PMA_Theme']->getImgPath();
522 * load layout file if exists
524 if (@file_exists
($_SESSION['PMA_Theme']->getLayoutFile())) {
525 include $_SESSION['PMA_Theme']->getLayoutFile();