[3526] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * eGroupWare |
---|
| 4 | * |
---|
| 5 | * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License |
---|
| 6 | * @link http://www.egroupware.org |
---|
| 7 | * @author Knut Moeller <k.moeller@metaways.de> |
---|
| 8 | * @copyright Knut Moeller <k.moeller@metaways.de> |
---|
| 9 | * @version $Id:$ |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | * |
---|
| 15 | * Spreadsheet main class |
---|
| 16 | * |
---|
| 17 | * |
---|
| 18 | */ |
---|
| 19 | class SpreadSheetDocument { |
---|
| 20 | |
---|
| 21 | // TODO generalize... |
---|
| 22 | // TODO internalize counts |
---|
| 23 | |
---|
| 24 | const TEMPDIR = '/tmp'; // system temppath TODO: better solution? |
---|
| 25 | const ZIP = 'zip'; // system zip command TODO: zip via php possible? no shellexec ? |
---|
| 26 | |
---|
| 27 | private $debug = 0; |
---|
| 28 | |
---|
| 29 | private $temppath; // temppath (to assemble zip file) |
---|
| 30 | private $tempzip; // temp zip file |
---|
| 31 | |
---|
| 32 | private $handle; // filehandle OO export file for importexport |
---|
| 33 | |
---|
| 34 | // xml nodes |
---|
| 35 | private $doc; |
---|
| 36 | private $styles; |
---|
| 37 | private $body; |
---|
| 38 | private $spreadsheet; // contains tables .... |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | // File handling, INIT |
---|
| 43 | |
---|
| 44 | function __construct($_handle) { |
---|
| 45 | $this->temppath = $this->mk_tempdir(self::TEMPDIR, 'cmsliteexport'); |
---|
| 46 | $this->tempzip = tempnam('/tmp','cmslite_export_outfile'); |
---|
| 47 | $this->handle = $_handle; |
---|
| 48 | $this->initSpreadsheet(); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | function __destruct() { |
---|
| 53 | @unlink($this->tempzip); |
---|
| 54 | @unlink($this->tempzip . '.zip'); |
---|
| 55 | $this->remove_directory($this->temppath.'/'); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * assemble the zipped OpenOffice Document |
---|
| 62 | * |
---|
| 63 | */ |
---|
| 64 | public function finalize() { |
---|
| 65 | |
---|
| 66 | @mkdir($this->temppath."/META-INF"); |
---|
| 67 | @mkdir($this->temppath."/Thumbnails"); |
---|
| 68 | @mkdir($this->temppath."/Configurations2"); |
---|
| 69 | @mkdir($this->temppath."/Configurations2/statusbar"); |
---|
| 70 | @mkdir($this->temppath."/Configurations2/accelerator"); |
---|
| 71 | @touch($this->temppath."/Configurations2/accelerator/current.xml"); |
---|
| 72 | @mkdir($this->temppath."/Configurations2/floater"); |
---|
| 73 | @mkdir($this->temppath."/Configurations2/popupmenu"); |
---|
| 74 | @mkdir($this->temppath."/Configurations2/progressbar"); |
---|
| 75 | @mkdir($this->temppath."/Configurations2/menubar"); |
---|
| 76 | @mkdir($this->temppath."/Configurations2/toolbar"); |
---|
| 77 | @mkdir($this->temppath."/Configurations2/images"); |
---|
| 78 | @mkdir($this->temppath."/Configurations2/images/Bitmaps"); |
---|
| 79 | |
---|
| 80 | // main content |
---|
| 81 | $this->write( $this->doc->saveXML(), "content.xml"); |
---|
| 82 | if ($this->debug > 0) error_log(print_r($this->doc->saveXML(),true)); |
---|
| 83 | |
---|
| 84 | $this->createMimetype(); |
---|
| 85 | $this->createManifest(); |
---|
| 86 | $this->createMeta(); |
---|
| 87 | |
---|
| 88 | // create zip |
---|
| 89 | shell_exec("cd $this->temppath; ".self::ZIP." -r $this->tempzip.zip * "); |
---|
| 90 | |
---|
| 91 | // copy to filehandle |
---|
| 92 | if ($this->handle) { |
---|
| 93 | $fh = fopen($this->tempzip.'.zip', "rb"); |
---|
| 94 | if($fh) { |
---|
| 95 | while (!feof($fh)) { |
---|
| 96 | fwrite($this->handle, fread($fh, 8192)); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | fclose($fh); |
---|
| 100 | } |
---|
| 101 | else error_log("output file error"); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | protected function createMeta() { |
---|
| 107 | |
---|
| 108 | //TODO generate |
---|
| 109 | $content = '<?xml version="1.0" encoding="UTF-8"?> |
---|
| 110 | <office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:ooo="http://openoffice.org/2004/office" office:version="1.0"> |
---|
| 111 | <office:meta><meta:generator>OpenOffice.org/2.0$Linux OpenOffice.org_project/680m5$Build-9011</meta:generator> |
---|
| 112 | <meta:creation-date>2005-01-18T12:05:29</meta:creation-date><dc:date>2006-10-25T10:35:55</dc:date><meta:print-date>2006-01-17T10:57:23</meta:print-date> |
---|
| 113 | <dc:language>de-DE</dc:language><meta:editing-cycles>218</meta:editing-cycles><meta:editing-duration>P1DT9H3M54S</meta:editing-duration> |
---|
| 114 | <meta:user-defined meta:name="Info 1"/><meta:user-defined meta:name="Info 2"/><meta:user-defined meta:name="Info 3"/> |
---|
| 115 | <meta:user-defined meta:name="Info 4"/><meta:document-statistic meta:table-count="12" meta:cell-count="969"/></office:meta></office:document-meta>'; |
---|
| 116 | $this->write($content,"meta.xml"); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | protected function createMimetype() { |
---|
| 120 | $content = 'application/vnd.oasis.opendocument.spreadsheet'; |
---|
| 121 | $this->write($content,"mimetype"); |
---|
| 122 | |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | protected function createManifest() { |
---|
| 126 | //TODO generate |
---|
| 127 | $content = '<?xml version="1.0" encoding="UTF-8"?> |
---|
| 128 | <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"> |
---|
| 129 | <manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.spreadsheet" manifest:full-path="/"/> |
---|
| 130 | <manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/statusbar/"/> |
---|
| 131 | <manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/accelerator/current.xml"/> |
---|
| 132 | <manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/accelerator/"/> |
---|
| 133 | <manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/floater/"/> |
---|
| 134 | <manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/popupmenu/"/> |
---|
| 135 | <manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/progressbar/"/> |
---|
| 136 | <manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/menubar/"/> |
---|
| 137 | <manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/toolbar/"/> |
---|
| 138 | <manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/images/Bitmaps/"/> |
---|
| 139 | <manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/images/"/> |
---|
| 140 | <manifest:file-entry manifest:media-type="application/vnd.sun.xml.ui.configuration" manifest:full-path="Configurations2/"/> |
---|
| 141 | <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/> |
---|
| 142 | <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="meta.xml"/> |
---|
| 143 | <manifest:file-entry manifest:media-type="" manifest:full-path="Thumbnails/"/> |
---|
| 144 | </manifest:manifest>'; |
---|
| 145 | |
---|
| 146 | $this->write($content,"META-INF/manifest.xml"); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | protected function initSpreadsheet() { |
---|
| 151 | |
---|
| 152 | $this->doc = new DOMDocument ('1.0', 'UTF-8'); |
---|
| 153 | $this->doc->formatOutput = true; |
---|
| 154 | |
---|
| 155 | $root = $this->doc->createElementNS('urn:oasis:names:tc:opendocument:xmlns:office:1.0', |
---|
| 156 | 'office:document-content'); |
---|
| 157 | $this->doc->appendChild($root); |
---|
| 158 | |
---|
| 159 | // define Namespaces.... |
---|
| 160 | |
---|
| 161 | $namespaces = array( |
---|
| 162 | 'style' => 'urn:oasis:names:tc:opendocument:xmlns:style:1.0', |
---|
| 163 | 'text' => 'urn:oasis:names:tc:opendocument:xmlns:text:1.0', |
---|
| 164 | 'table' => 'urn:oasis:names:tc:opendocument:xmlns:table:1.0', |
---|
| 165 | 'draw' => 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0', |
---|
| 166 | 'fo' => 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0', |
---|
| 167 | 'xlink' => 'http://www.w3.org/1999/xlink', |
---|
| 168 | 'dc' => 'http://purl.org/dc/elements/1.1/', |
---|
| 169 | 'meta' => 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0', |
---|
| 170 | 'number' => 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0', |
---|
| 171 | 'svg' => 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0', |
---|
| 172 | 'chart' => 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0', |
---|
| 173 | 'dr3d' => 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0', |
---|
| 174 | 'math' => 'http://www.w3.org/1998/Math/MathML', |
---|
| 175 | 'form' => 'urn:oasis:names:tc:opendocument:xmlns:form:1.0', |
---|
| 176 | 'script' => 'urn:oasis:names:tc:opendocument:xmlns:script:1.0', |
---|
| 177 | 'ooo' => 'http://openoffice.org/2004/office', |
---|
| 178 | 'ooow' => 'http://openoffice.org/2004/writer', |
---|
| 179 | 'oooc' => 'http://openoffice.org/2004/calc', |
---|
| 180 | 'dom' => 'http://www.w3.org/2001/xml-events', |
---|
| 181 | 'xforms' => 'http://www.w3.org/2002/xforms', |
---|
| 182 | 'xsd' => 'http://www.w3.org/2001/XMLSchema', |
---|
| 183 | 'xsi' => 'http://www.w3.org/2001/XMLSchema-instance'); |
---|
| 184 | |
---|
| 185 | foreach ($namespaces as $name => $url) { |
---|
| 186 | $root->setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:$name", $url); |
---|
| 187 | } |
---|
| 188 | $root->setAttribute('office:version', '1.0'); |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | // main parts |
---|
| 192 | |
---|
| 193 | $node = $root->appendChild( $this->doc->createElement('office:scripts') ); |
---|
| 194 | $node = $root->appendChild( $this->doc->createElement('office:font-face-decls') ); |
---|
| 195 | $this->styles = $root->appendChild( $this->doc->createElement('office:automatic-styles') ); |
---|
| 196 | $this->body = $root->appendChild( $this->doc->createElement('office:body') ); |
---|
| 197 | $this->spreadsheet = $this->body->appendChild( $this->doc->createElement('office:spreadsheet') ); |
---|
| 198 | |
---|
| 199 | // styles |
---|
| 200 | |
---|
| 201 | $this->initTableStyles(); |
---|
| 202 | $this->initColumnStyles(); |
---|
| 203 | $this->initRowStyles(); |
---|
| 204 | $this->initCellStyles(); |
---|
| 205 | $this->initNumberStyles(); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | /** |
---|
| 210 | * define the table styles |
---|
| 211 | */ |
---|
| 212 | protected function initTableStyles() { |
---|
| 213 | $style = $this->styles->appendChild($this->doc->createElement('style:style')); |
---|
| 214 | $style->setAttribute('style:name', 'ta1'); |
---|
| 215 | $style->setAttribute('style:family', 'table'); |
---|
| 216 | $style->setAttribute('style:master-page-name', 'Default'); |
---|
| 217 | $property = $style->appendChild($this->doc->createElement('style:table-properties')); |
---|
| 218 | $property->setAttribute('table:display', 'true'); |
---|
| 219 | $property->setAttribute('style:writing-mode', 'lr-tb'); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | /** |
---|
| 223 | * define the column styles |
---|
| 224 | */ |
---|
| 225 | protected function initColumnStyles() { |
---|
| 226 | $this->addColumnStyle('co1'); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | public function addColumnStyle($_name, $_width='2.267cm') { |
---|
| 230 | $style = $this->styles->appendChild($this->doc->createElement('style:style')); |
---|
| 231 | $style->setAttribute('style:name', $_name); |
---|
| 232 | $style->setAttribute('style:family', 'table-column'); |
---|
| 233 | $property = $style->appendChild($this->doc->createElement('style:table-column-properties')); |
---|
| 234 | $property->setAttribute('fo:break-before', 'auto'); |
---|
| 235 | $property->setAttribute('style:column-width', $_width); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * define the row styles |
---|
| 241 | */ |
---|
| 242 | protected function initRowStyles() { |
---|
| 243 | |
---|
| 244 | // standard |
---|
| 245 | $style = $this->styles->appendChild($this->doc->createElement('style:style')); |
---|
| 246 | $style->setAttribute('style:name', 'ro1'); |
---|
| 247 | $style->setAttribute('style:family', 'table-row'); |
---|
| 248 | $property = $style->appendChild($this->doc->createElement('style:table-row-properties')); |
---|
| 249 | $property->setAttribute('style:row-height', '0.48cm'); |
---|
| 250 | $property->setAttribute('fo:break-before', 'auto'); |
---|
| 251 | $property->setAttribute('style:use-optimal-row-height', 'true'); |
---|
| 252 | |
---|
| 253 | // double height |
---|
| 254 | $style = $this->styles->appendChild($this->doc->createElement('style:style')); |
---|
| 255 | $style->setAttribute('style:name', 'ro2'); |
---|
| 256 | $style->setAttribute('style:family', 'table-row'); |
---|
| 257 | $property = $style->appendChild($this->doc->createElement('style:table-row-properties')); |
---|
| 258 | $property->setAttribute('style:row-height', '1.053cm'); |
---|
| 259 | $property->setAttribute('fo:break-before', 'auto'); |
---|
| 260 | $property->setAttribute('style:use-optimal-row-height', 'false'); |
---|
| 261 | |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | |
---|
| 265 | /** |
---|
| 266 | * define the cell styles |
---|
| 267 | */ |
---|
| 268 | protected function initCellStyles() { |
---|
| 269 | |
---|
| 270 | // default |
---|
| 271 | $style = $this->styles->appendChild($this->doc->createElement('style:style')); |
---|
| 272 | $style->setAttribute('style:name', 'ce1'); |
---|
| 273 | $style->setAttribute('style:family', 'table-cell'); |
---|
| 274 | $style->setAttribute('style:parent-style-name', 'Default'); |
---|
| 275 | |
---|
| 276 | $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); |
---|
| 277 | $property->setAttribute('fo:border', 'none'); |
---|
| 278 | $this->appendTextAttributes($style); |
---|
| 279 | |
---|
| 280 | // default bold |
---|
| 281 | $style = $this->styles->appendChild($this->doc->createElement('style:style')); |
---|
| 282 | $style->setAttribute('style:name', 'ce2'); |
---|
| 283 | $style->setAttribute('style:family', 'table-cell'); |
---|
| 284 | $style->setAttribute('style:parent-style-name', 'Default'); |
---|
| 285 | |
---|
| 286 | $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); |
---|
| 287 | $property->setAttribute('fo:border', 'none'); |
---|
| 288 | $this->appendTextAttributes($style, true); |
---|
| 289 | |
---|
| 290 | // default bold + border |
---|
| 291 | $style = $this->styles->appendChild($this->doc->createElement('style:style')); |
---|
| 292 | $style->setAttribute('style:name', 'ce3'); |
---|
| 293 | $style->setAttribute('style:family', 'table-cell'); |
---|
| 294 | $style->setAttribute('style:parent-style-name', 'Default'); |
---|
| 295 | |
---|
| 296 | $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); |
---|
| 297 | $property->setAttribute('fo:border', '0.002cm solid #000000'); |
---|
| 298 | $this->appendTextAttributes($style, true); |
---|
| 299 | |
---|
| 300 | |
---|
| 301 | |
---|
| 302 | // N2 decimal |
---|
| 303 | $style = $this->styles->appendChild($this->doc->createElement('style:style')); |
---|
| 304 | $style->setAttribute('style:name', 'ce101'); |
---|
| 305 | $style->setAttribute('style:family', 'table-cell'); |
---|
| 306 | $style->setAttribute('style:parent-style-name', 'Default'); |
---|
| 307 | $style->setAttribute('style:data-style-name', 'N2'); |
---|
| 308 | |
---|
| 309 | $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); |
---|
| 310 | $property->setAttribute('fo:border', 'none'); |
---|
| 311 | $this->appendTextAttributes($style); |
---|
| 312 | |
---|
| 313 | |
---|
| 314 | // N0 decimal |
---|
| 315 | $style = $this->styles->appendChild($this->doc->createElement('style:style')); |
---|
| 316 | $style->setAttribute('style:name', 'ce100'); |
---|
| 317 | $style->setAttribute('style:family', 'table-cell'); |
---|
| 318 | $style->setAttribute('style:parent-style-name', 'Default'); |
---|
| 319 | $style->setAttribute('style:data-style-name', 'N0'); |
---|
| 320 | |
---|
| 321 | $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); |
---|
| 322 | $property->setAttribute('fo:border', 'none'); |
---|
| 323 | $this->appendTextAttributes($style); |
---|
| 324 | |
---|
| 325 | |
---|
| 326 | // N3 decimal Sum |
---|
| 327 | $style = $this->styles->appendChild($this->doc->createElement('style:style')); |
---|
| 328 | $style->setAttribute('style:name', 'ce102'); |
---|
| 329 | $style->setAttribute('style:family', 'table-cell'); |
---|
| 330 | $style->setAttribute('style:parent-style-name', 'Default'); |
---|
| 331 | $style->setAttribute('style:data-style-name', 'N2'); |
---|
| 332 | |
---|
| 333 | $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); |
---|
| 334 | $property->setAttribute('fo:border', 'none'); |
---|
| 335 | $this->appendTextAttributes($style, true); |
---|
| 336 | |
---|
| 337 | |
---|
| 338 | // N4 date |
---|
| 339 | $style = $this->styles->appendChild($this->doc->createElement('style:style')); |
---|
| 340 | $style->setAttribute('style:name', 'ce103'); |
---|
| 341 | $style->setAttribute('style:family', 'table-cell'); |
---|
| 342 | $style->setAttribute('style:parent-style-name', 'Default'); |
---|
| 343 | $style->setAttribute('style:data-style-name', 'N4'); |
---|
| 344 | |
---|
| 345 | $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); |
---|
| 346 | $property->setAttribute('fo:border', 'none'); |
---|
| 347 | $this->appendTextAttributes($style); |
---|
| 348 | |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | |
---|
| 352 | protected function appendTextAttributes($_style, $_bold=false) { |
---|
| 353 | $property = $_style->appendChild($this->doc->createElement('style:text-properties')); |
---|
| 354 | $property->setAttribute('style:use-window-font-color', 'true' ); |
---|
| 355 | $property->setAttribute('style:text-outline', 'false' ); |
---|
| 356 | $property->setAttribute('style:text-line-through-style', 'none' ); |
---|
| 357 | $property->setAttribute('style:font-name', 'Arial1' ); |
---|
| 358 | $property->setAttribute('style:text-underline-style', 'none' ); |
---|
| 359 | $property->setAttribute('style:font-size-asian', '10pt' ); |
---|
| 360 | $property->setAttribute('style:font-style-asian', 'normal' ); |
---|
| 361 | $property->setAttribute('style:font-weight-asian', 'bold' ); |
---|
| 362 | $property->setAttribute('style:font-size-complex', '10pt' ); |
---|
| 363 | $property->setAttribute('style:font-style-complex', 'normal' ); |
---|
| 364 | $property->setAttribute('style:font-weight-complex', 'bold' ); |
---|
| 365 | $property->setAttribute('fo:font-size', '10pt' ); |
---|
| 366 | $property->setAttribute('fo:font-style', 'normal' ); |
---|
| 367 | $property->setAttribute('fo:text-shadow', 'none' ); |
---|
| 368 | $property->setAttribute('fo:font-weight', ($_bold)?'bold':'normal' ); |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | |
---|
| 372 | /** |
---|
| 373 | * define the number styles |
---|
| 374 | */ |
---|
| 375 | protected function initNumberStyles() { |
---|
| 376 | |
---|
| 377 | // N2 : decimal precision 2 |
---|
| 378 | $style = $this->styles->appendChild($this->doc->createElement('number:number-style')); |
---|
| 379 | $style->setAttribute('style:name', 'N2'); |
---|
| 380 | $property = $style->appendChild($this->doc->createElement('number:number')); |
---|
| 381 | $property->setAttribute('number:decimal-places', '2'); |
---|
| 382 | $property->setAttribute('number:min-integer-digits', '1'); |
---|
| 383 | $property->setAttribute('number:grouping', 'false'); |
---|
| 384 | |
---|
| 385 | // N0 : plain integer N0 |
---|
| 386 | $style = $this->styles->appendChild($this->doc->createElement('number:number-style')); |
---|
| 387 | $style->setAttribute('style:name', 'N0'); |
---|
| 388 | $property = $style->appendChild($this->doc->createElement('number:number')); |
---|
| 389 | $property->setAttribute('number:decimal-places', '0'); |
---|
| 390 | $property->setAttribute('number:min-integer-digits', '0'); |
---|
| 391 | $property->setAttribute('number:grouping', 'false'); |
---|
| 392 | |
---|
| 393 | // N4 : date |
---|
| 394 | $style = $this->styles->appendChild($this->doc->createElement('number:date-style')); |
---|
| 395 | $style->setAttribute('style:name', 'N4'); |
---|
| 396 | $style->setAttribute('number:automatic-order', 'true'); |
---|
| 397 | |
---|
| 398 | $property = $style->appendChild($this->doc->createElement('number:day')); |
---|
| 399 | $property->setAttribute('number:style', 'long'); |
---|
| 400 | $property = $style->appendChild($this->doc->createElement('number:text', '.')); |
---|
| 401 | |
---|
| 402 | $property = $style->appendChild($this->doc->createElement('number:month')); |
---|
| 403 | $property->setAttribute('number:style', 'long'); |
---|
| 404 | $property = $style->appendChild($this->doc->createElement('number:text', '.')); |
---|
| 405 | |
---|
| 406 | $property = $style->appendChild($this->doc->createElement('number:year')); |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | |
---|
| 410 | |
---|
| 411 | |
---|
| 412 | /* GETTER,SETTER FUNCTIONS */ |
---|
| 413 | |
---|
| 414 | public function getDoc() { |
---|
| 415 | return $this->doc; |
---|
| 416 | } |
---|
| 417 | |
---|
| 418 | public function get() { |
---|
| 419 | return $this->spreadsheet; |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | public function toString() { |
---|
| 423 | return (is_object($this->doc)) ? $this->doc->saveXML() : 'empty' ; |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | |
---|
| 427 | |
---|
| 428 | /* HELPER FUNCTIONS */ |
---|
| 429 | |
---|
| 430 | private function mk_tempdir($dir, $prefix='', $mode=0700) { |
---|
| 431 | if (substr($dir, -1) != '/') $dir .= '/'; |
---|
| 432 | |
---|
| 433 | do { |
---|
| 434 | $path = $dir.$prefix.mt_rand(0, 9999999); |
---|
| 435 | } while (!mkdir($path, $mode)); |
---|
| 436 | |
---|
| 437 | return $path; |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | private function write($content, $file) { |
---|
| 441 | $fh = fopen ($this->temppath.'/'.$file, "w") or die("can't open tempfile ". $this->temppath.'/'.$file); |
---|
| 442 | fwrite($fh, $content) or die ("cannot write to tempfile ". $this->temppath.'/'.$file); |
---|
| 443 | fclose($fh); |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | private function remove_directory($dir) { |
---|
| 447 | $dir_contents = scandir($dir); |
---|
| 448 | foreach ($dir_contents as $item) { |
---|
| 449 | if (is_dir($dir.$item) && $item != '.' && $item != '..') { |
---|
| 450 | $this->remove_directory($dir.$item.'/'); |
---|
| 451 | } |
---|
| 452 | elseif (file_exists($dir.$item) && $item != '.' && $item != '..') { |
---|
| 453 | unlink($dir.$item); |
---|
| 454 | } |
---|
| 455 | } |
---|
| 456 | rmdir($dir); |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | } // class |
---|
| 460 | |
---|
| 461 | |
---|
| 462 | |
---|
| 463 | |
---|
| 464 | /** |
---|
| 465 | * |
---|
| 466 | * |
---|
| 467 | * |
---|
| 468 | */ |
---|
| 469 | class SpreadSheetTable { |
---|
| 470 | |
---|
| 471 | private $doc; |
---|
| 472 | private $table; |
---|
| 473 | private $columns; |
---|
| 474 | |
---|
| 475 | |
---|
| 476 | function __construct($_spreadsheet, $_name) { |
---|
| 477 | $this->doc = $_spreadsheet->getDoc(); |
---|
| 478 | $this->spreadsheet = $_spreadsheet->get(); |
---|
| 479 | |
---|
| 480 | if (is_object($this->doc)) { |
---|
| 481 | $this->table = $this->spreadsheet->appendChild( $this->doc->createElement('table:table') ); |
---|
| 482 | $this->table->setAttribute('table:name', $_name); |
---|
| 483 | $this->table->setAttribute('table:style-name', 'ta1'); |
---|
| 484 | $this->table->setAttribute('table:print','false'); |
---|
| 485 | } |
---|
| 486 | else return false; |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | /** |
---|
| 490 | * |
---|
| 491 | */ |
---|
| 492 | public function get() { |
---|
| 493 | return $this->table; |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | |
---|
| 497 | /** |
---|
| 498 | * add column |
---|
| 499 | * @return colElement |
---|
| 500 | */ |
---|
| 501 | public function addColumn($_name='co1', $_cellstylename='ce1', $_repeat=1) { |
---|
| 502 | $col = $this->table->appendChild( $this->doc->createElement('table:table-column') ); |
---|
| 503 | $col->setAttribute('table:style-name', $_name); |
---|
| 504 | $col->setAttribute('table:number-columns-repeated',$_repeat); |
---|
| 505 | $col->setAttribute('table:default-cell-style-name', $_cellstylename); |
---|
| 506 | |
---|
| 507 | // table:number-columns-repeated="2" |
---|
| 508 | return $col; |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | |
---|
| 512 | /** |
---|
| 513 | * add row |
---|
| 514 | * @return rowElement |
---|
| 515 | * @param type: normal, double (height) |
---|
| 516 | */ |
---|
| 517 | public function addRow($_type='normal') { |
---|
| 518 | $row = $this->table->appendChild( $this->doc->createElement('table:table-row') ); |
---|
| 519 | $row->setAttribute('table:style-name', ($_type=='normal') ? 'ro1' : 'ro2'); |
---|
| 520 | return $row; |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | |
---|
| 524 | /** |
---|
| 525 | * requires row element, adds one cell to it |
---|
| 526 | * @return cellElement |
---|
| 527 | */ |
---|
| 528 | public function addCell( $row, $type, $value, $attributes=array() ) { |
---|
| 529 | $cell = $row->appendChild( $this->doc->createElement('table:table-cell') ); |
---|
| 530 | cellType($this->doc, $cell, $type, $value, $attributes); |
---|
| 531 | return $cell; |
---|
| 532 | } |
---|
| 533 | |
---|
| 534 | |
---|
| 535 | /** |
---|
| 536 | * requires row element, adds multiple cells |
---|
| 537 | */ |
---|
| 538 | public function addCells( $row, $type, $values, $attributes=array() ) { |
---|
| 539 | if (!is_array($values)) { |
---|
| 540 | echo "array required"; |
---|
| 541 | return false; |
---|
| 542 | } |
---|
| 543 | foreach ($values as $value) { |
---|
| 544 | $this->addCell($row, $type, $value, $attributes); |
---|
| 545 | } |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | } // class |
---|
| 549 | |
---|
| 550 | |
---|
| 551 | |
---|
| 552 | |
---|
| 553 | /** |
---|
| 554 | * |
---|
| 555 | * type factory |
---|
| 556 | * |
---|
| 557 | */ |
---|
| 558 | function cellType($_doc, $_cell, $_type, $_value, $_attributes=array()) { |
---|
| 559 | |
---|
| 560 | //TODO think about attribute dispatching <-> xml cell definition |
---|
| 561 | |
---|
| 562 | switch ($_type) { |
---|
| 563 | case 'float' : return new SpreadSheetCellTypeFloat ($_doc, $_cell, $_value, $_attributes); |
---|
| 564 | case 'int' : return new SpreadSheetCellTypeInt ($_doc, $_cell, $_value, $_attributes); |
---|
| 565 | case 'formula' : return new SpreadSheetCellTypeFormula($_doc, $_cell, $_value, $_attributes); |
---|
| 566 | case 'date' : return new SpreadSheetCellTypeDate ($_doc, $_cell, $_value, $_attributes); |
---|
| 567 | default : return new SpreadSheetCellTypeString ($_doc, $_cell, $_value, $_attributes); |
---|
| 568 | } |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | |
---|
| 572 | /** |
---|
| 573 | * string type cell |
---|
| 574 | */ |
---|
| 575 | class SpreadSheetCellTypeString { |
---|
| 576 | |
---|
| 577 | function __construct($doc, $cell, $value, $_attributes) { |
---|
| 578 | $cell->setAttribute('office:value-type', 'string'); |
---|
| 579 | |
---|
| 580 | $name = 'ce1'; |
---|
| 581 | if (array_search('bold', $_attributes) !== FALSE ) { |
---|
| 582 | $name = 'ce2'; |
---|
| 583 | if (array_search('border', $_attributes) !== FALSE ) { |
---|
| 584 | $name = 'ce3'; |
---|
| 585 | } |
---|
| 586 | } |
---|
| 587 | $cell->setAttribute('table:style-name', $name); |
---|
| 588 | |
---|
| 589 | $cell->appendChild( $doc->createElement('text:p', $value) ); |
---|
| 590 | } |
---|
| 591 | } |
---|
| 592 | |
---|
| 593 | |
---|
| 594 | /** |
---|
| 595 | * float type cell |
---|
| 596 | */ |
---|
| 597 | class SpreadSheetCellTypeFloat { |
---|
| 598 | function __construct($doc, $cell, $value, $_attributes) { |
---|
| 599 | $cell->setAttribute('office:value-type', 'float'); |
---|
| 600 | $cell->setAttribute('office:value', number_format($value, 2, '.', '') ); |
---|
| 601 | $cell->setAttribute('table:style-name', 'ce101'); |
---|
| 602 | $cell->appendChild( $doc->createElement('text:p', number_format($value, 2, ',', '')) ); |
---|
| 603 | } |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | |
---|
| 607 | /** |
---|
| 608 | * int type cell |
---|
| 609 | */ |
---|
| 610 | class SpreadSheetCellTypeInt { |
---|
| 611 | function __construct($doc, $cell, $value, $_attributes) { |
---|
| 612 | $cell->setAttribute('office:value-type', 'float'); |
---|
| 613 | $cell->setAttribute('office:value', number_format($value, 0, ',', '')); |
---|
| 614 | $cell->setAttribute('table:style-name', 'ce100'); |
---|
| 615 | $cell->appendChild( $doc->createElement('text:p', number_format($value, 0, ',', '')) ); |
---|
| 616 | } |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | |
---|
| 620 | /** |
---|
| 621 | * date type cell |
---|
| 622 | */ |
---|
| 623 | class SpreadSheetCellTypeDate { |
---|
| 624 | function __construct($doc, $cell, $value, $_attributes) { |
---|
| 625 | if (($timestamp = strtotime($value)) === false) { |
---|
| 626 | echo "Wrong date ($value)"; |
---|
| 627 | } |
---|
| 628 | else { |
---|
| 629 | $cell->setAttribute('office:value-type', 'date'); |
---|
| 630 | $cell->setAttribute('office:date-value', strftime("%Y-%m-%d", $timestamp) ); |
---|
| 631 | $cell->setAttribute('table:style-name', 'ce103'); |
---|
| 632 | $cell->appendChild( $doc->createElement('text:p', strftime("%d.%m.%Y", $timestamp) ) ); |
---|
| 633 | } |
---|
| 634 | } |
---|
| 635 | } |
---|
| 636 | |
---|
| 637 | |
---|
| 638 | /** |
---|
| 639 | * formula type cell |
---|
| 640 | */ |
---|
| 641 | class SpreadSheetCellTypeFormula { |
---|
| 642 | |
---|
| 643 | function __construct($doc, $cell, $value, $_attributes) { |
---|
| 644 | $cell->setAttribute('table:formula', 'oooc:='.$value); |
---|
| 645 | $cell->setAttribute('office:value-type', 'float'); |
---|
| 646 | $cell->setAttribute('office:value', '0'); |
---|
| 647 | $cell->setAttribute('table:style-name', 'ce102'); |
---|
| 648 | $cell->appendChild( $doc->createElement('text:p', '0,00' )); |
---|
| 649 | } |
---|
| 650 | } |
---|
| 651 | |
---|
| 652 | |
---|
| 653 | ?> |
---|