Bug: Live query chart always zero
[phpmyadmin/tyronm.git] / libraries / Theme_Manager.class.php
blob840e611a97b1b1871c33b5ce49c8a464f5bb9cea
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package phpMyAdmin
6 */
8 /**
10 * @package phpMyAdmin
12 class PMA_Theme_Manager
14 /**
15 * @var string path to theme folder
16 * @access protected
18 var $_themes_path;
20 /**
21 * @var array available themes
23 var $themes = array();
25 /**
26 * @var string cookie name
28 var $cookie_name = 'pma_theme';
30 /**
31 * @var boolean
33 var $per_server = false;
35 /**
36 * @var string name of active theme
38 var $active_theme = '';
40 /**
41 * @var PMA_Theme PMA_Theme active theme
43 var $theme = null;
45 /**
46 * @var string
48 var $theme_default = 'original';
50 function __construct()
52 $this->init();
55 /**
56 * sets path to folder containing the themes
58 * @param string $path path to themes folder
59 * @return boolean success
61 function setThemesPath($path)
63 if (! $this->_checkThemeFolder($path)) {
64 return false;
67 $this->_themes_path = trim($path);
68 return true;
71 /**
72 * @public
73 * @return string
75 function getThemesPath()
77 return $this->_themes_path;
80 /**
81 * sets if there are different themes per server
83 * @param boolean $per_server
85 function setThemePerServer($per_server)
87 $this->per_server = (bool) $per_server;
90 function init()
92 $this->themes = array();
93 $this->theme_default = 'original';
94 $this->active_theme = '';
96 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
97 return false;
100 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
102 $this->loadThemes();
104 $this->theme = new PMA_Theme;
107 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
108 trigger_error(
109 sprintf(
110 __('Default theme %s not found!'),
111 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])
113 E_USER_ERROR
115 $GLOBALS['cfg']['ThemeDefault'] = false;
118 $this->theme_default = $GLOBALS['cfg']['ThemeDefault'];
120 // check if user have a theme cookie
121 if (! $this->getThemeCookie()
122 || ! $this->setActiveTheme($this->getThemeCookie())) {
123 // otherwise use default theme
124 if ($GLOBALS['cfg']['ThemeDefault']) {
125 $this->setActiveTheme($GLOBALS['cfg']['ThemeDefault']);
126 } else {
127 // or original theme
128 $this->setActiveTheme('original');
133 function checkConfig()
135 if ($this->_themes_path != trim($GLOBALS['cfg']['ThemePath'])
136 || $this->theme_default != $GLOBALS['cfg']['ThemeDefault']) {
137 $this->init();
138 } else {
139 // at least the theme path needs to be checked every time for new
140 // themes, as there is no other way at the moment to keep track of
141 // new or removed themes
142 $this->loadThemes();
146 function setActiveTheme($theme = null)
148 if (! $this->checkTheme($theme)) {
149 trigger_error(
150 sprintf(
151 __('Theme %s not found!'),
152 htmlspecialchars($theme)
154 E_USER_ERROR);
155 return false;
158 $this->active_theme = $theme;
159 $this->theme = $this->themes[$theme];
161 // need to set later
162 //$this->setThemeCookie();
164 return true;
168 * @return string cookie name
170 function getThemeCookieName()
172 // Allow different theme per server
173 if (isset($GLOBALS['server']) && $this->per_server) {
174 return $this->cookie_name . '-' . $GLOBALS['server'];
175 } else {
176 return $this->cookie_name;
181 * returns name of theme stored in the cookie
182 * @return string theme name from cookie
184 function getThemeCookie()
186 if (isset($_COOKIE[$this->getThemeCookieName()])) {
187 return $_COOKIE[$this->getThemeCookieName()];
190 return false;
194 * save theme in cookie
196 * @return bool true
198 function setThemeCookie()
200 $GLOBALS['PMA_Config']->setCookie($this->getThemeCookieName(), $this->theme->id,
201 $this->theme_default);
202 // force a change of a dummy session variable to avoid problems
203 // with the caching of phpmyadmin.css.php
204 $GLOBALS['PMA_Config']->set('theme-update', $this->theme->id);
205 return true;
209 * @private
210 * @param string $folder
211 * @return boolean
213 private function _checkThemeFolder($folder)
215 if (! is_dir($folder)) {
216 trigger_error(
217 sprintf(__('Theme path not found for theme %s!'),
218 htmlspecialchars($folder)),
219 E_USER_ERROR);
220 return false;
223 return true;
227 * read all themes
229 * @return bool true
231 function loadThemes()
233 $this->themes = array();
235 if ($handleThemes = opendir($this->getThemesPath())) {
236 // check for themes directory
237 while (false !== ($PMA_Theme = readdir($handleThemes))) {
238 // Skip non dirs, . and ..
239 if ($PMA_Theme == '.' || $PMA_Theme == '..' || ! is_dir($this->getThemesPath() . '/' . $PMA_Theme)) {
240 continue;
242 if (array_key_exists($PMA_Theme, $this->themes)) {
243 continue;
245 $new_theme = PMA_Theme::load($this->getThemesPath() . '/' . $PMA_Theme);
246 if ($new_theme) {
247 $new_theme->setId($PMA_Theme);
248 $this->themes[$PMA_Theme] = $new_theme;
250 } // end get themes
251 closedir($handleThemes);
252 } else {
253 trigger_error(
254 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
255 E_USER_WARNING);
256 return false;
257 } // end check for themes directory
259 ksort($this->themes);
260 return true;
264 * checks if given theme name is a known theme
266 * @param string $theme name fo theme to check for
267 * @return bool
269 function checkTheme($theme)
271 if (! array_key_exists($theme, $this->themes)) {
272 return false;
275 return true;
279 * returns HTML selectbox, with or without form enclosed
281 * @param boolean $form whether enclosed by from tags or not
282 * @return string
284 function getHtmlSelectBox($form = true)
286 $select_box = '';
288 if ($form) {
289 $select_box .= '<form name="setTheme" method="post" action="index.php"'
290 .' target="_parent">';
291 $select_box .= PMA_generate_common_hidden_inputs();
294 $theme_preview_path= './themes.php';
295 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" class="themeselect">';
296 $select_box .= $theme_preview_href . __('Theme') . '</a>:' . "\n";
298 $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr" class="autosubmit">';
299 foreach ($this->themes as $each_theme_id => $each_theme) {
300 $select_box .= '<option value="' . $each_theme_id . '"';
301 if ($this->active_theme === $each_theme_id) {
302 $select_box .= ' selected="selected"';
304 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
306 $select_box .= '</select>';
308 if ($form) {
309 $select_box .= '<noscript><input type="submit" value="' . __('Go') . '" /></noscript>';
310 $select_box .= '</form>';
313 return $select_box;
317 * enables backward compatibility
319 function makeBc()
321 $GLOBALS['theme'] = $this->theme->getId();
322 $GLOBALS['pmaThemePath'] = $this->theme->getPath();
323 $GLOBALS['pmaThemeImage'] = $this->theme->getImgPath();
326 * load layout file if exists
328 if (file_exists($this->theme->getLayoutFile())) {
329 include $this->theme->getLayoutFile();
336 * prints out preview for every theme
339 function printPreviews()
341 foreach ($this->themes as $each_theme) {
342 $each_theme->printPreview();
343 } // end 'open themes'
347 * returns PMA_Theme object for fall back theme
348 * @return object PMA_Theme
350 function getFallBackTheme()
352 if (isset($this->themes['original'])) {
353 return $this->themes['original'];
356 return false;
360 * prints css data
362 * @param string $type
363 * @return bool
365 function printCss($type)
367 if ($this->theme->loadCss($type)) {
368 return true;
371 // if loading css for this theme failed, try default theme css
372 $fallback_theme = $this->getFallBackTheme();
373 if ($fallback_theme && $fallback_theme->loadCss($type)) {
374 return true;
377 return false;