PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/war/WEB-INF/lib/Export/library/Odf.php

https://github.com/u1v3r/java-gwt
PHP | 105 lines | 63 code | 8 blank | 34 comment | 8 complexity | 399223aaa5c73dcae09204357c646f19 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. require_once 'DocumentAbstract.php';
  3. /**
  4. * Templating class for odt file
  5. * You need PHP 5.2 at least
  6. * You need Zip Extension or PclZip library
  7. * Encoding : ISO-8859-1
  8. * Last commit by $Author: neveldo & Radovan Dvorsky $
  9. * Date - $Date: 2009-06-17 11:11:57 +0200 (mer., 17 juin 2009) $
  10. * SVN Revision - $Rev: 42 $
  11. * Id : $Id: Odf.php 42 2009-06-17 09:11:57Z neveldo $
  12. *
  13. * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
  14. * @license http://www.gnu.org/copyleft/gpl.html GPL License
  15. * @version 1.3
  16. */
  17. class Odf extends DocumentAbstract
  18. {
  19. /**
  20. * Class constructor
  21. *
  22. * @param string $filename the name of the odt file
  23. * @throws OdfException
  24. */
  25. public function __construct($filename, $config = array())
  26. {
  27. if (! is_array($config)) {
  28. throw new DocumentException('Configuration data must be provided as array');
  29. }
  30. foreach ($config as $configKey => $configValue) {
  31. if (array_key_exists($configKey, $this->config)) {
  32. $this->config[$configKey] = $configValue;
  33. }
  34. }
  35. if (! class_exists($this->config['ZIP_PROXY'])) {
  36. throw new DocumentException($this->config['ZIP_PROXY'] . ' class not found - check your php settings');
  37. }
  38. $zipHandler = $this->config['ZIP_PROXY'];
  39. $this->file = new $zipHandler();
  40. if ($this->file->open($filename) !== true) {
  41. throw new DocumentException("Error while Opening the file '$filename' - Check your odt file");
  42. }
  43. if (($this->contentXml = $this->file->getFromName('content.xml')) === false) {
  44. throw new DocumentException("Nothing to parse - check that the content.xml file is correctly formed");
  45. }
  46. $this->file->close();
  47. $tmp = $this->config['PATH_TO_TMP'] . md5(uniqid());
  48. copy($filename, $tmp);
  49. $this->tmpfile = $tmp;
  50. $this->_moveRowSegments();
  51. }
  52. /**
  53. * Assign a template variable as a picture
  54. *
  55. * @param string $key name of the variable within the template
  56. * @param string $value path to the picture
  57. * @throws OdfException
  58. * @return Odf
  59. */
  60. public function setImage($key, $value)
  61. {
  62. $filename = strtok(strrchr($value, '/'), '/.');
  63. $file = substr(strrchr($value, '/'), 1);
  64. $size = @getimagesize($value);
  65. if ($size === false) {
  66. throw new DocumentException("Invalid image");
  67. }
  68. list ($width, $height) = $size;
  69. $width *= self::PIXEL_TO_CM;
  70. $height *= self::PIXEL_TO_CM;
  71. $xml = <<<IMG
  72. <draw:frame draw:style-name="fr1" draw:name="$filename" text:anchor-type="char" svg:width="{$width}cm" svg:height="{$height}cm" draw:z-index="3"><draw:image xlink:href="Pictures/$file" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/></draw:frame>
  73. IMG;
  74. $this->images[$value] = $file;
  75. $this->setVars($key, $xml, false);
  76. return $this;
  77. }
  78. /**
  79. * Internal save
  80. *
  81. * @throws OdfException
  82. * @return void
  83. */
  84. protected function _save()
  85. {
  86. $this->file->open($this->tmpfile);
  87. $this->_parse();
  88. if (! $this->file->addFromString('content.xml', $this->contentXml)) {
  89. throw new DocumentException('Error during file export');
  90. }
  91. foreach ($this->images as $imageKey => $imageValue) {
  92. $this->file->addFile($imageKey, 'Pictures/' . $imageValue);
  93. }
  94. $this->file->close(); // seems to bug on windows CLI sometimes
  95. }
  96. }
  97. ?>