Segment.php.svn-base 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. require '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$
  11. * Date - $Date$
  12. * SVN Revision - $Rev$
  13. * Id : $Id$
  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. $this->file->open($this->odf->getTmpfile());
  98. foreach ($this->images as $imageKey => $imageValue) {
  99. if ($this->file->getFromName('Pictures/' . $imageValue) === false) {
  100. $this->file->addFile($imageKey, 'Pictures/' . $imageValue);
  101. }
  102. }
  103. $this->file->close();
  104. return $this->xmlParsed;
  105. }
  106. /**
  107. * Analyse the XML code in order to find children
  108. *
  109. * @param string $xml
  110. * @return Segment
  111. */
  112. protected function _analyseChildren($xml)
  113. {
  114. // $reg2 = "#\[!--\sBEGIN\s([\S]*)\s--\](?:<\/text:p>)?(.*)(?:<text:p\s.*>)?\[!--\sEND\s(\\1)\s--\]#sm";
  115. $reg2 = "#\[!--\sBEGIN\s([\S]*)\s--\](.*)\[!--\sEND\s(\\1)\s--\]#sm";
  116. preg_match_all($reg2, $xml, $matches);
  117. for ($i = 0, $size = count($matches[0]); $i < $size; $i++) {
  118. if ($matches[1][$i] != $this->name) {
  119. $this->children[$matches[1][$i]] = new self($matches[1][$i], $matches[0][$i], $this->odf);
  120. } else {
  121. $this->_analyseChildren($matches[2][$i]);
  122. }
  123. }
  124. return $this;
  125. }
  126. /**
  127. * Assign a template variable to replace
  128. *
  129. * @param string $key
  130. * @param string $value
  131. * @throws SegmentException
  132. * @return Segment
  133. */
  134. public function setVars($key, $value, $encode = true, $charset = 'ISO-8859')
  135. {
  136. if (strpos($this->xml, $this->odf->getConfig('DELIMITER_LEFT') . $key . $this->odf->getConfig('DELIMITER_RIGHT')) === false) {
  137. throw new SegmentException("var $key not found in {$this->getName()}");
  138. }
  139. $value = $encode ? htmlspecialchars($value) : $value;
  140. $value = ($charset == 'ISO-8859') ? utf8_encode($value) : $value;
  141. $this->vars[$this->odf->getConfig('DELIMITER_LEFT') . $key . $this->odf->getConfig('DELIMITER_RIGHT')] = str_replace("\n", "<text:line-break/>", $value);
  142. return $this;
  143. }
  144. /**
  145. * Assign a template variable as a picture
  146. *
  147. * @param string $key name of the variable within the template
  148. * @param string $value path to the picture
  149. * @throws OdfException
  150. * @return Segment
  151. */
  152. public function setImage($key, $value)
  153. {
  154. $filename = strtok(strrchr($value, '/'), '/.');
  155. $file = substr(strrchr($value, '/'), 1);
  156. $size = @getimagesize($value);
  157. if ($size === false) {
  158. throw new OdfException("Invalid image");
  159. }
  160. list ($width, $height) = $size;
  161. $width *= Odf::PIXEL_TO_CM;
  162. $height *= Odf::PIXEL_TO_CM;
  163. $xml = <<<IMG
  164. <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>
  165. IMG;
  166. $this->images[$value] = $file;
  167. $this->setVars($key, $xml, false);
  168. return $this;
  169. }
  170. /**
  171. * Shortcut to retrieve a child
  172. *
  173. * @param string $prop
  174. * @return Segment
  175. * @throws SegmentException
  176. */
  177. public function __get($prop)
  178. {
  179. if (array_key_exists($prop, $this->children)) {
  180. return $this->children[$prop];
  181. } else {
  182. throw new SegmentException('child ' . $prop . ' does not exist');
  183. }
  184. }
  185. /**
  186. * Proxy for setVars
  187. *
  188. * @param string $meth
  189. * @param array $args
  190. * @return Segment
  191. */
  192. public function __call($meth, $args)
  193. {
  194. try {
  195. return $this->setVars($meth, $args[0]);
  196. } catch (SegmentException $e) {
  197. throw new SegmentException("method $meth nor var $meth exist");
  198. }
  199. }
  200. /**
  201. * Returns the parsed XML
  202. *
  203. * @return string
  204. */
  205. public function getXmlParsed()
  206. {
  207. return $this->xmlParsed;
  208. }
  209. }
  210. ?>