]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
typo fix
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.11 2002-01-21 21:06:13 carstenklapp Exp $');
2
3 // This relieves some work for these plugins:
4 //
5 // BackLinks, LikePages, Mostpopular, TitleSearch
6 //
7 // It also allows dynamic expansion of those plugins to include more
8 // columns in their output.
9 //
10 // There are still a few rough edges.
11 //
12 // FIXME: Make caption work properly with new HtmlElement
13 //
14 // FIXME: Add error recovery! If the column title isn't found due to a
15 //        typo, the wiki craps out, you will not even be able to edit the
16 //        page again to fix the typo in the column name!
17
18 class PageList {
19     function PageList ($pagelist_name = '') {
20         $this->name = $pagelist_name;
21         $this->_caption = "";
22         $this->_columns = array(_("Page Name"));
23         $this->_pages = array();
24         $this->_total = 0;
25         $this->_html = "";
26     }
27
28     function setCaption ($caption_string) {
29         $this->_caption = $caption_string;
30     }
31
32     function getCaption () {
33         // put the total into the caption if needed
34         if (strstr($this->_caption, '%d')) {
35             $this->setCaption(sprintf($this->_caption, $this->getTotal()));
36         }
37         return $this->_caption;
38     }
39
40     //FIXME: yuck, get rid of ucfirst
41     function addColumn ($new_columnname) {
42         array_push($this->_columns, ucfirst($new_columnname));
43     }
44
45     function insertColumn ($new_columnname) {
46         array_unshift($this->_columns, ucfirst($new_columnname));
47     }
48
49     function addPage ($page_handle) {
50         array_push($this->_pages, &$page_handle);
51         $this->_total = $this->_total + 1;
52     }
53
54     function getTotal () {
55         if (! $this->_total) {
56             //this might not be necessary
57             $this->_total = count($this->_pages);
58         }
59         return $this->_total;
60     }
61
62     function getContent() {
63         $caption = HTML::div(array('align' => 'left'), $this->getCaption());
64
65         if (count($this->_columns) == 1) {
66             return array($caption, $this->_generateList());
67         } else {
68             return $this->_generateTable($caption);
69         }
70     }
71
72     ////////////////////
73     // private
74     ////////////////////
75
76     // Some of these column_name aliases could probably be eliminated
77     // once standard titles have been agreed upon
78
79     // lookup alignment from column name
80     function _column_align($column_name) {
81         $map = array(
82                      _("Page Name")       => 'left',
83                      _("Page")            => 'left',
84                      _("Name")            => 'left',
85                      _("Last Modified")   => 'left',
86                      _("Hits")            => 'right',
87                      _("Visitors")        => 'right',
88                      _("Date Created")    => 'left',
89                      _("Creation Date")   => 'left',
90                      _("# Of Revisions")  => 'right',        //FIXME: count revisions in db
91                      _("Last Summary")    => 'left',
92                      _("Last Author")     => 'left',
93                      _("Last Edited By")  => 'left',
94                      _("Author")          => 'left',
95                      _("Original Author") => 'left',
96                      _("Created By")      => 'left',
97                      _("Locked")          => 'left',
98                      _("Minor Edit")      => 'left',
99                      _("Minor")           => 'left',
100                      );
101
102         $key = $map[$column_name];
103
104         if (! $key) {
105             //FIXME: localise after wording has been finalized.
106             trigger_error("_column_align: key for '$column_name' not found",
107                           E_USER_ERROR);
108         }
109         return $key;
110     }
111
112
113     // lookup database field from column name
114     function _colname_to_dbfield($column_name) {
115         $map = array(
116                      _("Page Name")       => 'pagename',
117                      _("Page")            => 'pagename',
118                      _("Name")            => 'pagename',
119                      _("Last Modified")   => 'mtime',
120                      _("Hits")            => 'hits',
121                      _("Visitors")        => 'hits',
122                      _("Date Created")    => '_ctime',
123                      _("Creation Date")   => '_ctime',
124                      _("# Of Revisions")  => '',        //FIXME: count revisions in db
125                      _("Last Summary")    => 'summary',
126                      _("Last Author")     => 'author',
127                      _("Last Edited By")  => 'author',
128                      _("Author")          => '_cauthor',
129                      _("Original Author") => '_cauthor',
130                      _("Created By")      => '_cauthor',
131                      _("Locked")          => 'locked',
132                      _("Minor Edit")      => 'is_minor_edit',
133                      _("Minor")           => 'is_minor_edit',
134                     );
135         $key = $map[$column_name];
136         if (! $key) {
137             //FIXME: localise after wording has been finalized.
138             trigger_error("_colname_to_dbfield: key for '$column_name' not found",
139                           E_USER_ERROR);
140         }
141         return $key;
142     }
143
144
145     // lookup whether fieldname requires page revision
146     function _does_require_rev($column_name) {
147         $map = array(
148                      _("Page Name")       => false,
149                      _("Page")            => false,
150                      _("Name")            => false,
151                      _("Last Modified")   => true,
152                      _("Hits")            => false,
153                      _("Visitors")        => false,
154                      _("Date Created")    => false,
155                      _("Creation Date")   => false,
156                      _("# Of Revisions")  => '',        //FIXME: count revisions in db
157                      _("Last Summary")    => true,
158                      _("Last Author")     => true,
159                      _("Last Edited By")  => true,
160                      _("Author")          => true,
161                      _("Original Author") => false,
162                      _("Created By")      => false,
163                      _("Locked")          => false,
164                      _("Minor Edit")      => true,
165                      _("Minor")           => true,
166                      );
167         $key = $map[$column_name];
168         return $key;
169     }
170
171     // make a table given the caption
172     function _generateTable($caption) {
173         $pad = new RawXml('&nbsp;&nbsp;');
174         $emdash = new RawXml('&#8212;');
175
176         $thead = HTML::thead();
177         foreach ($this->_columns as $column_name) {
178             if ($this->_column_align($column_name) == 'right') {
179                 $thead->pushContent(HTML::td(array('align' => 'right'), $pad, HTML::u($column_name)));
180             } else {
181                 $thead->pushContent(HTML::td($pad, HTML::u($column_name)));
182             }
183         }
184
185         $tbody = HTML::tbody();
186         foreach ($this->_pages as $page_handle) {
187             $row = HTML::tr();
188             global $Theme;
189
190             foreach ($this->_columns as $column_name) {
191                 $col = HTML::td();
192                 $field = $this->_colname_to_dbfield($column_name);
193
194                 if ($this->_does_require_rev($column_name)) {
195                     $current = $page_handle->getCurrentRevision();
196                     $value = $current->get($field);
197                 }
198                 //TODO: make method to determine formatting
199                 switch ($field) {
200                     // needs current rev
201                 case 'mtime' :
202                     $td = ($Theme->formatDateTime($value));
203                     break;
204                 case 'is_minor_edit' :
205                     $td = ($value) ? _("minor") : $emdash;
206                     break;
207
208                     // does not need current rev
209                 case 'pagename' :
210                     $td = new RawXml (LinkExistingWikiWord($page_handle->getName()));
211                     break;
212                 case 'locked' :
213                     $td = ($page_handle->get($field)) ? _("locked") : $emdash;
214                     break;
215                 case '_ctime' :
216                     $revision = $page_handle->getRevision(1);
217                     $td = $Theme->formatDateTime($revision->get('mtime'));
218                     break;
219                 case '_cauthor' :
220                     $revision = $page_handle->getRevision(1);
221                     $td = new RawXml (LinkExistingWikiWord($revision->get('author')));
222                     break;
223
224                 default :
225                     if ($this->_does_require_rev($column_name)) {
226                         $td = ($value);
227                     } else {
228                         $td = ($page_handle->get($field));
229                     }
230                 }
231
232                 if ($this->_column_align($column_name) == 'right') {
233                     $row->pushContent (HTML::td(array('align' => 'right'), $pad, $td));
234                 } else {
235                     $row->pushContent (HTML::td($pad, $td));
236                 }
237             }
238             $tbody->pushContent($row);
239         }
240
241         $summary = "FIXME: add brief summary and column names";
242
243         // Final table assembly
244         $table = HTML::table(array('summary'     => $summary,
245                                    'cellpadding' => 0,
246                                    'cellspacing' => 1,
247                                    'border'      => 0,
248                                    'width'       => '100%'));
249
250         $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
251         $table->pushContent($thead);
252         $table->pushContent($tbody);
253         return $table;
254     }
255
256     function _generateList() {
257         $list = HTML::ul();
258         foreach ($this->_pages as $page_handle) {
259             $list->pushContent(HTML::li(_LinkWikiWord($page_handle->getName())));
260         }
261         return $list;
262     }
263
264 };
265
266
267 // (c-file-style: "gnu")
268 // Local Variables:
269 // mode: php
270 // tab-width: 8
271 // c-basic-offset: 4
272 // c-hanging-comment-ender-p: nil
273 // indent-tabs-mode: nil
274 // End:   
275 ?>