1<?php
2/**
3 * ODTTableColumnStyle: class for ODT table column 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 'ODTStyle.php';
11
12ODTStyleStyle::register('ODTTableColumnStyle');
13
14/**
15 * The ODTTableColumnStyle class
16 */
17class ODTTableColumnStyle extends ODTStyleStyle
18{
19    static $table_column_fields = array(
20        'column-width'               => array ('style:column-width',              'table-column',  true),
21        'rel-column-width'           => array ('style:rel-column-width',          'table-column',  true),
22        'use-optimal-column-width'   => array ('style:use-optimal-column-width',  'table-column',  true),
23        'break-before'               => array ('fo:break-before',                 'table-column',  true),
24        'break-after'                => array ('fo:break-after',                  'table-column',  true),
25    );
26
27    /**
28     * Constructor.
29     */
30    public function __construct() {
31        parent::__construct();
32    }
33
34    /**
35     * Set style properties by importing values from a properties array.
36     * Properties might be disabled by setting them in $disabled.
37     * The style must have been previously created.
38     *
39     * @param  $properties Properties to be imported
40     * @param  $disabled Properties to be ignored
41     */
42    public function importProperties($properties, $disabled) {
43        $this->importPropertiesInternal(ODTStyleStyle::getStyleProperties (), $properties, $disabled);
44        $this->importPropertiesInternal(self::$table_column_fields, $properties, $disabled);
45        $this->setProperty('style-family', $this->getFamily());
46    }
47
48    /**
49     * Check if a style is a common style.
50     *
51     * @return bool Is common style
52     */
53    public function mustBeCommonStyle() {
54        return false;
55    }
56
57    /**
58     * Get the style family of a style.
59     *
60     * @return string Style family
61     */
62    static public function getFamily() {
63        return 'table-column';
64    }
65
66    /**
67     * Set a property.
68     *
69     * @param $property The name of the property to set
70     * @param $value    New value to set
71     */
72    public function setProperty($property, $value) {
73        $style_fields = ODTStyleStyle::getStyleProperties ();
74        if (array_key_exists ($property, $style_fields)) {
75            $this->setPropertyInternal
76                ($property, $style_fields [$property][0], $value, $style_fields [$property][1]);
77            return;
78        }
79        if (array_key_exists ($property, self::$table_column_fields)) {
80            $this->setPropertyInternal
81                ($property, self::$table_column_fields [$property][0], $value, self::$table_column_fields [$property][1]);
82            return;
83        }
84    }
85
86    /**
87     * Create new style by importing ODT style definition.
88     *
89     * @param  $xmlCode Style definition in ODT XML format
90     * @return ODTStyle New specific style
91     */
92    static public function importODTStyle($xmlCode) {
93        $style = new ODTTableColumnStyle();
94        $attrs = 0;
95
96        $open = XMLUtil::getElementOpenTag('style:style', $xmlCode);
97        if (!empty($open)) {
98            $attrs += $style->importODTStyleInternal(ODTStyleStyle::getStyleProperties (), $open);
99        } else {
100            $open = XMLUtil::getElementOpenTag('style:default-style', $xmlCode);
101            if (!empty($open)) {
102                $style->setDefault(true);
103                $attrs += $style->importODTStyleInternal(ODTStyleStyle::getStyleProperties (), $open);
104            }
105        }
106
107        $open = XMLUtil::getElementOpenTag('style:table-column-properties', $xmlCode);
108        if (!empty($open)) {
109            $attrs += $style->importODTStyleInternal(self::$table_column_fields, $open);
110        }
111
112        // If style has no meaningfull content then throw it away
113        if ( $attrs == 0 ) {
114            return NULL;
115        }
116
117        return $style;
118    }
119
120    static public function getTableColumnProperties () {
121        return self::$table_column_fields;
122    }
123
124    /**
125     * This function creates a table column style using the style as set in the assoziative array $properties.
126     * The parameters in the array should be named as the CSS property names e.g. 'color' or 'background-color'.
127     * Properties which shall not be used in the style can be disabled by setting the value in disabled_props
128     * to 1 e.g. $disabled_props ['color'] = 1 would block the usage of the color property.
129     *
130     * The currently supported properties are:
131     * width
132     *
133     * The function returns the name of the new style or NULL if all relevant properties are empty.
134     *
135     * @author LarsDW223
136     *
137     * @param $style
138     * @param $properties
139     * @param null $disabled_props
140     * @param null $style_name
141     * @return ODTUnknownStyle or NULL
142     */
143    public static function createTableColumnStyle(array $properties, array $disabled_props = NULL){
144        // Create style name (if not given).
145        $style_name = $properties ['style-name'];
146        if ( empty($style_name) ) {
147            $style_name = self::getNewStylename ('TableColumn');
148            $properties ['style-name'] = $style_name;
149        }
150
151        // Convert width to ODT format
152        $table_co_width = $properties ['width'];
153        if ( !empty ($table_co_width) ) {
154            $length = strlen ($table_co_width);
155            if ( $table_co_width [$length-1] != '%' ) {
156                $properties ['column-width'] = $table_co_width;
157            } else {
158                // Columns have a specific syntax for relative width in %!
159                // Change % to *.
160                // Also mutiply with 10:
161                // For some reason values for two columns like 10* and 90* do not give the result 10% and 90%.
162                // But 100* and 900* do give the wanted reuslt! (weird)
163                $table_co_width = trim ($table_co_width, '%');
164                $properties ['rel-column-width'] = ($table_co_width*10).'*';
165            }
166        }
167
168        // Create empty table column style.
169        $object = new ODTTableColumnStyle();
170        if ($object == NULL) {
171            return NULL;
172        }
173
174        // Import our properties
175        $object->importProperties($properties, $disabled_props);
176        return $object;
177    }
178}
179
180