1<?php
2/**
3 * ODTTableRowStyle: class for ODT table row styles.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     LarsDW223
7 * @package    ODT\Styles\ODTTableRowStyle
8 */
9
10/** Include XMLUtil and ODTStyle */
11require_once DOKU_INC.'lib/plugins/odt/ODT/XMLUtil.php';
12require_once 'ODTStyle.php';
13
14ODTStyleStyle::register('ODTTableRowStyle');
15
16/**
17 * The ODTTableRowStyle class.
18 */
19class ODTTableRowStyle extends ODTStyleStyle
20{
21    /** var array List of properties belonging to an ODT table. */
22    static $table_row_fields = array(
23        'row-height'               => array ('style:row-height',              'table-row',  true),
24        'min-row-height'           => array ('style:min-row-height',          'table-row',  true),
25        'use-optimal-row-height'   => array ('style:use-optimal-row-height',  'table-row',  true),
26        'background-color'         => array ('fo:background-color',           'table-row',  true),
27        'background-color'         => array ('fo:background-color',           'table-row',  true),
28        'break-before'             => array ('fo:break-before',               'table-row',  true),
29        'break-after'              => array ('fo:break-after',                'table-row',  true),
30        'keep-together'            => array ('fo:keep-together',              'table-row',  true),
31
32        // Fields for background-image
33        // (see '<define name="style-background-image"> in relax-ng schema)'
34        'repeat'                     => array ('style:repeat',                     'table-row-background-image',  true),
35        'position'                   => array ('style:position',                   'table-row-background-image',  true),
36        'style:filter-name'          => array ('style:filter-name',                'table-row-background-image',  true),
37        'opacity'                    => array ('draw:opacity',                     'table-row-background-image',  true),
38        'type'                       => array ('xlink:type',                       'table-row-background-image',  true),
39        'href'                       => array ('xlink:href',                       'table-row-background-image',  true),
40        'show'                       => array ('xlink:show',                       'table-row-background-image',  true),
41        'actuate'                    => array ('xlink:actuate',                    'table-row-background-image',  true),
42        'binary-data'                => array ('office:binary-data',               'table-row-background-image',  true),
43        'base64Binary'               => array ('base64Binary',                     'table-row-background-image',  true),
44    );
45
46    /**
47     * Constructor.
48     */
49    public function __construct() {
50        parent::__construct();
51    }
52
53    /**
54     * Set style properties by importing values from a properties array.
55     * Properties might be disabled by setting them in $disabled.
56     * The style must have been previously created.
57     *
58     * @param    $properties    Properties to be imported
59     * @param    $disabled      Properties to be ignored
60     */
61    public function importProperties($properties, $disabled) {
62        $this->importPropertiesInternal(ODTStyleStyle::getStyleProperties (), $properties, $disabled);
63        $this->importPropertiesInternal(self::$table_row_fields, $properties, $disabled);
64        $this->setProperty('style-family', $this->getFamily());
65    }
66
67    /**
68     * Check if a style is a common style.
69     *
70     * @return    bool    Is common style
71     */
72    public function mustBeCommonStyle() {
73        return false;
74    }
75
76    /**
77     * Get the style family of a style.
78     *
79     * @return    string    Style family
80     */
81    static public function getFamily() {
82        return 'table-row';
83    }
84
85    /**
86     * Set a property.
87     *
88     * @param    $property    The name of the property to set
89     * @param    $value       New value to set
90     */
91    public function setProperty($property, $value) {
92        $style_fields = ODTStyleStyle::getStyleProperties ();
93        if (array_key_exists ($property, $style_fields)) {
94            $this->setPropertyInternal
95                ($property, $style_fields [$property][0], $value, $style_fields [$property][1]);
96            return;
97        }
98        if (array_key_exists ($property, self::$table_row_fields)) {
99            $this->setPropertyInternal
100                ($property, self::$table_row_fields [$property][0], $value, self::$table_row_fields [$property][1]);
101            return;
102        }
103    }
104
105    /**
106     * Create new style by importing ODT style definition.
107     *
108     * @param     $xmlCode    Style definition in ODT XML format
109     * @return    ODTStyle    New specific style
110     */
111    static public function importODTStyle($xmlCode) {
112        $style = new ODTTableRowStyle();
113        $attrs = 0;
114
115        $open = XMLUtil::getElementOpenTag('style:style', $xmlCode);
116        if (!empty($open)) {
117            $attrs += $style->importODTStyleInternal(ODTStyleStyle::getStyleProperties (), $open);
118        } else {
119            $open = XMLUtil::getElementOpenTag('style:default-style', $xmlCode);
120            if (!empty($open)) {
121                $style->setDefault(true);
122                $attrs += $style->importODTStyleInternal(ODTStyleStyle::getStyleProperties (), $open);
123            }
124        }
125
126        $open = XMLUtil::getElementOpenTag('style:table-row-properties', $xmlCode);
127        if (!empty($open)) {
128            $attrs += $style->importODTStyleInternal(self::$table_row_fields, $open);
129        }
130
131        // If style has no meaningfull content then throw it away
132        if ( $attrs == 0 ) {
133            return NULL;
134        }
135
136        return $style;
137    }
138
139    /**
140     * Return an array listing the properties belonging to an ODT table row.
141     *
142     * @return    array    Properties
143     */
144    static public function getTableRowProperties () {
145        return self::$table_row_fields;
146    }
147
148    /**
149     * This function creates a table row style using the style as set in the assoziative array $properties.
150     * The parameters in the array should be named as the CSS property names e.g. 'color' or 'background-color'.
151     * Properties which shall not be used in the style can be disabled by setting the value in disabled_props
152     * to 1 e.g. $disabled_props ['color'] = 1 would block the usage of the color property.
153     *
154     * The currently supported properties are:
155     * height, background-color
156     *
157     * The function returns the name of the new style or NULL if all relevant properties are empty.
158     *
159     * @author    LarsDW223
160     * @param     array            $properties
161     * @param     array|null       $disabled_props
162     * @return    ODTTableRowStyle
163     */
164    public static function createTableRowStyle(array $properties, array $disabled_props = NULL){
165        // Create style name (if not given).
166        $style_name = $properties ['style-name'];
167        if ( empty($style_name) ) {
168            $style_name = self::getNewStylename ('TableRow');
169            $properties ['style-name'] = $style_name;
170        }
171
172        // Create empty table row style.
173        $object = new ODTTableRowStyle();
174        if ($object == NULL) {
175            return NULL;
176        }
177
178        // Import our properties
179        $object->importProperties($properties, $disabled_props);
180        return $object;
181    }
182}
183