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