Merge branch 'QA_3_4'
[phpmyadmin-regexreplace.git] / libraries / Theme.class.php
blob09f46e2853e973d0564c0e2172e29b45bb75ebae
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * hold PMA_Theme class
6 * @package phpMyAdmin
7 */
9 /**
10 * handles theme
12 * @todo add the possibility to make a theme depend on another theme and by default on original
13 * @todo make all components optional - get missing components from 'parent' theme
14 * @todo make css optionally replacing 'parent' css or extending it (by appending at the end)
15 * @todo add an optional global css file - which will be used for both frames
17 * @package phpMyAdmin
19 class PMA_Theme {
20 /**
21 * @var string theme version
22 * @access protected
24 var $version = '0.0.0.0';
26 /**
27 * @var string theme name
28 * @access protected
30 var $name = '';
32 /**
33 * @var string theme id
34 * @access protected
36 var $id = '';
38 /**
39 * @var string theme path
40 * @access protected
42 var $path = '';
44 /**
45 * @var string image path
46 * @access protected
48 var $img_path = '';
50 /**
51 * @var array valid css types
52 * @access protected
54 var $types = array('left', 'right', 'print');
56 /**
57 * @var integer last modification time for info file
58 * @access protected
60 var $mtime_info = 0;
62 /**
63 * needed because sometimes, the mtime for different themes
64 * is identical
65 * @var integer filesize for info file
66 * @access protected
68 var $filesize_info = 0;
70 /**
71 * @access public
72 * @uses PMA_Theme::getPath()
73 * @uses PMA_Theme::$mtime_info
74 * @uses PMA_Theme::setVersion()
75 * @uses PMA_Theme::setName()
76 * @uses filemtime()
77 * @uses filesize()
78 * @uses file_exists()
79 * @return boolean whether loading them info was successful or not
81 function loadInfo()
83 if (! file_exists($this->getPath() . '/info.inc.php')) {
84 return false;
87 if ($this->mtime_info === filemtime($this->getPath() . '/info.inc.php')) {
88 return true;
91 @include $this->getPath() . '/info.inc.php';
93 // was it set correctly?
94 if (! isset($theme_name)) {
95 return false;
98 $this->mtime_info = filemtime($this->getPath() . '/info.inc.php');
99 $this->filesize_info = filesize($this->getPath() . '/info.inc.php');
101 if (isset($theme_full_version)) {
102 $this->setVersion($theme_full_version);
103 } elseif (isset($theme_generation, $theme_version)) {
104 $this->setVersion($theme_generation . '.' . $theme_version);
106 $this->setName($theme_name);
108 return true;
112 * returns theme object loaded from given folder
113 * or false if theme is invalid
115 * @static
116 * @access public
117 * @uses PMA_Theme
118 * @uses PMA_Theme::setPath()
119 * @uses PMA_Theme::loadInfo()
120 * @uses PMA_Theme::checkImgPath()
121 * @param string $folder path to theme
122 * @return object PMA_Theme
124 static public function load($folder)
126 $theme = new PMA_Theme();
128 $theme->setPath($folder);
130 if (! $theme->loadInfo()) {
131 return false;
134 $theme->checkImgPath();
136 return $theme;
140 * checks image path for existance - if not found use img from original theme
142 * @access public
143 * @uses PMA_Theme::getPath()
144 * @uses PMA_Theme::setImgPath()
145 * @uses PMA_Theme::getName()
146 * @uses $GLOBALS['cfg']['ThemePath']
147 * @uses is_dir()
148 * @uses sprintf()
150 function checkImgPath()
152 if (is_dir($this->getPath() . '/img/')) {
153 $this->setImgPath($this->getPath() . '/img/');
154 return true;
155 } elseif (is_dir($GLOBALS['cfg']['ThemePath'] . '/original/img/')) {
156 $this->setImgPath($GLOBALS['cfg']['ThemePath'] . '/original/img/');
157 return true;
158 } else {
159 trigger_error(
160 sprintf(__('No valid image path for theme %s found!'), $this->getName()),
161 E_USER_ERROR);
162 return false;
167 * returns path to theme
169 * @access public
170 * @uses PMA_Theme::$path as return value
171 * @return string $path path to theme
173 function getPath()
175 return $this->path;
179 * returns layout file
181 * @access public
182 * @uses PMA_Theme::getPath()
183 * @return string layout file
185 function getLayoutFile()
187 return $this->getPath() . '/layout.inc.php';
191 * set path to theme
193 * @access public
194 * @uses PMA_Theme::$path to set it
195 * @param string $path path to theme
197 function setPath($path)
199 $this->path = trim($path);
203 * sets version
205 * @access public
206 * @uses PMA_Theme::$version
207 * @param string new version
209 function setVersion($version)
211 $this->version = trim($version);
215 * returns version
217 * @access public
218 * @uses PMA_Theme::$version
219 * @return string version
221 function getVersion()
223 return $this->version;
227 * checks theme version agaisnt $version
228 * returns true if theme version is equal or higher to $version
230 * @access public
231 * @uses version_compare()
232 * @uses PMA_Theme::getVersion()
233 * @param string $version version to compare to
234 * @return boolean
236 function checkVersion($version)
238 return version_compare($this->getVersion(), $version, 'lt');
242 * sets name
244 * @access public
245 * @uses PMA_Theme::$name to set it
246 * @uses trim()
247 * @param string $name new name
249 function setName($name)
251 $this->name = trim($name);
255 * returns name
257 * @access public
258 * @uses PMA_Theme::$name as return value
259 * @return string name
261 function getName()
263 return $this->name;
267 * sets id
269 * @access public
270 * @uses PMA_Theme::$id to set it
271 * @param string $id new id
273 function setId($id)
275 $this->id = trim($id);
279 * returns id
281 * @access public
282 * @uses PMA_Theme::$id as return value
283 * @return string id
285 function getId()
287 return $this->id;
291 * @access public
292 * @uses PMA_Theme::$img_path to set it
293 * @param string path to images for this theme
295 function setImgPath($path)
297 $this->img_path = $path;
301 * @access public
302 * @uses PMA_Theme::$img_path as retunr value
303 * @return string image path for this theme
305 function getImgPath()
307 return $this->img_path;
311 * load css (send to stdout, normally the browser)
313 * @access public
314 * @uses PMA_Theme::getPath()
315 * @uses PMA_Theme::$types
316 * @uses PMA_SQP_buildCssData()
317 * @uses file_exists()
318 * @uses in_array()
319 * @param string $type left, right or print
321 function loadCss(&$type)
323 if (empty($type) || ! in_array($type, $this->types)) {
324 $type = 'left';
327 if ($type == 'right') {
328 echo PMA_SQP_buildCssData();
331 $_css_file = $this->getPath()
332 . '/css/theme_' . $type . '.css.php';
334 if (! file_exists($_css_file)) {
335 return false;
338 if ($GLOBALS['text_dir'] === 'ltr') {
339 $right = 'right';
340 $left = 'left';
341 } else {
342 $right = 'left';
343 $left = 'right';
346 include $_css_file;
347 return true;
351 * prints out the preview for this theme
353 * @access public
354 * @uses PMA_Theme::getName()
355 * @uses PMA_Theme::getVersion()
356 * @uses PMA_Theme::getId()
357 * @uses PMA_Theme::getPath()
358 * @uses PMA_generate_common_url()
359 * @uses addslashes()
360 * @uses file_exists()
361 * @uses htmlspecialchars()
363 function printPreview()
365 echo '<div class="theme_preview">';
366 echo '<h2>' . htmlspecialchars($this->getName())
367 .' (' . htmlspecialchars($this->getVersion()) . ')</h2>'
368 .'<p>'
369 .'<a target="_top" href="index.php'
370 .PMA_generate_common_url(array('set_theme' => $this->getId())) . '"'
371 .' onclick="takeThis(\'' . addslashes($this->getId()) . '\');'
372 .' return false;">';
373 if (@file_exists($this->getPath() . '/screen.png')) {
374 // if screen exists then output
376 echo '<img src="' . $this->getPath() . '/screen.png" border="1"'
377 .' alt="' . htmlspecialchars($this->getName()) . '"'
378 .' title="' . htmlspecialchars($this->getName()) . '" /><br />';
379 } else {
380 echo __('No preview available.');
383 echo '[ <strong>' . __('take it') . '</strong> ]</a>'
384 .'</p>'
385 .'</div>';