]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
Don't add column if it already is present
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.22 2002-01-26 22:08:40 carstenklapp 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_author extends _PageList_Column {
105     function _getValue ($page_handle, &$revision_handle) {
106         global $WikiNameRegexp, $request, $Theme;
107         $dbi = $request->getDbh();
108
109         $author = _PageList_Column::_getValue($page_handle, $revision_handle);
110         if (preg_match("/^$WikiNameRegexp\$/", $author) && $dbi->isWikiPage($author))
111             return $Theme->linkExistingWikiWord($author);
112         else
113             return $author;
114     }
115 };
116
117 class _PageList_Column_pagename extends _PageList_Column_base {
118     function _PageList_Column_pagename () {
119         $this->_PageList_Column_base(_("Page Name"));
120     }
121     
122     function _getValue ($page_handle, &$revision_handle) {
123         global $Theme;
124         return $Theme->LinkExistingWikiWord($page_handle->getName());
125     }
126 };
127
128         
129 class PageList {
130     function PageList () {
131         $this->_caption = "";
132         $this->_columns = array(new _PageList_Column_pagename);
133         $this->_pages = array();
134         $this->_messageIfEmpty = _("<no matches>");
135         $this->_group_rows = 3;
136     }
137
138     function setCaption ($caption_string) {
139         $this->_caption = $caption_string;
140     }
141
142     function getCaption () {
143         // put the total into the caption if needed
144         if (is_string($this->_caption) && strstr($this->_caption, '%d'))
145             return sprintf($this->_caption, $this->getTotal());
146         return $this->_caption;
147     }
148
149     function setMessageIfEmpty ($msg) {
150         $this->_messageIfEmpty = $msg;
151     }
152
153     /**
154      * Add a column to the listing.
155      *
156      * @input $column string Which column to add.
157      * $Column can be one of <ul>
158      * <li>mtime
159      * <li>hits
160      * <li>summary
161      * <li>author
162      * <li>locked
163      * <li>minor
164      * </ul>
165      *
166      * If you would like to specify an alternate heading for the
167      * column, concatenate the desired adding to $column, after adding
168      * a colon.  E.g. 'hits:Page Views'.
169      */ 
170     function addColumn ($column) {
171         if (($col = $this->_getColumn($new_columnname))) {
172            if(! $this->column_exists($col->_heading)) {
173                 array_push($this->_columns, $col);
174             }
175         }
176     }
177
178     function insertColumn ($new_columnname) {
179         if (($col = $this->_getColumn($new_columnname))) {
180            if(! $this->column_exists($col->_heading)) {
181                 array_unshift($this->_columns, $col);
182             }
183         }
184     }
185
186     function column_exists ($heading) {
187         foreach ($this->_columns as $val) {
188             if ($val->_heading == $heading)
189                 return true;
190         }
191         return false;
192     }
193
194
195     function addPage ($page_handle) {
196         array_push($this->_pages, &$page_handle);
197     }
198
199     function getTotal () {
200         return count($this->_pages);
201     }
202
203     function isEmpty () {
204         return empty($this->_pages);
205     }
206     
207
208     function getContent() {
209         // Note that the <caption> element wants inline content.
210         $caption = $this->getCaption();
211
212         if ($this->isEmpty())
213             return $this->_emptyList($caption);
214         elseif (count($this->_columns) == 1)
215             return $this->_generateList($caption);
216         else
217             return $this->_generateTable($caption);
218     }
219
220     function printXML() {
221         PrintXML($this->getContent());
222     }
223
224     function asXML() {
225         return AsXML($this->getContent());
226     }
227
228
229     ////////////////////
230     // private
231     ////////////////////
232     function _getColumn ($column) {
233         static $types;
234         if (empty($types)) {
235             $types = array( 'mtime'
236                             => new _PageList_Column_time('rev:mtime', _("Last Modified")),
237                             'hits'
238                             => new _PageList_Column('hits',  _("Hits"), 'right'),
239                             'summary'
240                             => new _PageList_Column('rev:summary',  _("Last Summary")),
241                             'author'
242                             => new _PageList_Column_author('rev:author',  _("Last Author")),
243                             'locked'
244                             => new _PageList_Column_bool('locked',  _("Locked"), _("locked")),
245                             'minor'
246                             => new _PageList_Column_bool('rev:is_minor_edit',
247                                                          _("Minor Edit"), _("minor"))
248                             );
249         }
250
251         if (strstr($column, ':'))
252             list ($column, $heading) = explode(':', $column, 2);
253
254         if (!isset($types[$column])) {
255             trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
256             return false;
257         }
258
259         $col = $types[$column];
260         if (!empty($heading))
261             $col->setHeading($heading);
262         return $col;
263     }
264         
265     
266     // make a table given the caption
267     function _generateTable($caption) {
268         $table = HTML::table(array('cellpadding' => 0,
269                                    'cellspacing' => 1,
270                                    'border'      => 0,
271                                    'class'       => 'pagelist'));
272         $table->setAttr('summary', "FIXME: add brief summary and column names");
273
274
275         if ($caption)
276             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
277
278         $row = HTML::tr();
279         foreach ($this->_columns as $col)
280             $row->pushContent($col->heading());
281         $table->pushContent(HTML::thead($row));
282         
283
284         $tbody = HTML::tbody();
285         $n = 0;
286         foreach ($this->_pages as $page_handle) {
287             $row = HTML::tr();
288             $revision_handle = false;
289             foreach ($this->_columns as $col) {
290                 $row->pushContent($col->format($page_handle, $revision_handle));
291             }
292             $group = (int)($n++ / $this->_group_rows);
293             $row->setAttr('class', ($group % 2) ? 'oddrow' : 'evenrow');
294             $tbody->pushContent($row);
295         }
296         $table->pushContent($tbody);
297         return $table;
298     }
299
300     function _generateList($caption) {
301         $list = HTML::ul(array('class' => 'pagelist'));
302         $n = 0;
303         foreach ($this->_pages as $page_handle) {
304             $group = (int)($n++ / $this->_group_rows);
305             $class = ($group % 2) ? 'oddrow' : 'evenrow';
306
307             $list->pushContent(HTML::li(array('class' => $class),
308                                         LinkWikiWord($page_handle->getName())));
309         }
310         if ($caption)
311             $html[] = HTML::p($caption);
312         $html[] = $list;
313         return $html;
314     }
315
316     function _emptyList($caption) {
317         $html = array();
318         if ($caption)
319             $html[] = HTML::p($caption);
320         if ($this->_messageIfEmpty)
321             $html[] = HTML::blockquote(HTML::p($this->_messageIfEmpty));
322         return $html;
323     }
324 };
325
326
327 // (c-file-style: "gnu")
328 // Local Variables:
329 // mode: php
330 // tab-width: 8
331 // c-basic-offset: 4
332 // c-hanging-comment-ender-p: nil
333 // indent-tabs-mode: nil
334 // End:   
335 ?>