1<?php
2
3require_once DOKU_PLUGIN.'odt/ODT/elements/ODTStateElement.php';
4
5/**
6 * ODTElementTableCell:
7 * Class for handling the table cell element.
8 *
9 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
10 * @author  LarsDW223
11 */
12class ODTElementTableCell extends ODTStateElement
13{
14    protected $colspan;
15    protected $rowspan;
16    protected $value_type = 'string';
17
18    /**
19     * Constructor.
20     */
21    public function __construct($style_name=NULL, $colspan = 1, $rowspan = 1) {
22        parent::__construct();
23        $this->setClass ('table-cell');
24        if (isset($style_name)) {
25            $this->setStyleName ($style_name);
26        }
27        $this->setColumnSpan($colspan);
28        $this->setRowSpan($rowspan);
29    }
30
31    /**
32     * Return the elements name.
33     *
34     * @return string The ODT XML element name.
35     */
36    public function getElementName () {
37        return ('table:table-cell');
38    }
39
40    /**
41     * Return string with encoded opening tag.
42     *
43     * @return string The ODT XML code to open this element.
44     */
45    public function getOpeningTag () {
46        $colspan = $this->getColumnSpan();
47        $rowspan = $this->getRowSpan();
48
49        $encoded =  '<table:table-cell office:value-type="'.$this->getValueType().'"';
50        $encoded .= ' table:style-name="'.$this->getStyleName().'"';
51        if ( $colspan > 1 ) {
52            $encoded .= ' table:number-columns-spanned="'.$colspan.'"';
53        }
54        if ($rowspan > 1) {
55            $encoded .= ' table:number-rows-spanned="'.$rowspan.'"';
56        }
57        $encoded .= '>';
58        return $encoded;
59    }
60
61    /**
62     * Return string with encoded closing tag.
63     *
64     * @return string The ODT XML code to close this element.
65     */
66    public function getClosingTag () {
67        $content = '</table:table-cell>';
68        $colspan = $this->getColumnSpan();
69        for ($i = 1 ; $i < $colspan ; $i++) {
70            $content .= '<table:covered-table-cell/>';
71        }
72        return $content;
73    }
74
75    /**
76     * Are we in a paragraph or not?
77     * As a table cell we are not.
78     *
79     * @return boolean
80     */
81    public function getInParagraph() {
82        return false;
83    }
84
85    /**
86     * Determine and set the parent for this element.
87     * As a table cell our parent is the table element.
88     *
89     * @param ODTStateElement $previous
90     */
91    public function determineParent(ODTStateElement $previous) {
92        while (isset($previous)) {
93            if ($previous->getClass() == 'table') {
94                break;
95            }
96            $previous = $previous->getParent();
97        }
98        $this->setParent($previous);
99
100        $curr_column = $previous->getTableCurrentColumn();
101        $curr_column++;
102        $previous->setTableCurrentColumn($curr_column);
103
104        // Eventually increase max columns if out range
105        $max_columns = $previous->getTableMaxColumns();
106        if ( $curr_column > $max_columns ) {
107            $previous->setTableMaxColumns($max_columns + 1);
108        }
109    }
110
111    /**
112     * Return the table to which this column belongs.
113     *
114     * @return ODTStateElement
115     */
116    public function getTable () {
117        return $this->getParent();
118    }
119
120    /**
121     * Set the number of columns spanned by this cell.
122     *
123     * @param integer $value
124     */
125    public function setColumnSpan($value) {
126        $this->colspan = $value;
127    }
128
129    /**
130     * Get the number of columns spanned by this cell.
131     *
132     * @return integer
133     */
134    public function getColumnSpan() {
135        return $this->colspan;
136    }
137
138    /**
139     * Set the number of rows spanned by this cell.
140     *
141     * @param integer $value
142     */
143    public function setRowSpan($value) {
144        $this->rowspan = $value;
145    }
146
147    /**
148     * Get the number of rows spanned by this cell.
149     *
150     * @return integer
151     */
152    public function getRowSpan() {
153        return $this->rowspan;
154    }
155
156    /**
157     * Set the office value type for this cell.
158     *
159     * @param string $value
160     */
161    public function setValueType($value) {
162        $this->value_type = $value;
163    }
164
165    /**
166     * Get the office value type for this cell.
167     *
168     * @return string
169     */
170    public function getValueType() {
171        return $this->value_type;
172    }
173}
174