]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
fixed minor warnings: unchecked args, POST => Get urls for sortby e.g.
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.58 2004-02-24 15:20:05 rurban Exp $');
2
3 /**
4  * List a number of pagenames, optionally as table with various columns.
5  * This library relieves some work for these plugins:
6  *
7  * AllPages, BackLinks, LikePages, Mostpopular, TitleSearch and more
8  *
9  * It also allows dynamic expansion of those plugins to include more
10  * columns in their output.
11  *
12  * Column 'info=' arguments:
13  *
14  * 'pagename' _("Page Name")
15  * 'mtime'    _("Last Modified")
16  * 'hits'     _("Hits")
17  * 'summary'  _("Last Summary")
18  * 'version'  _("Version")),
19  * 'author'   _("Last Author")),
20  * 'locked'   _("Locked"), _("locked")
21  * 'minor'    _("Minor Edit"), _("minor")
22  * 'markup'   _("Markup")
23  * 'size'     _("Size")
24  * 'owner'    _("Owner"),  //todo: implement this again for PagePerm
25  * 'group'    _("Group"),  //todo: implement this for PagePerm
26
27  * More columns for special plugins:
28  *    Todo: move this admin action away, not really an info column
29  * 'checkbox'  A selectable checkbox appears at the left.
30  * 'remove'   _("Remove")     
31  * 'perm'     _("Permission Mask")
32  * 'acl'      _("ACL")
33  * 'renamed_pagename'   _("Rename to")
34  *
35  * Symbolic 'info=' arguments:
36  * 'all'       All columns except remove, content and renamed_pagename
37  * 'most'      pagename, mtime, author, size, hits, ...
38  * 'some'      pagename, mtime, author
39  *
40  * FIXME: In this refactoring I have un-implemented _ctime, _cauthor, and
41  * number-of-revision.  Note the _ctime and _cauthor as they were implemented
42  * were somewhat flawed: revision 1 of a page doesn't have to exist in the
43  * database.  If lots of revisions have been made to a page, it's more than likely
44  * that some older revisions (include revision 1) have been cleaned (deleted).
45  *
46  * TODO: 
47  *   limit, offset, rows arguments for multiple pages/multiple rows.
48  *
49  *   check PagePerm "list" access-type
50  *
51  *   ->supportedArgs() which arguments are supported, so that the plugin 
52  *                     doesn't explictly need to declare it
53  */
54 class _PageList_Column_base {
55     var $_tdattr = array();
56
57     function _PageList_Column_base ($default_heading, $align = false) {
58         $this->_heading = $default_heading;
59
60         if ($align) {
61             // align="char" isn't supported by any browsers yet :(
62             //if (is_array($align))
63             //    $this->_tdattr = $align;
64             //else
65             $this->_tdattr['align'] = $align;
66         }
67     }
68
69     function format ($pagelist, $page_handle, &$revision_handle) {
70         return HTML::td($this->_tdattr,
71                         HTML::raw('&nbsp;'),
72                         $this->_getValue($page_handle, $revision_handle),
73                         HTML::raw('&nbsp;'));
74     }
75
76     function setHeading ($heading) {
77         $this->_heading = $heading;
78     }
79
80     function heading () {
81         // allow sorting?
82         if (in_array($this->_field,PageList::sortable_columns())) {
83             // multiple comma-delimited sortby args: "+hits,+pagename"
84             // asc or desc: +pagename, -pagename
85             $sortby = PageList::sortby($this->_field,'flip_order');
86             //Fixme: pass all also other GET args along. (limit, p[])
87             $s = HTML::a(array('href' => 
88                                $GLOBALS['request']->GetURLtoSelf(array('sortby' => $sortby,
89                                                                        'nopurge' => 'cache')),
90                                'class' => 'pagetitle',
91                                'title' => sprintf(_("Sort by %s"),$this->_field)), 
92                          HTML::raw('&nbsp;'), HTML::u($this->_heading), HTML::raw('&nbsp;'));
93         } else {
94             $s = HTML(HTML::raw('&nbsp;'), HTML::u($this->_heading), HTML::raw('&nbsp;'));
95         }
96         return HTML::th(array('align' => 'center'),$s);
97     }
98
99     // new grid-style
100     function button_heading () {
101         global $Theme, $request;
102         // allow sorting?
103         if (in_array($this->_field,PageList::sortable_columns())) {
104             // multiple comma-delimited sortby args: "+hits,+pagename"
105             $src = false; 
106             if ($request->getArg('sortby')) {
107                 if (PageList::sortby($this->_field,'check')) { // show icon?
108                     $sortby = PageList::sortby($request->getArg('sortby'),'flip_order');
109                     $request->setArg('sortby',$sortby);
110                     $desc = (substr($sortby,0,1) == '-');      // asc or desc? (+pagename, -pagename)
111                     $src = $Theme->getButtonURL($desc ? 'asc_order' : 'desc_order');
112                 } else {
113                     $sortby = PageList::sortby($this->_field,'init');
114                 }
115             } else {
116                 $sortby = PageList::sortby($this->_field,'init');
117             }
118             $s = HTML::a(array('href' => 
119                                //Fixme: pass all also other GET args along. (limit, p[])
120                                $request->GetURLtoSelf(array('sortby' => $sortby,
121                                                             'nopurge' => 'cache')),
122                                'class' => 'gridbutton', 
123                                'title' => sprintf(_("Sort by %s"),$this->_field)), 
124                          $this->_heading, 
125                          $src ? HTML(HTML::raw('&nbsp;'),
126                                      HTML::img(array('src' => $src, 
127                                                      'width' => '', 
128                                                      'height' => '', 
129                                                      'alt' => _("Click to reverse sort order")))) 
130                               : HTML::raw(''));
131         } else {
132             $s = $this->_heading;
133         }
134         return HTML::th(array('align' => 'center', 'class' => 'gridbutton'), 
135                         HTML::raw('&nbsp;'), $s, HTML::raw('&nbsp;'));
136     }
137 };
138
139 class _PageList_Column extends _PageList_Column_base {
140     function _PageList_Column ($field, $default_heading, $align = false) {
141         $this->_PageList_Column_base($default_heading, $align);
142
143         $this->_need_rev = substr($field, 0, 4) == 'rev:';
144         if ($this->_need_rev)
145             $this->_field = substr($field, 4);
146         else
147             $this->_field = $field;
148     }
149
150     function _getValue ($page_handle, &$revision_handle) {
151         if ($this->_need_rev) {
152             if (!$revision_handle)
153                 $revision_handle = $page_handle->getCurrentRevision();
154             return $revision_handle->get($this->_field);
155         }
156         else {
157             return $page_handle->get($this->_field);
158         }
159     }
160 };
161
162 class _PageList_Column_size extends _PageList_Column {
163     function _getValue ($page_handle, &$revision_handle) {
164         if (!$revision_handle)
165             $revision_handle = $page_handle->getCurrentRevision();
166         return $this->_getSize($revision_handle);
167     }
168
169     function _getSize($revision_handle) {
170         $bytes = strlen($revision_handle->_data['%content']);
171         return ByteFormatter($bytes);
172     }
173 }
174
175
176 class _PageList_Column_bool extends _PageList_Column {
177     function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
178         $this->_PageList_Column($field, $default_heading, 'center');
179         $this->_textIfTrue = $text;
180         $this->_textIfFalse = new RawXml('&#8212;'); //mdash
181     }
182
183     function _getValue ($page_handle, &$revision_handle) {
184         $val = _PageList_Column::_getValue($page_handle, $revision_handle);
185         return $val ? $this->_textIfTrue : $this->_textIfFalse;
186     }
187 };
188
189 class _PageList_Column_checkbox extends _PageList_Column {
190     function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
191         $this->_name = $name;
192         $heading = HTML::input(array('type'  => 'button',
193                                      'title' => _("Click to de-/select all pages"),
194                                      //'width' => '100%',
195                                      'name'  => $default_heading,
196                                      'value' => $default_heading,
197                                      'onclick' => "flipAll(this.form)"
198                                      ));
199         $this->_PageList_Column($field, $heading, 'center');
200     }
201     function _getValue ($pagelist, $page_handle, &$revision_handle) {
202         $pagename = $page_handle->getName();
203         if (!empty($pagelist->_selected[$pagename])) {
204             return HTML::input(array('type' => 'checkbox',
205                                      'name' => $this->_name . "[$pagename]",
206                                      'value' => $pagename,
207                                      'checked' => 'CHECKED'));
208         } else {
209             return HTML::input(array('type' => 'checkbox',
210                                      'name' => $this->_name . "[$pagename]",
211                                      'value' => $pagename));
212         }
213     }
214     function format ($pagelist, $page_handle, &$revision_handle) {
215         return HTML::td($this->_tdattr,
216                         HTML::raw('&nbsp;'),
217                         $this->_getValue($pagelist, $page_handle, $revision_handle),
218                         HTML::raw('&nbsp;'));
219     }
220 };
221
222 class _PageList_Column_time extends _PageList_Column {
223     function _PageList_Column_time ($field, $default_heading) {
224         $this->_PageList_Column($field, $default_heading, 'right');
225         global $Theme;
226         $this->Theme = &$Theme;
227     }
228
229     function _getValue ($page_handle, &$revision_handle) {
230         $time = _PageList_Column::_getValue($page_handle, $revision_handle);
231         return $this->Theme->formatDateTime($time);
232     }
233 };
234
235 class _PageList_Column_version extends _PageList_Column {
236     function _getValue ($page_handle, &$revision_handle) {
237         if (!$revision_handle)
238             $revision_handle = $page_handle->getCurrentRevision();
239         return $revision_handle->getVersion();
240     }
241 };
242
243 // If needed this could eventually become a subclass
244 // of a new _PageList_Column_action class for other actions.
245 // only for WikiAdminRemove or WikiAdminSelect
246 class _PageList_Column_remove extends _PageList_Column {
247     function _getValue ($page_handle, &$revision_handle) {
248         return Button(array('action' => 'remove'), _("Remove"),
249                       $page_handle->getName());
250     }
251 };
252
253 // only for WikiAdminRename
254 class _PageList_Column_renamed_pagename extends _PageList_Column {
255     function _getValue ($page_handle, &$revision_handle) {
256         $post_args = $GLOBALS['request']->getArg('admin_rename');
257         $value = str_replace($post_args['from'], $post_args['to'],$page_handle->getName());
258         return HTML::div(" => ",HTML::input(array('type' => 'text',
259                                                   'name' => 'rename[]',
260                                                   'value' => $value)));
261     }
262 };
263
264 // Output is hardcoded to limit of first 50 bytes. Otherwise
265 // on very large Wikis this will fail if used with AllPages
266 // (PHP memory limit exceeded)
267 class _PageList_Column_content extends _PageList_Column {
268     function _PageList_Column_content ($field, $default_heading, $align = false) {
269         _PageList_Column::_PageList_Column($field, $default_heading, $align);
270         $this->bytes = 50;
271         if ($field == 'content') {
272             $this->_heading .= sprintf(_(" ... first %d bytes"),
273                                        $this->bytes);
274         } elseif ($field == 'hi_content') {
275             if (!empty($_POST['admin_replace'])) {
276               $search = $_POST['admin_replace']['from'];
277               $this->_heading .= sprintf(_(" ... around %s"),
278                                        '»'.$search.'«');
279             }
280         }
281     }
282     function _getValue ($page_handle, &$revision_handle) {
283         if (!$revision_handle)
284             $revision_handle = $page_handle->getCurrentRevision();
285         // Not sure why implode is needed here, I thought
286         // getContent() already did this, but it seems necessary.
287         $c = implode("\n", $revision_handle->getContent());
288         if ($this->_field == 'hi_content') {
289             $search = $_POST['admin_replace']['from'];
290             if ($search and ($i = strpos($c,$search))) {
291                 $l = strlen($search);
292                 $j = max(0,$i - ($this->bytes / 2));
293                 return HTML::div(array('style' => 'font-size:x-small'),
294                                  HTML::div(array('class' => 'transclusion'),
295                                            HTML::span(substr($c, $j, ($this->bytes / 2))),
296                                            HTML::span(array("style"=>"background:yellow"),$search),
297                                            HTML::span(substr($c, $i+$l, ($this->bytes / 2))))
298                                  );
299             } else {
300                 $c = sprintf(_("%s not found"),
301                              '»'.$search.'«');
302                 return HTML::div(array('style' => 'font-size:x-small','align'=>'center'),
303                                  $c);
304             }
305         } elseif (($len = strlen($c)) > $this->bytes) {
306             $c = substr($c, 0, $this->bytes);
307         }
308         include_once('lib/BlockParser.php');
309         // false --> don't bother processing hrefs for embedded WikiLinks
310         $ct = TransformText($c, $revision_handle->get('markup'), false);
311         return HTML::div(array('style' => 'font-size:x-small'),
312                          HTML::div(array('class' => 'transclusion'), $ct),
313                          // Don't show bytes here if size column present too
314                          ($this->parent->_columns_seen['size'] or !$len) ? "" :
315                            ByteFormatter($len, /*$longformat = */true));
316     }
317 };
318
319 class _PageList_Column_author extends _PageList_Column {
320     function _PageList_Column_author ($field, $default_heading, $align = false) {
321         _PageList_Column::_PageList_Column($field, $default_heading, $align);
322         global $WikiNameRegexp, $request;
323         $this->WikiNameRegexp = $WikiNameRegexp;
324         $this->dbi = &$request->getDbh();
325     }
326
327     function _getValue ($page_handle, &$revision_handle) {
328         $author = _PageList_Column::_getValue($page_handle, $revision_handle);
329         if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
330             return WikiLink($author);
331         else
332             return $author;
333     }
334 };
335
336 class _PageList_Column_perm extends _PageList_Column {
337     function _getValue ($page_handle, &$revision_handle) {
338         $perm_array = pagePermissions($page_handle->_pagename);
339         return pagePermissionsSimpleFormat($perm_array,
340                                            $page_handle->get('author'),$page_handle->get('group'));
341         if (0) {
342             ob_start();
343             var_dump($perm_array);
344             $xml = ob_get_contents();
345             ob_end_clean();
346             return $xml;
347         }
348     }
349 };
350
351 class _PageList_Column_acl extends _PageList_Column {
352     function _getValue ($page_handle, &$revision_handle) {
353         $perm_tree = pagePermissions($page_handle->_pagename);
354         return pagePermissionsAclFormat($perm_tree);
355         if (0) {
356             ob_start();
357             var_dump($perm_array);
358             $xml = ob_get_contents();
359             ob_end_clean();
360             return $xml;
361         }
362     }
363 };
364
365 class _PageList_Column_pagename extends _PageList_Column_base {
366     var $_field = 'pagename';
367
368     function _PageList_Column_pagename () {
369         $this->_PageList_Column_base(_("Page Name"));
370         global $request;
371         $this->dbi = &$request->getDbh();
372     }
373
374     function _getValue ($page_handle, &$revision_handle) {
375         if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
376             return WikiLink($page_handle);
377         else
378             return WikiLink($page_handle, 'unknown');
379     }
380 };
381
382
383
384 class PageList {
385     var $_group_rows = 3;
386     var $_columns = array();
387     var $_excluded_pages = array();
388     var $_rows = array();
389     var $_caption = "";
390     var $_pagename_seen = false;
391     var $_types = array();
392     var $_options = array();
393     var $_selected = array();
394     var $_sortby = array();
395
396     function PageList ($columns = false, $exclude = false, $options = false) {
397         $this->_initAvailableColumns();
398         $symbolic_columns = 
399             array(
400                   'all' =>  array_diff(array_keys($this->_types), // all but...
401                                        array('checkbox','remove','renamed_pagename',
402                                              'content','hi_content','perm','acl')),
403                   'most' => array('pagename','mtime','author','size','hits'),
404                   'some' => array('pagename','mtime','author')
405                   );
406         if ($columns) {
407             if (!is_array($columns))
408                 $columns = explode(',', $columns);
409             // expand symbolic columns:
410             foreach ($symbolic_columns as $symbol => $cols) {
411                 if (in_array($symbol,$columns)) { // e.g. 'checkbox,all'
412                     $columns = array_diff(array_merge($columns,$cols),array($symbol));
413                 }
414             }
415             if (!in_array('pagename',$columns))
416                 $this->_addColumn('pagename');
417             foreach ($columns as $col) {
418                 $this->_addColumn($col);
419             }
420         }
421         $this->_addColumn('pagename');
422
423         if ($exclude) {
424             if (!is_array($exclude))
425                 $exclude = explode(',', $exclude);
426             $this->_excluded_pages = $exclude;
427         }
428
429         $this->_options = $options;
430         if (!empty($options) and !empty($options['sortby'])) {
431             $this->sortby($options['sortby'],'init');
432         } else {
433             $this->sortby($GLOBALS['request']->getArg('sortby'),'init');
434         }
435             
436         $this->_messageIfEmpty = _("<no matches>");
437     }
438
439     // Currently PageList takes these arguments:
440     // 1: info, 2: exclude, 3: hash of options
441     // Here we declare which options are supported, so that 
442     // the calling plugin may simply merge this with its own default arguments 
443     function supportedArgs () {
444         return array(//Currently supported options:
445                      'info'              => 'pagename',
446                      'exclude'           => '',          // also wildcards and comma-seperated lists
447
448                      // for the sort buttons in <th>
449                      'sortby'            => '',   // same as for WikiDB::getAllPages
450
451                      //PageList pager options:
452                      // These options may also be given to _generate(List|Table) later
453                      // But limit and offset might help the query WikiDB::getAllPages()
454                      //cols    => 1,       // side-by-side display of list (1-3)
455                      //limit   => 50,      // length of one column
456                      //offset  => 0,       // needed internally for the pager
457                      //paging  => 'auto',  // '':     normal paging mode
458                      //                    // 'auto': drop 'info' columns and enhance rows 
459                      //                    //         when the list becomes large
460                      //                    // 'none': don't page at all
461                      );
462     }
463
464     function setCaption ($caption_string) {
465         $this->_caption = $caption_string;
466     }
467
468     function getCaption () {
469         // put the total into the caption if needed
470         if (is_string($this->_caption) && strstr($this->_caption, '%d'))
471             return sprintf($this->_caption, $this->getTotal());
472         return $this->_caption;
473     }
474
475     function setMessageIfEmpty ($msg) {
476         $this->_messageIfEmpty = $msg;
477     }
478
479
480     function getTotal () {
481         return count($this->_rows);
482     }
483
484     function isEmpty () {
485         return empty($this->_rows);
486     }
487
488     function addPage ($page_handle) {
489         if (is_string($page_handle)) {
490             if (in_array($page_handle, $this->_excluded_pages))
491                 return;             // exclude page.
492             $dbi = $GLOBALS['request']->getDbh();
493             $page_handle = $dbi->getPage($page_handle);
494         } elseif (is_object($page_handle)) {
495           if (in_array($page_handle->getName(), $this->_excluded_pages))
496             return;             // exclude page.
497         }
498
499         $group = (int)(count($this->_rows) / $this->_group_rows);
500         $class = ($group % 2) ? 'oddrow' : 'evenrow';
501         $revision_handle = false;
502
503         if (count($this->_columns) > 1) {
504             $row = HTML::tr(array('class' => $class));
505             foreach ($this->_columns as $col)
506                 $row->pushContent($col->format($this, $page_handle, $revision_handle));
507         }
508         else {
509             $col = $this->_columns[0];
510             $row = HTML::li(array('class' => $class),
511                             $col->_getValue($page_handle, $revision_handle));
512         }
513
514         $this->_rows[] = $row;
515     }
516
517     function addPages ($page_iter) {
518         while ($page = $page_iter->next())
519             $this->addPage($page);
520     }
521
522     function addPageList (&$list) {
523         reset ($list);
524         while ($page = next($list))
525             $this->addPage($page);
526     }
527
528     function getContent() {
529         // Note that the <caption> element wants inline content.
530         $caption = $this->getCaption();
531
532         if ($this->isEmpty())
533             return $this->_emptyList($caption);
534         elseif (count($this->_columns) == 1)
535             return $this->_generateList($caption);
536         else
537             return $this->_generateTable($caption);
538     }
539
540     function printXML() {
541         PrintXML($this->getContent());
542     }
543
544     function asXML() {
545         return AsXML($this->getContent());
546     }
547
548     function sortable_columns() {
549         return array('pagename','mtime','hits');
550     }
551
552     /** 
553      * Handles sortby requests for the DB iterator and table header links
554      * Prefix the column with + or - like "+pagename","-mtime", ...
555      * supported columns: 'pagename','mtime','hits'
556      * supported actions: 'flip_order' "mtime" => "+mtime" => "-mtime" ...
557      *                    'db'         "-pagename" => "pagename DESC"
558      */
559     function sortby ($column, $action) {
560         if (empty($column)) return;
561         //support multiple comma-delimited sortby args: "+hits,+pagename"
562         if (strstr($column,',')) {
563             $result = array();
564             foreach (explode(',',$column) as $col) {
565                 $result[] = $this->sortby($col,$action);
566             }
567             return join(",",$result);
568         }
569         if (substr($column,0,1) == '+') {
570             $order = '+'; $column = substr($column,1);
571         } elseif (substr($column,0,1) == '-') {
572             $order = '-'; $column = substr($column,1);
573         }
574         if (in_array($column,PageList::sortable_columns())) {
575             // default order: +pagename, -mtime, -hits
576             if (empty($order))
577                 if (in_array($column,array('mtime','hits')))
578                     $order = '-';
579                 else
580                     $order = '+';
581             if ($action == 'flip_order') {
582                 return ($order == '+' ? '-' : '+') . $column;
583             } elseif ($action == 'init') {
584                 $this->_sortby[$column] = $order;
585                 return $order . $column;
586             } elseif ($action == 'check') {
587                 return (!empty($this->_sortby[$column]) or 
588                         ($GLOBALS['request']->getArg('sortby') and 
589                          strstr($GLOBALS['request']->getArg('sortby'),$column)));
590             } elseif ($action == 'db') {
591                 // asc or desc: +pagename, -pagename
592                 return $column . ($order == '+' ? ' ASC' : ' DESC');
593             }
594         }
595         return '';
596     }
597
598     // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
599     function explodePageList($input, $perm = false, $sortby=false, $limit=false) {
600         // expand wildcards from list of all pages
601         if (preg_match('/[\?\*]/',$input)) {
602             $dbi = $GLOBALS['request']->getDbh();
603             $allPagehandles = $dbi->getAllPages($perm,$sortby,$limit);
604             while ($pagehandle = $allPagehandles->next()) {
605                 $allPages[] = $pagehandle->getName();
606             }
607             return explodeList($input, $allPages);
608         } else {
609             //TODO: do the sorting
610             return explode(',',$input);
611         }
612     }
613
614
615     ////////////////////
616     // private
617     ////////////////////
618     //Performance Fixme: Initialize only the requested objects
619     function _initAvailableColumns() {
620         if (!empty($this->_types))
621             return;
622
623         $this->_types =
624             array(
625                   'content'
626                   => new _PageList_Column_content('rev:content', _("Content")),
627                   'hi_content' // with highlighted search for SearchReplace
628                   => new _PageList_Column_content('rev:hi_content', _("Content")),
629                   'remove'
630                   => new _PageList_Column_remove('remove', _("Remove")),
631                   'renamed_pagename'
632                   => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
633                   'perm'
634                   => new _PageList_Column_perm('perm', _("Permission")),
635                   'acl'
636                   => new _PageList_Column_acl('acl', _("ACL")),
637                   'checkbox'
638                   => new _PageList_Column_checkbox('p', _("Select")),
639                   'pagename'
640                   => new _PageList_Column_pagename,
641                   'mtime'
642                   => new _PageList_Column_time('rev:mtime', _("Last Modified")),
643                   'hits'
644                   => new _PageList_Column('hits', _("Hits"), 'right'),
645                   'size'
646                   => new _PageList_Column_size('rev:size', _("Size"), 'right'),
647                                               /*array('align' => 'char', 'char' => ' ')*/
648                   'summary'
649                   => new _PageList_Column('rev:summary', _("Last Summary")),
650                   'version'
651                   => new _PageList_Column_version('rev:version', _("Version"),
652                                                  'right'),
653                   'author'
654                   => new _PageList_Column_author('rev:author', _("Last Author")),
655                   'owner'
656                   => new _PageList_Column_author('owner', _("Owner")),
657                   'group'
658                   => new _PageList_Column_author('group', _("Group")),
659                   'locked'
660                   => new _PageList_Column_bool('locked', _("Locked"),
661                                                _("locked")),
662                   'minor'
663                   => new _PageList_Column_bool('rev:is_minor_edit',
664                                                _("Minor Edit"), _("minor")),
665                   'markup'
666                   => new _PageList_Column('rev:markup', _("Markup"))
667                   );
668     }
669
670     function _addColumn ($column) {
671
672         $this->_initAvailableColumns();
673
674         if (isset($this->_columns_seen[$column]))
675             return false;       // Already have this one.
676         $this->_columns_seen[$column] = true;
677
678         if (strstr($column, ':'))
679             list ($column, $heading) = explode(':', $column, 2);
680
681         if (!isset($this->_types[$column])) {
682             trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
683             return false;
684         }
685
686         $col = $this->_types[$column];
687         if (!empty($heading))
688             $col->setHeading($heading);
689
690         $this->_columns[] = $col;
691
692         return true;
693     }
694
695     // make a table given the caption
696     function _generateTable($caption) {
697         $table = HTML::table(array('cellpadding' => 0,
698                                    'cellspacing' => 1,
699                                    'border'      => 0,
700                                    'class'       => 'pagelist'));
701         if ($caption)
702             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
703
704         //Warning: This is quite fragile. It depends solely on a private variable
705         //         in ->_addColumn()
706         if (in_array('checkbox',$this->_columns_seen)) {
707             $table->pushContent($this->_jsFlipAll());
708         }
709         $row = HTML::tr();
710         $table_summary = array();
711         foreach ($this->_columns as $col) {
712             $row->pushContent($col->button_heading());
713             if (is_string($col->_heading))
714                 $table_summary[] = $col->_heading;
715         }
716         // Table summary for non-visual browsers.
717         $table->setAttr('summary', sprintf(_("Columns: %s."), 
718                                            implode(", ", $table_summary)));
719
720         $table->pushContent(HTML::thead($row),
721                             HTML::tbody(false, $this->_rows));
722         return $table;
723     }
724
725     function _jsFlipAll() {
726       return JavaScript("
727 function flipAll(formObj) {
728   var isFirstSet = -1;
729   for (var i=0;i < formObj.length;i++) {
730       fldObj = formObj.elements[i];
731       if (fldObj.type == 'checkbox') { 
732          if (isFirstSet == -1)
733            isFirstSet = (fldObj.checked) ? true : false;
734          fldObj.checked = (isFirstSet) ? false : true;
735        }
736    }
737 }");
738     }
739
740     function _generateList($caption) {
741         $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
742         $out = HTML();
743         //Warning: This is quite fragile. It depends solely on a private variable
744         //         in ->_addColumn()
745         if (in_array('checkbox',$this->_columns_seen)) {
746             $out->pushContent($this->_jsFlipAll());
747         }
748         if ($caption)
749             $out->pushContent(HTML::p($caption));
750         $out->pushContent($list);
751         return $out;
752     }
753
754     function _emptyList($caption) {
755         $html = HTML();
756         if ($caption)
757             $html->pushContent(HTML::p($caption));
758         if ($this->_messageIfEmpty)
759             $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
760         return $html;
761     }
762
763 };
764
765 /* List pages with checkboxes to select from.
766  * The [Select] button toggles via _jsFlipAll
767  */
768
769 class PageList_Selectable
770 extends PageList {
771
772     function PageList_Selectable ($columns=false, $exclude=false) {
773         if ($columns) {
774             if (!is_array($columns))
775                 $columns = explode(',', $columns);
776             if (!in_array('checkbox',$columns))
777                 array_unshift($columns,'checkbox');
778         } else {
779             $columns = array('checkbox','pagename');
780         }
781         PageList::PageList($columns,$exclude);
782     }
783
784     function addPageList ($array) {
785         while (list($pagename,$selected) = each($array)) {
786             if ($selected) $this->addPageSelected($pagename);
787             $this->addPage($pagename);
788         }
789     }
790
791     function addPageSelected ($pagename) {
792         $this->_selected[$pagename] = 1;
793     }
794     //Todo:
795     //insert javascript when clicked on Selected Select/Deselect all
796 }
797
798 // (c-file-style: "gnu")
799 // Local Variables:
800 // mode: php
801 // tab-width: 8
802 // c-basic-offset: 4
803 // c-hanging-comment-ender-p: nil
804 // indent-tabs-mode: nil
805 // End:
806 ?>