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