first commit. dokuwiki.
[h2N7SspZmY.git] / lib / plugins / pagelist / helper.php
blob404ff8afc495496cb403d7dad7ece82d58eec6b7
1 <?php
2 /**
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>
6 */
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 {
17 /* public */
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
35 /* private */
37 var $_meta = NULL; // metadata array for page
39 /**
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(
50 'page' => true,
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',
62 'tag' => 'tags',
66 function getMethods() {
67 $result = array();
68 $result[] = array(
69 'name' => 'addColumn',
70 'desc' => 'adds an extra column for plugin data',
71 'params' => array(
72 'plugin name' => 'string',
73 'column key' => 'string'),
75 $result[] = array(
76 'name' => 'setFlags',
77 'desc' => 'overrides standard values for showfooter and firstseconly settings',
78 'params' => array('flags' => 'array'),
79 'return' => array('success' => 'boolean'),
81 $result[] = array(
82 'name' => 'startList',
83 'desc' => 'prepares the table header for the page list',
85 $result[] = array(
86 'name' => 'addPage',
87 'desc' => 'adds a page to the list',
88 'params' => array("page attributes, 'id' required, others optional" => 'array'),
90 $result[] = array(
91 'name' => 'finishList',
92 'desc' => 'returns the XHTML output',
93 'return' => array('xhtml' => 'string'),
95 return $result;
98 /**
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) {
114 switch ($flag) {
115 case 'default':
116 $this->style = 'default';
117 break;
118 case 'table':
119 $this->style = 'table';
120 break;
121 case 'list':
122 $this->style = 'list';
123 break;
124 case 'header':
125 $this->showheader = true;
126 break;
127 case 'noheader':
128 $this->showheader = false;
129 break;
130 case 'firsthl':
131 $this->showfirsthl = true;
132 break;
133 case 'nofirsthl':
134 $this->showfirsthl = false;
135 break;
137 if (substr($flag, 0, 2) == 'no') {
138 $value = false;
139 $flag = substr($flag, 2);
140 } else {
141 $value = true;
143 if (in_array($flag, $columns)) $this->column[$flag] = $value;
145 return true;
149 * Sets the list header
151 function startList() {
153 // table style
154 switch ($this->style) {
155 case 'table':
156 $class = 'inline';
157 break;
158 case 'list':
159 $class = 'ul';
160 break;
161 default:
162 $class = 'pagelist';
164 $this->doc = '<table class="'.$class.'">'.DOKU_LF;
165 $this->page = NULL;
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;
174 // header row
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;
192 return true;
196 * Sets a list row
198 function addPage($page) {
200 $id = $page['id'];
201 if (!$id) return false;
202 $this->page = $page;
203 $this->_meta = NULL;
205 // priority and draft
206 if (!isset($this->page['draft'])) {
207 $this->page['draft'] = ($this->_getMeta('type') == 'draft');
209 $class = '';
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;
226 return true;
230 * Sets the list footer
232 function finishList() {
233 if (!isset($this->page)) $this->doc = '';
234 else $this->doc .= '</table>'.DOKU_LF;
236 // reset defaults
237 $this->helper_plugin_pagelist();
239 return $this->doc;
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']).'"';
262 $title .= ' />';
263 } else {
264 if (!$this->page['title']) {
265 if($this->showfirsthl) {
266 $this->page['title'] = $this->_getMeta('title');
267 } else {
268 $this->page['title'] = $this->id;
271 if (!$this->page['title']) $this->page['title'] = str_replace('_', ' ', noNS($id));
272 $title = hsc($this->page['title']);
275 // produce output
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() {
286 global $conf;
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', '');
296 } else {
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);
309 } else {
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'));
322 } else {
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) {
342 if (!$content) {
343 $content = '&nbsp;';
344 $empty = true;
345 } else {
346 $empty = false;
348 $this->doc .= DOKU_TAB.DOKU_TAB.'<td class="'.$class.'">'.$content.'</td>'.DOKU_LF;
349 return (!$empty);
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: