1<?php
2/**
3 * ODT Plugin: Exports book consisting of more wikipages to ODT file
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Gerrit Uitslag <klapinklapin@gmail.com>
7 */
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11/**
12 * The Book Renderer.
13 *
14 * @package DokuWiki\Renderer\Book
15 */
16class renderer_plugin_odt_book extends renderer_plugin_odt_page {
17
18    /** @var int number of wikipages exported with the ODT renderer */
19    protected $wikipages_count = 0;
20    /** @var string document title*/
21    protected $title = '';
22    /**
23     * Stores action instance
24     *
25     * @var action_plugin_dw2pdf
26     */
27    private $actioninstance = null;
28
29    /**
30     * load action plugin instance
31     */
32    public function __construct() {
33        parent::__construct();
34        $this->actioninstance = plugin_load('action', 'odt_export');
35    }
36
37    /**
38     * clean out any per-use values
39     *
40     * This is called before each use of the renderer object and should normally be used to
41     * completely reset the state of the renderer to be reused for a new document.
42     * For ODT book it resets only some properties.
43     */
44    public function reset() {
45        $this->doc = '';
46    }
47    /**
48     * Initialize the document
49     */
50    public function document_start() {
51        global $ID;
52
53        // number of wiki pages included in ODT file
54        $this->wikipages_count ++;
55
56
57        if($this->isBookStart()) {
58            parent::document_start();
59        } else {
60            $this->pagebreak();
61            $this->set_page_bookmark($ID);
62        }
63    }
64
65    /**
66     * Closes the document
67     */
68    public function document_end() {
69        //ODT file creation is performed by finalize_ODTfile()
70
71        // Refresh certain config parameters e.g. 'disable_links'
72        // to switch links back to the configured value if they were enabled/disabled at some point
73        $this->config->refresh();
74    }
75
76    /**
77     * Completes the ODT file
78     */
79    public function finalize_ODTfile() {
80        // FIXME remove last pagebreak
81        // <text:p text:style-name="pagebreak"/>
82
83        $this->document->setTitle($this->title);
84
85        parent::finalize_ODTfile();
86    }
87
88    /**
89     * Book start at the first page
90     *
91     * @return bool
92     */
93    public function isBookStart() {
94        if($this->wikipages_count == 1) {
95            return true;
96        }
97        return false;
98    }
99
100    /**
101     * Set title for ODT document
102     *
103     * @param string $title
104     */
105    public function setTitle($title) {
106        $this->title = $title;
107    }
108
109    /**
110     * Render a wiki internal link.
111     * In book export mode a local link with a name/test will be inserted if the
112     * referenced page is included in the exported pages. Otherwise an external
113     * link will be created.
114     *
115     * @param string       $id   page ID to link to. eg. 'wiki:syntax'
116     * @param string|array $name name for the link, array for media file
117     * @param bool         $returnonly whether to return odt or write to doc attribute
118     *
119     * @author Andreas Gohr <andi@splitbrain.org>, LarsDW223
120     */
121    function internallink($id, $name = NULL, $returnonly = false) {
122        global $ID;
123        // default name is based on $id as given
124        $default = $this->_simpleTitle($id);
125        // now first resolve and clean up the $id
126        resolve_pageid(getNS($ID),$id,$exists);
127        $name = $this->_getLinkTitle($name, $default, $isImage, $id);
128
129        // build the absolute URL (keeping a hash if any)
130        list($id,$hash) = explode('#',$id,2);
131
132        // Is the link a link to a page included in the book?
133        $pages = $this->actioninstance->getExportedPages();
134        if ( in_array($id, $pages) ) {
135            // Yes, create a local link with a name
136            if($returnonly) {
137                return $this->locallink_with_text($hash, $id, $name, $returnonly);
138            } else {
139                $this->locallink_with_text($hash, $id, $name, $returnonly);
140            }
141            return;
142        }
143
144        // No, create an external link
145        $url = wl($id,'',true);
146        if($hash) $url .='#'.$hash;
147
148        if ($ID == $id) {
149            if($returnonly) {
150                return $this->locallink_with_text($hash, $id, $name, $returnonly);
151            } else {
152                $this->locallink_with_text($hash, $id, $name, $returnonly);
153            }
154        } else {
155            if($returnonly) {
156                return $this->_doLink($url, $name, $returnonly);
157            } else {
158                $this->_doLink($url, $name, $returnonly);
159            }
160        }
161    }
162}
163