1<?php
2/**
3 * Siteexport Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10if (!defined('DOKU_INC')) die();
11if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
12require_once(DOKU_PLUGIN . 'syntax.php');
13
14/**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
17 */
18class syntax_plugin_siteexport_aggregate extends DokuWiki_Syntax_Plugin {
19
20    private $headers = array();
21
22    public function getType() { return 'substition'; }
23    public function getPType() { return 'block'; }
24    public function getSort() { return 300; }
25
26    public function connectTo($mode) {
27        // $this->Lexer->addSpecialPattern('\{\{(?=siteexport|siteexportAGGREGATOR).*?\}\}', $mode, 'plugin_siteexport_aggregate');
28        $this->Lexer->addSpecialPattern('\{\{siteexportAGGREGATOR .*?\}\}', $mode, 'plugin_siteexport_aggregate');
29    }
30
31    public function handle($match, $state, $pos, Doku_Handler $handler) {
32
33        $options = explode(' ', trim(substr($match, 2, -2)?:""));
34
35        return $options;
36    }
37
38    private function checkComplete( &$item, $key, $namespaces ) {
39        foreach( $namespaces as $namespace ) {
40            if ( !(strpos($item[0], getNS($namespace)) > 0 || strpos($item[0], '|:' . getNS($namespace)) > 0) ) {
41                $item[0] .= '|:' . $namespace;
42            }
43        }
44    }
45
46    public function render($mode, Doku_Renderer $renderer, $data) {
47        global $ID, $conf;
48
49        // $isAggregator = (array_shift($data) == 'siteexportAGGREGATOR');
50        $isAggregator = true;
51        $namespace = array();
52        foreach( $data as $option ) {
53
54            list($key, $value) = explode('=', $option);
55            if ($key == "namespace") {
56                $ns = $value;
57                if ( substr($value, -1) != ':' ) {
58                    $ns .= ':';
59                }
60
61                // The following function wants an page but we only have an NS at this moment
62                $ns .= 'index';
63                $namespace[] = $ns;
64            }
65        }
66
67        if ( empty($namespace) ) {
68            $namespace[] = $ID;
69        }
70
71        if ($mode == 'xhtml'){
72
73            $renderer->info['toc'] = false;
74            $renderer->nocache();
75
76            $formParams = array( 'id' => sectionID('siteexport_site_aggregator', $this->headers), 'action' => wl($ID), 'class' => 'siteexport aggregator' );
77            $form = new Doku_Form($formParams);
78            $functions=& plugin_load('helper', 'siteexport');
79
80            $form->addHidden('ns', $ID);
81            $form->addHidden('site', $ID);
82
83            if ( $isAggregator ) {
84                $form->addHidden('siteexport_aggregate', '1');
85            }
86
87            $submitLabel = $this->getLang('AggregateSubmitLabel');
88            $introduction = $this->getLang('AggragateExportPages');
89            $listAllNamespaces = false;
90            foreach( $data as $option ) {
91
92                list($key, $value) = explode('=', $option);
93                switch ($key) {
94                    case "namespace":
95                    // Done at the top.
96                    break;
97                    case "buttonTitle":
98                    $submitLabel = urldecode($value);
99                    break;
100                    case "introduction":
101                    $introduction = urldecode($value);
102                    break;
103                    case "listAllNamespaces":
104                    $listAllNamespaces = boolval($value);
105                    break;
106                    default:
107                    $form->addHidden($key, $value);
108                    break;
109                }
110            }
111
112            $values = array();
113            $allNamespaces = $functions->__getOrderedListOfPagesForID( $listAllNamespaces ? $namespace : $namespace[0] );
114
115            foreach( $allNamespaces as $ns ) {
116                if ( !array_key_exists('_'.$ns[2], $values) ) {
117                    $values['_'.$ns[2]] = $ns;
118                } else if ( !in_array($ns[0], $values['_'.$ns[2]][4]) ) {
119                    $values['_'.$ns[2]][0] .= '|' . $ns[0];
120                }
121                $values['_'.$ns[2]][4][] = $ns[0];
122            }
123
124            array_walk( $values, array( $this, 'checkComplete'), $namespace);
125            $values = array_values($values);
126
127            $renderer->doc .= '<div class="siteaggregator">';
128
129            if ( empty($values) ) {
130                $renderer->doc .= '<span style="color: #a00">'.$this->getLang('NoEntriesFoundHint').'</span>';
131            } else {
132                $form->addElement(form_makeMenuField('baseID', $values, isset($_REQUEST['baseID']) ? $_REQUEST['baseID'] : $values[0], $introduction));
133                $form->addElement(form_makeButton('submit', 'siteexport', $submitLabel, array('class' => 'button download')));
134
135                ob_start();
136                $form->printForm();
137                $renderer->doc .= ob_get_contents();
138                ob_end_clean();
139            }
140
141            $renderer->doc .= '</div>';
142            return true;
143        } else if ($mode == 'metadata') {
144            $renderer->meta['siteexport']['hasaggregator'] = $isAggregator;
145            $renderer->meta['siteexport']['baseID'] = implode('|', $namespace);
146        }
147
148        return false;
149    }
150}
151
152//Setup VIM: ex: et ts=4 enc=utf-8 :
153