Translated using Weblate (Czech)
[phpmyadmin.git] / libraries / classes / ThemeManager.php
blob0ac38fcfee93d0e7bc546ef40108da4a67986ca6
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * phpMyAdmin theme manager
6 * @package PhpMyAdmin
7 */
8 namespace PhpMyAdmin;
10 use PhpMyAdmin\Theme;
11 use PhpMyAdmin\Url;
13 /**
14 * phpMyAdmin theme manager
16 * @package PhpMyAdmin
18 class ThemeManager
20 /**
21 * ThemeManager instance
23 * @access private
24 * @static
25 * @var ThemeManager
27 private static $_instance;
29 /**
30 * @var string path to theme folder
31 * @access protected
33 private $_themes_path = './themes/';
35 /**
36 * @var array available themes
38 var $themes = array();
40 /**
41 * @var string cookie name
43 var $cookie_name = 'pma_theme';
45 /**
46 * @var boolean
48 var $per_server = false;
50 /**
51 * @var string name of active theme
53 var $active_theme = '';
55 /**
56 * @var Theme Theme active theme
58 var $theme = null;
60 /**
61 * @var string
63 var $theme_default;
65 /**
66 * @const string The name of the fallback theme
68 const FALLBACK_THEME = 'pmahomme';
70 /**
71 * Constructor for Theme Manager class
73 * @access public
75 public function __construct()
77 $this->themes = array();
78 $this->theme_default = self::FALLBACK_THEME;
79 $this->active_theme = '';
81 if (! $this->setThemesPath('./themes/')) {
82 return;
85 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
87 $this->loadThemes();
89 $this->theme = new Theme;
91 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
92 trigger_error(
93 sprintf(
94 __('Default theme %s not found!'),
95 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])
97 E_USER_ERROR
99 $GLOBALS['cfg']['ThemeDefault'] = false;
102 $this->theme_default = $GLOBALS['cfg']['ThemeDefault'];
104 // check if user have a theme cookie
105 $cookie_theme = $this->getThemeCookie();
106 if (! $cookie_theme || ! $this->setActiveTheme($cookie_theme)) {
107 if ($GLOBALS['cfg']['ThemeDefault']) {
108 // otherwise use default theme
109 $this->setActiveTheme($this->theme_default);
110 } else {
111 // or fallback theme
112 $this->setActiveTheme(self::FALLBACK_THEME);
118 * Returns the singleton Response object
120 * @return Response object
122 public static function getInstance()
124 if (empty(self::$_instance)) {
125 self::$_instance = new ThemeManager();
127 return self::$_instance;
131 * sets path to folder containing the themes
133 * @param string $path path to themes folder
135 * @access public
136 * @return boolean success
138 public function setThemesPath($path)
140 if (! $this->_checkThemeFolder($path)) {
141 return false;
144 $this->_themes_path = trim($path);
145 return true;
149 * sets if there are different themes per server
151 * @param boolean $per_server Whether to enable per server flag
153 * @access public
154 * @return void
156 public function setThemePerServer($per_server)
158 $this->per_server = (bool) $per_server;
162 * Sets active theme
164 * @param string $theme theme name
166 * @access public
167 * @return bool true on success
169 public function setActiveTheme($theme = null)
171 if (! $this->checkTheme($theme)) {
172 trigger_error(
173 sprintf(
174 __('Theme %s not found!'),
175 htmlspecialchars($theme)
177 E_USER_ERROR
179 return false;
182 $this->active_theme = $theme;
183 $this->theme = $this->themes[$theme];
185 // need to set later
186 //$this->setThemeCookie();
188 return true;
192 * Returns name for storing theme
194 * @return string cookie name
195 * @access public
197 public function getThemeCookieName()
199 // Allow different theme per server
200 if (isset($GLOBALS['server']) && $this->per_server) {
201 return $this->cookie_name . '-' . $GLOBALS['server'];
204 return $this->cookie_name;
208 * returns name of theme stored in the cookie
210 * @return string theme name from cookie
211 * @access public
213 public function getThemeCookie()
215 /** @var Config $PMA_Config */
216 global $PMA_Config;
218 $name = $this->getThemeCookieName();
219 if ($PMA_Config->issetCookie($name)) {
220 return $PMA_Config->getCookie($name);
223 return false;
227 * save theme in cookie
229 * @return bool true
230 * @access public
232 public function setThemeCookie()
234 $GLOBALS['PMA_Config']->setCookie(
235 $this->getThemeCookieName(),
236 $this->theme->id,
237 $this->theme_default
239 // force a change of a dummy session variable to avoid problems
240 // with the caching of phpmyadmin.css.php
241 $GLOBALS['PMA_Config']->set('theme-update', $this->theme->id);
242 return true;
246 * Checks whether folder is valid for storing themes
248 * @param string $folder Folder name to test
250 * @return boolean
251 * @access private
253 private function _checkThemeFolder($folder)
255 if (! is_dir($folder)) {
256 trigger_error(
257 sprintf(
258 __('Theme path not found for theme %s!'),
259 htmlspecialchars($folder)
261 E_USER_ERROR
263 return false;
266 return true;
270 * read all themes
272 * @return bool true
273 * @access public
275 public function loadThemes()
277 $this->themes = array();
279 if (false === ($handleThemes = opendir($this->_themes_path))) {
280 trigger_error(
281 'phpMyAdmin-ERROR: cannot open themes folder: '
282 . $this->_themes_path,
283 E_USER_WARNING
285 return false;
288 // check for themes directory
289 while (false !== ($PMA_Theme = readdir($handleThemes))) {
290 // Skip non dirs, . and ..
291 if ($PMA_Theme == '.'
292 || $PMA_Theme == '..'
293 || ! @is_dir($this->_themes_path . $PMA_Theme)
295 continue;
297 if (array_key_exists($PMA_Theme, $this->themes)) {
298 continue;
300 $new_theme = Theme::load(
301 $this->_themes_path . $PMA_Theme
303 if ($new_theme) {
304 $new_theme->setId($PMA_Theme);
305 $this->themes[$PMA_Theme] = $new_theme;
307 } // end get themes
308 closedir($handleThemes);
310 ksort($this->themes);
311 return true;
315 * checks if given theme name is a known theme
317 * @param string $theme name fo theme to check for
319 * @return bool
320 * @access public
322 public function checkTheme($theme)
324 return array_key_exists($theme, $this->themes);
328 * returns HTML selectbox, with or without form enclosed
330 * @param boolean $form whether enclosed by from tags or not
332 * @return string
333 * @access public
335 public function getHtmlSelectBox($form = true)
337 $select_box = '';
339 if ($form) {
340 $select_box .= '<form name="setTheme" method="post"';
341 $select_box .= ' action="index.php" class="disableAjax">';
342 $select_box .= Url::getHiddenInputs();
345 $theme_preview_path= './themes.php';
346 $theme_preview_href = '<a href="'
347 . $theme_preview_path . '" target="themes" class="themeselect">';
348 $select_box .= $theme_preview_href . __('Theme:') . '</a>' . "\n";
350 $select_box .= '<select name="set_theme" lang="en" dir="ltr"'
351 . ' class="autosubmit">';
352 foreach ($this->themes as $each_theme_id => $each_theme) {
353 $select_box .= '<option value="' . $each_theme_id . '"';
354 if ($this->active_theme === $each_theme_id) {
355 $select_box .= ' selected="selected"';
357 $select_box .= '>' . htmlspecialchars($each_theme->getName())
358 . '</option>';
360 $select_box .= '</select>';
362 if ($form) {
363 $select_box .= '</form>';
366 return $select_box;
370 * Renders the previews for all themes
372 * @return string
373 * @access public
375 public function getPrintPreviews()
377 $retval = '';
378 foreach ($this->themes as $each_theme) {
379 $retval .= $each_theme->getPrintPreview();
380 } // end 'open themes'
381 return $retval;
385 * returns Theme object for fall back theme
387 * @return Theme fall back theme
388 * @access public
390 public function getFallBackTheme()
392 if (isset($this->themes[self::FALLBACK_THEME])) {
393 return $this->themes[self::FALLBACK_THEME];
396 return false;
400 * prints css data
402 * @return bool
403 * @access public
405 public function printCss()
407 if ($this->theme->loadCss()) {
408 return true;
411 // if loading css for this theme failed, try default theme css
412 $fallback_theme = $this->getFallBackTheme();
413 if ($fallback_theme && $fallback_theme->loadCss()) {
414 return true;
417 return false;
421 * Theme initialization
423 * @return void
424 * @access public
426 public static function initializeTheme()
428 $tmanager = self::getInstance();
431 * the theme object
433 * @global Theme $GLOBALS['PMA_Theme']
435 $GLOBALS['PMA_Theme'] = $tmanager->theme;
437 // BC
439 * the theme path
440 * @global string $GLOBALS['pmaThemePath']
442 $GLOBALS['pmaThemePath'] = $GLOBALS['PMA_Theme']->getPath();
444 * the theme image path
445 * @global string $GLOBALS['pmaThemeImage']
447 $GLOBALS['pmaThemeImage'] = $GLOBALS['PMA_Theme']->getImgPath();
450 * load layout file if exists
452 if (@file_exists($GLOBALS['PMA_Theme']->getLayoutFile())) {
453 include $GLOBALS['PMA_Theme']->getLayoutFile();