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