Translation update done using Pootle.
[phpmyadmin/crack.git] / libraries / Theme_Manager.class.php
blobc5298f378beaafc4e48c5a82ef170a47c594890b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package phpMyAdmin
6 */
8 /**
11 require_once './libraries/Theme.class.php';
13 /**
15 * @package phpMyAdmin
17 class PMA_Theme_Manager
19 /**
20 * @var string path to theme folder
21 * @access protected
23 var $_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 object PMA_Theme active theme
48 var $theme = null;
50 /**
51 * @var string
53 var $theme_default = 'original';
55 function __construct()
57 $this->init();
60 /**
61 * sets path to folder containing the themes
63 * @param string $path path to themes folder
64 * @return boolean success
66 function setThemesPath($path)
68 if (! $this->_checkThemeFolder($path)) {
69 return false;
72 $this->_themes_path = trim($path);
73 return true;
76 /**
77 * @public
78 * @return string
80 function getThemesPath()
82 return $this->_themes_path;
85 /**
86 * sets if there are different themes per server
88 * @param boolean $per_server
90 function setThemePerServer($per_server)
92 $this->per_server = (bool) $per_server;
95 function init()
97 $this->themes = array();
98 $this->theme_default = 'original';
99 $this->active_theme = '';
101 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
102 return false;
105 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
107 $this->loadThemes();
109 $this->theme = new PMA_Theme;
112 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
113 trigger_error(
114 sprintf(__('Default theme %s not found!'),
115 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])),
116 E_USER_ERROR);
117 $GLOBALS['cfg']['ThemeDefault'] = false;
120 $this->theme_default = $GLOBALS['cfg']['ThemeDefault'];
122 // check if user have a theme cookie
123 if (! $this->getThemeCookie()
124 || ! $this->setActiveTheme($this->getThemeCookie())) {
125 // otherwise use default theme
126 if ($GLOBALS['cfg']['ThemeDefault']) {
127 $this->setActiveTheme($GLOBALS['cfg']['ThemeDefault']);
128 } else {
129 // or original theme
130 $this->setActiveTheme('original');
135 function checkConfig()
137 if ($this->_themes_path != trim($GLOBALS['cfg']['ThemePath'])
138 || $this->theme_default != $GLOBALS['cfg']['ThemeDefault']) {
139 $this->init();
140 } else {
141 // at least the theme path needs to be checked every time for new
142 // themes, as there is no other way at the moment to keep track of
143 // new or removed themes
144 $this->loadThemes();
148 function setActiveTheme($theme = null)
150 if (! $this->checkTheme($theme)) {
151 trigger_error(
152 sprintf(__('Theme %s not found!'), htmlspecialchars($theme)),
153 E_USER_ERROR);
154 return false;
157 $this->active_theme = $theme;
158 $this->theme = $this->themes[$theme];
160 // need to set later
161 //$this->setThemeCookie();
163 return true;
167 * @return string cookie name
169 function getThemeCookieName()
171 // Allow different theme per server
172 if (isset($GLOBALS['server']) && $this->per_server) {
173 return $this->cookie_name . '-' . $GLOBALS['server'];
174 } else {
175 return $this->cookie_name;
180 * returns name of theme stored in the cookie
181 * @return string theme name from cookie
183 function getThemeCookie()
185 if (isset($_COOKIE[$this->getThemeCookieName()])) {
186 return $_COOKIE[$this->getThemeCookieName()];
189 return false;
193 * save theme in cookie
195 * @uses $GLOBALS['PMA_Config']->setCookie();
196 * @uses PMA_Theme_Manager::getThemeCookieName()
197 * @uses PMA_Theme_Manager::$theme
198 * @uses PMA_Theme_Manager::$theme_default
199 * @uses PMA_Theme::getId()
201 function setThemeCookie()
203 $GLOBALS['PMA_Config']->setCookie($this->getThemeCookieName(), $this->theme->id,
204 $this->theme_default);
205 // force a change of a dummy session variable to avoid problems
206 // with the caching of phpmyadmin.css.php
207 $GLOBALS['PMA_Config']->set('theme-update', $this->theme->id);
208 return true;
212 * @private
213 * @param string $folder
214 * @return boolean
216 /*private*/ function _checkThemeFolder($folder)
218 if (! is_dir($folder)) {
219 trigger_error(
220 sprintf(__('Theme path not found for theme %s!'),
221 htmlspecialchars($folder)),
222 E_USER_ERROR);
223 return false;
226 return true;
230 * read all themes
232 function loadThemes()
234 $this->themes = array();
236 if ($handleThemes = opendir($this->getThemesPath())) {
237 // check for themes directory
238 while (false !== ($PMA_Theme = readdir($handleThemes))) {
239 if (array_key_exists($PMA_Theme, $this->themes)) {
240 // this does nothing!
241 //$this->themes[$PMA_Theme] = $this->themes[$PMA_Theme];
242 continue;
244 $new_theme = PMA_Theme::load($this->getThemesPath() . '/' . $PMA_Theme);
245 if ($new_theme) {
246 $new_theme->setId($PMA_Theme);
247 $this->themes[$PMA_Theme] = $new_theme;
249 } // end get themes
250 closedir($handleThemes);
251 } else {
252 trigger_error(
253 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
254 E_USER_WARNING);
255 return false;
256 } // end check for themes directory
258 ksort($this->themes);
259 return true;
263 * checks if given theme name is a known theme
265 * @param string $theme name fo theme to check for
267 function checkTheme($theme)
269 if (! array_key_exists($theme, $this->themes)) {
270 return false;
273 return true;
277 * returns HTML selectbox, with or without form enclosed
279 * @param boolean $form whether enclosed by from tags or not
281 function getHtmlSelectBox($form = true)
283 $select_box = '';
285 if ($form) {
286 $select_box .= '<form name="setTheme" method="post" action="index.php"'
287 .' target="_parent">';
288 $select_box .= PMA_generate_common_hidden_inputs();
291 $theme_selected = FALSE;
292 $theme_preview_path= './themes.php';
293 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" onclick="'
294 . "window.open('" . $theme_preview_path . "','themes','left=10,top=20,width=510,height=350,scrollbars=yes,status=yes,resizable=yes');"
295 . '">';
296 $select_box .= $theme_preview_href . __('Theme / Style') . '</a>:' . "\n";
298 $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr"'
299 .' onchange="this.form.submit();" >';
300 foreach ($this->themes as $each_theme_id => $each_theme) {
301 $select_box .= '<option value="' . $each_theme_id . '"';
302 if ($this->active_theme === $each_theme_id) {
303 $select_box .= ' selected="selected"';
305 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
307 $select_box .= '</select>';
309 if ($form) {
310 $select_box .= '<noscript><input type="submit" value="' . __('Go') . '" /></noscript>';
311 $select_box .= '</form>';
314 return $select_box;
318 * enables backward compatibility
320 function makeBc()
322 $GLOBALS['theme'] = $this->theme->getId();
323 $GLOBALS['pmaThemePath'] = $this->theme->getPath();
324 $GLOBALS['pmaThemeImage'] = $this->theme->getImgPath();
327 * load layout file if exists
329 if (@file_exists($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
330 include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
337 * prints out preview for every theme
339 * @uses $this->themes
340 * @uses PMA_Theme::printPreview()
342 function printPreviews()
344 foreach ($this->themes as $each_theme) {
345 $each_theme->printPreview();
346 } // end 'open themes'
350 * returns PMA_Theme object for fall back theme
351 * @return object PMA_Theme
353 function getFallBackTheme()
355 if (isset($this->themes['original'])) {
356 return $this->themes['original'];
359 return false;
363 * prints css data
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;