Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / ThemeManager.php
blobb80d9673efa5d4e4af588157f51ed590bf4d13e5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * phpMyAdmin theme manager
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 use PMA\libraries\URL;
12 /**
13 * phpMyAdmin theme manager
15 * @package PhpMyAdmin
17 class ThemeManager
19 /**
20 * ThemeManager instance
22 * @access private
23 * @static
24 * @var ThemeManager
26 private static $_instance;
28 /**
29 * @var string path to theme folder
30 * @access protected
32 private $_themes_path = './themes/';
34 /**
35 * @var array available themes
37 var $themes = array();
39 /**
40 * @var string cookie name
42 var $cookie_name = 'pma_theme';
44 /**
45 * @var boolean
47 var $per_server = false;
49 /**
50 * @var string name of active theme
52 var $active_theme = '';
54 /**
55 * @var Theme Theme active theme
57 var $theme = null;
59 /**
60 * @var string
62 var $theme_default;
64 /**
65 * @const string The name of the fallback theme
67 const FALLBACK_THEME = 'pmahomme';
69 /**
70 * Constructor for Theme Manager class
72 * @access public
74 public function __construct()
76 $this->init();
79 /**
80 * Returns the singleton Response object
82 * @return Response object
84 public static function getInstance()
86 if (empty(self::$_instance)) {
87 self::$_instance = new ThemeManager();
89 return self::$_instance;
92 /**
93 * sets path to folder containing the themes
95 * @param string $path path to themes folder
97 * @access public
98 * @return boolean success
100 public function setThemesPath($path)
102 if (! $this->_checkThemeFolder($path)) {
103 return false;
106 $this->_themes_path = trim($path);
107 return true;
111 * sets if there are different themes per server
113 * @param boolean $per_server Whether to enable per server flag
115 * @access public
116 * @return void
118 public function setThemePerServer($per_server)
120 $this->per_server = (bool) $per_server;
124 * Initialise the class
126 * @access public
127 * @return void
129 public function init()
131 $this->themes = array();
132 $this->theme_default = self::FALLBACK_THEME;
133 $this->active_theme = '';
135 if (! $this->setThemesPath('./themes/')) {
136 return;
139 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
141 $this->loadThemes();
143 $this->theme = new Theme;
145 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
146 trigger_error(
147 sprintf(
148 __('Default theme %s not found!'),
149 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])
151 E_USER_ERROR
153 $GLOBALS['cfg']['ThemeDefault'] = false;
156 $this->theme_default = $GLOBALS['cfg']['ThemeDefault'];
158 // check if user have a theme cookie
159 if (! $this->getThemeCookie()
160 || ! $this->setActiveTheme($this->getThemeCookie())
162 if ($GLOBALS['cfg']['ThemeDefault']) {
163 // otherwise use default theme
164 $this->setActiveTheme($this->theme_default);
165 } else {
166 // or fallback theme
167 $this->setActiveTheme(self::FALLBACK_THEME);
173 * Checks configuration
175 * @access public
176 * @return void
178 public function checkConfig()
180 if ($this->theme_default != $GLOBALS['cfg']['ThemeDefault']
182 $this->init();
183 } else {
184 // at least the theme path needs to be checked every time for new
185 // themes, as there is no other way at the moment to keep track of
186 // new or removed themes
187 $this->loadThemes();
192 * Sets active theme
194 * @param string $theme theme name
196 * @access public
197 * @return bool true on success
199 public function setActiveTheme($theme = null)
201 if (! $this->checkTheme($theme)) {
202 trigger_error(
203 sprintf(
204 __('Theme %s not found!'),
205 htmlspecialchars($theme)
207 E_USER_ERROR
209 return false;
212 $this->active_theme = $theme;
213 $this->theme = $this->themes[$theme];
215 // need to set later
216 //$this->setThemeCookie();
218 return true;
222 * Returns name for storing theme
224 * @return string cookie name
225 * @access public
227 public function getThemeCookieName()
229 // Allow different theme per server
230 if (isset($GLOBALS['server']) && $this->per_server) {
231 return $this->cookie_name . '-' . $GLOBALS['server'];
232 } else {
233 return $this->cookie_name;
238 * returns name of theme stored in the cookie
240 * @return string theme name from cookie
241 * @access public
243 public function getThemeCookie()
245 if (isset($_COOKIE[$this->getThemeCookieName()])) {
246 return $_COOKIE[$this->getThemeCookieName()];
249 return false;
253 * save theme in cookie
255 * @return bool true
256 * @access public
258 public function setThemeCookie()
260 $GLOBALS['PMA_Config']->setCookie(
261 $this->getThemeCookieName(),
262 $this->theme->id,
263 $this->theme_default
265 // force a change of a dummy session variable to avoid problems
266 // with the caching of phpmyadmin.css.php
267 $GLOBALS['PMA_Config']->set('theme-update', $this->theme->id);
268 return true;
272 * Checks whether folder is valid for storing themes
274 * @param string $folder Folder name to test
276 * @return boolean
277 * @access private
279 private function _checkThemeFolder($folder)
281 if (! is_dir($folder)) {
282 trigger_error(
283 sprintf(
284 __('Theme path not found for theme %s!'),
285 htmlspecialchars($folder)
287 E_USER_ERROR
289 return false;
292 return true;
296 * read all themes
298 * @return bool true
299 * @access public
301 public function loadThemes()
303 $this->themes = array();
305 if (false === ($handleThemes = opendir($this->_themes_path))) {
306 trigger_error(
307 'phpMyAdmin-ERROR: cannot open themes folder: '
308 . $this->_themes_path,
309 E_USER_WARNING
311 return false;
314 // check for themes directory
315 while (false !== ($PMA_Theme = readdir($handleThemes))) {
316 // Skip non dirs, . and ..
317 if ($PMA_Theme == '.'
318 || $PMA_Theme == '..'
319 || ! is_dir($this->_themes_path . $PMA_Theme)
321 continue;
323 if (array_key_exists($PMA_Theme, $this->themes)) {
324 continue;
326 $new_theme = Theme::load(
327 $this->_themes_path . $PMA_Theme
329 if ($new_theme) {
330 $new_theme->setId($PMA_Theme);
331 $this->themes[$PMA_Theme] = $new_theme;
333 } // end get themes
334 closedir($handleThemes);
336 ksort($this->themes);
337 return true;
341 * checks if given theme name is a known theme
343 * @param string $theme name fo theme to check for
345 * @return bool
346 * @access public
348 public function checkTheme($theme)
350 if (! array_key_exists($theme, $this->themes)) {
351 return false;
354 return true;
358 * returns HTML selectbox, with or without form enclosed
360 * @param boolean $form whether enclosed by from tags or not
362 * @return string
363 * @access public
365 public function getHtmlSelectBox($form = true)
367 $select_box = '';
369 if ($form) {
370 $select_box .= '<form name="setTheme" method="get"';
371 $select_box .= ' action="index.php" class="disableAjax">';
372 $select_box .= URL::getHiddenInputs();
375 $theme_preview_path= './themes.php';
376 $theme_preview_href = '<a href="'
377 . $theme_preview_path . '" target="themes" class="themeselect">';
378 $select_box .= $theme_preview_href . __('Theme:') . '</a>' . "\n";
380 $select_box .= '<select name="set_theme" lang="en" dir="ltr"'
381 . ' class="autosubmit">';
382 foreach ($this->themes as $each_theme_id => $each_theme) {
383 $select_box .= '<option value="' . $each_theme_id . '"';
384 if ($this->active_theme === $each_theme_id) {
385 $select_box .= ' selected="selected"';
387 $select_box .= '>' . htmlspecialchars($each_theme->getName())
388 . '</option>';
390 $select_box .= '</select>';
392 if ($form) {
393 $select_box .= '</form>';
396 return $select_box;
400 * enables backward compatibility
402 * @return void
403 * @access public
405 public function makeBc()
407 $GLOBALS['theme'] = $this->theme->getId();
408 $GLOBALS['pmaThemePath'] = $this->theme->getPath();
409 $GLOBALS['pmaThemeImage'] = $this->theme->getImgPath();
412 * load layout file if exists
414 if (file_exists($this->theme->getLayoutFile())) {
415 include $this->theme->getLayoutFile();
420 * Renders the previews for all themes
422 * @return string
423 * @access public
425 public function getPrintPreviews()
427 $retval = '';
428 foreach ($this->themes as $each_theme) {
429 $retval .= $each_theme->getPrintPreview();
430 } // end 'open themes'
431 return $retval;
435 * returns Theme object for fall back theme
437 * @return Theme fall back theme
438 * @access public
440 public function getFallBackTheme()
442 if (isset($this->themes[self::FALLBACK_THEME])) {
443 return $this->themes[self::FALLBACK_THEME];
446 return false;
450 * prints css data
452 * @return bool
453 * @access public
455 public function printCss()
457 if ($this->theme->loadCss()) {
458 return true;
461 // if loading css for this theme failed, try default theme css
462 $fallback_theme = $this->getFallBackTheme();
463 if ($fallback_theme && $fallback_theme->loadCss()) {
464 return true;
467 return false;
471 * Theme initialization
473 * @return void
474 * @access public
476 public static function initializeTheme()
478 $tmanager = self::getInstance();
480 // for the theme per server feature
481 if (isset($_REQUEST['server']) && ! isset($_REQUEST['set_theme'])) {
482 $GLOBALS['server'] = $_REQUEST['server'];
483 $tmp = $tmanager->getThemeCookie();
484 if (empty($tmp)) {
485 $tmp = $tmanager->theme_default;
487 $tmanager->setActiveTheme($tmp);
490 * @todo move into ThemeManager::__wakeup()
492 if (isset($_REQUEST['set_theme'])) {
493 // if user selected a theme
494 $tmanager->setActiveTheme($_REQUEST['set_theme']);
498 * the theme object
500 * @global Theme $_SESSION['PMA_Theme']
502 $_SESSION['PMA_Theme'] = $tmanager->theme;
504 // BC
506 * the active theme
507 * @global string $GLOBALS['theme']
509 $GLOBALS['theme'] = $_SESSION['PMA_Theme']->getName();
511 * the theme path
512 * @global string $GLOBALS['pmaThemePath']
514 $GLOBALS['pmaThemePath'] = $_SESSION['PMA_Theme']->getPath();
516 * the theme image path
517 * @global string $GLOBALS['pmaThemeImage']
519 $GLOBALS['pmaThemeImage'] = $_SESSION['PMA_Theme']->getImgPath();
522 * load layout file if exists
524 if (@file_exists($_SESSION['PMA_Theme']->getLayoutFile())) {
525 include $_SESSION['PMA_Theme']->getLayoutFile();