1 <?php rcs_id('$Id: Theme.php,v 1.14 2002-01-22 03:17:47 dairiki Exp $');
3 require_once('lib/HtmlElement.php');
4 require_once('lib/ButtonFactory.php');
8 function Theme ($theme_name) {
9 $this->_name = $theme_name;
10 $themes_dir = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/themes" : "themes";
11 $this->_theme_dir = "$themes_dir/$theme_name";
12 $this->_path = array($this->_theme_dir,
13 "$themes_dir/default");
16 function _findFile ($file, $missing_okay = false) {
17 foreach ($this->_path as $dir) {
18 if (file_exists("$dir/$file"))
21 // FIXME: this is a short-term hack. Delete this after all files
22 // get moved into themes/...
23 if (file_exists($file))
27 trigger_error("$file: not found", E_USER_ERROR);
32 function _findData ($file, $missing_okay = false) {
33 $path = $this->_findFile($file, $missing_okay);
37 if (defined('DATA_PATH'))
38 return DATA_PATH . "/$path";
42 function file ($file) {
43 return "$this->_theme_dir/$file";
46 ////////////////////////////////////////////////////////////////
48 // Date and Time formatting
50 ////////////////////////////////////////////////////////////////
52 var $_dateTimeFormat = "%B %e, %Y";
53 var $_dateFormat = "%B %e, %Y";
55 function setDateFormat ($fs) {
56 $this->_dateFormat = $fs;
59 function formatDate ($time_t) {
60 return strftime($this->_dateFormat, $time_t);
63 function setDateTimeFormat ($fs) {
64 $this->_dateTimeFormat = $fs;
67 function formatDateTime ($time_t) {
68 return strftime($this->_dateTimeFormat, $time_t);
72 function formatTime ($time_t) {
73 //FIXME: make 24-hour mode configurable?
74 return preg_replace('/^0/', ' ',
75 strtolower(strftime("%I:%M %p", $time_t)));
79 ////////////////////////////////////////////////////////////////
81 // Hooks for other formatting
83 ////////////////////////////////////////////////////////////////
85 //FIXME: PHP 4.1 Warnings
86 //lib/Theme.php:84: Notice[8]: The call_user_method() function is deprecated,
87 //use the call_user_func variety with the array(&$obj, "method") syntax instead
89 function getFormatter ($type, $format) {
90 $method = strtolower("get${type}Formatter");
91 if (method_exists($this, $method))
92 return @call_user_method($method, $this, $format);
97 ////////////////////////////////////////////////////////////////
101 ////////////////////////////////////////////////////////////////
103 var $_autosplitWikiWords = false;
105 function setAutosplitWikiWords($autosplit=false) {
106 $this->_autosplitWikiWords = $autosplit ? true : false;
109 function maybeSplitWikiWord ($wikiword) {
110 if ($this->_autosplitWikiWords)
111 return split_pagename($wikiword);
116 function linkExistingWikiWord($wikiword, $linktext = '', $version = false) {
117 if ($version !== false)
118 $url = WikiURL($wikiword, array('version' => $version));
120 $url = WikiURL($wikiword);
122 $link = HTML::a(array('href' => $url));
124 if (!empty($linktext)) {
125 $link->pushContent($linktext);
126 $link->setAttr('class', 'named-wiki');
129 $link->pushContent($this->maybeSplitWikiWord($wikiword));
130 $link->setAttr('class', 'wiki');
135 function linkUnknownWikiWord($wikiword, $linktext = '') {
136 $url = WikiURL($wikiword, array('action' => 'edit'));
137 //$link = HTML::span(HTML::a(array('href' => $url), '?'));
138 $link = HTML::span($this->makeButton('?', $url));
140 if (!empty($linktext)) {
141 $link->pushContent(HTML::u($linktext));
142 $link->setAttr('class', 'named-wikiunknown');
145 $link->pushContent(HTML::u($this->maybeSplitWikiWord($wikiword)));
146 $link->setAttr('class', 'wikiunknown');
152 ////////////////////////////////////////////////////////////////
156 ////////////////////////////////////////////////////////////////
158 function addImageAlias ($alias, $image_name) {
159 $this->_imageAliases[$alias] = $image_name;
162 function getImageURL ($image) {
163 $aliases = &$this->_imageAliases;
165 if (isset($aliases[$image]))
166 $image = $aliases[$image];
168 // If not extension, default to .png.
169 if (!preg_match('/\.\w+$/', $image))
172 // FIXME: this should probably be made to fall back
173 // automatically to .gif, .jpg.
174 // Also try .gif before .png if browser doesn't like png.
176 return $this->_findData("images/$image", 'missing okay');
179 function setLinkIcon($proto, $image = false) {
183 $this->_linkIcons[$proto] = $image;
186 function getLinkIconURL ($proto) {
187 $icons = &$this->_linkIcons;
188 if (!empty($icons[$proto]))
189 return $this->getImageURL($icons[$proto]);
190 elseif (!empty($icons['*']))
191 return $this->getImageURL($icons['*']);
195 function addButtonAlias ($text, $alias) {
196 $this->_buttonAliases[$text] = $alias;
199 function getButtonURL ($text) {
200 $aliases = &$this->_buttonAliases;
201 if (isset($aliases[$text]))
202 $text = $aliases[$text];
204 $qtext = urlencode($text);
206 // search no-label buttons in ./buttons
207 // FIXME: this probably needs the untranslated button label to work correctly
208 $path = $this->_findData("buttons/$qtext.png", 'missing okay');
212 // search languages ./buttons/??
213 $path = FindLocalizedButtonFile("$qtext.png", 'missing okay');
217 // search english ./buttons/en in case word is spelled the same
218 $path = $this->_findData("buttons/en/$qtext.png", 'missing okay');
226 ////////////////////////////////////////////////////////////////
230 ////////////////////////////////////////////////////////////////
232 function makeButton ($text, $url, $class = false) {
233 // FIXME: don't always try for image button?
235 $imgurl = $this->getButtonURL($text);
237 return new ImageButton($text, $url, $class, $imgurl);
239 return new Button($text, $url, $class);
242 function setButtonSeparator($separator=false) {
243 $this->_buttonSeparator = $separator ? $separator : " | ";
246 function getButtonSeparator() {
247 if (! @$this->_buttonSeparator)
248 $this->setButtonSeparator();
250 return $this->_buttonSeparator;
254 ////////////////////////////////////////////////////////////////
258 ////////////////////////////////////////////////////////////////
260 function _CSSlink($title, $css_file, $media, $is_alt = false) {
261 $link = HTML::link(array('rel' => $is_alt ? 'alternate stylesheet' : 'stylesheet',
263 'type' => 'text/css',
265 'href' => $this->_findData($css_file)));
267 $link->setAttr('media', $media);
271 function setDefaultCSS ($title, $css_file, $media = false) {
272 if (isset($this->_alternateCSS))
273 unset($this->_alternateCSS[$title]);
274 $this->_defaultCSS = $this->_CSSlink($title, $css_file, $media);
277 function addAlternateCSS ($title, $css_file, $media = false) {
278 $this->_alternateCSS[$title] = $this->_CSSlink($title, $css_file, $media, true);
282 * @return string HTML for CSS.
285 $css[] = $this->_defaultCSS;
286 if (!empty($this->_alternateCSS))
287 foreach ($this->_alternateCSS as $link)
292 function findTemplate ($name) {
293 return $this->_findFile("templates/$name.tmpl");
298 // (c-file-style: "gnu")
303 // c-hanging-comment-ender-p: nil
304 // indent-tabs-mode: nil