3 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author Esther Brunner <wikidesign@gmail.com>
5 * @author Gina Häußge <osd@foosel.net>
8 // must be run within Dokuwiki
9 if (!defined('DOKU_INC')) die();
11 if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
12 if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
13 if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC
.'lib/plugins/');
15 class helper_plugin_pagelist
extends DokuWiki_Plugin
{
19 var $page = NULL; // associative array for page to list
20 // must contain a value to key 'id'
21 // can contain: 'title', 'date', 'user', 'desc', 'comments',
22 // 'tags', 'status' and 'priority'
24 var $style = ''; // table style: 'default', 'table', 'list'
25 var $showheader = false; // show a heading line
26 var $column = array(); // which columns to show
27 var $header = array(); // language strings for table headers
29 var $plugins = array(); // array of plugins to extend the pagelist
30 var $discussion = NULL; // discussion class object
31 var $tag = NULL; // tag class object
33 var $doc = ''; // the final output XHTML string
37 var $_meta = NULL; // metadata array for page
40 * Constructor gets default preferences
42 * These can be overriden by plugins using this class
44 function helper_plugin_pagelist() {
45 $this->style
= $this->getConf('style');
46 $this->showheader
= $this->getConf('showheader');
47 $this->showfirsthl
= $this->getConf('showfirsthl');
49 $this->column
= array(
51 'date' => $this->getConf('showdate'),
52 'user' => $this->getConf('showuser'),
53 'desc' => $this->getConf('showdesc'),
54 'comments' => $this->getConf('showcomments'),
55 'linkbacks'=> $this->getConf('showlinkbacks'),
56 'tags' => $this->getConf('showtags'),
59 $this->plugins
= array(
60 'discussion' => 'comments',
61 'linkback' => 'linkbacks',
66 function getMethods() {
69 'name' => 'addColumn',
70 'desc' => 'adds an extra column for plugin data',
72 'plugin name' => 'string',
73 'column key' => 'string'),
77 'desc' => 'overrides standard values for showfooter and firstseconly settings',
78 'params' => array('flags' => 'array'),
79 'return' => array('success' => 'boolean'),
82 'name' => 'startList',
83 'desc' => 'prepares the table header for the page list',
87 'desc' => 'adds a page to the list',
88 'params' => array("page attributes, 'id' required, others optional" => 'array'),
91 'name' => 'finishList',
92 'desc' => 'returns the XHTML output',
93 'return' => array('xhtml' => 'string'),
99 * Adds an extra column for plugins
101 function addColumn($plugin, $col) {
102 $this->plugins
[$plugin] = $col;
103 $this->column
[$col] = true;
107 * Overrides standard values for style, showheader and show(column) settings
109 function setFlags($flags) {
110 if (!is_array($flags)) return false;
112 $columns = array('date', 'user', 'desc', 'comments', 'linkbacks', 'tags');
113 foreach ($flags as $flag) {
116 $this->style
= 'default';
119 $this->style
= 'table';
122 $this->style
= 'list';
125 $this->showheader
= true;
128 $this->showheader
= false;
131 $this->showfirsthl
= true;
134 $this->showfirsthl
= false;
137 if (substr($flag, 0, 2) == 'no') {
139 $flag = substr($flag, 2);
143 if (in_array($flag, $columns)) $this->column
[$flag] = $value;
149 * Sets the list header
151 function startList() {
154 switch ($this->style
) {
164 $this->doc
= '<table class="'.$class.'">'.DOKU_LF
;
167 // check if some plugins are available - if yes, load them!
168 foreach ($this->plugins
as $plug => $col) {
169 if (!$this->column
[$col]) continue;
170 if (plugin_isdisabled($plug) ||
(!$this->$plug = plugin_load('helper', $plug)))
171 $this->column
[$col] = false;
175 if ($this->showheader
) {
176 $this->doc
.= DOKU_TAB
.'<tr>'.DOKU_LF
.DOKU_TAB
.DOKU_TAB
;
177 $columns = array('page', 'date', 'user', 'desc');
178 foreach ($columns as $col) {
179 if ($this->column
[$col]) {
180 if (!$this->header
[$col]) $this->header
[$col] = hsc($this->getLang($col));
181 $this->doc
.= '<th class="'.$col.'">'.$this->header
[$col].'</th>';
184 foreach ($this->plugins
as $plug => $col) {
185 if ($this->column
[$col]) {
186 if (!$this->header
[$col]) $this->header
[$col] = hsc($this->$plug->th());
187 $this->doc
.= '<th class="'.$col.'">'.$this->header
[$col].'</th>';
190 $this->doc
.= DOKU_LF
.DOKU_TAB
.'</tr>'.DOKU_LF
;
198 function addPage($page) {
201 if (!$id) return false;
205 // priority and draft
206 if (!isset($this->page
['draft'])) {
207 $this->page
['draft'] = ($this->_getMeta('type') == 'draft');
210 if (isset($this->page
['priority'])) $class .= 'priority'.$this->page
['priority']. ' ';
211 if ($this->page
['draft']) $class .= 'draft ';
212 if ($this->page
['class']) $class .= $this->page
['class'];
213 if(!empty($class)) $class = ' class="' . $class . '"';
215 $this->doc
.= DOKU_TAB
.'<tr'.$class.'>'.DOKU_LF
;
217 $this->_pageCell($id);
218 if ($this->column
['date']) $this->_dateCell();
219 if ($this->column
['user']) $this->_userCell();
220 if ($this->column
['desc']) $this->_descCell();
221 foreach ($this->plugins
as $plug => $col) {
222 if ($this->column
[$col]) $this->_pluginCell($plug, $col, $id);
225 $this->doc
.= DOKU_TAB
.'</tr>'.DOKU_LF
;
230 * Sets the list footer
232 function finishList() {
233 if (!isset($this->page
)) $this->doc
= '';
234 else $this->doc
.= '</table>'.DOKU_LF
;
237 $this->helper_plugin_pagelist();
242 /* ---------- Private Methods ---------- */
245 * Page title / link to page
247 function _pageCell($id) {
249 // check for page existence
250 if (!isset($this->page
['exists'])) {
251 if (!isset($this->page
['file'])) $this->page
['file'] = wikiFN($id);
252 $this->page
['exists'] = @file_exists
($this->page
['file']);
254 if ($this->page
['exists']) $class = 'wikilink1';
255 else $class = 'wikilink2';
257 // handle image and text titles
258 if ($this->page
['image']) {
259 $title = '<img src="'.ml($this->page
['image']).'" class="media"';
260 if ($this->page
['title']) $title .= ' title="'.hsc($this->page
['title']).'"'.
261 ' alt="'.hsc($this->page
['title']).'"';
264 if (!$this->page
['title']) {
265 if($this->showfirsthl
) {
266 $this->page
['title'] = $this->_getMeta('title');
268 $this->page
['title'] = $this->id
;
271 if (!$this->page
['title']) $this->page
['title'] = str_replace('_', ' ', noNS($id));
272 $title = hsc($this->page
['title']);
276 $content = '<a href="'.wl($id).($this->page
['section'] ?
'#'.$this->page
['section'] : '').
277 '" class="'.$class.'" title="'.$id.'">'.$title.'</a>';
278 if ($this->style
== 'list') $content = '<ul><li>'.$content.'</li></ul>';
279 return $this->_printCell('page', $content);
283 * Date - creation or last modification date if not set otherwise
285 function _dateCell() {
288 if($this->column
['date'] == 2) {
289 $this->page
['date'] = $this->_getMeta(array('date', 'modified'));
290 } elseif(!$this->page
['date'] && $this->page
['exists']) {
291 $this->page
['date'] = $this->_getMeta(array('date', 'created'));
294 if ((!$this->page
['date']) ||
(!$this->page
['exists'])) {
295 return $this->_printCell('date', '');
297 return $this->_printCell('date', strftime($conf['dformat'], $this->page
['date']));
302 * User - page creator or contributors if not set otherwise
304 function _userCell() {
305 if (!array_key_exists('user', $this->page
)) {
306 if ($this->column
['user'] == 2) {
307 $users = $this->_getMeta('contributor');
308 if (is_array($users)) $this->page
['user'] = join(', ', $users);
310 $this->page
['user'] = $this->_getMeta('creator');
313 return $this->_printCell('user', hsc($this->page
['user']));
317 * Description - (truncated) auto abstract if not set otherwise
319 function _descCell() {
320 if (!array_key_exists('desc', $this->page
)) {
321 $desc = $this->_getMeta(array('description', 'abstract'));
323 $desc = $this->page
['desc'];
325 $max = $this->column
['desc'];
326 if (($max > 1) && (strlen($desc) > $max)) $desc = substr($desc, 0, $max).'…';
327 return $this->_printCell('desc', hsc($desc));
331 * Plugins - respective plugins must be installed!
333 function _pluginCell($plug, $col, $id) {
334 if (!isset($this->page
[$col])) $this->page
[$col] = $this->$plug->td($id);
335 return $this->_printCell($col, $this->page
[$col]);
339 * Produce XHTML cell output
341 function _printCell($class, $content) {
348 $this->doc
.= DOKU_TAB
.DOKU_TAB
.'<td class="'.$class.'">'.$content.'</td>'.DOKU_LF
;
354 * Get default value for an unset element
356 function _getMeta($key) {
357 if (!$this->page
['exists']) return false;
358 if (!isset($this->_meta
)) $this->_meta
= p_get_metadata($this->page
['id']);
359 if (is_array($key)) return $this->_meta
[$key[0]][$key[1]];
360 else return $this->_meta
[$key];
363 // vim:ts=4:sw=4:et:enc=utf-8: