added japanese language
[openemr.git] / phpmyadmin / libraries / Theme_Manager.class.php
blobd12689ea0560b94616b016c906211d8accf54a78
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * phpMyAdmin theme manager
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * phpMyAdmin theme manager
15 * @package PhpMyAdmin
17 class PMA_Theme_Manager
19 /**
20 * @var string path to theme folder
21 * @access protected
23 private $_themes_path;
25 /**
26 * @var array available themes
28 var $themes = array();
30 /**
31 * @var string cookie name
33 var $cookie_name = 'pma_theme';
35 /**
36 * @var boolean
38 var $per_server = false;
40 /**
41 * @var string name of active theme
43 var $active_theme = '';
45 /**
46 * @var PMA_Theme PMA_Theme active theme
48 var $theme = null;
50 /**
51 * @var string
53 var $theme_default;
55 /**
56 * @const string The name of the fallback theme
58 const FALLBACK_THEME = 'pmahomme';
60 /**
61 * Constructor for Theme Manager class
63 * @access public
65 public function __construct()
67 $this->init();
70 /**
71 * sets path to folder containing the themes
73 * @param string $path path to themes folder
75 * @access public
76 * @return boolean success
78 public function setThemesPath($path)
80 if (! $this->_checkThemeFolder($path)) {
81 return false;
84 $this->_themes_path = trim($path);
85 return true;
88 /**
89 * Returns path to folder containing themes
91 * @access public
92 * @return string theme path
94 public function getThemesPath()
96 return $this->_themes_path;
99 /**
100 * sets if there are different themes per server
102 * @param boolean $per_server Whether to enable per server flag
104 * @access public
105 * @return void
107 public function setThemePerServer($per_server)
109 $this->per_server = (bool) $per_server;
113 * Initialise the class
115 * @access public
116 * @return boolean|void
118 public function init()
120 $this->themes = array();
121 $this->theme_default = self::FALLBACK_THEME;
122 $this->active_theme = '';
124 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
125 return false;
128 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
130 $this->loadThemes();
132 $this->theme = new PMA_Theme;
135 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
136 trigger_error(
137 sprintf(
138 __('Default theme %s not found!'),
139 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])
141 E_USER_ERROR
143 $GLOBALS['cfg']['ThemeDefault'] = false;
146 $this->theme_default = $GLOBALS['cfg']['ThemeDefault'];
148 // check if user have a theme cookie
149 if (! $this->getThemeCookie()
150 || ! $this->setActiveTheme($this->getThemeCookie())
152 if ($GLOBALS['cfg']['ThemeDefault']) {
153 // otherwise use default theme
154 $this->setActiveTheme($this->theme_default);
155 } else {
156 // or fallback theme
157 $this->setActiveTheme(self::FALLBACK_THEME);
163 * Checks configuration
165 * @access public
166 * @return void
168 public function checkConfig()
170 if ($this->_themes_path != trim($GLOBALS['cfg']['ThemePath'])
171 || $this->theme_default != $GLOBALS['cfg']['ThemeDefault']
173 $this->init();
174 } else {
175 // at least the theme path needs to be checked every time for new
176 // themes, as there is no other way at the moment to keep track of
177 // new or removed themes
178 $this->loadThemes();
183 * Sets active theme
185 * @param string $theme theme name
187 * @access public
188 * @return bool true on success
190 public function setActiveTheme($theme = null)
192 if (! $this->checkTheme($theme)) {
193 trigger_error(
194 sprintf(
195 __('Theme %s not found!'),
196 htmlspecialchars($theme)
198 E_USER_ERROR
200 return false;
203 $this->active_theme = $theme;
204 $this->theme = $this->themes[$theme];
206 // need to set later
207 //$this->setThemeCookie();
209 return true;
213 * Returns name for storing theme
215 * @return string cookie name
216 * @access public
218 public function getThemeCookieName()
220 // Allow different theme per server
221 if (isset($GLOBALS['server']) && $this->per_server) {
222 return $this->cookie_name . '-' . $GLOBALS['server'];
223 } else {
224 return $this->cookie_name;
229 * returns name of theme stored in the cookie
231 * @return string theme name from cookie
232 * @access public
234 public function getThemeCookie()
236 if (isset($_COOKIE[$this->getThemeCookieName()])) {
237 return $_COOKIE[$this->getThemeCookieName()];
240 return false;
244 * save theme in cookie
246 * @return bool true
247 * @access public
249 public function setThemeCookie()
251 $GLOBALS['PMA_Config']->setCookie(
252 $this->getThemeCookieName(),
253 $this->theme->id,
254 $this->theme_default
256 // force a change of a dummy session variable to avoid problems
257 // with the caching of phpmyadmin.css.php
258 $GLOBALS['PMA_Config']->set('theme-update', $this->theme->id);
259 return true;
263 * Checks whether folder is valid for storing themes
265 * @param string $folder Folder name to test
267 * @return boolean
268 * @access private
270 private function _checkThemeFolder($folder)
272 if (! is_dir($folder)) {
273 trigger_error(
274 sprintf(
275 __('Theme path not found for theme %s!'),
276 htmlspecialchars($folder)
278 E_USER_ERROR
280 return false;
283 return true;
287 * read all themes
289 * @return bool true
290 * @access public
292 public function loadThemes()
294 $this->themes = array();
296 if ($handleThemes = opendir($this->getThemesPath())) {
297 // check for themes directory
298 while (false !== ($PMA_Theme = readdir($handleThemes))) {
299 // Skip non dirs, . and ..
300 if ($PMA_Theme == '.'
301 || $PMA_Theme == '..'
302 || ! is_dir($this->getThemesPath() . '/' . $PMA_Theme)
304 continue;
306 if (array_key_exists($PMA_Theme, $this->themes)) {
307 continue;
309 $new_theme = PMA_Theme::load(
310 $this->getThemesPath() . '/' . $PMA_Theme
312 if ($new_theme) {
313 $new_theme->setId($PMA_Theme);
314 $this->themes[$PMA_Theme] = $new_theme;
316 } // end get themes
317 closedir($handleThemes);
318 } else {
319 trigger_error(
320 'phpMyAdmin-ERROR: cannot open themes folder: '
321 . $this->getThemesPath(),
322 E_USER_WARNING
324 return false;
325 } // end check for themes directory
327 ksort($this->themes);
328 return true;
332 * checks if given theme name is a known theme
334 * @param string $theme name fo theme to check for
336 * @return bool
337 * @access public
339 public function checkTheme($theme)
341 if (! array_key_exists($theme, $this->themes)) {
342 return false;
345 return true;
349 * returns HTML selectbox, with or without form enclosed
351 * @param boolean $form whether enclosed by from tags or not
353 * @return string
354 * @access public
356 public function getHtmlSelectBox($form = true)
358 $select_box = '';
360 if ($form) {
361 $select_box .= '<form name="setTheme" method="get"';
362 $select_box .= ' action="index.php" class="disableAjax">';
363 $select_box .= PMA_URL_getHiddenInputs();
366 $theme_preview_path= './themes.php';
367 $theme_preview_href = '<a href="'
368 . $theme_preview_path . '" target="themes" class="themeselect">';
369 $select_box .= $theme_preview_href . __('Theme:') . '</a>' . "\n";
371 $select_box .= '<select name="set_theme" lang="en" dir="ltr"'
372 . ' class="autosubmit">';
373 foreach ($this->themes as $each_theme_id => $each_theme) {
374 $select_box .= '<option value="' . $each_theme_id . '"';
375 if ($this->active_theme === $each_theme_id) {
376 $select_box .= ' selected="selected"';
378 $select_box .= '>' . htmlspecialchars($each_theme->getName())
379 . '</option>';
381 $select_box .= '</select>';
383 if ($form) {
384 $select_box .= '</form>';
387 return $select_box;
391 * enables backward compatibility
393 * @return void
394 * @access public
396 public function makeBc()
398 $GLOBALS['theme'] = $this->theme->getId();
399 $GLOBALS['pmaThemePath'] = $this->theme->getPath();
400 $GLOBALS['pmaThemeImage'] = $this->theme->getImgPath();
403 * load layout file if exists
405 if (file_exists($this->theme->getLayoutFile())) {
406 include $this->theme->getLayoutFile();
411 * Renders the previews for all themes
413 * @return string
414 * @access public
416 public function getPrintPreviews()
418 $retval = '';
419 foreach ($this->themes as $each_theme) {
420 $retval .= $each_theme->getPrintPreview();
421 } // end 'open themes'
422 return $retval;
426 * returns PMA_Theme object for fall back theme
428 * @return PMA_Theme fall back theme
429 * @access public
431 public function getFallBackTheme()
433 if (isset($this->themes[self::FALLBACK_THEME])) {
434 return $this->themes[self::FALLBACK_THEME];
437 return false;
441 * prints css data
443 * @return bool
444 * @access public
446 public function printCss()
448 if ($this->theme->loadCss()) {
449 return true;
452 // if loading css for this theme failed, try default theme css
453 $fallback_theme = $this->getFallBackTheme();
454 if ($fallback_theme && $fallback_theme->loadCss()) {
455 return true;
458 return false;