Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: Theme_Manager.class.php,v 1.5.2.5 2006/05/02 09:28:57 nijel Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5 require_once('./libraries/Theme.class.php');
6  
7 class PMA_Theme_Manager {
8  
9 /**
10 * @var string path to theme folder
11 * @protected
12 */
13 var $_themes_path;
14  
15 /**
16 * @var array available themes
17 */
18 var $themes = array();
19  
20 /**
21 * @var string cookie name
22 */
23 var $cookie_name = 'pma_theme';
24  
25 /**
26 * @var boolean
27 */
28 var $per_server = false;
29  
30 /**
31 * @var string name of active theme
32 */
33 var $active_theme = '';
34  
35 /**
36 * @var object PMA_Theme active theme
37 */
38 var $theme = null;
39  
40 /**
41 * @var string
42 */
43 var $theme_default = 'original';
44  
45 function __construct()
46 {
47 $this->init();
48 }
49  
50 /**
51 * sets path to folder containing the themes
52 *
53 * @param string $path path to themes folder
54 * @return boolean success
55 */
56 function setThemesPath($path)
57 {
58 if (! $this->_checkThemeFolder($path)) {
59 return false;
60 }
61  
62 $this->_themes_path = trim($path);
63 return true;
64 }
65  
66 /**
67 * @public
68 * @return string
69 */
70 function getThemesPath()
71 {
72 return $this->_themes_path;
73 }
74  
75 /**
76 * sets if there are different themes per server
77 *
78 * @param boolean $per_server
79 */
80 function setThemePerServer($per_server)
81 {
82 $this->per_server = (bool) $per_server;
83 }
84  
85 function init()
86 {
87 $this->themes = array();
88 $this->theme_default = 'original';
89 $this->active_theme = '';
90  
91 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
92 return false;
93 }
94  
95 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
96  
97 $this->loadThemes();
98  
99 $this->theme = new PMA_Theme;
100  
101  
102 if ( ! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
103 $GLOBALS['PMA_errors'][] = sprintf( $GLOBALS['strThemeDefaultNotFound'],
104 $GLOBALS['cfg']['ThemeDefault'] );
105 trigger_error(
106 sprintf($GLOBALS['strThemeDefaultNotFound'],
107 $GLOBALS['cfg']['ThemeDefault']),
108 E_USER_WARNING);
109 $GLOBALS['cfg']['ThemeDefault'] = false;
110 }
111  
112 $this->theme_default = $GLOBALS['cfg']['ThemeDefault'];
113  
114 // check if user have a theme cookie
115 if (! $this->getThemeCookie()
116 || ! $this->setActiveTheme($this->getThemeCookie())) {
117 // otherwise use default theme
118 if ($GLOBALS['cfg']['ThemeDefault']) {
119 $this->setActiveTheme($GLOBALS['cfg']['ThemeDefault']);
120 } else {
121 // or original theme
122 $this->setActiveTheme('original');
123 }
124 }
125 }
126  
127 function checkConfig()
128 {
129 if ($this->_themes_path != trim($GLOBALS['cfg']['ThemePath'])
130 || $this->theme_default != $GLOBALS['cfg']['ThemeDefault']) {
131 $this->init();
132 }
133 }
134  
135 function setActiveTheme($theme = null)
136 {
137 if ( ! $this->checkTheme($theme)) {
138 $GLOBALS['PMA_errors'][] = sprintf($GLOBALS['strThemeNotFound'],
139 PMA_sanitize($theme));
140 trigger_error(
141 sprintf($GLOBALS['strThemeNotFound'], PMA_sanitize($theme)),
142 E_USER_WARNING);
143 return false;
144 }
145  
146 $this->active_theme = $theme;
147 $this->theme = $this->themes[$theme];
148  
149 // need to set later
150 //$this->setThemeCookie();
151  
152 return true;
153 }
154  
155 /**
156 * @return string cookie name
157 */
158 function getThemeCookieName()
159 {
160 // Allow different theme per server
161 if (isset($GLOBALS['server']) && $this->per_server) {
162 return $this->cookie_name . '-' . $GLOBALS['server'];
163 } else {
164 return $this->cookie_name;
165 }
166 }
167  
168 /**
169 * returns name of theme stored in the cookie
170 * @return string theme name from cookie
171 */
172 function getThemeCookie()
173 {
174 if (isset($_COOKIE[$this->getThemeCookieName()])) {
175 return $_COOKIE[$this->getThemeCookieName()];
176 }
177  
178 return false;
179 }
180  
181 /**
182 * save theme in cookie
183 *
184 * @uses PMA_setCookie();
185 * @uses PMA_Theme_Manager::getThemeCookieName()
186 * @uses PMA_Theme_Manager::$theme
187 * @uses PMA_Theme_Manager::$theme_default
188 * @uses PMA_Theme::getId()
189 */
190 function setThemeCookie()
191 {
192 PMA_setCookie($this->getThemeCookieName(), $this->theme->id,
193 $this->theme_default);
194 return true;
195 }
196  
197 /**
198 * old PHP 4 constructor
199 */
200 function PMA_Theme_Manager()
201 {
202 $this->__construct();
203 }
204  
205 /**
206 * @private
207 * @param string $folder
208 * @return boolean
209 */
210 /*private*/ function _checkThemeFolder($folder)
211 {
212 if (! is_dir($folder)) {
213 $GLOBALS['PMA_errors'][] =
214 sprintf($GLOBALS['strThemePathNotFound'],
215 htmlspecialchars($folder));
216 trigger_error(
217 sprintf($GLOBALS['strThemePathNotFound'],
218 htmlspecialchars($folder)),
219 E_USER_WARNING);
220 return false;
221 }
222  
223 return true;
224 }
225  
226 /**
227 * read all themes
228 */
229 function loadThemes()
230 {
231 $this->themes = array();
232  
233 if ($handleThemes = opendir($this->getThemesPath())) {
234 // check for themes directory
235 while (false !== ($PMA_Theme = readdir($handleThemes))) {
236 if (array_key_exists($PMA_Theme, $this->themes)) {
237 $this->themes[$PMA_Theme] = $this->themes[$PMA_Theme];
238 continue;
239 }
240 $new_theme = PMA_Theme::load($this->getThemesPath() . '/' . $PMA_Theme);
241 if ($new_theme) {
242 $new_theme->setId($PMA_Theme);
243 $this->themes[$PMA_Theme] = $new_theme;
244 }
245 } // end get themes
246 closedir($handleThemes);
247 } else {
248 trigger_error(
249 'phpMyAdmin-ERROR: can not open themes folder: ' . $this->getThemesPath(),
250 E_USER_WARNING);
251 return false;
252 } // end check for themes directory
253  
254 ksort($this->themes);
255 return true;
256 }
257  
258 /**
259 * checks if given theme name is a known theme
260 *
261 * @param string $theme name fo theme to check for
262 */
263 function checkTheme($theme)
264 {
265 if (! array_key_exists($theme, $this->themes)) {
266 return false;
267 }
268  
269 return true;
270 }
271  
272 /**
273 * returns HTML selectbox, with or without form enclsoed
274 *
275 * @param boolean $form wether enclosed by from tags or not
276 */
277 function getHtmlSelectBox($form = true)
278 {
279 $select_box = '';
280  
281 if ($form) {
282 $select_box .= '<form name="setTheme" method="post" action="index.php"'
283 .' target="_parent">';
284 $select_box .= PMA_generate_common_hidden_inputs();
285 }
286  
287 $theme_selected = FALSE;
288 $theme_preview_path= './themes.php';
289 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" onclick="'
290 . "window.open('" . $theme_preview_path . "','themes','left=10,top=20,width=510,height=350,scrollbars=yes,status=yes,resizable=yes');"
291 . '">';
292 $select_box .= $theme_preview_href . $GLOBALS['strTheme'] . '</a>:' . "\n";
293  
294 $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr"'
295 .' onchange="this.form.submit();" >';
296 foreach ($this->themes as $each_theme_id => $each_theme) {
297 $select_box .= '<option value="' . $each_theme_id . '"';
298 if ($this->active_theme === $each_theme_id) {
299 $select_box .= ' selected="selected"';
300 }
301 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
302 }
303 $select_box .= '</select>';
304  
305 if ($form) {
306 $select_box .= '<noscript><input type="submit" value="' . $GLOBALS['strGo'] . '" /></noscript>';
307 $select_box .= '</form>';
308 }
309  
310 return $select_box;
311 }
312  
313 /**
314 * enables backward compatibility
315 */
316 function makeBc()
317 {
318 $GLOBALS['theme'] = $this->theme->getId();
319 $GLOBALS['pmaThemePath'] = $this->theme->getPath();
320 $GLOBALS['pmaThemeImage'] = $this->theme->getImgPath();
321  
322 /**
323 * load layout file if exists
324 */
325 if (@file_exists($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
326 include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
327 }
328  
329  
330 }
331  
332 /**
333 * prints out preview for every theme
334 *
335 * @uses $this->themes
336 * @uses PMA_Theme::printPreview()
337 */
338 function printPreviews()
339 {
340 foreach ($this->themes as $each_theme) {
341 $each_theme->printPreview();
342 } // end 'open themes'
343 }
344 }
345 ?>