1<?php
2// must be run within Dokuwiki
3if (!defined('DOKU_INC')) die();
4
5class helper_plugin_tabinclude extends DokuWiki_Plugin {
6    var $sort = ''; // sort key
7    /**
8     * Constructor
9     */
10    function helper_plugin_tabinclude() {
11        global $conf;
12        // load sort key from settings
13        $this->sort = $this->getConf('sortkey');
14    }
15
16    function getMethods() {
17        $result = array();
18        $result[] = array(
19                'name'   => 'renderTabsHtml',
20                'desc'   => 'Render tabs HTML of DokuWiki pages',
21                'params' => array(
22                        'renderer'         => 'renderer',
23                        'tabs'             => array('page'=>'page ID','title'=>'page title','error'=>'error msg'),
24                        'init_page_idx'    => 'int',
25                        'class (optional)' => 'string'),
26                'return' => array('html' => 'string'),
27        );
28        $result[] = array(
29                'name'   => 'getOdtHtml',
30                'desc'   => 'Render tabs ODT of DokuWiki pages',
31                'params' => array(
32                        'renderer' => 'renderer',
33                        'tabs'     =>  array('page'=>'page ID','title'=>'page title','error'=>'error msg'),),
34                        'return' => array('odt' => 'string'),
35        );
36
37        return $result;
38    }
39
40
41    /**
42     * Get Tabpages information
43     */
44    function getTabPages($match,$isInline=true){
45        global $ID;
46        $page_delim = $isInline?",":"\n";
47        list($class, $match) = explode('>', $match); // extract class
48        $class=trim($class);
49        $pages = explode($page_delim,$match); // extract page names
50        $sz = count($pages);
51        if($sz==0) return array();
52        $sz = count($pages);
53
54        // loop for tabs
55        $tabs = array();
56        $init_page_idx = 0; // initial page index
57        for($i=0;$i<$sz;$i++){
58            // Put page ID into $page
59            // Put text in tab into $title
60            $title='';
61            $page = trim($pages[$i]);
62
63            if($isInline){
64                // Inline description
65                if($page[0]=='*'){
66                    $init_page_idx=count($tabs);
67                    $page = substr($page,1);
68                }
69                $items = explode('|',$page);
70                if(count($items)>1){
71                    list($page,$title)=$items;
72                }
73            }else{
74                // Lines description
75
76                if (preg_match('/\[\[(.+?)\]\]/', $page, $match)){
77                    // Pagelist like syntax,
78                    // each tabbed page is provided as DokuWiki unordered list syntax:
79                    // *[[id|title]] or **[[id|title]].
80                    $p = substr($page,0,strpos($page,'[['));
81                    $page = $match[1];
82                    if($page=='') continue;
83                    if (strpos($p,'*') !== strrpos($p,'*')) {
84                        // multiple '*' means initial page!!
85                        $init_page_idx = count($tabs);
86                    }
87                } else {
88                    // original syntax
89                    $asterisk = false;
90                    while($page[0]=='*'){
91                        $page = substr($page,1);
92                        $asterisk = true;
93                    }
94                    if($page=='') continue;
95                    if($asterisk) $init_page_idx = count($tabs);
96                }
97                list($page, $title) = explode('|', $page, 2);
98                list($page ,$section) = explode('#', $page, 2);
99                if($page=='') continue;
100            }
101
102            // Build tab title
103            resolve_pageid(getNS($ID),$page,$exists);
104            if($title==''){
105                // Show first heading as tab name ?
106                if($this->getConf('use_first_heading')){
107                    $meta_title= p_get_metadata($page,'title');
108                    if($meta_title!=''){
109                        $title = $meta_title;
110                    }
111                }
112
113                // Show namespace(s) of page name in tab ?
114                if($title==''){
115                    $title = $this->getConf('namespace_in_tab')?$page:noNS($page);
116                }
117            }
118
119            // Check errors
120            if(page_exists($page)==false){
121                // page in tab exists ?
122                $tabs[] = array('error'=>tpl_link(wl($page),$page,'',true).' - '.$this->getLang('error_notfound'));
123            }else if($ID==$page){
124                // page is identical to parent ?
125                $tabs[] = array('error'=>$this->getLang('error_parent'));
126            }else{
127                $tabs[] = array('page'=>hsc($page),'title'=>hsc($title));
128            }
129        }
130        return array(DOKU_LEXER_MATCHED,$tabs,$init_page_idx,hsc(trim($class)));
131    }
132
133    /**
134     * Get AJAX tab XHTML from pagenames
135     */
136    function renderTabsHtml(&$renderer,$tabs,$init_page_idx,$class='') {
137        // render
138        if($class) $class=' class="'.$class.'"';
139        $html= '<div id="dwpl-ti-container"'.$class.'>'.NL;
140
141        $html.='<ul class="dwpl-ti">'.NL;
142        $sz = count($tabs);
143        for($i=0;$i<$sz;$i++){
144            if(empty($tabs[$i]['error'])){
145                $selected_class=($init_page_idx==$i)?' selected':'';
146                $html.='<li class="dwpl-ti-tab"><div class="dwpl-ti-tab-title'.$selected_class.'" value="'.$tabs[$i]['page'].'">'.$tabs[$i]['title'].'</div></li>'.NL;
147            }else{
148                $html.='<li class="dwpl-ti-tab"><div class="dwpl-ti-tab-title error">'.$tabs[$i]['error'].'</div></li>'.NL;
149            }
150        }
151        $html.= '</ul>'.NL;
152        $html.='<div class="dwpl-ti-content-box">';
153        if($this->getConf('hideloading')!=1){
154            $html.='<div id="dwpl-ti-loading" class="dwpl-ti-loading">'.$this->getLang('loading').'</div>';
155        }
156        $html.='<div id="dwpl-ti-content" class="dwpl-ti-content">';
157        if($this->getConf('ajax_init_page')!=0){
158            $html.='<div id="dwpl-ti-read-init-page" class="hidden" value="'.$tabs[$init_page_idx]['page'].'"></div>';
159        }
160
161        $goto = $this->getLang('gotohere');
162        $pagelink = tpl_link(wl($tabs[$init_page_idx]['page']),$goto,'',true);
163        if($this->getConf('goto_link_header')!=0)
164            $html.= '<div class="dwpl-ti-permalink-header">'.$pagelink.'</div>'.NL;
165        if($this->getConf('ajax_init_page')==0){
166            $html.=tpl_include_page($tabs[$init_page_idx]['page'],false);
167        }
168        if($this->getConf('goto_link_footer')!=0)
169            $html.= '<div class="dwpl-ti-permalink-footer">'.$pagelink.'</div>'.NL;
170        $html.= '</div></div>'.NL.'</div>'.NL;
171
172
173        $renderer->doc.=$html;
174
175        global $conf;
176        if($conf['allowdebug']==1){
177            $renderer->doc.="<!-- \n".print_r(array($tabs,$init_page_idx,$class),true)."\n -->";
178        }
179
180        return true;
181    }
182    /**
183     * Get links of tab XHTML from pagenames
184     */
185    function renderLinkTabs(&$renderer,$tabs,$init_page_idx,$class='') {
186        global $ID;
187        // Selected page defined ?
188        if(isset($_GET['tabpage_idx'])) $init_page_idx = $_GET['tabpage_idx'];
189
190        // render
191        if($class) $class=' class="'.$class.'"';
192        $html.= '<div id="dwpl-ti-container"'.$class.'>'.NL;
193        $html.='<ul class="dwpl-ti">'.NL;
194        $sz = count($tabs);
195        for($i=0;$i<$sz;$i++){
196            if(empty($tabs[$i]['error'])){
197                $selected_class=($init_page_idx==$i)?' selected':'';
198                $html.='<li class="dwpl-ti-tab">';
199                $html.=tpl_link(wl($ID,'tabpage_idx='.$i.'#dokuwiki__content'),$tabs[$i]['title'],'class="'.$selected_class.'"',true);
200                $html.='</li>'.NL;
201            }else{
202                $html.='<li class="dwpl-ti-tab">';
203                $html.='<div class="dwpl-ti-tab-title error">'.$tabs[$i]['error'].'</div>';
204                $html.='</li>'.NL;
205            }
206        }
207        $html.= '</ul>'.NL;
208        $html.='<div class="dwpl-ti-content-box">';
209        $html.='<div id="dwpl-ti-content" class="dwpl-ti-content">';
210
211        $goto = $this->getLang('gotohere');
212        $pagelink = tpl_link(wl($tabs[$init_page_idx]['page']),$goto,'',true);
213        if($this->getConf('goto_link_header')!=0)
214            $html.= '<div class="dwpl-ti-permalink-header">'.$pagelink.'</div>'.NL;
215        $html.=tpl_include_page($tabs[$init_page_idx]['page'],false);
216        if($this->getConf('goto_link_footer')!=0)
217            $html.= '<div class="dwpl-ti-permalink-footer">'.$pagelink.'</div>'.NL;
218        $html.= '</div></div>'.NL.'</div>'.NL;
219
220        $renderer->doc.=$html;
221
222        global $conf;
223        if($conf['allowdebug']==1){
224            $renderer->doc.="<!-- \n".print_r(array($tabs,$init_page_idx,$class),true)."\n -->";
225        }
226
227        return true;
228    }
229
230    /**
231     * Get embed tabs XHTML from pagenames
232     */
233    function renderEmbedTabs(&$renderer,$tabs,$init_page_idx,$class='') {
234        global $ID;
235        // Selected page defined ?
236        if(isset($_GET['tabpage_idx'])) $init_page_idx = $_GET['tabpage_idx'];
237
238        // render all tabs !
239        if($class) $class=' class="'.$class.'"';
240        $html.= '<div id="dwpl-ti-container"'.$class.'>'.NL;
241        $html.='<ul class="dwpl-ti">'.NL;
242        $sz = count($tabs);
243        for($i=0;$i<$sz;$i++){
244            if(empty($tabs[$i]['error'])){
245                $selected_class=($init_page_idx==$i)?' selected':'';
246                $html.='<li class="dwpl-ti-tab">';
247                $html.='<div class="dwpl-ti-tab-embd-title'.$selected_class.'">'.$tabs[$i]['title'].'</div></li>'.NL;
248                $html.='</li>'.NL;
249            }else{
250                $html.='<li class="dwpl-ti-tab">';
251                $html.='<div class="dwpl-ti-tab-title error">'.$tabs[$i]['error'].'</div>';
252                $html.='</li>'.NL;
253            }
254        }
255        $html.= '</ul>'.NL;
256        $html.='<div class="dwpl-ti-content-box">';
257
258        for($i=0;$i<$sz;$i++){
259            $html.='<div id="dwpl-ti-content" class="dwpl-ti-content">';
260            if($i==$init_page_idx)
261                $html.='<div class="dwpl-ti-tab-embd">';
262            else
263                $html.='<div class="dwpl-ti-tab-embd hidden">';
264            $goto = $this->getLang('gotohere');
265            $pagelink = tpl_link(wl($tabs[$i]['page']),$goto,'',true);
266            if($this->getConf('goto_link_header')!=0)
267                $html.= '<div class="dwpl-ti-permalink-header">'.$pagelink.'</div>'.NL;
268            $html.=tpl_include_page($tabs[$i]['page'],false);
269            if($this->getConf('goto_link_footer')!=0)
270                $html.= '<div class="dwpl-ti-permalink-footer">'.$pagelink.'</div>'.NL;
271            $html.= '</div></div>'.NL;
272        }
273        $html.='</div>'.NL;
274        $html.='</div>'.NL;
275
276        $renderer->doc.=$html;
277
278        global $conf;
279        if($conf['allowdebug']==1){
280            $renderer->doc.="<!-- \n".print_r(array($tabs,$init_page_idx,$class),true)."\n -->";
281        }
282
283        return true;
284    }
285    /**
286     * Get tab ODT from pagenames
287     */
288    function getOdtHtml(&$renderer,$tabs){
289        $renderer->strong_open();
290        $renderer->doc.='Tab pages';
291        $renderer->strong_close();
292        $renderer->p_close();
293
294        $renderer->listu_open();
295        for($i=0;$i<$sz;$i++){
296            $page = $tabs[$i]['page'];
297            $title = $tabs[$i]['title'];
298            $desc = p_get_metadata($page,'description');
299
300            if(empty($tabs[$i]['error'])){
301                $renderer->listitem_open();
302                $renderer->p_open();
303                $renderer->internallink($page,$title);
304                $renderer->p_close();
305
306                if(is_array($desc)){
307                    $renderer->p_open();
308                    $renderer->doc.=hsc($desc['abstract']);
309                    $renderer->p_close();
310                }
311                $renderer->listitem_close();
312            }else{
313                $renderer->p_open();
314                $renderer->doc.=$tabs[$i]['error'];
315                $renderer->p_close();
316            }
317        }
318        $renderer->listu_close();
319        $renderer->p_open();
320
321        global $conf;
322        if($conf['allowdebug']==1){
323            $renderer->doc.="<!-- \n".print_r(array($tabs,$init_page_idx,$class),true)."\n -->";
324        }
325
326        return true;
327    }
328}
329