]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Theme.php
Another refactor of the OOP HTML/XML generation code. (Sorry.)
[SourceForge/phpwiki.git] / lib / Theme.php
1 <?php rcs_id('$Id: Theme.php,v 1.25 2002-01-28 18:49:08 dairiki Exp $');
2
3 require_once('lib/HtmlElement.php');
4
5 class Theme {
6     function Theme ($theme_name = 'default') {
7         $this->_name = $theme_name;
8         $themes_dir = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/themes" : "themes";
9         
10         $this->_path  = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/" : "";
11         $this->_theme = "themes/$theme_name";
12
13         if ($theme_name != 'default')
14             $this->_default_theme = new Theme;
15     }
16
17     function file ($file) {
18         return $this->_path . "$this->_theme/$file";
19     }
20
21     function _findFile ($file, $missing_okay = false) {
22         if (file_exists($this->_path . "$this->_theme/$file"))
23             return "$this->_theme/$file";
24         
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))
28             return $file;
29         
30
31         if (isset($this->_default_theme)) {
32             return $this->_default_theme->_findFile($file, $missing_okay);
33         }
34         else if (!$missing_okay) {
35             trigger_error("$file: not found", E_USER_ERROR);
36         }
37         return false;
38     }
39
40     function _findData ($file, $missing_okay = false) {
41         $path = $this->_findFile($file, $missing_okay);
42         if (!$path)
43             return false;
44         
45         if (defined('DATA_PATH'))
46             return DATA_PATH . "/$path";
47         return $path;
48     }
49
50     ////////////////////////////////////////////////////////////////
51     //
52     // Date and Time formatting
53     //
54     ////////////////////////////////////////////////////////////////
55     
56     var $_dateTimeFormat = "%B %e, %Y";
57     var $_dateFormat = "%B %e, %Y";
58
59     function setDateFormat ($fs) {
60         $this->_dateFormat = $fs;
61     }
62
63     function formatDate ($time_t) {
64         return strftime($this->_dateFormat, $time_t);
65     }
66
67     function setDateTimeFormat ($fs) {
68         $this->_dateTimeFormat = $fs;
69     }
70
71     function formatDateTime ($time_t) {
72         return strftime($this->_dateTimeFormat, $time_t);
73     }
74
75     
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)));
80     }
81
82
83     ////////////////////////////////////////////////////////////////
84     //
85     // Hooks for other formatting
86     //
87     ////////////////////////////////////////////////////////////////
88
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
92
93     function getFormatter ($type, $format) {
94         $method = strtolower("get${type}Formatter");
95         if (method_exists($this, $method))
96             return $this->{$method}($format);
97         return false;
98     }
99
100     ////////////////////////////////////////////////////////////////
101     //
102     // Links
103     //
104     ////////////////////////////////////////////////////////////////
105
106     var $_autosplitWikiWords = false;
107     
108     function setAutosplitWikiWords($autosplit=false) {
109         $this->_autosplitWikiWords = $autosplit ? true : false;
110     }
111
112     function maybeSplitWikiWord ($wikiword) {
113         if ($this->_autosplitWikiWords)
114             return split_pagename($wikiword);
115         else
116             return $wikiword;
117     }
118
119     function linkExistingWikiWord($wikiword, $linktext = '', $version = false) {
120         if ($version !== false)
121             $url = WikiURL($wikiword, array('version' => $version));
122         else
123             $url = WikiURL($wikiword);
124
125         $link = HTML::a(array('href' => $url));
126
127         if (!empty($linktext)) {
128             $link->pushContent($linktext);
129             $link->setAttr('class', 'named-wiki');
130         }
131         else {
132             $link->pushContent($this->maybeSplitWikiWord($wikiword));
133             $link->setAttr('class', 'wiki');
134         }
135         return $link;
136     }
137
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));
142         
143         if (!empty($linktext)) {
144             $link->pushContent(HTML::u($linktext));
145             $link->setAttr('class', 'named-wikiunknown');
146         }
147         else {
148             $link->pushContent(HTML::u($this->maybeSplitWikiWord($wikiword)));
149             $link->setAttr('class', 'wikiunknown');
150         }
151         
152         return $link;
153     }
154
155     ////////////////////////////////////////////////////////////////
156     //
157     // Images and Icons
158     //
159     ////////////////////////////////////////////////////////////////
160
161     /**
162      *
163      * (To disable an image, alias the image to <code>false</code>.
164      */
165     function addImageAlias ($alias, $image_name) {
166         $this->_imageAliases[$alias] = $image_name;
167     }
168     
169     function getImageURL ($image) {
170         $aliases = &$this->_imageAliases;
171         
172         if (isset($aliases[$image])) {
173             $image = $aliases[$image];
174             if (!$image)
175                 return false;
176         }
177
178         // If not extension, default to .png.
179         if (!preg_match('/\.\w+$/', $image))
180             $image .= '.png';
181
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.
185         
186         return $this->_findData("images/$image", 'missing okay');
187     }
188
189     function setLinkIcon($proto, $image = false) {
190         if (!$image)
191             $image = $proto;
192         
193         $this->_linkIcons[$proto] = $image;
194     }
195     
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['*']);
202         return false;
203     }
204
205     function addButtonAlias ($text, $alias = false) {
206         $aliases = &$this->_buttonAliases;
207
208         if (is_array($text))
209             $aliases = array_merge($aliases, $text);
210         elseif ($alias === false)
211             unset($aliases[$text]);
212         else
213             $aliases[$text] = $alias;
214     }
215
216     function getButtonURL ($text) {
217         $aliases = &$this->_buttonAliases;
218         if (isset($aliases[$text]))
219             $text = $aliases[$text];
220
221         $qtext = urlencode($text);
222         $url = $this->_findButton("$qtext.png");
223         if ($url && strstr($url, '%')) {
224             $url = preg_replace('|([^/]+)$|e', 'urlencode("\\1")', $url);
225         }
226         return $url;
227     }
228
229     function _findButton ($button_file) {
230         if (!isset($this->_button_path))
231             $this->_button_path = $this->_getButtonPath();
232         
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;
237         }
238         return false;
239     }
240
241     function _getButtonPath () {
242         $button_dir = $this->file("buttons");
243         if (!file_exists($button_dir) || !is_dir($button_dir))
244             return array();
245
246         $path = array('buttons');
247
248         $dir = dir($button_dir);
249         while (($subdir = $dir->read()) !== false) {
250             if ($subdir[0] == '.')
251                 continue;
252             if (is_dir("$button_dir/$subdir"))
253                 $path[] = "buttons/$subdir";
254         }
255         $dir->close();
256         
257         return $path;
258     }
259         
260     ////////////////////////////////////////////////////////////////
261     //
262     // Button style
263     //
264     ////////////////////////////////////////////////////////////////
265
266     function makeButton ($text, $url, $class = false) {
267         // FIXME: don't always try for image button?
268
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);
273         
274         $imgurl = $this->getButtonURL($text);
275         if ($imgurl)
276             return new ImageButton($text, $url, $class, $imgurl);
277         else
278             return new Button($text, $url, $class);
279     }
280
281     function makeSubmitButton ($text, $name, $class = false) {
282         $imgurl = $this->getButtonURL($text);
283
284         if ($imgurl)
285             return new SubmitImageButton($text, $name, $class, $imgurl);
286         else
287             return new SubmitButton($text, $name, $class);
288     }
289
290     /**
291      * Make button to perform action.
292      *
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...)
296      *
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'.
299      *
300      * @param $label string Textual label for the button.  If left empty,
301      * a suitable name will be guessed.
302      *
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.
309      *
310      * @return object A Button object.
311      */
312     function makeActionButton ($action, $label = false, $page_or_rev = false) {
313         extract($this->_get_name_and_rev($page_or_rev));
314
315         if (is_array($action)) {
316             $attr = $action;
317             $action = isset($attr['action']) ? $attr['action'] : 'browse';
318         }
319         else
320             $attr['action'] = $action;
321
322         $class = is_safe_action($action) ? 'wikiaction' : 'wikiadmin';
323         if (!$label)
324             $label = $this->_labelForAction($action);
325
326         if ($version)
327             $attr['version'] = $version;
328
329         if ($action == 'browse')
330             unset($attr['action']);
331
332         return $this->makeButton($label, WikiURL($pagename, $attr), $class);
333     }
334
335     /**
336      * Make a "button" which links to a wiki-page.
337      *
338      * These are really just regular WikiLinks, possibly
339      * disguised (e.g. behind an image button) by the theme.
340      *
341      * This method should probably only be used for links
342      * which appear in page navigation bars, or similar places.
343      *
344      * Use linkExistingWikiWord, or LinkWikiWord for normal links.
345      *
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.
352      *
353      * @return object A Button object.
354      */
355     function makeLinkButton ($page_or_rev) {
356         extract($this->_get_name_and_rev($page_or_rev));
357         
358         $args = $version ? array('version' => $version) : false;
359         
360         return $this->makeButton($pagename, WikiURL($pagename, $args), 'wiki');
361     }
362
363     function _get_name_and_rev ($page_or_rev) {
364         $version = false;
365         
366         if (empty($page_or_rev)) {
367             global $request;
368             $pagename = $request->getArg("pagename");
369             $version = $request->getArg("version");
370         }
371         elseif (is_object($page_or_rev)) {
372             if (isa($page_or_rev, 'WikiDB_PageRevision')) {
373                 $rev = $page_or_rev;
374                 $page = $rev->getPage();
375                 $version = $rev->getVersion();
376             }
377             else {
378                 $page = $page_or_rev;
379             }
380             $pagename = $page->getName();
381         }
382         else {
383             $pagename = (string) $page_or_rev;
384         }
385         return compact('pagename', 'version');
386     }
387
388     function _labelForAction ($action) {
389         switch ($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");
397         default:
398             // I don't think the rest of these actually get used.
399             // 'setprefs'
400             // 'upload' 'dumpserial' 'loadfile' 'zip'
401             // 'save' 'browse'
402             return ucfirst($action);
403         }
404     }
405  
406     //----------------------------------------------------------------
407     var $_buttonSeparator = ' | ';
408     
409     function setButtonSeparator($separator) {
410         $this->_buttonSeparator = $separator;
411     }
412
413     function getButtonSeparator() {
414         return $this->_buttonSeparator;
415     }
416
417
418     ////////////////////////////////////////////////////////////////
419     //
420     // CSS
421     //
422     ////////////////////////////////////////////////////////////////
423     
424     function _CSSlink($title, $css_file, $media, $is_alt = false) {
425         $link = HTML::link(array('rel'    => $is_alt ? 'alternate stylesheet' : 'stylesheet',
426                                  'title'  => $title,
427                                  'type'   => 'text/css',
428                                  'charset'=> CHARSET,
429                                  'href'   => $this->_findData($css_file)));
430         if ($media)
431             $link->setAttr('media', $media);
432         return $link;
433     }
434
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);
439     }
440
441     function addAlternateCSS ($title, $css_file, $media = false) {
442         $this->_alternateCSS[$title] = $this->_CSSlink($title, $css_file, $media, true);
443     }
444     
445     /**
446      * @return string HTML for CSS.
447      */
448     function getCSS () {
449         $css = HTML($this->_defaultCSS);
450         if (!empty($this->_alternateCSS))
451             $css->pushContent($this->_alternateCSS);
452         return $css;
453     }
454
455     function findTemplate ($name) {
456         return $this->_path . $this->_findFile("templates/$name.tmpl");
457     }
458 };
459
460
461 /**
462  * A class representing a clickable "button".
463  *
464  * In it's simplest (default) form, a "button" is just a link associated
465  * with some sort of wiki-action.
466  */
467 class Button extends HtmlElement {
468     /** Constructor
469      *
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.
473      */
474     function Button ($text, $url, $class = false) {
475         $this->HtmlElement('a', array('href' => $url));
476         if ($class)
477             $this->setAttr('class', $class);
478         $this->pushContent($text);
479     }
480
481 };
482
483
484 /**
485  * A clickable image button.
486  */
487 class ImageButton extends Button {
488     /** Constructor
489      *
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 &lt;img&gt; tag.
495      */
496     function ImageButton ($text, $url, $class, $img_url, $img_attr = false) {
497         $this->HtmlElement('a', array('href' => $url));
498         if ($class)
499             $this->setAttr('class', $class);
500  
501         if (!is_array($img_attr))
502             $img_attr = array();
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));
508     }
509 };
510
511 /**
512  * A class representing a form <samp>submit</samp> button.
513  */
514 class SubmitButton extends HtmlElement {
515     /** Constructor
516      *
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.
520      */
521     function SubmitButton ($text, $name = false, $class = false) {
522         $this->HtmlElement('input', array('type' => 'submit',
523                                           'value' => $text));
524         if ($name)
525             $this->setAttr('name', $name);
526         if ($class)
527             $this->setAttr('class', $class);
528     }
529
530 };
531
532
533 /**
534  * A class representing an image form <samp>submit</samp> button.
535  */
536 class SubmitImageButton extends SubmitButton {
537     /** Constructor
538      *
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 &lt;img&gt; tag.
544      */
545     function SubmitImageButton ($text, $name = false, $class = false, $img_url) {
546         $this->HtmlElement('input', array('type' => 'image',
547                                           'src' => $img_url,
548                                           'value' => $text,
549                                           'alt' => $text));
550         if ($name)
551             $this->setAttr('name', $name);
552         if ($class)
553             $this->setAttr('class', $class);
554     }
555
556 };
557
558 // (c-file-style: "gnu")
559 // Local Variables:
560 // mode: php
561 // tab-width: 8
562 // c-basic-offset: 4
563 // c-hanging-comment-ender-p: nil
564 // indent-tabs-mode: nil
565 // End:   
566 ?>