3.4.0-rc1
[phpmyadmin/thilanka.git] / libraries / Theme_Manager.class.php
blob4ca8517da84865755bad181badd5d55d38385bb8
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 object 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(__('Default theme %s not found!'),
110 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])),
111 E_USER_ERROR);
112 $GLOBALS['cfg']['ThemeDefault'] = false;
115 $this->theme_default = $GLOBALS['cfg']['ThemeDefault'];
117 // check if user have a theme cookie
118 if (! $this->getThemeCookie()
119 || ! $this->setActiveTheme($this->getThemeCookie())) {
120 // otherwise use default theme
121 if ($GLOBALS['cfg']['ThemeDefault']) {
122 $this->setActiveTheme($GLOBALS['cfg']['ThemeDefault']);
123 } else {
124 // or original theme
125 $this->setActiveTheme('original');
130 function checkConfig()
132 if ($this->_themes_path != trim($GLOBALS['cfg']['ThemePath'])
133 || $this->theme_default != $GLOBALS['cfg']['ThemeDefault']) {
134 $this->init();
135 } else {
136 // at least the theme path needs to be checked every time for new
137 // themes, as there is no other way at the moment to keep track of
138 // new or removed themes
139 $this->loadThemes();
143 function setActiveTheme($theme = null)
145 if (! $this->checkTheme($theme)) {
146 trigger_error(
147 sprintf(__('Theme %s not found!'), htmlspecialchars($theme)),
148 E_USER_ERROR);
149 return false;
152 $this->active_theme = $theme;
153 $this->theme = $this->themes[$theme];
155 // need to set later
156 //$this->setThemeCookie();
158 return true;
162 * @return string cookie name
164 function getThemeCookieName()
166 // Allow different theme per server
167 if (isset($GLOBALS['server']) && $this->per_server) {
168 return $this->cookie_name . '-' . $GLOBALS['server'];
169 } else {
170 return $this->cookie_name;
175 * returns name of theme stored in the cookie
176 * @return string theme name from cookie
178 function getThemeCookie()
180 if (isset($_COOKIE[$this->getThemeCookieName()])) {
181 return $_COOKIE[$this->getThemeCookieName()];
184 return false;
188 * save theme in cookie
190 * @uses $GLOBALS['PMA_Config']->setCookie();
191 * @uses PMA_Theme_Manager::getThemeCookieName()
192 * @uses PMA_Theme_Manager::$theme
193 * @uses PMA_Theme_Manager::$theme_default
194 * @uses PMA_Theme::getId()
196 function setThemeCookie()
198 $GLOBALS['PMA_Config']->setCookie($this->getThemeCookieName(), $this->theme->id,
199 $this->theme_default);
200 // force a change of a dummy session variable to avoid problems
201 // with the caching of phpmyadmin.css.php
202 $GLOBALS['PMA_Config']->set('theme-update', $this->theme->id);
203 return true;
207 * @private
208 * @param string $folder
209 * @return boolean
211 /*private*/ function _checkThemeFolder($folder)
213 if (! is_dir($folder)) {
214 trigger_error(
215 sprintf(__('Theme path not found for theme %s!'),
216 htmlspecialchars($folder)),
217 E_USER_ERROR);
218 return false;
221 return true;
225 * read all themes
227 function loadThemes()
229 $this->themes = array();
231 if ($handleThemes = opendir($this->getThemesPath())) {
232 // check for themes directory
233 while (false !== ($PMA_Theme = readdir($handleThemes))) {
234 if (array_key_exists($PMA_Theme, $this->themes)) {
235 // this does nothing!
236 //$this->themes[$PMA_Theme] = $this->themes[$PMA_Theme];
237 continue;
239 $new_theme = PMA_Theme::load($this->getThemesPath() . '/' . $PMA_Theme);
240 if ($new_theme) {
241 $new_theme->setId($PMA_Theme);
242 $this->themes[$PMA_Theme] = $new_theme;
244 } // end get themes
245 closedir($handleThemes);
246 } else {
247 trigger_error(
248 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
249 E_USER_WARNING);
250 return false;
251 } // end check for themes directory
253 ksort($this->themes);
254 return true;
258 * checks if given theme name is a known theme
260 * @param string $theme name fo theme to check for
262 function checkTheme($theme)
264 if (! array_key_exists($theme, $this->themes)) {
265 return false;
268 return true;
272 * returns HTML selectbox, with or without form enclosed
274 * @param boolean $form whether enclosed by from tags or not
276 function getHtmlSelectBox($form = true)
278 $select_box = '';
280 if ($form) {
281 $select_box .= '<form name="setTheme" method="post" action="index.php"'
282 .' target="_parent">';
283 $select_box .= PMA_generate_common_hidden_inputs();
286 $theme_selected = FALSE;
287 $theme_preview_path= './themes.php';
288 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" onclick="'
289 . "window.open('" . $theme_preview_path . "','themes','left=10,top=20,width=510,height=350,scrollbars=yes,status=yes,resizable=yes');"
290 . '">';
291 $select_box .= $theme_preview_href . __('Theme / Style') . '</a>:' . "\n";
293 $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr"'
294 .' onchange="this.form.submit();" >';
295 foreach ($this->themes as $each_theme_id => $each_theme) {
296 $select_box .= '<option value="' . $each_theme_id . '"';
297 if ($this->active_theme === $each_theme_id) {
298 $select_box .= ' selected="selected"';
300 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
302 $select_box .= '</select>';
304 if ($form) {
305 $select_box .= '<noscript><input type="submit" value="' . __('Go') . '" /></noscript>';
306 $select_box .= '</form>';
309 return $select_box;
313 * enables backward compatibility
315 function makeBc()
317 $GLOBALS['theme'] = $this->theme->getId();
318 $GLOBALS['pmaThemePath'] = $this->theme->getPath();
319 $GLOBALS['pmaThemeImage'] = $this->theme->getImgPath();
322 * load layout file if exists
324 if (@file_exists($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
325 include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
332 * prints out preview for every theme
334 * @uses $this->themes
335 * @uses PMA_Theme::printPreview()
337 function printPreviews()
339 foreach ($this->themes as $each_theme) {
340 $each_theme->printPreview();
341 } // end 'open themes'
345 * returns PMA_Theme object for fall back theme
346 * @return object PMA_Theme
348 function getFallBackTheme()
350 if (isset($this->themes['original'])) {
351 return $this->themes['original'];
354 return false;
358 * prints css data
360 function printCss($type)
362 if ($this->theme->loadCss($type)) {
363 return true;
366 // if loading css for this theme failed, try default theme css
367 $fallback_theme = $this->getFallBackTheme();
368 if ($fallback_theme && $fallback_theme->loadCss($type)) {
369 return true;
372 return false;