PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/u1v3r/java-gwt
PHP | 217 lines | 124 code | 2 blank | 91 comment | 12 complexity | 69c7246bc758a03d26ffdafc15469e62 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. require_once 'SegmentIterator.php';
  3. class SegmentException extends Exception
  4. {}
  5. /**
  6. * Class for handling templating segments with odt files
  7. * You need PHP 5.2 at least
  8. * You need Zip Extension or PclZip library
  9. * Encoding : ISO-8859-1
  10. * Last commit by $Author: neveldo $
  11. * Date - $Date: 2009-06-17 12:12:59 +0200 (mer., 17 juin 2009) $
  12. * SVN Revision - $Rev: 44 $
  13. * Id : $Id: Segment.php 44 2009-06-17 10:12:59Z neveldo $
  14. *
  15. * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
  16. * @license http://www.gnu.org/copyleft/gpl.html GPL License
  17. * @version 1.3
  18. */
  19. class Segment implements IteratorAggregate, Countable
  20. {
  21. protected $xml;
  22. protected $xmlParsed = '';
  23. protected $name;
  24. protected $children = array();
  25. protected $vars = array();
  26. protected $images = array();
  27. protected $odf;
  28. protected $file;
  29. /**
  30. * Constructor
  31. *
  32. * @param string $name name of the segment to construct
  33. * @param string $xml XML tree of the segment
  34. */
  35. public function __construct($name, $xml, $odf)
  36. {
  37. $this->name = (string) $name;
  38. $this->xml = (string) $xml;
  39. $this->odf = $odf;
  40. $zipHandler = $this->odf->getConfig('ZIP_PROXY');
  41. $this->file = new $zipHandler();
  42. $this->_analyseChildren($this->xml);
  43. }
  44. /**
  45. * Returns the name of the segment
  46. *
  47. * @return string
  48. */
  49. public function getName()
  50. {
  51. return $this->name;
  52. }
  53. /**
  54. * Does the segment have children ?
  55. *
  56. * @return bool
  57. */
  58. public function hasChildren()
  59. {
  60. return $this->getIterator()->hasChildren();
  61. }
  62. /**
  63. * Countable interface
  64. *
  65. * @return int
  66. */
  67. public function count()
  68. {
  69. return count($this->children);
  70. }
  71. /**
  72. * IteratorAggregate interface
  73. *
  74. * @return Iterator
  75. */
  76. public function getIterator()
  77. {
  78. return new RecursiveIteratorIterator(new SegmentIterator($this->children), 1);
  79. }
  80. /**
  81. * Replace variables of the template in the XML code
  82. * All the children are also called
  83. *
  84. * @return string
  85. */
  86. public function merge()
  87. {
  88. $this->xmlParsed .= str_replace(array_keys($this->vars), array_values($this->vars), $this->xml);
  89. if ($this->hasChildren()) {
  90. foreach ($this->children as $child) {
  91. $this->xmlParsed = str_replace($child->xml, ($child->xmlParsed=="")?$child->merge():$child->xmlParsed, $this->xmlParsed);
  92. $child->xmlParsed = '';
  93. }
  94. }
  95. $reg = "/\[!--\sBEGIN\s$this->name\s--\](.*)\[!--\sEND\s$this->name\s--\]/sm";
  96. $this->xmlParsed = preg_replace($reg, '$1', $this->xmlParsed);
  97. /****************************
  98. * POZOR nefunguje pre WORD
  99. */
  100. if(count($this->images) > 0){
  101. $this->file->open($this->odf->getTmpfile());
  102. foreach ($this->images as $imageKey => $imageValue) {
  103. if ($this->file->getFromName('Pictures/' . $imageValue) === false) {
  104. $this->file->addFile($imageKey, 'Pictures/' . $imageValue);
  105. }
  106. }
  107. $this->file->close();
  108. }
  109. return $this->xmlParsed;
  110. }
  111. /**
  112. * Analyse the XML code in order to find children
  113. *
  114. * @param string $xml
  115. * @return Segment
  116. */
  117. protected function _analyseChildren($xml)
  118. {
  119. // $reg2 = "#\[!--\sBEGIN\s([\S]*)\s--\](?:<\/text:p>)?(.*)(?:<text:p\s.*>)?\[!--\sEND\s(\\1)\s--\]#sm";
  120. $reg2 = "#\[!--\sBEGIN\s([\S]*)\s--\](.*)\[!--\sEND\s(\\1)\s--\]#sm";
  121. preg_match_all($reg2, $xml, $matches);
  122. for ($i = 0, $size = count($matches[0]); $i < $size; $i++) {
  123. if ($matches[1][$i] != $this->name) {
  124. $this->children[$matches[1][$i]] = new self($matches[1][$i], $matches[0][$i], $this->odf);
  125. } else {
  126. $this->_analyseChildren($matches[2][$i]);
  127. }
  128. }
  129. return $this;
  130. }
  131. /**
  132. * Assign a template variable to replace
  133. *
  134. * @param string $key
  135. * @param string $value
  136. * @throws SegmentException
  137. * @return Segment
  138. */
  139. public function setVars($key, $value, $encode = true, $charset = 'UTF-8')
  140. {
  141. if (strpos($this->xml, $this->odf->getConfig('DELIMITER_LEFT') . $key . $this->odf->getConfig('DELIMITER_RIGHT')) === false) {
  142. throw new SegmentException("var $key not found in {$this->getName()}");
  143. }
  144. $value = $encode ? htmlspecialchars($value) : $value;
  145. $value = ($charset == 'ISO-8859') ? utf8_encode($value) : $value;
  146. $this->vars[$this->odf->getConfig('DELIMITER_LEFT') . $key . $this->odf->getConfig('DELIMITER_RIGHT')] = str_replace("\n", "<text:line-break/>", $value);
  147. return $this;
  148. }
  149. /**
  150. * Assign a template variable as a picture
  151. *
  152. * @param string $key name of the variable within the template
  153. * @param string $value path to the picture
  154. * @throws OdfException
  155. * @return Segment
  156. */
  157. public function setImage($key, $value)
  158. {
  159. $filename = strtok(strrchr($value, '/'), '/.');
  160. $file = substr(strrchr($value, '/'), 1);
  161. $size = @getimagesize($value);
  162. if ($size === false) {
  163. throw new DocumentException("Invalid image");
  164. }
  165. list ($width, $height) = $size;
  166. $width *= Doc::PIXEL_TO_CM;
  167. $height *= Doc::PIXEL_TO_CM;
  168. $xml = <<<IMG
  169. <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>
  170. IMG;
  171. $this->images[$value] = $file;
  172. $this->setVars($key, $xml, false);
  173. return $this;
  174. }
  175. /**
  176. * Shortcut to retrieve a child
  177. *
  178. * @param string $prop
  179. * @return Segment
  180. * @throws SegmentException
  181. */
  182. public function __get($prop)
  183. {
  184. if (array_key_exists($prop, $this->children)) {
  185. return $this->children[$prop];
  186. } else {
  187. throw new SegmentException('child ' . $prop . ' does not exist');
  188. }
  189. }
  190. /**
  191. * Proxy for setVars
  192. *
  193. * @param string $meth
  194. * @param array $args
  195. * @return Segment
  196. */
  197. public function __call($meth, $args)
  198. {
  199. try {
  200. return $this->setVars($meth, $args[0]);
  201. } catch (SegmentException $e) {
  202. throw new SegmentException("method $meth nor var $meth exist");
  203. }
  204. }
  205. /**
  206. * Returns the parsed XML
  207. *
  208. * @return string
  209. */
  210. public function getXmlParsed()
  211. {
  212. return $this->xmlParsed;
  213. }
  214. }
  215. ?>