Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / Theme.php
blobfad3820e9089b28f68a3a562da33c63e44bd1afb
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * hold Theme class
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 use PMA\libraries\URL;
12 /**
13 * handles theme
15 * @todo add the possibility to make a theme depend on another theme
16 * and by default on original
17 * @todo make all components optional - get missing components from 'parent' theme
19 * @package PhpMyAdmin
21 class Theme
23 /**
24 * @var string theme version
25 * @access protected
27 var $version = '0.0.0.0';
29 /**
30 * @var string theme name
31 * @access protected
33 var $name = '';
35 /**
36 * @var string theme id
37 * @access protected
39 var $id = '';
41 /**
42 * @var string theme path
43 * @access protected
45 var $path = '';
47 /**
48 * @var string image path
49 * @access protected
51 var $img_path = '';
53 /**
54 * @var integer last modification time for info file
55 * @access protected
57 var $mtime_info = 0;
59 /**
60 * needed because sometimes, the mtime for different themes
61 * is identical
62 * @var integer filesize for info file
63 * @access protected
65 var $filesize_info = 0;
67 /**
68 * @var array List of css files to load
69 * @access private
71 private $_cssFiles = array(
72 'common',
73 'enum_editor',
74 'gis',
75 'navigation',
76 'pmd',
77 'rte',
78 'codemirror',
79 'jqplot',
80 'resizable-menu'
83 /**
84 * Loads theme information
86 * @return boolean whether loading them info was successful or not
87 * @access public
89 function loadInfo()
91 if (! file_exists($this->getPath() . '/info.inc.php')) {
92 return false;
95 if ($this->mtime_info === filemtime($this->getPath() . '/info.inc.php')) {
96 return true;
99 @include $this->getPath() . '/info.inc.php';
101 // was it set correctly?
102 if (! isset($theme_name)) {
103 return false;
106 $this->mtime_info = filemtime($this->getPath() . '/info.inc.php');
107 $this->filesize_info = filesize($this->getPath() . '/info.inc.php');
109 if (isset($theme_full_version)) {
110 $this->setVersion($theme_full_version);
111 } elseif (isset($theme_generation, $theme_version)) {
112 $this->setVersion($theme_generation . '.' . $theme_version);
114 $this->setName($theme_name);
116 return true;
120 * returns theme object loaded from given folder
121 * or false if theme is invalid
123 * @param string $folder path to theme
125 * @return Theme|false
126 * @static
127 * @access public
129 static public function load($folder)
131 $theme = new Theme();
133 $theme->setPath($folder);
135 if (! $theme->loadInfo()) {
136 return false;
139 $theme->checkImgPath();
141 return $theme;
145 * checks image path for existence - if not found use img from fallback theme
147 * @access public
148 * @return bool
150 public function checkImgPath()
152 // try current theme first
153 if (is_dir($this->getPath() . '/img/')) {
154 $this->setImgPath($this->getPath() . '/img/');
155 return true;
158 // try fallback theme
159 $fallback = './themes/' . ThemeManager::FALLBACK_THEME . '/img/';
160 if (is_dir($fallback)) {
161 $this->setImgPath($fallback);
162 return true;
165 // we failed
166 trigger_error(
167 sprintf(
168 __('No valid image path for theme %s found!'),
169 $this->getName()
171 E_USER_ERROR
173 return false;
177 * returns path to theme
179 * @access public
180 * @return string path to theme
182 public function getPath()
184 return $this->path;
188 * returns layout file
190 * @access public
191 * @return string layout file
193 public function getLayoutFile()
195 return $this->getPath() . '/layout.inc.php';
199 * set path to theme
201 * @param string $path path to theme
203 * @return void
204 * @access public
206 public function setPath($path)
208 $this->path = trim($path);
212 * sets version
214 * @param string $version version to set
216 * @return void
217 * @access public
219 public function setVersion($version)
221 $this->version = trim($version);
225 * returns version
227 * @return string version
228 * @access public
230 public function getVersion()
232 return $this->version;
236 * checks theme version against $version
237 * returns true if theme version is equal or higher to $version
239 * @param string $version version to compare to
241 * @return boolean true if theme version is equal or higher to $version
242 * @access public
244 public function checkVersion($version)
246 return version_compare($this->getVersion(), $version, 'lt');
250 * sets name
252 * @param string $name name to set
254 * @return void
255 * @access public
257 public function setName($name)
259 $this->name = trim($name);
263 * returns name
265 * @access public
266 * @return string name
268 public function getName()
270 return $this->name;
274 * sets id
276 * @param string $id new id
278 * @return void
279 * @access public
281 public function setId($id)
283 $this->id = trim($id);
287 * returns id
289 * @return string id
290 * @access public
292 public function getId()
294 return $this->id;
298 * Sets path to images for the theme
300 * @param string $path path to images for this theme
302 * @return void
303 * @access public
305 public function setImgPath($path)
307 $this->img_path = $path;
311 * Returns the path to image for the theme.
312 * If filename is given, it possibly fallbacks to fallback
313 * theme for it if image does not exist.
315 * @param string $file file name for image
317 * @access public
318 * @return string image path for this theme
320 public function getImgPath($file = null)
322 if (is_null($file)) {
323 return $this->img_path;
326 if (is_readable($this->img_path . $file)) {
327 return $this->img_path . $file;
330 return './themes/' . ThemeManager::FALLBACK_THEME . '/img/' . $file;
334 * load css (send to stdout, normally the browser)
336 * @return bool
337 * @access public
339 public function loadCss()
341 $success = true;
343 if ($GLOBALS['text_dir'] === 'ltr') {
344 $right = 'right';
345 $left = 'left';
346 } else {
347 $right = 'left';
348 $left = 'right';
351 foreach ($this->_cssFiles as $file) {
352 $path = $this->getPath() . "/css/$file.css.php";
353 $fallback = "./themes/"
354 . ThemeManager::FALLBACK_THEME . "/css/$file.css.php";
356 if (is_readable($path)) {
357 echo "\n/* FILE: " , $file , ".css.php */\n";
358 include $path;
359 } else if (is_readable($fallback)) {
360 echo "\n/* FILE: " , $file , ".css.php */\n";
361 include $fallback;
362 } else {
363 $success = false;
367 $sprites = $this->getSpriteData();
368 /* Check if there is a valid data file for sprites */
369 if (count($sprites) > 0) {
371 $bg = $this->getImgPath() . 'sprites.png?v=' . urlencode(PMA_VERSION);
373 /* Icon sprites */
374 .icon {
375 margin: 0;
376 margin-<?php echo $left; ?>: .3em;
377 padding: 0 !important;
378 width: 16px;
379 height: 16px;
380 background-image: url('<?php echo $bg; ?>') !important;
381 background-repeat: no-repeat !important;
382 background-position: top left !important;
384 <?php
386 $template = ".ic_%s { background-position: 0 -%upx !important;%s%s }\n";
387 foreach ($sprites as $name => $data) {
388 // generate the CSS code for each icon
389 $width = '';
390 $height = '';
391 // if either the height or width of an icon is 16px,
392 // then it's pointless to set this as a parameter,
393 //since it will be inherited from the "icon" class
394 if ($data['width'] != 16) {
395 $width = " width: " . $data['width'] . "px;";
397 if ($data['height'] != 16) {
398 $height = " height: " . $data['height'] . "px;";
400 printf(
401 $template,
402 $name,
403 ($data['position'] * 16),
404 $width,
405 $height
410 return $success;
414 * Loads sprites data
416 * @return array with sprites
418 public function getSpriteData()
420 $sprites = array();
421 $filename = $this->getPath() . '/sprites.lib.php';
422 if (is_readable($filename)) {
424 // This defines sprites array
425 include $filename;
427 // Backwards compatibility for themes from 4.6 and older
428 if (function_exists('PMA_sprites')) {
429 $sprites = PMA_sprites();
432 return $sprites;
436 * Renders the preview for this theme
438 * @return string
439 * @access public
441 public function getPrintPreview()
443 $url_params = array('set_theme' => $this->getId());
444 $url = 'index.php' . URL::getCommon($url_params);
446 $retval = '<div class="theme_preview">';
447 $retval .= '<h2>';
448 $retval .= htmlspecialchars($this->getName());
449 $retval .= ' (' . htmlspecialchars($this->getVersion()) . ') ';
450 $retval .= '</h2>';
451 $retval .= '<p>';
452 $retval .= '<a class="take_theme" ';
453 $retval .= 'name="' . htmlspecialchars($this->getId()) . '" ';
454 $retval .= 'href="' . $url . '">';
455 if (@file_exists($this->getPath() . '/screen.png')) {
456 // if screen exists then output
457 $retval .= '<img src="' . $this->getPath() . '/screen.png" border="1"';
458 $retval .= ' alt="' . htmlspecialchars($this->getName()) . '"';
459 $retval .= ' title="' . htmlspecialchars($this->getName()) . '" />';
460 $retval .= '<br />';
461 } else {
462 $retval .= __('No preview available.');
464 $retval .= '[ <strong>' . __('take it') . '</strong> ]';
465 $retval .= '</a>';
466 $retval .= '</p>';
467 $retval .= '</div>';
468 return $retval;
472 * Gets currently configured font size.
474 * @return String with font size.
476 function getFontSize()
478 $fs = $GLOBALS['PMA_Config']->get('fontsize');
479 if (!is_null($fs)) {
480 return $fs;
482 if (isset($_COOKIE['pma_fontsize'])) {
483 return htmlspecialchars($_COOKIE['pma_fontsize']);
485 return '82%';
489 * Generates code for CSS gradient using various browser extensions.
491 * @param string $start_color Color of gradient start, hex value without #
492 * @param string $end_color Color of gradient end, hex value without #
494 * @return string CSS code.
496 function getCssGradient($start_color, $end_color)
498 $result = array();
499 // Opera 9.5+, IE 9
500 $result[] = 'background-image: url(./themes/svg_gradient.php?from='
501 . $start_color . '&to=' . $end_color . ');';
502 $result[] = 'background-size: 100% 100%;';
503 // Safari 4-5, Chrome 1-9
504 $result[] = 'background: '
505 . '-webkit-gradient(linear, left top, left bottom, from(#'
506 . $start_color . '), to(#' . $end_color . '));';
507 // Safari 5.1, Chrome 10+
508 $result[] = 'background: -webkit-linear-gradient(top, #'
509 . $start_color . ', #' . $end_color . ');';
510 // Firefox 3.6+
511 $result[] = 'background: -moz-linear-gradient(top, #'
512 . $start_color . ', #' . $end_color . ');';
513 // IE 10
514 $result[] = 'background: -ms-linear-gradient(top, #'
515 . $start_color . ', #' . $end_color . ');';
516 // Opera 11.10
517 $result[] = 'background: -o-linear-gradient(top, #'
518 . $start_color . ', #' . $end_color . ');';
519 return implode("\n", $result);