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