Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / Theme_Manager.class.php
blobcd784977d747c3a8efb55244b21479e24642b6fa
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * phpMyAdmin theme manager
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * phpMyAdmin theme manager
15 * @package PhpMyAdmin
17 class PMA_Theme_Manager
19 /**
20 * @var string path to theme folder
21 * @access protected
23 private $_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 PMA_Theme PMA_Theme active theme
48 var $theme = null;
50 /**
51 * @var string
53 var $theme_default;
55 /**
56 * @const string The name of the fallback theme
58 const FALLBACK_THEME = 'pmahomme';
60 /**
61 * Constructor for Theme Manager class
63 * @access public
65 public function __construct()
67 $this->init();
70 /**
71 * sets path to folder containing the themes
73 * @param string $path path to themes folder
75 * @access public
76 * @return boolean success
78 public function setThemesPath($path)
80 if (! $this->_checkThemeFolder($path)) {
81 return false;
84 $this->_themes_path = trim($path);
85 return true;
88 /**
89 * Returns path to folder containing themes
91 * @access public
92 * @return string theme path
94 public function getThemesPath()
96 return $this->_themes_path;
99 /**
100 * sets if there are different themes per server
102 * @param boolean $per_server Whether to enable per server flag
104 * @access public
105 * @return void
107 public function setThemePerServer($per_server)
109 $this->per_server = (bool) $per_server;
113 * Initialise the class
115 * @access public
116 * @return void
118 public function init()
120 $this->themes = array();
121 $this->theme_default = self::FALLBACK_THEME;
122 $this->active_theme = '';
124 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
125 return;
128 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
130 $this->loadThemes();
132 $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;
212 * Returns name for storing theme
214 * @return string cookie name
215 * @access public
217 public function getThemeCookieName()
219 // Allow different theme per server
220 if (isset($GLOBALS['server']) && $this->per_server) {
221 return $this->cookie_name . '-' . $GLOBALS['server'];
222 } else {
223 return $this->cookie_name;
228 * returns name of theme stored in the cookie
230 * @return string theme name from cookie
231 * @access public
233 public function getThemeCookie()
235 if (isset($_COOKIE[$this->getThemeCookieName()])) {
236 return $_COOKIE[$this->getThemeCookieName()];
239 return false;
243 * save theme in cookie
245 * @return bool true
246 * @access public
248 public function setThemeCookie()
250 $GLOBALS['PMA_Config']->setCookie(
251 $this->getThemeCookieName(),
252 $this->theme->id,
253 $this->theme_default
255 // force a change of a dummy session variable to avoid problems
256 // with the caching of phpmyadmin.css.php
257 $GLOBALS['PMA_Config']->set('theme-update', $this->theme->id);
258 return true;
262 * Checks whether folder is valid for storing themes
264 * @param string $folder Folder name to test
266 * @return boolean
267 * @access private
269 private function _checkThemeFolder($folder)
271 if (! is_dir($folder)) {
272 trigger_error(
273 sprintf(
274 __('Theme path not found for theme %s!'),
275 htmlspecialchars($folder)
277 E_USER_ERROR
279 return false;
282 return true;
286 * read all themes
288 * @return bool true
289 * @access public
291 public function loadThemes()
293 $this->themes = array();
295 if (false === ($handleThemes = opendir($this->getThemesPath()))) {
296 trigger_error(
297 'phpMyAdmin-ERROR: cannot open themes folder: '
298 . $this->getThemesPath(),
299 E_USER_WARNING
301 return false;
304 // check for themes directory
305 while (false !== ($PMA_Theme = readdir($handleThemes))) {
306 // Skip non dirs, . and ..
307 if ($PMA_Theme == '.'
308 || $PMA_Theme == '..'
309 || ! is_dir($this->getThemesPath() . '/' . $PMA_Theme)
311 continue;
313 if (array_key_exists($PMA_Theme, $this->themes)) {
314 continue;
316 $new_theme = PMA_Theme::load(
317 $this->getThemesPath() . '/' . $PMA_Theme
319 if ($new_theme) {
320 $new_theme->setId($PMA_Theme);
321 $this->themes[$PMA_Theme] = $new_theme;
323 } // end get themes
324 closedir($handleThemes);
326 ksort($this->themes);
327 return true;
331 * checks if given theme name is a known theme
333 * @param string $theme name fo theme to check for
335 * @return bool
336 * @access public
338 public function checkTheme($theme)
340 if (! array_key_exists($theme, $this->themes)) {
341 return false;
344 return true;
348 * returns HTML selectbox, with or without form enclosed
350 * @param boolean $form whether enclosed by from tags or not
352 * @return string
353 * @access public
355 public function getHtmlSelectBox($form = true)
357 $select_box = '';
359 if ($form) {
360 $select_box .= '<form name="setTheme" method="get"';
361 $select_box .= ' action="index.php" class="disableAjax">';
362 $select_box .= PMA_URL_getHiddenInputs();
365 $theme_preview_path= './themes.php';
366 $theme_preview_href = '<a href="'
367 . $theme_preview_path . '" target="themes" class="themeselect">';
368 $select_box .= $theme_preview_href . __('Theme:') . '</a>' . "\n";
370 $select_box .= '<select name="set_theme" lang="en" dir="ltr"'
371 . ' class="autosubmit">';
372 foreach ($this->themes as $each_theme_id => $each_theme) {
373 $select_box .= '<option value="' . $each_theme_id . '"';
374 if ($this->active_theme === $each_theme_id) {
375 $select_box .= ' selected="selected"';
377 $select_box .= '>' . htmlspecialchars($each_theme->getName())
378 . '</option>';
380 $select_box .= '</select>';
382 if ($form) {
383 $select_box .= '</form>';
386 return $select_box;
390 * enables backward compatibility
392 * @return void
393 * @access public
395 public function makeBc()
397 $GLOBALS['theme'] = $this->theme->getId();
398 $GLOBALS['pmaThemePath'] = $this->theme->getPath();
399 $GLOBALS['pmaThemeImage'] = $this->theme->getImgPath();
402 * load layout file if exists
404 if (file_exists($this->theme->getLayoutFile())) {
405 include $this->theme->getLayoutFile();
410 * Renders the previews for all themes
412 * @return string
413 * @access public
415 public function getPrintPreviews()
417 $retval = '';
418 foreach ($this->themes as $each_theme) {
419 $retval .= $each_theme->getPrintPreview();
420 } // end 'open themes'
421 return $retval;
425 * returns PMA_Theme object for fall back theme
427 * @return PMA_Theme fall back theme
428 * @access public
430 public function getFallBackTheme()
432 if (isset($this->themes[self::FALLBACK_THEME])) {
433 return $this->themes[self::FALLBACK_THEME];
436 return false;
440 * prints css data
442 * @return bool
443 * @access public
445 public function printCss()
447 if ($this->theme->loadCss()) {
448 return true;
451 // if loading css for this theme failed, try default theme css
452 $fallback_theme = $this->getFallBackTheme();
453 if ($fallback_theme && $fallback_theme->loadCss()) {
454 return true;
457 return false;