3.3.6-rc1
[phpmyadmin/dkf.git] / libraries / Theme_Manager.class.php
blob91da35e00252aeb67b3d69876fd701ce1683bdec
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 * @package phpMyAdmin
7 */
9 /**
12 require_once './libraries/Theme.class.php';
14 /**
16 * @package phpMyAdmin
18 class PMA_Theme_Manager
20 /**
21 * @var string path to theme folder
22 * @access protected
24 var $_themes_path;
26 /**
27 * @var array available themes
29 var $themes = array();
31 /**
32 * @var string cookie name
34 var $cookie_name = 'pma_theme';
36 /**
37 * @var boolean
39 var $per_server = false;
41 /**
42 * @var string name of active theme
44 var $active_theme = '';
46 /**
47 * @var object PMA_Theme active theme
49 var $theme = null;
51 /**
52 * @var string
54 var $theme_default = 'original';
56 function __construct()
58 $this->init();
61 /**
62 * sets path to folder containing the themes
64 * @param string $path path to themes folder
65 * @return boolean success
67 function setThemesPath($path)
69 if (! $this->_checkThemeFolder($path)) {
70 return false;
73 $this->_themes_path = trim($path);
74 return true;
77 /**
78 * @public
79 * @return string
81 function getThemesPath()
83 return $this->_themes_path;
86 /**
87 * sets if there are different themes per server
89 * @param boolean $per_server
91 function setThemePerServer($per_server)
93 $this->per_server = (bool) $per_server;
96 function init()
98 $this->themes = array();
99 $this->theme_default = 'original';
100 $this->active_theme = '';
102 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
103 return false;
106 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
108 $this->loadThemes();
110 $this->theme = new PMA_Theme;
113 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
114 trigger_error(
115 sprintf($GLOBALS['strThemeDefaultNotFound'],
116 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])),
117 E_USER_ERROR);
118 $GLOBALS['cfg']['ThemeDefault'] = false;
121 $this->theme_default = $GLOBALS['cfg']['ThemeDefault'];
123 // check if user have a theme cookie
124 if (! $this->getThemeCookie()
125 || ! $this->setActiveTheme($this->getThemeCookie())) {
126 // otherwise use default theme
127 if ($GLOBALS['cfg']['ThemeDefault']) {
128 $this->setActiveTheme($GLOBALS['cfg']['ThemeDefault']);
129 } else {
130 // or original theme
131 $this->setActiveTheme('original');
136 function checkConfig()
138 if ($this->_themes_path != trim($GLOBALS['cfg']['ThemePath'])
139 || $this->theme_default != $GLOBALS['cfg']['ThemeDefault']) {
140 $this->init();
141 } else {
142 // at least the theme path needs to be checked every time for new
143 // themes, as there is no other way at the moment to keep track of
144 // new or removed themes
145 $this->loadThemes();
149 function setActiveTheme($theme = null)
151 if (! $this->checkTheme($theme)) {
152 trigger_error(
153 sprintf($GLOBALS['strThemeNotFound'], 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 * @uses PMA_setCookie();
197 * @uses PMA_Theme_Manager::getThemeCookieName()
198 * @uses PMA_Theme_Manager::$theme
199 * @uses PMA_Theme_Manager::$theme_default
200 * @uses PMA_Theme::getId()
202 function setThemeCookie()
204 PMA_setCookie($this->getThemeCookieName(), $this->theme->id,
205 $this->theme_default);
206 // force a change of a dummy session variable to avoid problems
207 // with the caching of phpmyadmin.css.php
208 $_SESSION['PMA_Config']->set('theme-update', $this->theme->id);
209 return true;
213 * @private
214 * @param string $folder
215 * @return boolean
217 /*private*/ function _checkThemeFolder($folder)
219 if (! is_dir($folder)) {
220 trigger_error(
221 sprintf($GLOBALS['strThemePathNotFound'],
222 htmlspecialchars($folder)),
223 E_USER_ERROR);
224 return false;
227 return true;
231 * read all themes
233 function loadThemes()
235 $this->themes = array();
237 if ($handleThemes = opendir($this->getThemesPath())) {
238 // check for themes directory
239 while (false !== ($PMA_Theme = readdir($handleThemes))) {
240 if (array_key_exists($PMA_Theme, $this->themes)) {
241 // this does nothing!
242 //$this->themes[$PMA_Theme] = $this->themes[$PMA_Theme];
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
268 function checkTheme($theme)
270 if (! array_key_exists($theme, $this->themes)) {
271 return false;
274 return true;
278 * returns HTML selectbox, with or without form enclosed
280 * @param boolean $form whether enclosed by from tags or not
282 function getHtmlSelectBox($form = true)
284 $select_box = '';
286 if ($form) {
287 $select_box .= '<form name="setTheme" method="post" action="index.php"'
288 .' target="_parent">';
289 $select_box .= PMA_generate_common_hidden_inputs();
292 $theme_selected = FALSE;
293 $theme_preview_path= './themes.php';
294 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" onclick="'
295 . "window.open('" . $theme_preview_path . "','themes','left=10,top=20,width=510,height=350,scrollbars=yes,status=yes,resizable=yes');"
296 . '">';
297 $select_box .= $theme_preview_href . $GLOBALS['strTheme'] . '</a>:' . "\n";
299 $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr"'
300 .' onchange="this.form.submit();" >';
301 foreach ($this->themes as $each_theme_id => $each_theme) {
302 $select_box .= '<option value="' . $each_theme_id . '"';
303 if ($this->active_theme === $each_theme_id) {
304 $select_box .= ' selected="selected"';
306 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
308 $select_box .= '</select>';
310 if ($form) {
311 $select_box .= '<noscript><input type="submit" value="' . $GLOBALS['strGo'] . '" /></noscript>';
312 $select_box .= '</form>';
315 return $select_box;
319 * enables backward compatibility
321 function makeBc()
323 $GLOBALS['theme'] = $this->theme->getId();
324 $GLOBALS['pmaThemePath'] = $this->theme->getPath();
325 $GLOBALS['pmaThemeImage'] = $this->theme->getImgPath();
328 * load layout file if exists
330 if (@file_exists($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
331 include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
338 * prints out preview for every theme
340 * @uses $this->themes
341 * @uses PMA_Theme::printPreview()
343 function printPreviews()
345 foreach ($this->themes as $each_theme) {
346 $each_theme->printPreview();
347 } // end 'open themes'
351 * returns PMA_Theme object for fall back theme
352 * @return object PMA_Theme
354 function getFallBackTheme()
356 if (isset($this->themes['original'])) {
357 return $this->themes['original'];
360 return false;
364 * prints css data
366 function printCss($type)
368 if ($this->theme->loadCss($type)) {
369 return true;
372 // if loading css for this theme failed, try default theme css
373 $fallback_theme = $this->getFallBackTheme();
374 if ($fallback_theme && $fallback_theme->loadCss($type)) {
375 return true;
378 return false;