1<?php
2/**
3 * ODTMasterPageStyle: class for ODT text list styles.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author LarsDW223
7 */
8
9require_once DOKU_INC.'lib/plugins/odt/ODT/XMLUtil.php';
10require_once DOKU_INC.'lib/plugins/odt/ODT/styles/ODTStyle.php';
11
12/**
13 * The ODTMasterPageStyle class
14 */
15class ODTMasterPageStyle extends ODTStyle
16{
17    static $master_fields = array(
18        // Fields belonging to "style:master-page"
19        'style-name'                       => array ('style:name',                         'style', false),
20        'style-display-name'               => array ('style:display-name',                 'style', false),
21        'style-page-layout-name'           => array ('style:page-layout-name',             'style', false),
22        'draw-style-name'                  => array ('draw:style-name',                    'style', false),
23        'style-next'                       => array ('style:next-style-name',              'style', true),
24    );
25
26    static $header_footer_fields = array(
27        // Fields belonging to "style:header", "style:footer",
28        // "style:header-left" and "style:footer-left"
29        // The content/child-elements of "style:header" are saved as is
30        'style-display'                    => array ('style:display',                      'header', true),
31    );
32
33    protected $master_style = array();
34    protected $style_header = array();
35    protected $style_footer = array();
36    protected $style_header_left = array();
37    protected $style_footer_left = array();
38    protected $content_header = NULL;
39    protected $content_footer = NULL;
40    protected $content_header_left = NULL;
41    protected $content_footer_left = NULL;
42
43    /**
44     * Get the element name for the ODT XML encoding of the style.
45     */
46    public function getElementName() {
47        return 'style:master-page';
48    }
49
50    /**
51     * Set style properties by importing values from a properties array.
52     * Properties might be disabled by setting them in $disabled.
53     * The style must have been previously created.
54     *
55     * @param  $properties Properties to be imported
56     * @param  $disabled Properties to be ignored
57     */
58    public function importProperties($properties, $disabled) {
59        $this->importPropertiesInternal(self::$master_fields, $properties, $disabled, $this->master_style);
60    }
61
62    /**
63     * Check if a style is a common style.
64     *
65     * @return bool Is common style
66     */
67    public function mustBeCommonStyle() {
68        return false;
69    }
70
71    /**
72     * Set a property.
73     *
74     * @param $property The name of the property to set
75     * @param $value    New value to set
76     */
77    public function setProperty($property, $value) {
78        if (array_key_exists ($property, self::$master_fields)) {
79            $this->setPropertyInternal
80                ($property, self::$master_fields [$property][0], $value, self::$master_fields [$property][1], $this->master_style);
81            return;
82        }
83    }
84
85    /**
86     * Get the value of a property.
87     *
88     * @param  $property The property name
89     * @return string The current value of the property
90     */
91    public function getProperty($property) {
92        if (array_key_exists ($property, self::$master_fields)) {
93            return $this->master_style [$property]['value'];
94        }
95        return NULL;
96    }
97
98    /**
99     * Create new style by importing ODT style definition.
100     *
101     * @param  $xmlCode Style definition in ODT XML format
102     * @return ODTStyle New specific style
103     */
104    static public function importODTStyle($xmlCode) {
105        $style = new ODTMasterPageStyle();
106
107        // Get attributes for element 'style:master-page'
108        $open = XMLUtil::getElementOpenTag('style:master-page', $xmlCode);
109        if (!empty($open)) {
110            $style->importODTStyleInternal(self::$master_fields, $open, $style->master_style);
111        }
112
113        // Get attributes for element 'style:header'
114        $open = XMLUtil::getElementOpenTag('style:header', $xmlCode);
115        if (!empty($open)) {
116            $style->importODTStyleInternal(self::$header_footer_fields, $open, $style->style_header);
117            $content_header = XMLUtil::getElementContent ('style:header', $xmlCode);
118        }
119
120        // Get attributes for element 'style:footer'
121        $open = XMLUtil::getElementOpenTag('style:footer', $xmlCode);
122        if (!empty($open)) {
123            $style->importODTStyleInternal(self::$header_footer_fields, $open, $style->style_footer);
124            $content_footer = XMLUtil::getElementContent ('style:footer', $xmlCode);
125        }
126
127        // Get attributes for element 'style:header-left'
128        $open = XMLUtil::getElementOpenTag('style:header-left', $xmlCode);
129        if (!empty($open)) {
130            $style->importODTStyleInternal(self::$header_footer_fields, $open, $style->style_header_left);
131            $content_header_left = XMLUtil::getElementContent ('style:header-left', $xmlCode);
132        }
133
134        // Get attributes for element 'style:footer-left'
135        $open = XMLUtil::getElementOpenTag('style:footer-left', $xmlCode);
136        if (!empty($open)) {
137            $style->importODTStyleInternal(self::$header_footer_fields, $open, $style->style_footer_left);
138            $content_footer_left = XMLUtil::getElementContent ('style:footer-left', $xmlCode);
139        }
140
141        return $style;
142    }
143
144    /**
145     * Encode current style values in a string and return it.
146     *
147     * @return string ODT XML encoded style
148     */
149    public function toString() {
150        $style = '';
151        $master = '';
152        $header = '';
153        $footer = '';
154        $header_left = '';
155        $footer_left = '';
156
157        // Get master style ODT properties
158        foreach ($this->master_style as $property => $items) {
159            $master .= $items ['odt_property'].'="'.$items ['value'].'" ';
160        }
161
162        // Get header ODT properties
163        foreach ($this->style_header as $property => $items) {
164            $header .= $items ['odt_property'].'="'.$items ['value'].'" ';
165        }
166
167        // Get footer ODT properties
168        foreach ($this->style_footer as $property => $items) {
169            $footer .= $items ['odt_property'].'="'.$items ['value'].'" ';
170        }
171
172        // Get header-left ODT properties
173        foreach ($this->style_header_left as $property => $items) {
174            $header_left .= $items ['odt_property'].'="'.$items ['value'].'" ';
175        }
176
177        // Get footer-left ODT properties
178        foreach ($this->style_footer_left as $property => $items) {
179            $footer_left .= $items ['odt_property'].'="'.$items ['value'].'" ';
180        }
181
182        // Build style.
183        $style  = '<style:master-page '.$master.">\n";
184        if ( !empty($header) || !empty($content_header) ) {
185            $style .= '<style:header '.$header.">\n";
186            $style .= $content_header;
187            $style .= '</style:header>'."\n";
188        }
189        if ( !empty($footer) || !empty($content_footer) ) {
190            $style .= '<style:footer '.$footer.">\n";
191            $style .= $content_footer;
192            $style .= '</style:footer>'."\n";
193        }
194        if ( !empty($header_left) || !empty($content_header_left) ) {
195            $style .= '<style:header-left '.$header_left.">\n";
196            $style .= $content_header_left;
197            $style .= '</style:header-left>'."\n";
198        }
199        if ( !empty($footer_left) || !empty($content_footer_left) ) {
200            $style .= '<style:footer-left '.$footer_left.">\n";
201            $style .= $content_footer_left;
202            $style .= '</style:footer-left>'."\n";
203        }
204        $style .= '</style:master-page'.">\n";
205        return $style;
206    }
207}
208
209