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