1<?php
2/**
3 * Plugin nspages : Displays nicely a list of the pages of a namespace
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 */
7
8if(!defined('DOKU_INC')) die();
9require_once 'sorters.php';
10
11abstract class nspages_printer {
12    protected $plugin;
13    protected $renderer;
14    protected $mode;
15    private $pos;
16    private $actualTitleLevel;
17    private $natOrder;
18    private $nbItemsMax;
19    private $dictOrder;
20    protected $_displayModificationDate;
21    protected $_sorter;
22    protected $includeItemsInTOC;
23
24    // Static to prevent conflicts if there are several <nspages> tag in a same page
25    static private $builtSectionIds = array();
26
27    function __construct($plugin, $mode, $renderer, $data){
28      $this->plugin = $plugin;
29      $this->renderer =& $renderer;
30      $this->mode = $mode;
31      $this->pos = $data['pos'];
32      $this->natOrder = $data['natOrder'];
33      $this->actualTitleLevel = $data['actualTitleLevel'];
34      $this->nbItemsMax = $data['nbItemsMax'];
35      $this->dictOrder = $data['dictOrder'];
36      $this->_displayModificationDate = $data['displayModificationDate']
37        || $data['modificationDateOnPictures']; // This is a deprecated option. We should kill it after checking no users are still using it
38      $this->_sorter = $this->_getSorter($data['reverse']);
39      $this->includeItemsInTOC = $data['includeItemsInTOC'] && $mode === 'xhtml';
40    }
41
42    function printTOC($tab, $type, $text, $hideno){
43        $this->_printHeader($tab, $type, $text, $hideno);
44
45        if(empty($tab)) {
46            return;
47        }
48
49        $this->_print($tab, $type);
50    }
51
52    abstract function _print($tab, $type);
53
54    function printUnusableNamespace($wantedNS){
55         $this->renderer->section_open(1);
56         $this->renderer->cdata($this->plugin->getLang('doesntexist').$wantedNS);
57         $this->renderer->section_close();
58    }
59
60    private function _printHeader(&$tab, $type, $text, $hideno) {
61        if(empty($tab) && $hideno) return;
62
63        $this->_sorter->sort($tab);
64        $this->_keepOnlyNMaxItems($tab);
65
66        if($text != '') {
67            if($this->actualTitleLevel){
68                $this->renderer->header($text, $this->actualTitleLevel, $this->pos);
69            } else if($this->mode == 'xhtml') {
70                $this->renderer->doc .= '<p class="catpageheadline">';
71                $this->renderer->cdata($text);
72                $this->renderer->doc .= '</p>';
73            } else {
74                $this->renderer->linebreak();
75                $this->renderer->p_open();
76                $this->renderer->cdata($text);
77                $this->renderer->p_close();
78            }
79        }
80
81        if(empty($tab)) {
82            $this->renderer->p_open();
83            $this->renderer->cdata($this->plugin->getLang(($type == 'page') ? 'nopages' : 'nosubns'));
84            $this->renderer->p_close();
85        }
86    }
87
88    private function _getSorter($reverse) {
89        if ( $this->natOrder ){
90            return new nspages_naturalOrder_sorter($reverse);
91        } else if ($this->dictOrder) {
92            return new nspages_dictOrder_sorter($reverse, $this->dictOrder);
93        } else {
94            return new nspages_default_sorter($reverse);
95        }
96    }
97
98    private function _keepOnlyNMaxItems(&$tab){
99        if ($this->nbItemsMax){
100            $tab = array_slice($tab, 0, $this->nbItemsMax);
101        }
102    }
103
104    /**
105     * @param Array        $item      Represents the file
106     * @param bool $node true when a node; false when a leaf
107     */
108    protected function _printElement($item, $level=1) {
109        $this->_printElementOpen($item, $level);
110        $this->_printElementContent($item, $level);
111        $this->_printElementClose();
112    }
113
114    protected function _printElementOpen($item, $level) {
115        if($item == null || $item['type'] !== 'd') {
116            $this->renderer->listitem_open($level, false);
117        } else { //Case of a subnamespace
118          $this->renderer->listitem_open($level, true);
119        }
120    }
121
122    protected function _printElementContent($item, $level=1) {
123        $this->renderer->listcontent_open();
124        $this->_printElementLink($item, $level);
125        $this->renderer->listcontent_close();
126    }
127
128    protected function _printElementLink($item, $level=1) {
129        $linkText = "";
130        if ($this->_displayModificationDate) {
131          $linkText = '[' . date('Y-m-d', $item["mtime"]) . '] - ';
132        }
133        $linkText .= $item['nameToDisplay'];
134        if ($this->includeItemsInTOC){
135          $anchorId = $this->buildAnchorId($item);
136          $this->renderer->doc .= '<span id="' . $anchorId . '">';
137          $this->renderer->toc_additem($anchorId, $linkText, $this->renderer->getLastLevel() + $level);
138        }
139        $this->renderer->internallink(':'.$item['id'], $linkText);
140        if ($this->includeItemsInTOC){
141          $this->renderer->doc .= "</span>";
142        }
143    }
144
145    protected function buildAnchorId($item){
146      // Prefix with "nspages_" to avoid collisions with headers
147      return "nspages_" . sectionID($item['id'], self::$builtSectionIds);
148    }
149
150    protected function _printElementClose() {
151        $this->renderer->listitem_close();
152    }
153
154    function printBeginning(){
155        if($this->mode == 'xhtml') {
156            $this->renderer->doc .= '<div class="plugin_nspages">';
157        }
158    }
159
160    function printEnd(){
161        //this is needed to make sure everything after the plugin is written below the output
162        if($this->mode == 'xhtml') {
163            $this->renderer->doc .= '<div class="catpageeofidx"></div>';
164            $this->renderer->doc .= '</div>';
165        } else {
166            $this->renderer->linebreak();
167        }
168    }
169
170    function printTransition(){ }
171}
172