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