]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
Converted to use new HtmlElement functions.
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.5 2002-01-21 16:28:28 carstenklapp Exp $');
2
3 // This will relieve some of the work of plugins like LikePages,
4 // MostPopular and allows dynamic expansion of those plugins do
5 // include more columns in their output.
6 //
7 // There are still a few rough edges.
8
9 class PageList {
10     function PageList ($pagelist_name = '') {
11         $this->name = $pagelist_name;
12         $this->_caption = "";
13         $this->_columns = array(_("Page Name"));
14         $this->_pages = array();
15         $this->_total = 0;
16         $this->_html = "";
17     }
18
19     function setCaption ($caption_string) {
20         $this->_caption = $caption_string;
21     }
22
23     function getCaption () {
24         // put the total into the caption if needed
25         if (strstr($this->_caption, '%d')) {
26             $this->setCaption(sprintf($this->_caption, $this->getTotal()));
27         }
28         return $this->_caption;
29     }
30
31     function addColumn ($new_columnname) {
32         array_push($this->_columns, $new_columnname);
33     }
34
35     function insertColumn ($new_columnname) {
36         array_unshift($this->_columns, $new_columnname);
37     }
38
39     function addPage ($page_handle) {
40         array_push($this->_pages, &$page_handle);
41         $this->_total = $this->_total + 1;
42     }
43
44     function getTotal () {
45         if (! $this->_total) {
46             //this might not be necessary
47             $this->_total = count($this->_pages);
48         }
49         return $this->_total;
50     }
51     
52     function getHTML() {
53         // TODO: Generate a list instead of a table when only one
54         // column (pagenames only).
55
56         $summary = "FIXME: add brief summary and column names";
57
58         $table = HTML::table(array('cellpadding' => 0,
59                                    'cellspacing' => 1,
60                                    'border' => 0,
61                                    'summary' => $summary)
62                             );
63
64         $pad = new RawXml('&nbsp;&nbsp;');
65
66         $head = HTML::tr();
67         foreach ($this->_columns as $column_name) {
68             if ($this->_column_align($column_name) == 'right') {
69                 $head->pushContent(HTML::td(array('align' => 'right'), $pad, HTML::u($column_name)));
70             } else {
71                 $head->pushContent(HTML::td($pad, HTML::u($column_name)));
72             }
73         }
74         $table->pushContent($head);
75
76         foreach ($this->_pages as $page_handle) {
77             $row = HTML::tr();
78             foreach ($this->_columns as $column_name) {
79                 $col = HTML::td();
80                 $field = $this->_colname_to_dbfield($column_name);
81                 if ($this->_does_require_rev($column_name)) {
82                     $current = $page_handle->getCurrentRevision();
83                     $value = $current->get($field);
84                     if ($field=='mtime') {
85                         global $Theme;
86                         $td = ($Theme->formatDateTime($value));
87                     } else {
88                         $td = ($value);
89                     }
90                 } else {
91                     //TODO: make method to determine formatting
92                     if ($field=='pagename') {
93                         $td = (new RawXml (LinkExistingWikiWord($page_handle->getName())));
94                     } else {
95                         $td = ($page_handle->get($field));
96                     }
97                 }
98
99                 if ($this->_column_align($column_name) == 'right') {
100                     $row->pushContent (HTML::td(array('align' => 'right'), $pad, $td));
101                 } else {
102                     $row->pushContent (HTML::td($pad, $td));
103                 }
104             }
105             $table->pushContent(HTML::tr($row));
106         }
107         return array(HTML::p($this->getCaption()), $table);
108     }
109     
110     ////////////////////
111     // private
112     ////////////////////
113     
114     // lookup alignment from column name
115     function _column_align($column_name) {
116         $map = array(
117                      _("Page Name")      => 'left',
118                      _("Last Modified")  => 'left',
119                      _("Hits")           => 'right',
120                      _("Date Created")   => 'left',
121                      _("# of revisions") => 'right',
122                      _("Last Summary")   => 'left',
123                      _("Last Edited By") => 'left'
124                      );
125
126         $key = $map[$column_name];
127
128         if (! $key) {
129             //FIXME: localise after wording has been finalized.
130             trigger_error("_column_align: key for '$column_name' not found",
131                           E_USER_ERROR);
132         }
133         return $key;
134     }
135
136
137     // lookup database field from column name
138     function _colname_to_dbfield($column_name) {
139         $map = array(
140                      _("Page Name")      => 'pagename',
141                      _("Last Modified")  => 'mtime',
142                      _("Hits")           => 'hits',
143                      _("Date Created")   => '',
144                      _("# of revisions") => '',
145                      _("Last Summary")   => 'summary',
146                      _("Last Edited By") => ''
147                     );
148         $key = $map[$column_name];
149         if (! $key) {
150             //FIXME: localise after wording has been finalized.
151             trigger_error("_colname_to_dbfield: key for '$column_name' not found",
152                           E_USER_ERROR);
153         }
154         return $key;
155     }
156
157     
158     // lookup whether fieldname requires page revision
159     function _does_require_rev($column_name) {
160         $map = array(
161                      _("Page Name")      => false,
162                      _("Last Modified")  => true,
163                      _("Hits")           => false,
164                      _("Date Created")   => '',
165                      _("# of revisions") => '',
166                      _("Last Summary")   => true,
167                      _("Last Edited By") => true
168                      );
169         $key = $map[$column_name];
170         return $key;
171     }
172     
173 };
174
175
176 // (c-file-style: "gnu")
177 // Local Variables:
178 // mode: php
179 // tab-width: 8
180 // c-basic-offset: 4
181 // c-hanging-comment-ender-p: nil
182 // indent-tabs-mode: nil
183 // End:   
184 ?>