PageRenderTime 23ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/library/php/odtphp/Segment.php

http://gevu.googlecode.com/
PHP | 238 lines | 133 code | 5 blank | 100 comment | 19 complexity | 17a7e0819e635905ef472d6feba5df32 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  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: 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. $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. $this->odf->addImageToManifest($imageValue);
  103. }
  104. $this->file->close();
  105. return $this->xmlParsed;
  106. }
  107. /**
  108. * Analyse the XML code in order to find children
  109. *
  110. * @param string $xml
  111. * @return Segment
  112. */
  113. protected function _analyseChildren($xml)
  114. {
  115. // $reg2 = "#\[!--\sBEGIN\s([\S]*)\s--\](?:<\/text:p>)?(.*)(?:<text:p\s.*>)?\[!--\sEND\s(\\1)\s--\]#sm";
  116. $reg2 = "#\[!--\sBEGIN\s([\S]*)\s--\](.*)\[!--\sEND\s(\\1)\s--\]#sm";
  117. preg_match_all($reg2, $xml, $matches);
  118. for ($i = 0, $size = count($matches[0]); $i < $size; $i++) {
  119. if ($matches[1][$i] != $this->name) {
  120. $this->children[$matches[1][$i]] = new self($matches[1][$i], $matches[0][$i], $this->odf);
  121. } else {
  122. $this->_analyseChildren($matches[2][$i]);
  123. }
  124. }
  125. return $this;
  126. }
  127. /**
  128. * Assign a template variable to replace
  129. *
  130. * @param string $key
  131. * @param string $value
  132. * @throws SegmentException
  133. * @return Segment
  134. */
  135. public function setVars($key, $value, $encode = true, $charset = 'UTF-8')
  136. {
  137. if (strpos($this->xml, $this->odf->getConfig('DELIMITER_LEFT') . $key . $this->odf->getConfig('DELIMITER_RIGHT')) === false) {
  138. throw new SegmentException("var $key not found in {$this->getName()}");
  139. }
  140. $value = $encode ? htmlspecialchars($value) : $value;
  141. $value = ($charset == 'ISO-8859') ? utf8_encode($value) : $value;
  142. $this->vars[$this->odf->getConfig('DELIMITER_LEFT') . $key . $this->odf->getConfig('DELIMITER_RIGHT')] = str_replace("\n", "<text:line-break/>", $value);
  143. return $this;
  144. }
  145. /**
  146. * Assign a template variable as a picture
  147. *
  148. * @param string $key name of the variable within the template
  149. * @param string $value path to the picture
  150. * @throws OdfException
  151. * @return Segment
  152. */
  153. public function setImage($key, $value, $width=0, $height=0)
  154. {
  155. $filename = strtok(strrchr($value, '/'), '/.');
  156. $file = substr(strrchr($value, '/'), 1);
  157. $size = @getimagesize($value);
  158. if ($size === false) {
  159. throw new SegmentException("Invalid image");
  160. }
  161. if (($width==0)&&($height==0)){
  162. list ($width, $height) = $size;
  163. $width *= Odf::PIXEL_TO_CM;
  164. $height *= Odf::PIXEL_TO_CM;
  165. } else {
  166. list ($owidth, $oheight) = $size;
  167. if (($width > 0) && ($height == 0)){
  168. $height = $width * ($oheight/$owidth);
  169. }
  170. if (($width == 0) && ($height > 0)){
  171. $width = $height * ($owidth/$oheight);
  172. }
  173. /*Remove this section if no GD/temp directory
  174. $widthp = round($width / Odf::PIXEL_TO_CM, 0);
  175. $heightp = round($height / Odf::PIXEL_TO_CM, 0);
  176. $save = $yourtempdirectory . date("Y-m-d_H-i-s") . rand() . '.jpg';
  177. $tn = imagecreatetruecolor($widthp, $heightp) ;
  178. $image = imagecreatefromjpeg($value);
  179. imagecopyresampled($tn, $image, 0, 0, 0, 0, $widthp, $heightp, $owidth, $oheight) ;
  180. imagejpeg($tn, $save, 100);
  181. $value = $save;
  182. $filename = strtok(strrchr($value, '/'), '/.');
  183. $file = substr(strrchr($value, '/'), 1);
  184. Remove to here*/
  185. }
  186. $xml = <<<IMG
  187. <draw:frame draw:style-name="fr1" draw:name="$filename" text:anchor-type="as-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>
  188. IMG;
  189. $this->images[$value] = $file;
  190. $this->setVars($key, $xml, false);
  191. return $this;
  192. }
  193. /**
  194. * Shortcut to retrieve a child
  195. *
  196. * @param string $prop
  197. * @return Segment
  198. * @throws SegmentException
  199. */
  200. public function __get($prop)
  201. {
  202. if (array_key_exists($prop, $this->children)) {
  203. return $this->children[$prop];
  204. } else {
  205. throw new SegmentException('child ' . $prop . ' does not exist');
  206. }
  207. }
  208. /**
  209. * Proxy for setVars
  210. *
  211. * @param string $meth
  212. * @param array $args
  213. * @return Segment
  214. */
  215. public function __call($meth, $args)
  216. {
  217. try {
  218. return $this->setVars($meth, $args[0]);
  219. } catch (SegmentException $e) {
  220. throw new SegmentException("method $meth nor var $meth exist");
  221. }
  222. }
  223. /**
  224. * Returns the parsed XML
  225. *
  226. * @return string
  227. */
  228. public function getXmlParsed()
  229. {
  230. return $this->xmlParsed;
  231. }
  232. }
  233. ?>