1<?php
2
3require_once DOKU_PLUGIN . 'odt/ODT/ODTDocument.php';
4
5/**
6 * ODTHeading:
7 * Class containing static code for handling headings.
8 *
9 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
10 */
11class ODTHeading
12{
13    /**
14     * Render a heading
15     *
16     * @param string $text  the text to display
17     * @param int    $level header level
18     * @param int    $pos   byte position in the original source
19     */
20    static public function heading(ODTInternalParams $params, $text, $level, $element=NULL, $attributes=NULL){
21        // Close any open paragraph first
22        $params->document->paragraphClose();
23
24        $hid = self::headerToLink($params->document, $text, true);
25        $TOCRef = $params->document->buildTOCReferenceID($text);
26        $style = $params->document->getStyleName('heading'.$level);
27
28        // Change page format if pending
29        if ( $params->document->pageFormatChangeIsPending() ) {
30            $pageStyle = $params->document->doPageFormatChange($style);
31            if ( isset($pageStyle) ) {
32                $style = $pageStyle;
33
34                // Delete pagebreak, the format change will also introduce a pagebreak.
35                $params->document->setPagebreakPending(false);
36            }
37        }
38
39        // Insert pagebreak if pending
40        if ( $params->document->pagebreakIsPending() ) {
41            $style = $params->document->createPagebreakStyle ($style);
42            $params->document->setPagebreakPending(false);
43        }
44        $params->content .= '<text:h text:style-name="'.$style.'" text:outline-level="'.$level.'">';
45
46        // Insert page bookmark if requested and not done yet.
47        $params->document->insertPendingPageBookmark();
48
49        $params->content .= '<text:bookmark-start text:name="'.$TOCRef.'"/>';
50        $params->content .= '<text:bookmark-start text:name="'.$hid.'"/>';
51        $params->content .= $params->document->replaceXMLEntities($text);
52        $params->content .= '<text:bookmark-end text:name="'.$TOCRef.'"/>';
53        $params->content .= '<text:bookmark-end text:name="'.$hid.'"/>';
54        $params->content .= '</text:h>';
55
56        // Do not add headings in frames
57        $frame = $params->document->state->getCurrentFrame();
58        if (!isset($frame)) {
59            $params->document->tocAddItemInternal($TOCRef, $hid, $text, $level);
60        }
61    }
62
63    /**
64     * Creates a linkid from a headline
65     *
66     * @param string $title The headline title
67     * @param boolean $create Create a new unique ID?
68     * @return string
69     *
70     * @author Andreas Gohr <andi@splitbrain.org>
71     */
72    static protected function headerToLink(ODTDocument $doc, $title,$create=false) {
73        // FIXME: not DokuWiki dependant function woud be nicer...
74        $title = str_replace(':','',cleanID($title));
75        $title = ltrim($title,'0123456789._-');
76        if(empty($title)) {
77            $title='section';
78        }
79
80        if($create){
81            // Make sure tiles are unique
82            $num = '';
83            while($doc->headerExists($title.$num)){
84                ($num) ? $num++ : $num = 1;
85            }
86            $title = $title.$num;
87            $doc->addHeader($title);
88        }
89
90        return $title;
91    }
92}
93