]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Theme.php
Minor theme cleanups.
[SourceForge/phpwiki.git] / lib / Theme.php
1 <?php rcs_id('$Id: Theme.php,v 1.10 2002-01-19 22:32:01 dairiki 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     function getFormatter ($type, $format) {
82         $method = strtolower("get${type}Formatter");
83         if (method_exists($this, $method))
84             return call_user_method($method, $this, $format);
85         return false;
86     }
87
88
89     ////////////////////////////////////////////////////////////////
90     //
91     // Links
92     //
93     ////////////////////////////////////////////////////////////////
94
95     function setAutosplitWikiWords($autosplit=false) {
96         $this->_autosplitWikiWords = $autosplit ? true : false;
97     }
98
99     function getAutoSplitWikiWords() {
100         if (! @$this->_autosplitWikiWords)
101             $this->setAutosplitWikiWords();
102         
103         return $this->_autosplitWikiWords;
104     }
105
106     function LinkExistingWikiWord($wikiword, $linktext = '', $version = false) {
107         if (empty($linktext)) {
108             $linktext = $wikiword;
109             if ($this->getAutoSplitWikiWords())
110                 $linktext = split_pagename($linktext);
111             $class = 'wiki';
112         }
113         else
114             $class = 'named-wiki';
115
116         $attr = array();
117         if ($version !== false)
118             $attr['version'] = $version;
119
120         return QElement('a', array('href'  => WikiURL($wikiword, $attr),
121                                    'class' => $class),
122                         $linktext);
123     }
124
125     function LinkUnknownWikiWord($wikiword, $linktext = '') {
126         if (empty($linktext)) {
127             $linktext = $wikiword;
128             if ($this->getAutoSplitWikiWords())
129                 $linktext=split_pagename($linktext);
130             $class = 'wikiunknown';
131         } else
132             $class = 'named-wikiunknown';
133
134         return Element('span', array('class' => $class),
135                        QElement('a',
136                                 array('href' => WikiURL($wikiword,
137                                                         array('action' => 'edit'))),
138                                 '?') . Element('u', $linktext));
139     }
140
141
142     ////////////////////////////////////////////////////////////////
143     //
144     // Images and Icons
145     //
146     ////////////////////////////////////////////////////////////////
147
148     function addImageAlias ($alias, $image_name) {
149         $this->_imageAliases[$alias] = $image_name;
150     }
151     
152     function getImageURL ($image) {
153         $aliases = &$this->_imageAliases;
154         
155         if (isset($aliases[$image]))
156             $image = $aliases[$image];
157
158         // If not extension, default to .png.
159         if (!preg_match('/\.\w+$/', $image))
160             $image .= '.png';
161
162         // FIXME: this should probably be made to fall back
163         //        automatically to .gif, .jpg.
164         //        Also try .gif before .png if browser doesn't like png.
165         
166         return $this->_findData("images/$image", 'missing okay');
167     }
168
169     function setLinkIcon($proto, $image = false) {
170         if (!$image)
171             $image = $proto;
172         
173         $this->_linkIcons[$proto] = $image;
174     }
175     
176     function getLinkIconURL ($proto) {
177         $icons = &$this->_linkIcons;
178         if (!empty($icons[$proto]))
179             return $this->getImageURL($icons[$proto]);
180         elseif (!empty($icons['*']))
181             return $this->getImageURL($icons['*']);
182         return false;
183     }
184
185     function addButtonAlias ($text, $alias) {
186         $this->_buttonAliases[$text] = $alias;
187     }
188
189     function getButtonURL ($text) {
190         $aliases = &$this->_buttonAliases;
191         if (isset($aliases[$text]))
192             $text = $aliases[$text];
193         
194         $qtext = urlencode($text);
195         // FIXME: search other languages too.
196         foreach (array("buttons/en/$qtext.png",
197                        "buttons/$qtext.png") as $file) {
198             $path = $this->_findData($file, 'missing okay');
199             if ($path)
200                 return $path;
201         }
202         return false;
203     }
204
205     ////////////////////////////////////////////////////////////////
206     //
207     // Button style
208     //
209     ////////////////////////////////////////////////////////////////
210
211     function makeButton ($text, $url, $class = false) {
212         // FIXME: don't always try for image button?
213         
214         $imgurl = $this->getButtonURL($text);
215         if ($imgurl)
216             return new ImageButton($text, $url, $class, $imgurl);
217         else
218             return new Button($text, $url, $class);
219     }
220
221     function setButtonSeparator($separator=false) {
222         $this->_buttonSeparator = $separator ? $separator : " | ";
223     }
224
225     function getButtonSeparator() {
226         if (! @$this->_buttonSeparator)
227             $this->setButtonSeparator();
228         
229         return $this->_buttonSeparator;
230     }
231
232     
233     ////////////////////////////////////////////////////////////////
234     //
235     // CSS
236     //
237     ////////////////////////////////////////////////////////////////
238     
239     function _CSSlink($title, $css_file, $media, $is_alt = false) {
240         $attr = array('rel'     => $is_alt ? 'alternate stylesheet' : 'stylesheet',
241                       'title'   => $title,
242                       'type'    => 'text/css',
243                       'charset' => CHARSET);
244         $attr['href'] = $this->_findData($css_file);
245         if ($media)
246             $attr['media'] = $media;
247         return Element('link', $attr);
248     }
249
250     function setDefaultCSS ($title, $css_file, $media = false) {
251         if (isset($this->_alternateCSS))
252             unset($this->_alternateCSS[$title]);
253         $this->_defaultCSS = $this->_CSSlink($title, $css_file, $media);
254     }
255
256     function addAlternateCSS ($title, $css_file, $media = false) {
257         $this->_alternateCSS[$title] = $this->_CSSlink($title, $css_file, $media, true);
258     }
259     
260     /**
261      * @return string HTML for CSS.
262      */
263     function getCSS () {
264         $css = "$this->_defaultCSS\n";
265         if (!empty($this->_alternateCSS))
266             $css .= join("\n", $this->_alternateCSS) . "\n";
267         return $css;
268     }
269
270     function findTemplate ($name) {
271         return $this->_findFile("templates/$name.tmpl");
272     }
273 };
274
275
276 // (c-file-style: "gnu")
277 // Local Variables:
278 // mode: php
279 // tab-width: 8
280 // c-basic-offset: 4
281 // c-hanging-comment-ender-p: nil
282 // indent-tabs-mode: nil
283 // End:   
284 ?>