1 <?php rcs_id('$Id: Theme.php,v 1.25 2002-01-28 18:49:08 dairiki Exp $');
3 require_once('lib/HtmlElement.php');
6 function Theme ($theme_name = 'default') {
7 $this->_name = $theme_name;
8 $themes_dir = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/themes" : "themes";
10 $this->_path = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/" : "";
11 $this->_theme = "themes/$theme_name";
13 if ($theme_name != 'default')
14 $this->_default_theme = new Theme;
17 function file ($file) {
18 return $this->_path . "$this->_theme/$file";
21 function _findFile ($file, $missing_okay = false) {
22 if (file_exists($this->_path . "$this->_theme/$file"))
23 return "$this->_theme/$file";
25 // FIXME: this is a short-term hack. Delete this after all files
26 // get moved into themes/...
27 if (file_exists($this->_path . $file))
31 if (isset($this->_default_theme)) {
32 return $this->_default_theme->_findFile($file, $missing_okay);
34 else if (!$missing_okay) {
35 trigger_error("$file: not found", E_USER_ERROR);
40 function _findData ($file, $missing_okay = false) {
41 $path = $this->_findFile($file, $missing_okay);
45 if (defined('DATA_PATH'))
46 return DATA_PATH . "/$path";
50 ////////////////////////////////////////////////////////////////
52 // Date and Time formatting
54 ////////////////////////////////////////////////////////////////
56 var $_dateTimeFormat = "%B %e, %Y";
57 var $_dateFormat = "%B %e, %Y";
59 function setDateFormat ($fs) {
60 $this->_dateFormat = $fs;
63 function formatDate ($time_t) {
64 return strftime($this->_dateFormat, $time_t);
67 function setDateTimeFormat ($fs) {
68 $this->_dateTimeFormat = $fs;
71 function formatDateTime ($time_t) {
72 return strftime($this->_dateTimeFormat, $time_t);
76 function formatTime ($time_t) {
77 //FIXME: make 24-hour mode configurable?
78 return preg_replace('/^0/', ' ',
79 strtolower(strftime("%I:%M %p", $time_t)));
83 ////////////////////////////////////////////////////////////////
85 // Hooks for other formatting
87 ////////////////////////////////////////////////////////////////
89 //FIXME: PHP 4.1 Warnings
90 //lib/Theme.php:84: Notice[8]: The call_user_method() function is deprecated,
91 //use the call_user_func variety with the array(&$obj, "method") syntax instead
93 function getFormatter ($type, $format) {
94 $method = strtolower("get${type}Formatter");
95 if (method_exists($this, $method))
96 return $this->{$method}($format);
100 ////////////////////////////////////////////////////////////////
104 ////////////////////////////////////////////////////////////////
106 var $_autosplitWikiWords = false;
108 function setAutosplitWikiWords($autosplit=false) {
109 $this->_autosplitWikiWords = $autosplit ? true : false;
112 function maybeSplitWikiWord ($wikiword) {
113 if ($this->_autosplitWikiWords)
114 return split_pagename($wikiword);
119 function linkExistingWikiWord($wikiword, $linktext = '', $version = false) {
120 if ($version !== false)
121 $url = WikiURL($wikiword, array('version' => $version));
123 $url = WikiURL($wikiword);
125 $link = HTML::a(array('href' => $url));
127 if (!empty($linktext)) {
128 $link->pushContent($linktext);
129 $link->setAttr('class', 'named-wiki');
132 $link->pushContent($this->maybeSplitWikiWord($wikiword));
133 $link->setAttr('class', 'wiki');
138 function linkUnknownWikiWord($wikiword, $linktext = '') {
139 $url = WikiURL($wikiword, array('action' => 'edit'));
140 //$link = HTML::span(HTML::a(array('href' => $url), '?'));
141 $link = HTML::span($this->makeButton('?', $url));
143 if (!empty($linktext)) {
144 $link->pushContent(HTML::u($linktext));
145 $link->setAttr('class', 'named-wikiunknown');
148 $link->pushContent(HTML::u($this->maybeSplitWikiWord($wikiword)));
149 $link->setAttr('class', 'wikiunknown');
155 ////////////////////////////////////////////////////////////////
159 ////////////////////////////////////////////////////////////////
163 * (To disable an image, alias the image to <code>false</code>.
165 function addImageAlias ($alias, $image_name) {
166 $this->_imageAliases[$alias] = $image_name;
169 function getImageURL ($image) {
170 $aliases = &$this->_imageAliases;
172 if (isset($aliases[$image])) {
173 $image = $aliases[$image];
178 // If not extension, default to .png.
179 if (!preg_match('/\.\w+$/', $image))
182 // FIXME: this should probably be made to fall back
183 // automatically to .gif, .jpg.
184 // Also try .gif before .png if browser doesn't like png.
186 return $this->_findData("images/$image", 'missing okay');
189 function setLinkIcon($proto, $image = false) {
193 $this->_linkIcons[$proto] = $image;
196 function getLinkIconURL ($proto) {
197 $icons = &$this->_linkIcons;
198 if (!empty($icons[$proto]))
199 return $this->getImageURL($icons[$proto]);
200 elseif (!empty($icons['*']))
201 return $this->getImageURL($icons['*']);
205 function addButtonAlias ($text, $alias = false) {
206 $aliases = &$this->_buttonAliases;
209 $aliases = array_merge($aliases, $text);
210 elseif ($alias === false)
211 unset($aliases[$text]);
213 $aliases[$text] = $alias;
216 function getButtonURL ($text) {
217 $aliases = &$this->_buttonAliases;
218 if (isset($aliases[$text]))
219 $text = $aliases[$text];
221 $qtext = urlencode($text);
222 $url = $this->_findButton("$qtext.png");
223 if ($url && strstr($url, '%')) {
224 $url = preg_replace('|([^/]+)$|e', 'urlencode("\\1")', $url);
229 function _findButton ($button_file) {
230 if (!isset($this->_button_path))
231 $this->_button_path = $this->_getButtonPath();
233 foreach ($this->_button_path as $dir) {
234 $path = "$this->_theme/$dir/$button_file";
235 if (file_exists($this->_path . $path))
236 return defined('DATA_PATH') ? DATA_PATH . "/$path" : $path;
241 function _getButtonPath () {
242 $button_dir = $this->file("buttons");
243 if (!file_exists($button_dir) || !is_dir($button_dir))
246 $path = array('buttons');
248 $dir = dir($button_dir);
249 while (($subdir = $dir->read()) !== false) {
250 if ($subdir[0] == '.')
252 if (is_dir("$button_dir/$subdir"))
253 $path[] = "buttons/$subdir";
260 ////////////////////////////////////////////////////////////////
264 ////////////////////////////////////////////////////////////////
266 function makeButton ($text, $url, $class = false) {
267 // FIXME: don't always try for image button?
269 // Special case: URLs like 'submit:preview' generate form
270 // submission buttons.
271 if (preg_match('/^submit:(.*)$/', $url, $m))
272 return $this->makeSubmitButton($text, $m[1], $class);
274 $imgurl = $this->getButtonURL($text);
276 return new ImageButton($text, $url, $class, $imgurl);
278 return new Button($text, $url, $class);
281 function makeSubmitButton ($text, $name, $class = false) {
282 $imgurl = $this->getButtonURL($text);
285 return new SubmitImageButton($text, $name, $class, $imgurl);
287 return new SubmitButton($text, $name, $class);
291 * Make button to perform action.
293 * This constructs a button which performs an action on the
294 * currently selected version of the current page. (Or another
295 * page or version, if you want...)
297 * @param $action string The action to perform (e.g. 'edit', 'lock').
298 * This can also be the name of an "action page" like 'LikePages'.
300 * @param $label string Textual label for the button. If left empty,
301 * a suitable name will be guessed.
303 * @param $page_or_rev mixed The page to link to. This can be
304 * given as a string (the page name), a WikiDB_Page object, or as
305 * WikiDB_PageRevision object. If given as a WikiDB_PageRevision
306 * object, the button will link to a specific version of the
307 * designated page, otherwise the button links to the most recent
308 * version of the page.
310 * @return object A Button object.
312 function makeActionButton ($action, $label = false, $page_or_rev = false) {
313 extract($this->_get_name_and_rev($page_or_rev));
315 if (is_array($action)) {
317 $action = isset($attr['action']) ? $attr['action'] : 'browse';
320 $attr['action'] = $action;
322 $class = is_safe_action($action) ? 'wikiaction' : 'wikiadmin';
324 $label = $this->_labelForAction($action);
327 $attr['version'] = $version;
329 if ($action == 'browse')
330 unset($attr['action']);
332 return $this->makeButton($label, WikiURL($pagename, $attr), $class);
336 * Make a "button" which links to a wiki-page.
338 * These are really just regular WikiLinks, possibly
339 * disguised (e.g. behind an image button) by the theme.
341 * This method should probably only be used for links
342 * which appear in page navigation bars, or similar places.
344 * Use linkExistingWikiWord, or LinkWikiWord for normal links.
346 * @param $page_or_rev mixed The page to link to. This can be
347 * given as a string (the page name), a WikiDB_Page object, or as
348 * WikiDB_PageRevision object. If given as a WikiDB_PageRevision
349 * object, the button will link to a specific version of the
350 * designated page, otherwise the button links to the most recent
351 * version of the page.
353 * @return object A Button object.
355 function makeLinkButton ($page_or_rev) {
356 extract($this->_get_name_and_rev($page_or_rev));
358 $args = $version ? array('version' => $version) : false;
360 return $this->makeButton($pagename, WikiURL($pagename, $args), 'wiki');
363 function _get_name_and_rev ($page_or_rev) {
366 if (empty($page_or_rev)) {
368 $pagename = $request->getArg("pagename");
369 $version = $request->getArg("version");
371 elseif (is_object($page_or_rev)) {
372 if (isa($page_or_rev, 'WikiDB_PageRevision')) {
374 $page = $rev->getPage();
375 $version = $rev->getVersion();
378 $page = $page_or_rev;
380 $pagename = $page->getName();
383 $pagename = (string) $page_or_rev;
385 return compact('pagename', 'version');
388 function _labelForAction ($action) {
390 case 'edit': return _("Edit");
391 case 'diff': return _("Diff");
392 case 'logout': return _("Sign Out");
393 case 'login': return _("Sign In");
394 case 'lock': return _("Lock Page");
395 case 'unlock': return _("Unlock Page");
396 case 'remove': return _("Remove Page");
398 // I don't think the rest of these actually get used.
400 // 'upload' 'dumpserial' 'loadfile' 'zip'
402 return ucfirst($action);
406 //----------------------------------------------------------------
407 var $_buttonSeparator = ' | ';
409 function setButtonSeparator($separator) {
410 $this->_buttonSeparator = $separator;
413 function getButtonSeparator() {
414 return $this->_buttonSeparator;
418 ////////////////////////////////////////////////////////////////
422 ////////////////////////////////////////////////////////////////
424 function _CSSlink($title, $css_file, $media, $is_alt = false) {
425 $link = HTML::link(array('rel' => $is_alt ? 'alternate stylesheet' : 'stylesheet',
427 'type' => 'text/css',
429 'href' => $this->_findData($css_file)));
431 $link->setAttr('media', $media);
435 function setDefaultCSS ($title, $css_file, $media = false) {
436 if (isset($this->_alternateCSS))
437 unset($this->_alternateCSS[$title]);
438 $this->_defaultCSS = $this->_CSSlink($title, $css_file, $media);
441 function addAlternateCSS ($title, $css_file, $media = false) {
442 $this->_alternateCSS[$title] = $this->_CSSlink($title, $css_file, $media, true);
446 * @return string HTML for CSS.
449 $css = HTML($this->_defaultCSS);
450 if (!empty($this->_alternateCSS))
451 $css->pushContent($this->_alternateCSS);
455 function findTemplate ($name) {
456 return $this->_path . $this->_findFile("templates/$name.tmpl");
462 * A class representing a clickable "button".
464 * In it's simplest (default) form, a "button" is just a link associated
465 * with some sort of wiki-action.
467 class Button extends HtmlElement {
470 * @param $text string The text for the button.
471 * @param $url string The url (href) for the button.
472 * @param $class string The CSS class for the button.
474 function Button ($text, $url, $class = false) {
475 $this->HtmlElement('a', array('href' => $url));
477 $this->setAttr('class', $class);
478 $this->pushContent($text);
485 * A clickable image button.
487 class ImageButton extends Button {
490 * @param $text string The text for the button.
491 * @param $url string The url (href) for the button.
492 * @param $class string The CSS class for the button.
493 * @param $img_url string URL for button's image.
494 * @param $img_attr array Additional attributes for the <img> tag.
496 function ImageButton ($text, $url, $class, $img_url, $img_attr = false) {
497 $this->HtmlElement('a', array('href' => $url));
499 $this->setAttr('class', $class);
501 if (!is_array($img_attr))
503 $img_attr['src'] = $img_url;
504 $img_attr['alt'] = $text;
505 $img_attr['class'] = 'wiki-button';
506 $img_attr['border'] = 0;
507 $this->pushContent(HTML::img($img_attr));
512 * A class representing a form <samp>submit</samp> button.
514 class SubmitButton extends HtmlElement {
517 * @param $text string The text for the button.
518 * @param $name string The name of the form field.
519 * @param $class string The CSS class for the button.
521 function SubmitButton ($text, $name = false, $class = false) {
522 $this->HtmlElement('input', array('type' => 'submit',
525 $this->setAttr('name', $name);
527 $this->setAttr('class', $class);
534 * A class representing an image form <samp>submit</samp> button.
536 class SubmitImageButton extends SubmitButton {
539 * @param $text string The text for the button.
540 * @param $name string The name of the form field.
541 * @param $class string The CSS class for the button.
542 * @param $img_url string URL for button's image.
543 * @param $img_attr array Additional attributes for the <img> tag.
545 function SubmitImageButton ($text, $name = false, $class = false, $img_url) {
546 $this->HtmlElement('input', array('type' => 'image',
551 $this->setAttr('name', $name);
553 $this->setAttr('class', $class);
558 // (c-file-style: "gnu")
563 // c-hanging-comment-ender-p: nil
564 // indent-tabs-mode: nil