]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Theme.php
Search buttons/lang for button images. It's very kludgy but it mostly works. Unlabell...
[SourceForge/phpwiki.git] / lib / Theme.php
1 <?php rcs_id('$Id: Theme.php,v 1.13 2002-01-22 03:12:59 carstenklapp Exp $');
2
3 require_once('lib/HtmlElement.php');
4
5 class Theme {
6     function Theme ($theme_name) {
7         $this->_name = $theme_name;
8         $themes_dir = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/themes" : "themes";
9         $this->_theme_dir = "$themes_dir/$theme_name";
10         $this->_path = array($this->_theme_dir,
11                              "$themes_dir/default");
12     }
13
14     function _findFile ($file, $missing_okay = false) {
15         foreach ($this->_path as $dir) {
16             if (file_exists("$dir/$file"))
17                 return "$dir/$file";
18         }
19         // FIXME: this is a short-term hack.  Delete this after all files
20         // get moved into themes/...
21         if (file_exists($file))
22             return $file;
23         
24         if (!$missing_okay) {
25             trigger_error("$file: not found", E_USER_ERROR);
26         }
27         return false;
28     }
29     
30     function _findData ($file, $missing_okay = false) {
31         $path = $this->_findFile($file, $missing_okay);
32         if (!$path)
33             return false;
34         
35         if (defined('DATA_PATH'))
36             return DATA_PATH . "/$path";
37         return $path;
38     }
39
40     function file ($file) {
41         return "$this->_theme_dir/$file";
42     }
43     
44     ////////////////////////////////////////////////////////////////
45     //
46     // Date and Time formatting
47     //
48     ////////////////////////////////////////////////////////////////
49     
50     var $_dateTimeFormat = "%B %e, %Y";
51     var $_dateFormat = "%B %e, %Y";
52
53     function setDateFormat ($fs) {
54         $this->_dateFormat = $fs;
55     }
56
57     function formatDate ($time_t) {
58         return strftime($this->_dateFormat, $time_t);
59     }
60
61     function setDateTimeFormat ($fs) {
62         $this->_dateTimeFormat = $fs;
63     }
64
65     function formatDateTime ($time_t) {
66         return strftime($this->_dateTimeFormat, $time_t);
67     }
68
69     
70     function formatTime ($time_t) {
71         //FIXME: make 24-hour mode configurable?
72         return preg_replace('/^0/', ' ',
73                             strtolower(strftime("%I:%M %p", $time_t)));
74     }
75
76
77     ////////////////////////////////////////////////////////////////
78     //
79     // Hooks for other formatting
80     //
81     ////////////////////////////////////////////////////////////////
82
83     //FIXME: PHP 4.1 Warnings
84     //lib/Theme.php:84: Notice[8]: The call_user_method() function is deprecated,
85     //use the call_user_func variety with the array(&$obj, "method") syntax instead
86
87     function getFormatter ($type, $format) {
88         $method = strtolower("get${type}Formatter");
89         if (method_exists($this, $method))
90             return @call_user_method($method, $this, $format);
91         return false;
92     }
93
94
95     ////////////////////////////////////////////////////////////////
96     //
97     // Links
98     //
99     ////////////////////////////////////////////////////////////////
100
101     var $_autosplitWikiWords = false;
102     
103     function setAutosplitWikiWords($autosplit=false) {
104         $this->_autosplitWikiWords = $autosplit ? true : false;
105     }
106
107     function maybeSplitWikiWord ($wikiword) {
108         if ($this->_autosplitWikiWords)
109             return split_pagename($wikiword);
110         else
111             return $wikiword;
112     }
113
114     function linkExistingWikiWord($wikiword, $linktext = '', $version = false) {
115         if ($version !== false)
116             $url = WikiURL($wikiword, array('version' => $version));
117         else
118             $url = WikiURL($wikiword);
119
120         $link = HTML::a(array('href' => $url));
121
122         if (!empty($linktext)) {
123             $link->pushContent($linktext);
124             $link->setAttr('class', 'named-wiki');
125         }
126         else {
127             $link->pushContent($this->maybeSplitWikiWord($wikiword));
128             $link->setAttr('class', 'wiki');
129         }
130         return $link;
131     }
132
133     function linkUnknownWikiWord($wikiword, $linktext = '') {
134         $url = WikiURL($wikiword, array('action' => 'edit'));
135         $link = HTML::span(HTML::a(array('href' => $url), '?'));
136
137         if (!empty($linktext)) {
138             $link->pushContent(HTML::u($linktext));
139             $link->setAttr('class', 'named-wikiunknown');
140         }
141         else {
142             $link->pushContent(HTML::u($this->maybeSplitWikiWord($wikiword)));
143             $link->setAttr('class', 'wikiunknown');
144         }
145         
146         return $link;
147     }
148
149     ////////////////////////////////////////////////////////////////
150     //
151     // Images and Icons
152     //
153     ////////////////////////////////////////////////////////////////
154
155     function addImageAlias ($alias, $image_name) {
156         $this->_imageAliases[$alias] = $image_name;
157     }
158     
159     function getImageURL ($image) {
160         $aliases = &$this->_imageAliases;
161         
162         if (isset($aliases[$image]))
163             $image = $aliases[$image];
164
165         // If not extension, default to .png.
166         if (!preg_match('/\.\w+$/', $image))
167             $image .= '.png';
168
169         // FIXME: this should probably be made to fall back
170         //        automatically to .gif, .jpg.
171         //        Also try .gif before .png if browser doesn't like png.
172         
173         return $this->_findData("images/$image", 'missing okay');
174     }
175
176     function setLinkIcon($proto, $image = false) {
177         if (!$image)
178             $image = $proto;
179         
180         $this->_linkIcons[$proto] = $image;
181     }
182     
183     function getLinkIconURL ($proto) {
184         $icons = &$this->_linkIcons;
185         if (!empty($icons[$proto]))
186             return $this->getImageURL($icons[$proto]);
187         elseif (!empty($icons['*']))
188             return $this->getImageURL($icons['*']);
189         return false;
190     }
191
192     function addButtonAlias ($text, $alias) {
193         $this->_buttonAliases[$text] = $alias;
194     }
195
196     function getButtonURL ($text) {
197         $aliases = &$this->_buttonAliases;
198         if (isset($aliases[$text]))
199             $text = $aliases[$text];
200         
201         $qtext = urlencode($text);
202
203             // search no-label buttons in ./buttons
204             // FIXME: this probably needs the untranslated button label to work correctly 
205             $path = $this->_findData("buttons/$qtext.png", 'missing okay');
206             if ($path)
207                 return $path;
208
209             // search languages ./buttons/??
210             $path = FindLocalizedButtonFile("$qtext.png", 'missing okay');
211             if ($path)
212                 return $path;
213
214             // search english ./buttons/en in case word is spelled the same
215             $path = $this->_findData("buttons/en/$qtext.png", 'missing okay');
216             if ($path)
217                 return $path;
218
219
220         return false;
221     }
222
223     ////////////////////////////////////////////////////////////////
224     //
225     // Button style
226     //
227     ////////////////////////////////////////////////////////////////
228
229     function makeButton ($text, $url, $class = false) {
230         // FIXME: don't always try for image button?
231         
232         $imgurl = $this->getButtonURL($text);
233         if ($imgurl)
234             return new ImageButton($text, $url, $class, $imgurl);
235         else
236             return new Button($text, $url, $class);
237     }
238
239     function setButtonSeparator($separator=false) {
240         $this->_buttonSeparator = $separator ? $separator : " | ";
241     }
242
243     function getButtonSeparator() {
244         if (! @$this->_buttonSeparator)
245             $this->setButtonSeparator();
246         
247         return $this->_buttonSeparator;
248     }
249
250     
251     ////////////////////////////////////////////////////////////////
252     //
253     // CSS
254     //
255     ////////////////////////////////////////////////////////////////
256     
257     function _CSSlink($title, $css_file, $media, $is_alt = false) {
258         $link = HTML::link(array('rel'    => $is_alt ? 'alternate stylesheet' : 'stylesheet',
259                                  'title'  => $title,
260                                  'type'   => 'text/css',
261                                  'charset'=> CHARSET,
262                                  'href'   => $this->_findData($css_file)));
263         if ($media)
264             $link->setAttr('media', $media);
265         return $link;
266     }
267
268     function setDefaultCSS ($title, $css_file, $media = false) {
269         if (isset($this->_alternateCSS))
270             unset($this->_alternateCSS[$title]);
271         $this->_defaultCSS = $this->_CSSlink($title, $css_file, $media);
272     }
273
274     function addAlternateCSS ($title, $css_file, $media = false) {
275         $this->_alternateCSS[$title] = $this->_CSSlink($title, $css_file, $media, true);
276     }
277     
278     /**
279      * @return string HTML for CSS.
280      */
281     function getCSS () {
282         $css[] = $this->_defaultCSS;
283         if (!empty($this->_alternateCSS))
284             foreach ($this->_alternateCSS as $link)
285                 $css[] = $link;
286         return $css;
287     }
288
289     function findTemplate ($name) {
290         return $this->_findFile("templates/$name.tmpl");
291     }
292 };
293
294
295 // (c-file-style: "gnu")
296 // Local Variables:
297 // mode: php
298 // tab-width: 8
299 // c-basic-offset: 4
300 // c-hanging-comment-ender-p: nil
301 // indent-tabs-mode: nil
302 // End:   
303 ?>