1<?php
2/**
3 * ODTTableCellStyle: class for ODT table cell 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('ODTTableCellStyle');
13
14/**
15 * The ODTTableCellStyle class
16 */
17class ODTTableCellStyle extends ODTStyleStyle
18{
19    static $table_cell_fields = array(
20        'vertical-align'                 => array ('style:vertical-align',              'table-cell',  true),
21        'text-align-source'              => array ('style:text-align-source',           'table-cell',  true),
22        'direction'                      => array ('style:direction',                   'table-cell',  true),
23        'glyph-orientation-vertical'     => array ('style:glyph-orientation-vertical',  'table-cell',  true),
24        'writing-mode'                   => array ('style:writing-mode',                'table-cell',  true),
25        'shadow'                         => array ('style:shadow',                      'table-cell',  true),
26        'background-color'               => array ('fo:background-color',               'table-cell',  true),
27        'border'                         => array ('fo:border',                         'table-cell',  true),
28        'border-top'                     => array ('fo:border-top',                     'table-cell',  true),
29        'border-right'                   => array ('fo:border-right',                   'table-cell',  true),
30        'border-bottom'                  => array ('fo:border-bottom',                  'table-cell',  true),
31        'border-left'                    => array ('fo:border-left',                    'table-cell',  true),
32        'diagonal-tl-br'                 => array ('style:diagonal-tl-br',              'table-cell',  true),
33        'diagonal-tl-br-widths'          => array ('style:diagonal-tl-br-widths',       'table-cell',  true),
34        'diagonal-bl-tr'                 => array ('style:diagonal-bl-tr',              'table-cell',  true),
35        'diagonal-bl-tr-widths'          => array ('style:diagonal-bl-tr-widths',       'table-cell',  true),
36        'border-line-width'              => array ('style:border-line-width',           'table-cell',  true),
37        'border-line-width-top'          => array ('style:border-line-width-top',       'table-cell',  true),
38        'border-line-width-bottom'       => array ('style:border-line-width-bottom',    'table-cell',  true),
39        'border-line-width-left'         => array ('style:border-line-width-left',      'table-cell',  true),
40        'border-line-width-right'        => array ('style:border-line-width-right',     'table-cell',  true),
41        'padding'                        => array ('fo:padding',                        'table-cell',  true),
42        'padding-top'                    => array ('fo:padding-top',                    'table-cell',  true),
43        'padding-right'                  => array ('fo:padding-right',                  'table-cell',  true),
44        'padding-bottom'                 => array ('fo:padding-bottom',                 'table-cell',  true),
45        'padding-left'                   => array ('fo:padding-left',                   'table-cell',  true),
46        'wrap-option'                    => array ('fo:wrap-option',                    'table-cell',  true),
47        'rotation-angle'                 => array ('style:rotation-angle',              'table-cell',  true),
48        'rotation-align'                 => array ('style:rotation-align',              'table-cell',  true),
49        'cell-protect'                   => array ('style:cell-protect',                'table-cell',  true),
50        'print-content'                  => array ('style:print-content',               'table-cell',  true),
51        'decimal-places'                 => array ('style:decimal-places',              'table-cell',  true),
52        'repeat-content'                 => array ('style:repeat-content',              'table-cell',  true),
53        'shrink-to-fit'                  => array ('style:shrink-to-fit',               'table-cell',  true),
54
55        // Fields for background-image
56        // (see '<define name="style-background-image"> in relax-ng schema)'
57        'repeat'                     => array ('style:repeat',                     'table-cell-background-image',  true),
58        'position'                   => array ('style:position',                   'table-cell-background-image',  true),
59        'style:filter-name'          => array ('style:filter-name',                'table-cell-background-image',  true),
60        'opacity'                    => array ('draw:opacity',                     'table-cell-background-image',  true),
61        'type'                       => array ('xlink:type',                       'table-cell-background-image',  true),
62        'href'                       => array ('xlink:href',                       'table-cell-background-image',  true),
63        'show'                       => array ('xlink:show',                       'table-cell-background-image',  true),
64        'actuate'                    => array ('xlink:actuate',                    'table-cell-background-image',  true),
65        'binary-data'                => array ('office:binary-data',               'table-cell-background-image',  true),
66        'base64Binary'               => array ('base64Binary',                     'table-cell-background-image',  true),
67    );
68
69    /**
70     * Constructor.
71     */
72    public function __construct() {
73        parent::__construct();
74    }
75
76    /**
77     * Set style properties by importing values from a properties array.
78     * Properties might be disabled by setting them in $disabled.
79     * The style must have been previously created.
80     *
81     * @param  $properties Properties to be imported
82     * @param  $disabled Properties to be ignored
83     */
84    public function importProperties($properties, $disabled=array()) {
85        $this->importPropertiesInternal(ODTStyleStyle::getStyleProperties (), $properties, $disabled);
86        $this->importPropertiesInternal(ODTTextStyle::getTextProperties (), $properties, $disabled);
87        $this->importPropertiesInternal(ODTParagraphStyle::getParagraphProperties (), $properties, $disabled);
88        $this->importPropertiesInternal(self::$table_cell_fields, $properties, $disabled);
89        $this->setProperty('style-family', $this->getFamily());
90    }
91
92    /**
93     * Check if a style is a common style.
94     *
95     * @return bool Is common style
96     */
97    public function mustBeCommonStyle() {
98        return false;
99    }
100
101    /**
102     * Get the style family of a style.
103     *
104     * @return string Style family
105     */
106    static public function getFamily() {
107        return 'table-cell';
108    }
109
110    /**
111     * Set a property.
112     *
113     * @param $property The name of the property to set
114     * @param $value    New value to set
115     */
116    public function setProperty($property, $value) {
117        $style_fields = ODTStyleStyle::getStyleProperties ();
118        if (array_key_exists ($property, $style_fields)) {
119            $this->setPropertyInternal
120                ($property, $style_fields [$property][0], $value, $style_fields [$property][1]);
121            return;
122        }
123        if (array_key_exists ($property, self::$table_cell_fields)) {
124            $this->setPropertyInternal
125                ($property, self::$table_cell_fields [$property][0], $value, self::$table_cell_fields [$property][1]);
126            return;
127        }
128        $paragraph_fields = ODTParagraphStyle::getParagraphProperties ();
129        if (array_key_exists ($property, $paragraph_fields)) {
130            $this->setPropertyInternal
131                ($property, $paragraph_fields [$property][0], $value, $paragraph_fields [$property][1]);
132            return;
133        }
134        $text_fields = ODTTextStyle::getTextProperties ();
135        if (array_key_exists ($property, $text_fields)) {
136            $this->setPropertyInternal
137                ($property, $text_fields [$property][0], $value, $text_fields [$property][1]);
138            return;
139        }
140    }
141
142    /**
143     * Create new style by importing ODT style definition.
144     *
145     * @param  $xmlCode Style definition in ODT XML format
146     * @return ODTStyle New specific style
147     */
148    static public function importODTStyle($xmlCode) {
149        $style = new ODTTableCellStyle();
150        $attrs = 0;
151
152        $open = XMLUtil::getElementOpenTag('style:style', $xmlCode);
153        if (!empty($open)) {
154            $attrs += $style->importODTStyleInternal(ODTStyleStyle::getStyleProperties (), $open);
155        } else {
156            $open = XMLUtil::getElementOpenTag('style:default-style', $xmlCode);
157            if (!empty($open)) {
158                $style->setDefault(true);
159                $attrs += $style->importODTStyleInternal(ODTStyleStyle::getStyleProperties (), $open);
160            }
161        }
162
163        $open = XMLUtil::getElementOpenTag('style:paragraph-properties', $xmlCode);
164        if (!empty($open)) {
165            $attrs += $style->importODTStyleInternal(ODTParagraphStyle::getParagraphProperties (), $xmlCode);
166        }
167
168        $open = XMLUtil::getElementOpenTag('style:text-properties', $xmlCode);
169        if (!empty($open)) {
170            $attrs += $style->importODTStyleInternal(ODTTextStyle::getTextProperties (), $open);
171        }
172
173        $open = XMLUtil::getElementOpenTag('style:table-cell-properties', $xmlCode);
174        if (!empty($open)) {
175            $attrs += $style->importODTStyleInternal(self::$table_cell_fields, $open);
176        }
177
178        // If style has no meaningfull content then throw it away
179        if ( $attrs == 0 ) {
180            return NULL;
181        }
182
183        return $style;
184    }
185
186    static public function getTableCellProperties () {
187        return self::$table_cell_fields;
188    }
189
190    /**
191     * This function creates a table cell style using the style as set in the assoziative array $properties.
192     * The parameters in the array should be named as the CSS property names e.g. 'color' or 'background-color'.
193     * Properties which shall not be used in the style can be disabled by setting the value in disabled_props
194     * to 1 e.g. $disabled_props ['color'] = 1 would block the usage of the color property.
195     *
196     * The currently supported properties are:
197     * background-color, vertical-align
198     *
199     * The function returns the name of the new style or NULL if all relevant properties are empty.
200     *
201     * @author LarsDW223
202     * @param $properties
203     * @param null $disabled_props
204     * @return ODTTableCellStyle or NULL
205     */
206    public static function createTableCellStyle(array $properties, array $disabled_props = NULL){
207        // Create style name (if not given).
208        $style_name = $properties ['style-name'];
209        if ( empty($style_name) ) {
210            $style_name = self::getNewStylename ('TableCell');
211            $properties ['style-name'] = $style_name;
212        }
213
214        // Create empty table cell style.
215        $object = new ODTTableCellStyle();
216        if (!isset($object)) {
217            return NULL;
218        }
219
220        // Import our properties
221        $object->importProperties($properties, $disabled_props);
222        return $object;
223    }
224}
225