]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
Minor refactor of PageList row coloring.
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.17 2002-01-22 15:36:42 dairiki Exp $');
2
3 /**
4  * This library relieves some work for these plugins:
5  *
6  * BackLinks, LikePages, Mostpopular, TitleSearch
7  *
8  * It also allows dynamic expansion of those plugins to include more
9  * columns in their output.
10  *
11  *
12  * Column arguments:
13  *
14  * 'mtime'   _("Last Modified")
15  * 'hits'    _("Hits")
16  * 'summary' _("Last Summary")
17  * 'author'  _("Last Author")),
18  * 'locked'  _("Locked"), _("locked")
19  * 'minor'   _("Minor Edit"), _("minor")
20  *
21  *
22  * FIXME?: Make caption work properly with new HtmlElement
23  *
24  * FIXME: In this refactoring I have un-implemented _ctime, _cauthor, and
25  * number-of-revision.  Note the _ctime and _cauthor as they were implemented
26  * were somewhat flawed: revision 1 of a page doesn't have to exist in the
27  * database.  If lots of revisions have been made to a page, it's more than likely
28  * that some older revisions (include revision 1) have been cleaned (deleted).
29  */
30 class _PageList_Column_base {
31     function _PageList_Column_base ($default_heading, $align = false) {
32         $this->_heading = $default_heading;
33
34         $this->_tdattr = array();
35         if ($align)
36             $this->_tdattr['align'] = $align;
37     }
38     
39     function format ($page_handle, &$revision_handle) {
40         return HTML::td($this->_tdattr,
41                         NBSP,
42                         $this->_getValue($page_handle, &$revision_handle),
43                         NBSP);
44     }
45
46     function setHeading ($heading) {
47         $this->_heading = $heading;
48     }
49     
50     function heading () {
51         return HTML::td(array('align' => 'center'),
52                         NBSP, HTML::u($this->_heading), NBSP);
53     }
54 };
55
56 class _PageList_Column extends _PageList_Column_base {
57     function _PageList_Column ($field, $default_heading, $align = false) {
58         $this->_PageList_Column_base($default_heading, $align);
59         
60         $this->_need_rev = substr($field, 0, 4) == 'rev:';
61         if ($this->_need_rev)
62             $this->_field = substr($field, 4);
63         else
64             $this->_field = $field;
65     }
66     
67     function _getValue ($page_handle, &$revision_handle) {
68         if ($this->_need_rev) {
69             if (!$revision_handle)
70                 $revision_handle = $page_handle->getCurrentRevision();
71             return $revision_handle->get($this->_field);
72         }
73         else {
74             return $page_handle->get($this->_field);
75         }
76     }
77 };
78
79 class _PageList_Column_bool extends _PageList_Column {
80     function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
81         $this->_PageList_Column($field, $default_heading, 'center');
82         $this->_textIfTrue = $text;
83         $this->_textIfFalse = new RawXml('&#8212;');
84     }
85     
86     function _getValue ($page_handle, &$revision_handle) {
87         $val = _PageList_Column::_getValue($page_handle, $revision_handle);
88         return $val ? $this->_textIfTrue : $this->_textIfFalse;
89     }
90 };
91
92 class _PageList_Column_time extends _PageList_Column {
93     function _PageList_Column_time ($field, $default_heading) {
94         $this->_PageList_Column($field, $default_heading, 'right');
95     }
96     
97     function _getValue ($page_handle, &$revision_handle) {
98         global $Theme;
99         $time = _PageList_Column::_getValue($page_handle, $revision_handle);
100         return $Theme->formatDateTime($time);
101     }
102 };
103
104 class _PageList_Column_pagename extends _PageList_Column_base {
105     function _PageList_Column_pagename () {
106         $this->_PageList_Column_base(_("Page Name"));
107     }
108     
109     function _getValue ($page_handle, &$revision_handle) {
110         return LinkExistingWikiWord($page_handle->getName());
111     }
112 };
113
114         
115 class PageList {
116     function PageList () {
117         $this->_caption = "";
118         $this->_columns = array(new _PageList_Column_pagename);
119         $this->_pages = array();
120         $this->_messageIfEmpty = _("<no matches>");
121         $this->_group_rows = 3;
122     }
123
124     function setCaption ($caption_string) {
125         $this->_caption = $caption_string;
126     }
127
128     function getCaption () {
129         // put the total into the caption if needed
130         if (is_string($this->_caption) && strstr($this->_caption, '%d'))
131             return sprintf($this->_caption, $this->getTotal());
132         return $this->_caption;
133     }
134
135     function setMessageIfEmpty ($msg) {
136         $this->_messageIfEmpty = $msg;
137     }
138
139     /**
140      * Add a column to the listing.
141      *
142      * @input $column string Which column to add.
143      * $Column can be one of <ul>
144      * <li>mtime
145      * <li>hits
146      * <li>summary
147      * <li>author
148      * <li>locked
149      * <li>minor
150      * </ul>
151      *
152      * If you would like to specify an alternate heading for the
153      * column, concatenate the desired adding to $column, after adding
154      * a colon.  E.g. 'hits:Page Views'.
155      */ 
156     function addColumn ($column) {
157         if (($col = $this->_getColumn($new_columnname)))
158             array_push($this->_columns, $col);
159     }
160
161     function insertColumn ($new_columnname) {
162         if (($col = $this->_getColumn($new_columnname)))
163             array_unshift($this->_columns, $col);
164     }
165
166
167
168     function addPage ($page_handle) {
169         array_push($this->_pages, &$page_handle);
170     }
171
172     function getTotal () {
173         return count($this->_pages);
174     }
175
176     function isEmpty () {
177         return empty($this->_pages);
178     }
179     
180
181     function getContent() {
182         // Note that the <caption> element wants inline content.
183         $caption = $this->getCaption();
184
185         if ($this->isEmpty())
186             return $this->_emptyList($caption);
187         elseif (count($this->_columns) == 1)
188             return $this->_generateList($caption);
189         else
190             return $this->_generateTable($caption);
191     }
192
193     function printXML() {
194         PrintXML($this->getContent());
195     }
196
197     function asXML() {
198         return AsXML($this->getContent());
199     }
200
201
202     ////////////////////
203     // private
204     ////////////////////
205     function _getColumn ($column) {
206         static $types;
207         if (empty($types)) {
208             $types = array( 'mtime'
209                             => new _PageList_Column_time('rev:mtime', _("Last Modified")),
210                             'hits'
211                             => new _PageList_Column('hits',  _("Hits"), 'right'),
212                             'summary'
213                             => new _PageList_Column('rev:summary',  _("Last Summary")),
214                             'author'
215                             => new _PageList_Column('rev:author',  _("Last Author")),
216                             'locked'
217                             => new _PageList_Column_bool('locked',  _("Locked"), _("locked")),
218                             'minor'
219                             => new _PageList_Column_bool('rev:is_minor_edit',
220                                                          _("Minor Edit"), _("minor"))
221                             );
222         }
223
224         if (strstr($column, ':'))
225             list ($column, $heading) = explode(':', $column, 2);
226
227         if (!isset($types[$column])) {
228             trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
229             return false;
230         }
231
232         $col = $types[$column];
233         if (!empty($heading))
234             $col->setHeading($heading);
235         return $col;
236     }
237         
238     
239     // make a table given the caption
240     function _generateTable($caption) {
241         $table = HTML::table(array('cellpadding' => 0,
242                                    'cellspacing' => 1,
243                                    'border'      => 0,
244                                    'class'       => 'pagelist'));
245         $table->setAttr('summary', "FIXME: add brief summary and column names");
246
247
248         if ($caption)
249             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
250
251         $row = HTML::tr();
252         foreach ($this->_columns as $col)
253             $row->pushContent($col->heading());
254         $table->pushContent(HTML::thead($row));
255         
256
257         $tbody = HTML::tbody();
258         $n = 0;
259         foreach ($this->_pages as $page_handle) {
260             $row = HTML::tr();
261             $revision_handle = false;
262             foreach ($this->_columns as $col) {
263                 $row->pushContent($col->format($page_handle, $revision_handle));
264             }
265             $group = (int)($n++ / $this->_group_rows);
266             $row->setAttr('class', ($group % 2) ? 'oddrow' : 'evenrow');
267             $tbody->pushContent($row);
268         }
269         $table->pushContent($tbody);
270         return $table;
271     }
272
273     function _generateList($caption) {
274         $list = HTML::ul(array('class' => 'pagelist'));
275         $n = 0;
276         foreach ($this->_pages as $page_handle) {
277             $group = (int)($n++ / $this->_group_rows);
278             $class = ($group % 2) ? 'oddrow' : 'evenrow';
279
280             $list->pushContent(HTML::li(array('class' => $class),
281                                         LinkWikiWord($page_handle->getName())));
282         }
283         if ($caption)
284             $html[] = HTML::p($caption);
285         $html[] = $list;
286         return $html;
287     }
288
289     function _emptyList($caption) {
290         $html = array();
291         if ($caption)
292             $html[] = HTML::p($caption);
293         if ($this->_messageIfEmpty)
294             $html[] = HTML::blockquote(HTML::p($this->_messageIfEmpty));
295         return $html;
296     }
297 };
298
299
300 // (c-file-style: "gnu")
301 // Local Variables:
302 // mode: php
303 // tab-width: 8
304 // c-basic-offset: 4
305 // c-hanging-comment-ender-p: nil
306 // indent-tabs-mode: nil
307 // End:   
308 ?>