Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / Theme_Manager.class.php
blob75205165139a4ee3d7fa64efea0c65313946c967
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package PhpMyAdmin
6 */
7 if (! defined('PHPMYADMIN')) {
8 exit;
11 /**
13 * @package PhpMyAdmin
15 class PMA_Theme_Manager
17 /**
18 * @var string path to theme folder
19 * @access protected
21 private $_themes_path;
23 /**
24 * @var array available themes
26 var $themes = array();
28 /**
29 * @var string cookie name
31 var $cookie_name = 'pma_theme';
33 /**
34 * @var boolean
36 var $per_server = false;
38 /**
39 * @var string name of active theme
41 var $active_theme = '';
43 /**
44 * @var PMA_Theme PMA_Theme active theme
46 var $theme = null;
48 /**
49 * @var string
51 var $theme_default;
53 /**
54 * @const string The name of the fallback theme
56 const FALLBACK_THEME = 'pmahomme';
58 /**
59 * Constructor for Theme Manager class
61 * @access public
62 * @return void
64 public function __construct()
66 $this->init();
69 /**
70 * sets path to folder containing the themes
72 * @param string $path path to themes folder
74 * @access public
75 * @return boolean success
77 public function setThemesPath($path)
79 if (! $this->_checkThemeFolder($path)) {
80 return false;
83 $this->_themes_path = trim($path);
84 return true;
87 /**
88 * Returns path to folder containing themes
90 * @access public
91 * @return string theme path
93 public function getThemesPath()
95 return $this->_themes_path;
98 /**
99 * sets if there are different themes per server
101 * @param boolean $per_server
103 * @access public
104 * @return void
106 public function setThemePerServer($per_server)
108 $this->per_server = (bool) $per_server;
112 * Initialise the class
114 * @access public
115 * @return void
117 public function init()
119 $this->themes = array();
120 $this->theme_default = self::FALLBACK_THEME;
121 $this->active_theme = '';
123 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
124 return false;
127 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
129 $this->loadThemes();
131 $this->theme = new PMA_Theme;
134 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
135 trigger_error(
136 sprintf(
137 __('Default theme %s not found!'),
138 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])
140 E_USER_ERROR
142 $GLOBALS['cfg']['ThemeDefault'] = false;
145 $this->theme_default = $GLOBALS['cfg']['ThemeDefault'];
147 // check if user have a theme cookie
148 if (! $this->getThemeCookie()
149 || ! $this->setActiveTheme($this->getThemeCookie())
151 if ($GLOBALS['cfg']['ThemeDefault']) {
152 // otherwise use default theme
153 $this->setActiveTheme($this->theme_default);
154 } else {
155 // or fallback theme
156 $this->setActiveTheme(self::FALLBACK_THEME);
162 * Checks configuration
164 * @access public
165 * @return void
167 public function checkConfig()
169 if ($this->_themes_path != trim($GLOBALS['cfg']['ThemePath'])
170 || $this->theme_default != $GLOBALS['cfg']['ThemeDefault']
172 $this->init();
173 } else {
174 // at least the theme path needs to be checked every time for new
175 // themes, as there is no other way at the moment to keep track of
176 // new or removed themes
177 $this->loadThemes();
182 * Sets active theme
184 * @param string $theme theme name
186 * @access public
187 * @return bool true on success
189 public function setActiveTheme($theme = null)
191 if (! $this->checkTheme($theme)) {
192 trigger_error(
193 sprintf(
194 __('Theme %s not found!'),
195 htmlspecialchars($theme)
197 E_USER_ERROR
199 return false;
202 $this->active_theme = $theme;
203 $this->theme = $this->themes[$theme];
205 // need to set later
206 //$this->setThemeCookie();
208 return true;
213 * @return string cookie name
214 * @access public
216 public function getThemeCookieName()
218 // Allow different theme per server
219 if (isset($GLOBALS['server']) && $this->per_server) {
220 return $this->cookie_name . '-' . $GLOBALS['server'];
221 } else {
222 return $this->cookie_name;
227 * returns name of theme stored in the cookie
229 * @return string theme name from cookie
230 * @access public
232 public function getThemeCookie()
234 if (isset($_COOKIE[$this->getThemeCookieName()])) {
235 return $_COOKIE[$this->getThemeCookieName()];
238 return false;
242 * save theme in cookie
244 * @return bool true
245 * @access public
247 public function setThemeCookie()
249 $GLOBALS['PMA_Config']->setCookie(
250 $this->getThemeCookieName(),
251 $this->theme->id,
252 $this->theme_default
254 // force a change of a dummy session variable to avoid problems
255 // with the caching of phpmyadmin.css.php
256 $GLOBALS['PMA_Config']->set('theme-update', $this->theme->id);
257 return true;
261 * @param string $folder
263 * @return boolean
264 * @access private
266 private function _checkThemeFolder($folder)
268 if (! is_dir($folder)) {
269 trigger_error(
270 sprintf(
271 __('Theme path not found for theme %s!'),
272 htmlspecialchars($folder)
274 E_USER_ERROR
276 return false;
279 return true;
283 * read all themes
285 * @return bool true
286 * @access public
288 public function loadThemes()
290 $this->themes = array();
292 if ($handleThemes = opendir($this->getThemesPath())) {
293 // check for themes directory
294 while (false !== ($PMA_Theme = readdir($handleThemes))) {
295 // Skip non dirs, . and ..
296 if ($PMA_Theme == '.'
297 || $PMA_Theme == '..'
298 || ! is_dir($this->getThemesPath() . '/' . $PMA_Theme)
300 continue;
302 if (array_key_exists($PMA_Theme, $this->themes)) {
303 continue;
305 $new_theme = PMA_Theme::load(
306 $this->getThemesPath() . '/' . $PMA_Theme
308 if ($new_theme) {
309 $new_theme->setId($PMA_Theme);
310 $this->themes[$PMA_Theme] = $new_theme;
312 } // end get themes
313 closedir($handleThemes);
314 } else {
315 trigger_error(
316 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
317 E_USER_WARNING
319 return false;
320 } // end check for themes directory
322 ksort($this->themes);
323 return true;
327 * checks if given theme name is a known theme
329 * @param string $theme name fo theme to check for
331 * @return bool
332 * @access public
334 public function checkTheme($theme)
336 if (! array_key_exists($theme, $this->themes)) {
337 return false;
340 return true;
344 * returns HTML selectbox, with or without form enclosed
346 * @param boolean $form whether enclosed by from tags or not
348 * @return string
349 * @access public
351 public function getHtmlSelectBox($form = true)
353 $select_box = '';
355 if ($form) {
356 $select_box .= '<form name="setTheme" method="get"';
357 $select_box .= ' action="index.php" class="disableAjax">';
358 $select_box .= PMA_generate_common_hidden_inputs();
361 $theme_preview_path= './themes.php';
362 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" class="themeselect">';
363 $select_box .= $theme_preview_href . __('Theme') . '</a>:' . "\n";
365 $select_box .= '<select name="set_theme" lang="en" dir="ltr" class="autosubmit">';
366 foreach ($this->themes as $each_theme_id => $each_theme) {
367 $select_box .= '<option value="' . $each_theme_id . '"';
368 if ($this->active_theme === $each_theme_id) {
369 $select_box .= ' selected="selected"';
371 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
373 $select_box .= '</select>';
375 if ($form) {
376 $select_box .= '</form>';
379 return $select_box;
383 * enables backward compatibility
385 * @return void
386 * @access public
388 public function makeBc()
390 $GLOBALS['theme'] = $this->theme->getId();
391 $GLOBALS['pmaThemePath'] = $this->theme->getPath();
392 $GLOBALS['pmaThemeImage'] = $this->theme->getImgPath();
395 * load layout file if exists
397 if (file_exists($this->theme->getLayoutFile())) {
398 include $this->theme->getLayoutFile();
403 * Renders the previews for all themes
405 * @return string
406 * @access public
408 public function getPrintPreviews()
410 $retval = '';
411 foreach ($this->themes as $each_theme) {
412 $retval .= $each_theme->getPrintPreview();
413 } // end 'open themes'
414 return $retval;
418 * returns PMA_Theme object for fall back theme
420 * @return object PMA_Theme
421 * @access public
423 public function getFallBackTheme()
425 if (isset($this->themes[self::FALLBACK_THEME])) {
426 return $this->themes[self::FALLBACK_THEME];
429 return false;
433 * prints css data
435 * @return bool
436 * @access public
438 public function printCss()
440 if ($this->theme->loadCss()) {
441 return true;
444 // if loading css for this theme failed, try default theme css
445 $fallback_theme = $this->getFallBackTheme();
446 if ($fallback_theme && $fallback_theme->loadCss()) {
447 return true;
450 return false;