| 23 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
require_once 'class.odt.php';
|
|
|
4 |
|
|
|
5 |
/**
|
|
|
6 |
* A Class representing a table.
|
|
|
7 |
*
|
|
|
8 |
* @author Issam RACHDI
|
|
|
9 |
*/
|
|
|
10 |
class Table {
|
|
|
11 |
|
|
|
12 |
private $contentDocument;
|
|
|
13 |
private $tableElement;
|
|
|
14 |
private $columns;
|
|
|
15 |
private $columnsStyles;
|
|
|
16 |
private $rows;
|
|
|
17 |
private $rowsStyles;
|
|
|
18 |
private $cells;
|
|
|
19 |
private $cellsStyles;
|
|
|
20 |
private $tableName;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
*
|
|
|
24 |
* @param DOMDocument $contentDoc The DOMDocument instance of content.xml
|
|
|
25 |
* @param TableStyle $tableStyle A TableStyle object representing table style properties
|
|
|
26 |
*/
|
|
|
27 |
public function __construct($tableName, $tableStyle = null) {
|
|
|
28 |
|
|
|
29 |
$this->contentDocument = ODT::getInstance()->getDocumentContent();
|
|
|
30 |
$this->tableName = $tableName;
|
|
|
31 |
$this->tableElement = $this->contentDocument->createElement('table:table');
|
|
|
32 |
$this->tableElement->setAttribute('table:name', $tableName);
|
|
|
33 |
if ($tableStyle != null) {
|
|
|
34 |
if ($tableStyle->getStyleName() != $tableName) {
|
|
|
35 |
$tableStyle->setStyleName($tableName);
|
|
|
36 |
}
|
|
|
37 |
$this->tableElement->setAttribute('table:style-name', $tableStyle->getStyleName());
|
|
|
38 |
}
|
|
|
39 |
$this->columns = array();
|
|
|
40 |
$this->columnsStyles = array();
|
|
|
41 |
$this->rows = array();
|
|
|
42 |
$this->rowsStyles = array();
|
|
|
43 |
$this->cells = array();
|
|
|
44 |
$this->cellsStyles = array();
|
|
|
45 |
|
|
|
46 |
$this->contentDocument->getElementsByTagName('office:text')->item(0)->appendChild($this->tableElement);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Create the number of columns specified. If the DOMDocument representing the styles is passed as the
|
|
|
51 |
* second argument, a ColumnStyle is created for each column, and can be retrieved by
|
|
|
52 |
* the method {@link #getColumnStyle getColumnStyle()}
|
|
|
53 |
*
|
|
|
54 |
* @param integer $nbCols The number of columns
|
|
|
55 |
* @param DOMDocument $styleDoc
|
|
|
56 |
*/
|
|
|
57 |
public function createColumns($nbCols, $createStyles = true) {
|
|
|
58 |
$letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
|
|
|
59 |
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
|
|
|
60 |
$columnsElem = $this->contentDocument->createElement('table:table-columns');
|
|
|
61 |
$letterIndex = 0;
|
|
|
62 |
for ($i = 0; $i < $nbCols; $i++) {
|
|
|
63 |
$column = $this->contentDocument->createElement('table:table-column');
|
|
|
64 |
$styleName = $this->tableName . '.' . ($i < 26 ? $letters[$i] : $letters[0] . $letters[$i - 26]);
|
|
|
65 |
// $styleName = 'Col'.($i+1);
|
|
|
66 |
$column->setAttribute('table:style-name', $styleName);
|
|
|
67 |
$columnsElem->appendChild($column);
|
|
|
68 |
$this->columns[] = $column;
|
|
|
69 |
if ($createStyles) {
|
|
|
70 |
$this->columnsStyles[] = new ColumnStyle($styleName);
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
$this->tableElement->appendChild($columnsElem);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Creates a header for each column using the elments of the array passed.
|
|
|
78 |
*
|
|
|
79 |
* @param array $headers
|
|
|
80 |
*/
|
|
|
81 |
public function addHeader($headers) {
|
|
|
82 |
$letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
|
|
|
83 |
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
|
|
|
84 |
$rowHeader = $this->contentDocument->createElement('table:table-row');
|
|
|
85 |
$j = 0;
|
|
|
86 |
foreach ($headers as $header) {
|
|
|
87 |
$cell = $this->contentDocument->createElement('table:table-cell');
|
|
|
88 |
// $styleName = $this->tableName.'.'.($j < 26 ? $letters[$j] : $letters[0].$letters[$j - 26]).'1';
|
|
|
89 |
// $cell->setAttribute('table:style-name', $styleName);
|
|
|
90 |
$p = $this->contentDocument->createElement('text:p', $header);
|
|
|
91 |
$cell->appendChild($p);
|
|
|
92 |
$rowHeader->appendChild($cell);
|
|
|
93 |
$j++;
|
|
|
94 |
}
|
|
|
95 |
$headersElement = $this->contentDocument->createElement('table:table-header-rows');
|
|
|
96 |
$headersElement->appendChild($rowHeader);
|
|
|
97 |
$this->tableElement->appendChild($headersElement);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Add rows to the table.
|
|
|
102 |
*
|
|
|
103 |
* @param array $rows A two dimension array, representing the rows, and the cells inside each row
|
|
|
104 |
* @param DOMDocument $styleDoc The DOMDocument representing the styles
|
|
|
105 |
*/
|
|
|
106 |
public function addRows($rows, $createStyles = true) {
|
|
|
107 |
$letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
|
|
|
108 |
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
|
|
|
109 |
$rowsElement = $this->contentDocument->createElement('table:table-rows');
|
|
|
110 |
$i = 0;
|
|
|
111 |
foreach ($rows as $row) {
|
|
|
112 |
$j = 0;
|
|
|
113 |
$rowElement = $this->contentDocument->createElement('table:table-row');
|
|
|
114 |
$rowElement->setAttribute('office:value-type', 'string');
|
|
|
115 |
$this->rows[] = $rowElement;
|
|
|
116 |
foreach ($row as $cell) {
|
|
|
117 |
$cellElement = $this->contentDocument->createElement('table:table-cell');
|
|
|
118 |
if ($createStyles) {
|
|
|
119 |
$styleName = $this->tableName . '.' . ($j < 26 ? $letters[$j] : $letters[0] . $letters[$j - 26]) . ($i + 1);
|
|
|
120 |
$this->cellsStyles[$i][$j] = new CellStyle($styleName);
|
|
|
121 |
$cellElement->setAttribute('office:value-type', 'string');
|
|
|
122 |
$cellElement->setAttribute('table:style-name', $styleName);
|
|
|
123 |
}
|
|
|
124 |
if ($cell instanceof Paragraph) {
|
|
|
125 |
$p = $cell->getDOMElement();
|
|
|
126 |
} else {
|
|
|
127 |
$p = $this->contentDocument->createElement('text:p', $cell);
|
|
|
128 |
}
|
|
|
129 |
$cellElement->appendChild($p);
|
|
|
130 |
$this->cells[$i][$j] = $cellElement;
|
|
|
131 |
$rowElement->appendChild($cellElement);
|
|
|
132 |
$j++;
|
|
|
133 |
}
|
|
|
134 |
$rowsElement->appendChild($rowElement);
|
|
|
135 |
$i++;
|
|
|
136 |
}
|
|
|
137 |
$this->tableElement->appendChild($rowsElement);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Affect a RowStyle to the row at the position $rowIndex
|
|
|
142 |
*
|
|
|
143 |
* @param integer $rowIndex
|
|
|
144 |
* @param RowStyle $rowStyle
|
|
|
145 |
*/
|
|
|
146 |
public function setRowStyle($rowIndex, $rowStyle) {
|
|
|
147 |
$this->rows[$rowIndex]->setAttribute('table:style-name', $rowStyle->getStyleName());
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Affect a CellStyle to the cell at the position ($colIndex, $rowIndex)
|
|
|
152 |
* @param integer $colIndex
|
|
|
153 |
* @param integer $rowIndex
|
|
|
154 |
* @param CellStyle $cellStyle
|
|
|
155 |
*/
|
|
|
156 |
public function setCellStyle($colIndex, $rowIndex, $cellStyle) {
|
|
|
157 |
$this->cells[$rowIndex][$colIndex]->setAttribute('table:style-name', $cellStyle->getStyleName());
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Return the ColumnStyle of the given index
|
|
|
162 |
*
|
|
|
163 |
* @param integer $index
|
|
|
164 |
* @return ColumnStyle
|
|
|
165 |
*/
|
|
|
166 |
public function getColumnStyle($index) {
|
|
|
167 |
return $this->columnsStyles[$index];
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
/**
|
|
|
171 |
* Return the CellStyle of the given position
|
|
|
172 |
*
|
|
|
173 |
* @param integer $col
|
|
|
174 |
* @param integer $row
|
|
|
175 |
* @return CellStyle
|
|
|
176 |
*/
|
|
|
177 |
public function getCellStyle($col, $row) {
|
|
|
178 |
return $this->cellsStyles[$row][$col];
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
function setStyle($tableStyle) {
|
|
|
182 |
if ($tableStyle->getStyleName() != $this->tableName) {
|
|
|
183 |
$tableStyle->setStyleName($this->tableName);
|
|
|
184 |
}
|
|
|
185 |
$this->tableElement->setAttribute('table:style-name', $tableStyle->getStyleName());
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
function getTableName() {
|
|
|
189 |
return $this->tableName;
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
?>
|