]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Theme.php
Attempts to fix auto-selection of printer CSS when printing.
[SourceForge/phpwiki.git] / lib / Theme.php
1 <?php rcs_id('$Id: Theme.php,v 1.64 2003-02-24 22:06:14 dairiki Exp $');
2
3 require_once('lib/HtmlElement.php');
4
5
6 /**
7  * Make a link to a wiki page (in this wiki).
8  *
9  * This is a convenience function.
10  *
11  * @param mixed $page_or_rev
12  * Can be:<dl>
13  * <dt>A string</dt><dd>The page to link to.</dd>
14  * <dt>A WikiDB_Page object</dt><dd>The page to link to.</dd>
15  * <dt>A WikiDB_PageRevision object</dt><dd>A specific version of the page to link to.</dd>
16  * </dl>
17  *
18  * @param string $type
19  * One of:<dl>
20  * <dt>'unknown'</dt><dd>Make link appropriate for a non-existant page.</dd>
21  * <dt>'known'</dt><dd>Make link appropriate for an existing page.</dd>
22  * <dt>'auto'</dt><dd>Either 'unknown' or 'known' as appropriate.</dd>
23  * <dt>'button'</dt><dd>Make a button-style link.</dd>
24  * <dt>'if_known'</dt><dd>Only linkify if page exists.</dd>
25  * </dl>
26  * Unless $type of of the latter form, the link will be of class 'wiki', 'wikiunknown',
27  * 'named-wiki', or 'named-wikiunknown', as appropriate.
28  *
29  * @param mixed $label (string or XmlContent object)
30  * Label for the link.  If not given, defaults to the page name.
31  *
32  * @return XmlContent The link
33  */
34 function WikiLink ($page_or_rev, $type = 'known', $label = false) {
35     global $Theme, $request;
36
37     if ($type == 'button') {
38         return $Theme->makeLinkButton($page_or_rev, $label);
39     }
40
41     $version = false;
42     
43     if (isa($page_or_rev, 'WikiDB_PageRevision')) {
44         $version = $page_or_rev->getVersion();
45         $page = $page_or_rev->getPage();
46         $pagename = $page->getName();
47         $wikipage = $pagename;
48         $exists = true;
49     }
50     elseif (isa($page_or_rev, 'WikiDB_Page')) {
51         $page = $page_or_rev;
52         $pagename = $page->getName();
53         $wikipage = $pagename;
54     }
55     elseif (isa($page_or_rev, 'WikiPageName')) {
56         $wikipage = $page_or_rev;
57         $pagename = $wikipage->name;
58     }
59     else {
60         $wikipage = new WikiPageName($page_or_rev, $request->getPage());
61         $pagename = $wikipage->name;
62     }
63     
64
65     if ($type == 'auto' or $type == 'if_known') {
66         if (isset($page)) {
67             $current = $page->getCurrentRevision();
68             $exists = ! $current->hasDefaultContents();
69         }
70         else {
71             $dbi = $request->getDbh();
72             $exists = $dbi->isWikiPage($wikipage->name);
73         }
74     }
75     elseif ($type == 'unknown') {
76         $exists = false;
77     }
78     else {
79         $exists = true;
80     }
81
82     // FIXME: this should be somewhere else, if really needed.
83     // WikiLink makes A link, not a string of fancy ones.
84     // (I think that the fancy split links are just confusing.)
85     // Todo: test external ImageLinks http://some/images/next.gif
86     if (isa($wikipage, 'WikiPageName') and !$label 
87         and strchr(substr($wikipage->shortName,1), SUBPAGE_SEPARATOR)) {
88         $parts = explode(SUBPAGE_SEPARATOR, $wikipage->shortName);
89         $last_part = array_pop($parts);
90         $sep = '';
91         $link = HTML::span();
92         foreach ($parts as $part) {
93             $path[] = $part;
94             $parent = join(SUBPAGE_SEPARATOR, $path);
95             if ($part)
96                 $link->pushContent($Theme->linkExistingWikiWord($parent, $sep . $part));
97             $sep = SUBPAGE_SEPARATOR;
98         }
99         if ($exists)
100             $link->pushContent($Theme->linkExistingWikiWord($wikipage, $sep . $last_part, $version));
101         else
102             $link->pushContent($Theme->linkUnknownWikiWord($wikipage, $last_part));
103         return $link;
104     }
105
106     if ($exists) {
107         return $Theme->linkExistingWikiWord($wikipage, $label, $version);
108     }
109     elseif ($type == 'if_known') {
110         if (!$label && isa($wikipage, 'WikiPageName'))
111             $label = $wikipage->shortName;
112         return HTML($label ? $label : $pagename);
113     }
114     else {
115         return $Theme->linkUnknownWikiWord($wikipage, $label);
116     }
117 }
118
119
120
121 /**
122  * Make a button.
123  *
124  * This is a convenience function.
125  *
126  * @param $action string
127  * One of <dl>
128  * <dt>[action]</dt><dd>Perform action (e.g. 'edit') on the selected page.</dd>
129  * <dt>[ActionPage]</dt><dd>Run the actionpage (e.g. 'BackLinks') on the selected page.</dd>
130  * <dt>'submit:'[name]</dt><dd>Make a form submission button with the given name.
131  *      ([name] can be blank for a nameless submit button.)</dd>
132  * <dt>a hash</dt><dd>Query args for the action. E.g.<pre>
133  *      array('action' => 'diff', 'previous' => 'author')
134  * </pre></dd>
135  * </dl>
136  *
137  * @param $label string
138  * A label for the button.  If ommited, a suitable default (based on the valued of $action)
139  * will be picked.
140  *
141  * @param $page_or_rev mixed
142  * Which page (& version) to perform the action on.
143  * Can be one of:<dl>
144  * <dt>A string</dt><dd>The pagename.</dd>
145  * <dt>A WikiDB_Page object</dt><dd>The page.</dd>
146  * <dt>A WikiDB_PageRevision object</dt><dd>A specific version of the page.</dd>
147  * </dl>
148  * ($Page_or_rev is ignored for submit buttons.)
149  */
150 function Button ($action, $label = false, $page_or_rev = false) {
151     global $Theme;
152
153     if (!is_array($action) && preg_match('/submit:(.*)/A', $action, $m))
154         return $Theme->makeSubmitButton($label, $m[1], $class = $page_or_rev);
155     else
156         return $Theme->makeActionButton($action, $label, $page_or_rev);
157 }
158
159
160 class Theme {
161     var $HTML_DUMP_SUFFIX = '';
162     function Theme ($theme_name = 'default') {
163         $this->_name = $theme_name;
164         $themes_dir = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/themes" : "themes";
165
166         $this->_path  = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/" : "";
167         $this->_theme = "themes/$theme_name";
168
169         if ($theme_name != 'default')
170             $this->_default_theme = new Theme;
171
172         $this->_css = array();
173     }
174
175     function file ($file) {
176         return $this->_path . "$this->_theme/$file";
177     }
178
179     function _findFile ($file, $missing_okay = false) {
180         if (file_exists($this->_path . "$this->_theme/$file"))
181             return "$this->_theme/$file";
182
183         // FIXME: this is a short-term hack.  Delete this after all files
184         // get moved into themes/...
185         if (file_exists($this->_path . $file))
186             return $file;
187
188
189         if (isset($this->_default_theme)) {
190             return $this->_default_theme->_findFile($file, $missing_okay);
191         }
192         else if (!$missing_okay) {
193             trigger_error("$file: not found", E_USER_NOTICE);
194         }
195         return false;
196     }
197
198     function _findData ($file, $missing_okay = false) {
199         $path = $this->_findFile($file, $missing_okay);
200         if (!$path)
201             return false;
202
203         if (defined('DATA_PATH'))
204             return DATA_PATH . "/$path";
205         return $path;
206     }
207
208     ////////////////////////////////////////////////////////////////
209     //
210     // Date and Time formatting
211     //
212     ////////////////////////////////////////////////////////////////
213
214     // Note:  Windows' implemetation of strftime does not include certain
215         // format specifiers, such as %e (for date without leading zeros).  In
216         // general, see:
217         // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strftime.2c_.wcsftime.asp
218         // As a result, we have to use %d, and strip out leading zeros ourselves.
219
220     var $_dateFormat = "%B %d, %Y";
221     var $_timeFormat = "%I:%M %p";
222
223     var $_showModTime = true;
224
225     /**
226      * Set format string used for dates.
227      *
228      * @param $fs string Format string for dates.
229      *
230      * @param $show_mod_time bool If true (default) then times
231      * are included in the messages generated by getLastModifiedMessage(),
232      * otherwise, only the date of last modification will be shown.
233      */
234     function setDateFormat ($fs, $show_mod_time = true) {
235         $this->_dateFormat = $fs;
236         $this->_showModTime = $show_mod_time;
237     }
238
239     /**
240      * Set format string used for times.
241      *
242      * @param $fs string Format string for times.
243      */
244     function setTimeFormat ($fs) {
245         $this->_timeFormat = $fs;
246     }
247
248     /**
249      * Format a date.
250      *
251      * Any time zone offset specified in the users preferences is
252      * taken into account by this method.
253      *
254      * @param $time_t integer Unix-style time.
255      *
256      * @return string The date.
257      */
258     function formatDate ($time_t) {
259         global $request;
260         
261         $offset_time = $time_t + 3600 * $request->getPref('timeOffset');
262         // strip leading zeros from date elements (ie space followed by zero)
263         return preg_replace('/ 0/', ' ', 
264                             strftime($this->_dateFormat, $offset_time));
265     }
266
267     /**
268      * Format a date.
269      *
270      * Any time zone offset specified in the users preferences is
271      * taken into account by this method.
272      *
273      * @param $time_t integer Unix-style time.
274      *
275      * @return string The time.
276      */
277     function formatTime ($time_t) {
278         //FIXME: make 24-hour mode configurable?
279         global $request;
280         $offset_time = $time_t + 3600 * $request->getPref('timeOffset');
281         return preg_replace('/^0/', ' ',
282                             strtolower(strftime($this->_timeFormat, $offset_time)));
283     }
284
285     /**
286      * Format a date and time.
287      *
288      * Any time zone offset specified in the users preferences is
289      * taken into account by this method.
290      *
291      * @param $time_t integer Unix-style time.
292      *
293      * @return string The date and time.
294      */
295     function formatDateTime ($time_t) {
296         return $this->formatDate($time_t) . ' ' . $this->formatTime($time_t);
297     }
298
299     /**
300      * Format a (possibly relative) date.
301      *
302      * If enabled in the users preferences, this method might
303      * return a relative day (e.g. 'Today', 'Yesterday').
304      *
305      * Any time zone offset specified in the users preferences is
306      * taken into account by this method.
307      *
308      * @param $time_t integer Unix-style time.
309      *
310      * @return string The day.
311      */
312     function getDay ($time_t) {
313         global $request;
314         
315         if ($request->getPref('relativeDates') && ($date = $this->_relativeDay($time_t))) {
316             return ucfirst($date);
317         }
318         return $this->formatDate($time_t);
319     }
320     
321     /**
322      * Format the "last modified" message for a page revision.
323      *
324      * @param $revision object A WikiDB_PageRevision object.
325      *
326      * @param $show_version bool Should the page version number
327      * be included in the message.  (If this argument is omitted,
328      * then the version number will be shown only iff the revision
329      * is not the current one.
330      *
331      * @return string The "last modified" message.
332      */
333     function getLastModifiedMessage ($revision, $show_version = 'auto') {
334         global $request;
335
336         // dates >= this are considered invalid.
337         if (! defined('EPOCH'))
338             define('EPOCH', 0); // seconds since ~ January 1 1970
339
340         $mtime = $revision->get('mtime');
341         if ($mtime <= EPOCH)
342             return fmt("Never edited.");
343
344         if ($show_version == 'auto')
345             $show_version = !$revision->isCurrent();
346
347         if ($request->getPref('relativeDates') && ($date = $this->_relativeDay($mtime))) {
348             if ($this->_showModTime)
349                 $date =  sprintf(_("%s at %s"),
350                                  $date, $this->formatTime($mtime));
351             
352             if ($show_version)
353                 return fmt("Version %s, saved %s.", $revision->getVersion(), $date);
354             else
355                 return fmt("Last edited %s.", $date);
356         }
357
358         if ($this->_showModTime)
359             $date = $this->formatDateTime($mtime);
360         else
361             $date = $this->formatDate($mtime);
362         
363         if ($show_version)
364             return fmt("Version %s, saved on %s.", $revision->getVersion(), $date);
365         else
366             return fmt("Last edited on %s.", $date);
367     }
368     
369     function _relativeDay ($time_t) {
370         global $request;
371         $offset = 3600 * $request->getPref('timeOffset');
372
373         $now = time() + $offset;
374         $today = localtime($now, true);
375         $time = localtime($time_t + $offset, true);
376
377         if ($time['tm_yday'] == $today['tm_yday'] && $time['tm_year'] == $today['tm_year'])
378             return _("today");
379         
380         // Note that due to daylight savings chages (and leap seconds), $now minus
381         // 24 hours is not guaranteed to be yesterday.
382         $yesterday = localtime($now - (12 + $today['tm_hour']) * 3600, true);
383         if ($time['tm_yday'] == $yesterday['tm_yday'] && $time['tm_year'] == $yesterday['tm_year'])
384             return _("yesterday");
385
386         return false;
387     }
388
389     ////////////////////////////////////////////////////////////////
390     //
391     // Hooks for other formatting
392     //
393     ////////////////////////////////////////////////////////////////
394
395     //FIXME: PHP 4.1 Warnings
396     //lib/Theme.php:84: Notice[8]: The call_user_method() function is deprecated,
397     //use the call_user_func variety with the array(&$obj, "method") syntax instead
398
399     function getFormatter ($type, $format) {
400         $method = strtolower("get${type}Formatter");
401         if (method_exists($this, $method))
402             return $this->{$method}($format);
403         return false;
404     }
405
406     ////////////////////////////////////////////////////////////////
407     //
408     // Links
409     //
410     ////////////////////////////////////////////////////////////////
411
412     var $_autosplitWikiWords = false;
413
414     function setAutosplitWikiWords($autosplit=false) {
415         $this->_autosplitWikiWords = $autosplit ? true : false;
416     }
417
418     function maybeSplitWikiWord ($wikiword) {
419         if ($this->_autosplitWikiWords)
420             return split_pagename($wikiword);
421         else
422             return $wikiword;
423     }
424
425     function linkExistingWikiWord($wikiword, $linktext = '', $version = false) {
426         global $request;
427
428         if ($version !== false)
429             $url = WikiURL($wikiword, array('version' => $version));
430         else
431             $url = WikiURL($wikiword);
432
433         // Extra steps for dumping page to an html file.
434         // FIXME: shouldn't this be in WikiURL?
435         if ($this->HTML_DUMP_SUFFIX) {
436             // urlencode for pagenames with accented letters
437             $url = rawurlencode($url);
438             $url = preg_replace('/^\./', '%2e', $url);
439             $url .= $this->HTML_DUMP_SUFFIX;
440         }
441
442         $link = HTML::a(array('href' => $url));
443
444         if (isa($wikiword, 'WikiPageName'))
445             $default_text = $wikiword->shortName;
446         else
447             $default_text = $wikiword;
448         
449         if (!empty($linktext)) {
450             $link->pushContent($linktext);
451             $link->setAttr('class', 'named-wiki');
452             $link->setAttr('title', $this->maybeSplitWikiWord($default_text));
453         }
454         else {
455             $link->pushContent($this->maybeSplitWikiWord($default_text));
456             $link->setAttr('class', 'wiki');
457         }
458         if ($request->getArg('frame'))
459             $link->setAttr('target', '_top');
460         return $link;
461     }
462
463     function linkUnknownWikiWord($wikiword, $linktext = '') {
464         global $request;
465
466         // Get rid of anchors on unknown wikiwords
467         if (isa($wikiword, 'WikiPageName')) {
468             $default_text = $wikiword->shortName;
469             $wikiword = $wikiword->name;
470         }
471         else {
472             $default_text = $wikiword;
473         }
474         
475         $url = WikiURL($wikiword, array('action' => 'edit'));
476         //$link = HTML::span(HTML::a(array('href' => $url), '?'));
477         $button = $this->makeButton('?', $url);
478         $button->addTooltip(sprintf(_("Edit: %s"), $wikiword));
479         $link = HTML::span($button);
480
481
482         if (!empty($linktext)) {
483             $link->pushContent(HTML::u($linktext));
484             $link->setAttr('class', 'named-wikiunknown');
485         }
486         else {
487             $link->pushContent(HTML::u($this->maybeSplitWikiWord($default_text)));
488             $link->setAttr('class', 'wikiunknown');
489         }
490         if ($request->getArg('frame'))
491             $link->setAttr('target', '_top');
492
493         return $link;
494     }
495
496     ////////////////////////////////////////////////////////////////
497     //
498     // Images and Icons
499     //
500     ////////////////////////////////////////////////////////////////
501     var $_imageAliases = array();
502     var $_imageAlt = array();
503
504     /**
505      *
506      * (To disable an image, alias the image to <code>false</code>.
507      */
508     function addImageAlias ($alias, $image_name) {
509         $this->_imageAliases[$alias] = $image_name;
510     }
511
512     function addImageAlt ($alias, $alt_text) {
513         $this->_imageAlt[$alias] = $alt_text;
514     }
515     function getImageAlt ($alias) {
516         return $this->_imageAlt[$alias];
517     }
518
519     function getImageURL ($image) {
520         $aliases = &$this->_imageAliases;
521
522         if (isset($aliases[$image])) {
523             $image = $aliases[$image];
524             if (!$image)
525                 return false;
526         }
527
528         // If not extension, default to .png.
529         if (!preg_match('/\.\w+$/', $image))
530             $image .= '.png';
531
532         // FIXME: this should probably be made to fall back
533         //        automatically to .gif, .jpg.
534         //        Also try .gif before .png if browser doesn't like png.
535
536         $path = $this->_findData("images/$image", 'missing okay');
537         if (!$path) // search explicit images/ or button/ links also
538             return $this->_findData("$image", 'missing okay');
539         else 
540             return $path;       
541     }
542
543     function setLinkIcon($proto, $image = false) {
544         if (!$image)
545             $image = $proto;
546
547         $this->_linkIcons[$proto] = $image;
548     }
549
550     function getLinkIconURL ($proto) {
551         $icons = &$this->_linkIcons;
552         if (!empty($icons[$proto]))
553             return $this->getImageURL($icons[$proto]);
554         elseif (!empty($icons['*']))
555             return $this->getImageURL($icons['*']);
556         return false;
557     }
558
559     function addButtonAlias ($text, $alias = false) {
560         $aliases = &$this->_buttonAliases;
561
562         if (is_array($text))
563             $aliases = array_merge($aliases, $text);
564         elseif ($alias === false)
565             unset($aliases[$text]);
566         else
567             $aliases[$text] = $alias;
568     }
569
570     function getButtonURL ($text) {
571         $aliases = &$this->_buttonAliases;
572         if (isset($aliases[$text]))
573             $text = $aliases[$text];
574
575         $qtext = urlencode($text);
576         $url = $this->_findButton("$qtext.png");
577         if ($url && strstr($url, '%')) {
578             $url = preg_replace('|([^/]+)$|e', 'urlencode("\\1")', $url);
579         }
580         if (!$url) {// Jeff complained about png not supported everywhere. This is not PC
581             $url = $this->_findButton("$qtext.gif");
582             if ($url && strstr($url, '%')) {
583                 $url = preg_replace('|([^/]+)$|e', 'urlencode("\\1")', $url);
584             }
585         }
586         return $url;
587     }
588
589     function _findButton ($button_file) {
590         if (!isset($this->_button_path))
591             $this->_button_path = $this->_getButtonPath();
592
593         foreach ($this->_button_path as $dir) {
594             $path = "$this->_theme/$dir/$button_file";
595             if (file_exists($this->_path . $path))
596                 return defined('DATA_PATH') ? DATA_PATH . "/$path" : $path;
597         }
598         return false;
599     }
600
601     function _getButtonPath () {
602         $button_dir = $this->file("buttons");
603         if (!file_exists($button_dir) || !is_dir($button_dir))
604             return array();
605
606         $path = array('buttons');
607
608         $dir = dir($button_dir);
609         while (($subdir = $dir->read()) !== false) {
610             if ($subdir[0] == '.')
611                 continue;
612             if (is_dir("$button_dir/$subdir"))
613                 $path[] = "buttons/$subdir";
614         }
615         $dir->close();
616
617         return $path;
618     }
619
620     ////////////////////////////////////////////////////////////////
621     //
622     // Button style
623     //
624     ////////////////////////////////////////////////////////////////
625
626     function makeButton ($text, $url, $class = false) {
627         // FIXME: don't always try for image button?
628
629         // Special case: URLs like 'submit:preview' generate form
630         // submission buttons.
631         if (preg_match('/^submit:(.*)$/', $url, $m))
632             return $this->makeSubmitButton($text, $m[1], $class);
633
634         $imgurl = $this->getButtonURL($text);
635         if ($imgurl)
636             return new ImageButton($text, $url, $class, $imgurl);
637         else
638             return new Button($text, $url, $class);
639     }
640
641     function makeSubmitButton ($text, $name, $class = false) {
642         $imgurl = $this->getButtonURL($text);
643
644         if ($imgurl)
645             return new SubmitImageButton($text, $name, $class, $imgurl);
646         else
647             return new SubmitButton($text, $name, $class);
648     }
649
650     /**
651      * Make button to perform action.
652      *
653      * This constructs a button which performs an action on the
654      * currently selected version of the current page.
655      * (Or anotherpage or version, if you want...)
656      *
657      * @param $action string The action to perform (e.g. 'edit', 'lock').
658      * This can also be the name of an "action page" like 'LikePages'.
659      * Alternatively you can give a hash of query args to be applied
660      * to the page.
661      *
662      * @param $label string Textual label for the button.  If left empty,
663      * a suitable name will be guessed.
664      *
665      * @param $page_or_rev mixed  The page to link to.  This can be
666      * given as a string (the page name), a WikiDB_Page object, or as
667      * WikiDB_PageRevision object.  If given as a WikiDB_PageRevision
668      * object, the button will link to a specific version of the
669      * designated page, otherwise the button links to the most recent
670      * version of the page.
671      *
672      * @return object A Button object.
673      */
674     function makeActionButton ($action, $label = false, $page_or_rev = false) {
675         extract($this->_get_name_and_rev($page_or_rev));
676
677         if (is_array($action)) {
678             $attr = $action;
679             $action = isset($attr['action']) ? $attr['action'] : 'browse';
680         }
681         else
682             $attr['action'] = $action;
683
684         $class = is_safe_action($action) ? 'wikiaction' : 'wikiadmin';
685         if (!$label)
686             $label = $this->_labelForAction($action);
687
688         if ($version)
689             $attr['version'] = $version;
690
691         if ($action == 'browse')
692             unset($attr['action']);
693
694         return $this->makeButton($label, WikiURL($pagename, $attr), $class);
695     }
696
697     /**
698      * Make a "button" which links to a wiki-page.
699      *
700      * These are really just regular WikiLinks, possibly
701      * disguised (e.g. behind an image button) by the theme.
702      *
703      * This method should probably only be used for links
704      * which appear in page navigation bars, or similar places.
705      *
706      * Use linkExistingWikiWord, or LinkWikiWord for normal links.
707      *
708      * @param $page_or_rev mixed The page to link to.  This can be
709      * given as a string (the page name), a WikiDB_Page object, or as
710      * WikiDB_PageRevision object.  If given as a WikiDB_PageRevision
711      * object, the button will link to a specific version of the
712      * designated page, otherwise the button links to the most recent
713      * version of the page.
714      *
715      * @return object A Button object.
716      */
717     function makeLinkButton ($page_or_rev, $label = false) {
718         extract($this->_get_name_and_rev($page_or_rev));
719
720         $args = $version ? array('version' => $version) : false;
721
722         return $this->makeButton($label ? $label : $this->maybeSplitWikiWord($pagename), 
723                                  WikiURL($pagename, $args), 'wiki');
724     }
725
726     function _get_name_and_rev ($page_or_rev) {
727         $version = false;
728
729         if (empty($page_or_rev)) {
730             global $request;
731             $pagename = $request->getArg("pagename");
732             $version = $request->getArg("version");
733         }
734         elseif (is_object($page_or_rev)) {
735             if (isa($page_or_rev, 'WikiDB_PageRevision')) {
736                 $rev = $page_or_rev;
737                 $page = $rev->getPage();
738                 $version = $rev->getVersion();
739             }
740             else {
741                 $page = $page_or_rev;
742             }
743             $pagename = $page->getName();
744         }
745         else {
746             $pagename = (string) $page_or_rev;
747         }
748         return compact('pagename', 'version');
749     }
750
751     function _labelForAction ($action) {
752         switch ($action) {
753             case 'edit':   return _("Edit");
754             case 'diff':   return _("Diff");
755             case 'logout': return _("Sign Out");
756             case 'login':  return _("Sign In");
757             case 'lock':   return _("Lock Page");
758             case 'unlock': return _("Unlock Page");
759             case 'remove': return _("Remove Page");
760             default:
761                 // I don't think the rest of these actually get used.
762                 // 'setprefs'
763                 // 'upload' 'dumpserial' 'loadfile' 'zip'
764                 // 'save' 'browse'
765                 return gettext(ucfirst($action));
766         }
767     }
768
769     //----------------------------------------------------------------
770     var $_buttonSeparator = "\n | ";
771
772     function setButtonSeparator($separator) {
773         $this->_buttonSeparator = $separator;
774     }
775
776     function getButtonSeparator() {
777         return $this->_buttonSeparator;
778     }
779
780
781     ////////////////////////////////////////////////////////////////
782     //
783     // CSS
784     //
785     // Notes:
786     //
787     // Based on testing with Galeon 1.2.7 (Mozilla 1.2):
788     // Automatic media-based style selection (via <link> tags) only
789     // seems to work for the default style, not for alternate styles.
790     //
791     // Doing
792     //
793     //  <link rel="stylesheet" type="text/css" href="phpwiki.css" />
794     //  <link rel="stylesheet" type="text/css" href="phpwiki-printer.css" media="print" />
795     //
796     // works to make it so that the printer style sheet get used
797     // automatically when printing (or print-previewing) a page
798     // (but when only when the default style is selected.)
799     //
800     // Attempts like:
801     //
802     //  <link rel="alternate stylesheet" title="Modern"
803     //        type="text/css" href="phpwiki-modern.css" />
804     //  <link rel="alternate stylesheet" title="Modern"
805     //        type="text/css" href="phpwiki-printer.css" media="print" />
806     //
807     // Result in two "Modern" choices when trying to select alternate style.
808     // If one selects the first of those choices, one gets phpwiki-modern
809     // both when browsing and printing.  If one selects the second "Modern",
810     // one gets no CSS when browsing, and phpwiki-printer when printing.
811     //
812     // The Real Fix?
813     // =============
814     //
815     // We should probably move to doing the media based style
816     // switching in the CSS files themselves using, e.g.:
817     //
818     //  @import url(print.css) print;
819     //
820     ////////////////////////////////////////////////////////////////
821
822     function _CSSlink($title, $css_file, $media, $is_alt = false) {
823         // Don't set title on default style.  This makes it clear to
824         // the user which is the default (i.e. most supported) style.
825         $link = HTML::link(array('rel'     => $is_alt ? 'alternate stylesheet' : 'stylesheet',
826                                  'type'    => 'text/css',
827                                  'charset' => CHARSET,
828                                  'href'    => $this->_findData($css_file)));
829         if ($is_alt)
830             $link->setAttr('title', $title);
831
832         if ($media) 
833             $link->setAttr('media', $media);
834         
835         return $link;
836     }
837
838     /** Set default CSS source for this theme.
839      *
840      * To set styles to be used for different media, pass a
841      * hash for the second argument, e.g.
842      *
843      * $theme->setDefaultCSS('default', array('' => 'normal.css',
844      *                                        'print' => 'printer.css'));
845      *
846      * If you call this more than once, the last one called takes
847      * precedence as the default style.
848      *
849      * @param string $title Name of style (currently ignored, unless
850      * you call this more than once, in which case, some of the style
851      * will become alternate (rather than default) styles, and then their
852      * titles will be used.
853      *
854      * @param mixed $css_files Name of CSS file, or hash containing a mapping
855      * between media types and CSS file names.  Use a key of '' (the empty string)
856      * to set the default CSS for non-specified media.  (See above for an example.)
857      */
858     function setDefaultCSS ($title, $css_files) {
859         if (!is_array($css_files))
860             $css_files = array('' => $css_files);
861         // Add to the front of $this->_css
862         unset($this->_css[$title]);
863         $this->_css = array_merge(array($title => $css_files), $this->_css);
864     }
865
866     /** Set alternate CSS source for this theme.
867      *
868      * @param string $title Name of style.
869      * @param string $css_files Name of CSS file.
870      */
871     function addAlternateCSS ($title, $css_files) {
872         if (!is_array($css_files))
873             $css_files = array('' => $css_files);
874         $this->_css[$title] = $css_files;
875     }
876
877     /**
878         * @return string HTML for CSS.
879      */
880     function getCSS () {
881         $css = array();
882         $is_alt = false;
883         foreach ($this->_css as $title => $css_files) {
884             aksort($css_files); // move $css_files[''] to front.
885             foreach ($css_files as $media => $css_file) {
886                 $css[] = $this->_CSSlink($title, $css_file, $media, $is_alt);
887                 if ($is_alt) break;
888                 
889             }
890             $is_alt = true;
891         }
892         return HTML($css);
893     }
894     
895
896     function findTemplate ($name) {
897         return $this->_path . $this->_findFile("templates/$name.tmpl");
898     }
899 };
900
901
902 /**
903  * A class representing a clickable "button".
904  *
905  * In it's simplest (default) form, a "button" is just a link associated
906  * with some sort of wiki-action.
907  */
908 class Button extends HtmlElement {
909     /** Constructor
910      *
911      * @param $text string The text for the button.
912      * @param $url string The url (href) for the button.
913      * @param $class string The CSS class for the button.
914      */
915     function Button ($text, $url, $class = false) {
916         global $request;
917         $this->HtmlElement('a', array('href' => $url));
918         if ($class)
919             $this->setAttr('class', $class);
920         if ($request->getArg('frame'))
921             $this->setAttr('target', '_top');
922         $this->pushContent($GLOBALS['Theme']->maybeSplitWikiWord($text));
923     }
924
925 };
926
927
928 /**
929  * A clickable image button.
930  */
931 class ImageButton extends Button {
932     /** Constructor
933      *
934      * @param $text string The text for the button.
935      * @param $url string The url (href) for the button.
936      * @param $class string The CSS class for the button.
937      * @param $img_url string URL for button's image.
938      * @param $img_attr array Additional attributes for the &lt;img&gt; tag.
939      */
940     function ImageButton ($text, $url, $class, $img_url, $img_attr = false) {
941         $this->HtmlElement('a', array('href' => $url));
942         if ($class)
943             $this->setAttr('class', $class);
944
945         if (!is_array($img_attr))
946             $img_attr = array();
947         $img_attr['src'] = $img_url;
948         $img_attr['alt'] = $text;
949         $img_attr['class'] = 'wiki-button';
950         $img_attr['border'] = 0;
951         $this->pushContent(HTML::img($img_attr));
952     }
953 };
954
955 /**
956  * A class representing a form <samp>submit</samp> button.
957  */
958 class SubmitButton extends HtmlElement {
959     /** Constructor
960      *
961      * @param $text string The text for the button.
962      * @param $name string The name of the form field.
963      * @param $class string The CSS class for the button.
964      */
965     function SubmitButton ($text, $name = false, $class = false) {
966         $this->HtmlElement('input', array('type' => 'submit',
967                                           'value' => $text));
968         if ($name)
969             $this->setAttr('name', $name);
970         if ($class)
971             $this->setAttr('class', $class);
972     }
973
974 };
975
976
977 /**
978  * A class representing an image form <samp>submit</samp> button.
979  */
980 class SubmitImageButton extends SubmitButton {
981     /** Constructor
982      *
983      * @param $text string The text for the button.
984      * @param $name string The name of the form field.
985      * @param $class string The CSS class for the button.
986      * @param $img_url string URL for button's image.
987      * @param $img_attr array Additional attributes for the &lt;img&gt; tag.
988      */
989     function SubmitImageButton ($text, $name = false, $class = false, $img_url) {
990         $this->HtmlElement('input', array('type'  => 'image',
991                                           'src'   => $img_url,
992                                           'value' => $text,
993                                           'alt'   => $text));
994         if ($name)
995             $this->setAttr('name', $name);
996         if ($class)
997             $this->setAttr('class', $class);
998     }
999
1000 };
1001
1002 // $Log: not supported by cvs2svn $
1003 // Revision 1.63  2003/02/23 03:37:05  dairiki
1004 // Stupid typo/bug fix.
1005 //
1006 // Revision 1.62  2003/02/21 04:14:52  dairiki
1007 // New WikiLink type 'if_known'.  This gives linkified name if page
1008 // exists, otherwise, just plain text.
1009 //
1010 // Revision 1.61  2003/02/18 21:52:05  dairiki
1011 // Fix so that one can still link to wiki pages with # in their names.
1012 // (This was made difficult by the introduction of named tags, since
1013 // '[Page #1]' is now a link to anchor '1' in page 'Page'.
1014 //
1015 // Now the ~ escape for page names should work: [Page ~#1].
1016 //
1017 // Revision 1.60  2003/02/15 01:59:47  dairiki
1018 // Theme::getCSS():  Add Default-Style HTTP(-eqiv) header in attempt
1019 // to fix default stylesheet selection on some browsers.
1020 // For details on the Default-Style header, see:
1021 //  http://home.dairiki.org/docs/html4/present/styles.html#h-14.3.2
1022 //
1023 // Revision 1.59  2003/01/04 22:30:16  carstenklapp
1024 // New: display a "Never edited." message instead of an invalid epoch date.
1025 //
1026
1027 // (c-file-style: "gnu")
1028 // Local Variables:
1029 // mode: php
1030 // tab-width: 8
1031 // c-basic-offset: 4
1032 // c-hanging-comment-ender-p: nil
1033 // indent-tabs-mode: nil
1034 // End:
1035 ?>