PageRenderTime 33ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/app/vendors/PHPExcel_deprecated/PHPExcel/Writer/Excel2007/Comments.php

https://github.com/konscript/roskilde-el-old
PHP | 304 lines | 137 code | 55 blank | 112 comment | 6 complexity | 5c6a4bbb9d6d54d82d8ffc8cc46a138a MD5 | raw file
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2010 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Writer_Excel2007
  23. * @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.2, 2010-01-11
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../');
  33. }
  34. /** PHPExcel */
  35. require_once PHPEXCEL_ROOT . 'PHPExcel.php';
  36. /** PHPExcel_Writer_Excel2007 */
  37. require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007.php';
  38. /** PHPExcel_Writer_Excel2007_WriterPart */
  39. require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/WriterPart.php';
  40. /** PHPExcel_Worksheet */
  41. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet.php';
  42. /** PHPExcel_Comment */
  43. require_once PHPEXCEL_ROOT . 'PHPExcel/Comment.php';
  44. /** PHPExcel_RichText */
  45. require_once PHPEXCEL_ROOT . 'PHPExcel/RichText.php';
  46. /** PHPExcel_Cell */
  47. require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
  48. /** PHPExcel_Style_Color */
  49. require_once PHPEXCEL_ROOT . 'PHPExcel/Style/Color.php';
  50. /** PHPExcel_Shared_XMLWriter */
  51. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/XMLWriter.php';
  52. /**
  53. * PHPExcel_Writer_Excel2007_Comments
  54. *
  55. * @category PHPExcel
  56. * @package PHPExcel_Writer_Excel2007
  57. * @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  58. */
  59. class PHPExcel_Writer_Excel2007_Comments extends PHPExcel_Writer_Excel2007_WriterPart
  60. {
  61. /**
  62. * Write comments to XML format
  63. *
  64. * @param PHPExcel_Worksheet $pWorksheet
  65. * @return string XML Output
  66. * @throws Exception
  67. */
  68. public function writeComments(PHPExcel_Worksheet $pWorksheet = null)
  69. {
  70. // Create XML writer
  71. $objWriter = null;
  72. if ($this->getParentWriter()->getUseDiskCaching()) {
  73. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  74. } else {
  75. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  76. }
  77. // XML header
  78. $objWriter->startDocument('1.0','UTF-8','yes');
  79. // Comments cache
  80. $comments = $pWorksheet->getComments();
  81. // Authors cache
  82. $authors = array();
  83. $authorId = 0;
  84. foreach ($comments as $comment) {
  85. if (!isset($authors[$comment->getAuthor()])) {
  86. $authors[$comment->getAuthor()] = $authorId++;
  87. }
  88. }
  89. // comments
  90. $objWriter->startElement('comments');
  91. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
  92. // Loop through authors
  93. $objWriter->startElement('authors');
  94. foreach ($authors as $author => $index) {
  95. $objWriter->writeElement('author', $author);
  96. }
  97. $objWriter->endElement();
  98. // Loop through comments
  99. $objWriter->startElement('commentList');
  100. foreach ($comments as $key => $value) {
  101. $this->_writeComment($objWriter, $key, $value, $authors);
  102. }
  103. $objWriter->endElement();
  104. $objWriter->endElement();
  105. // Return
  106. return $objWriter->getData();
  107. }
  108. /**
  109. * Write comment to XML format
  110. *
  111. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  112. * @param string $pCellReference Cell reference
  113. * @param PHPExcel_Comment $pComment Comment
  114. * @param array $pAuthors Array of authors
  115. * @throws Exception
  116. */
  117. public function _writeComment(PHPExcel_Shared_XMLWriter $objWriter = null, $pCellReference = 'A1', PHPExcel_Comment $pComment = null, $pAuthors = null)
  118. {
  119. // comment
  120. $objWriter->startElement('comment');
  121. $objWriter->writeAttribute('ref', $pCellReference);
  122. $objWriter->writeAttribute('authorId', $pAuthors[$pComment->getAuthor()]);
  123. // text
  124. $objWriter->startElement('text');
  125. $this->getParentWriter()->getWriterPart('stringtable')->writeRichText($objWriter, $pComment->getText());
  126. $objWriter->endElement();
  127. $objWriter->endElement();
  128. }
  129. /**
  130. * Write VML comments to XML format
  131. *
  132. * @param PHPExcel_Worksheet $pWorksheet
  133. * @return string XML Output
  134. * @throws Exception
  135. */
  136. public function writeVMLComments(PHPExcel_Worksheet $pWorksheet = null)
  137. {
  138. // Create XML writer
  139. $objWriter = null;
  140. if ($this->getParentWriter()->getUseDiskCaching()) {
  141. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  142. } else {
  143. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  144. }
  145. // XML header
  146. $objWriter->startDocument('1.0','UTF-8','yes');
  147. // Comments cache
  148. $comments = $pWorksheet->getComments();
  149. // xml
  150. $objWriter->startElement('xml');
  151. $objWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
  152. $objWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
  153. $objWriter->writeAttribute('xmlns:x', 'urn:schemas-microsoft-com:office:excel');
  154. // o:shapelayout
  155. $objWriter->startElement('o:shapelayout');
  156. $objWriter->writeAttribute('v:ext', 'edit');
  157. // o:idmap
  158. $objWriter->startElement('o:idmap');
  159. $objWriter->writeAttribute('v:ext', 'edit');
  160. $objWriter->writeAttribute('data', '1');
  161. $objWriter->endElement();
  162. $objWriter->endElement();
  163. // v:shapetype
  164. $objWriter->startElement('v:shapetype');
  165. $objWriter->writeAttribute('id', '_x0000_t202');
  166. $objWriter->writeAttribute('coordsize', '21600,21600');
  167. $objWriter->writeAttribute('o:spt', '202');
  168. $objWriter->writeAttribute('path', 'm,l,21600r21600,l21600,xe');
  169. // v:stroke
  170. $objWriter->startElement('v:stroke');
  171. $objWriter->writeAttribute('joinstyle', 'miter');
  172. $objWriter->endElement();
  173. // v:path
  174. $objWriter->startElement('v:path');
  175. $objWriter->writeAttribute('gradientshapeok', 't');
  176. $objWriter->writeAttribute('o:connecttype', 'rect');
  177. $objWriter->endElement();
  178. $objWriter->endElement();
  179. // Loop through comments
  180. foreach ($comments as $key => $value) {
  181. $this->_writeVMLComment($objWriter, $key, $value);
  182. }
  183. $objWriter->endElement();
  184. // Return
  185. return $objWriter->getData();
  186. }
  187. /**
  188. * Write VML comment to XML format
  189. *
  190. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  191. * @param string $pCellReference Cell reference
  192. * @param PHPExcel_Comment $pComment Comment
  193. * @throws Exception
  194. */
  195. public function _writeVMLComment(PHPExcel_Shared_XMLWriter $objWriter = null, $pCellReference = 'A1', PHPExcel_Comment $pComment = null)
  196. {
  197. // Metadata
  198. list($column, $row) = PHPExcel_Cell::coordinateFromString($pCellReference);
  199. $column = PHPExcel_Cell::columnIndexFromString($column);
  200. $id = 1024 + $column + $row;
  201. $id = substr($id, 0, 4);
  202. // v:shape
  203. $objWriter->startElement('v:shape');
  204. $objWriter->writeAttribute('id', '_x0000_s' . $id);
  205. $objWriter->writeAttribute('type', '#_x0000_t202');
  206. $objWriter->writeAttribute('style', 'position:absolute;margin-left:' . $pComment->getMarginLeft() . ';margin-top:' . $pComment->getMarginTop() . ';width:' . $pComment->getWidth() . ';height:' . $pComment->getHeight() . ';z-index:1;visibility:' . ($pComment->getVisible() ? 'visible' : 'hidden'));
  207. $objWriter->writeAttribute('fillcolor', '#' . $pComment->getFillColor()->getRGB());
  208. $objWriter->writeAttribute('o:insetmode', 'auto');
  209. // v:fill
  210. $objWriter->startElement('v:fill');
  211. $objWriter->writeAttribute('color2', '#' . $pComment->getFillColor()->getRGB());
  212. $objWriter->endElement();
  213. // v:shadow
  214. $objWriter->startElement('v:shadow');
  215. $objWriter->writeAttribute('on', 't');
  216. $objWriter->writeAttribute('color', 'black');
  217. $objWriter->writeAttribute('obscured', 't');
  218. $objWriter->endElement();
  219. // v:path
  220. $objWriter->startElement('v:path');
  221. $objWriter->writeAttribute('o:connecttype', 'none');
  222. $objWriter->endElement();
  223. // v:textbox
  224. $objWriter->startElement('v:textbox');
  225. $objWriter->writeAttribute('style', 'mso-direction-alt:auto');
  226. // div
  227. $objWriter->startElement('div');
  228. $objWriter->writeAttribute('style', 'text-align:left');
  229. $objWriter->endElement();
  230. $objWriter->endElement();
  231. // x:ClientData
  232. $objWriter->startElement('x:ClientData');
  233. $objWriter->writeAttribute('ObjectType', 'Note');
  234. // x:MoveWithCells
  235. $objWriter->writeElement('x:MoveWithCells', '');
  236. // x:SizeWithCells
  237. $objWriter->writeElement('x:SizeWithCells', '');
  238. // x:Anchor
  239. //$objWriter->writeElement('x:Anchor', $column . ', 15, ' . ($row - 2) . ', 10, ' . ($column + 4) . ', 15, ' . ($row + 5) . ', 18');
  240. // x:AutoFill
  241. $objWriter->writeElement('x:AutoFill', 'False');
  242. // x:Row
  243. $objWriter->writeElement('x:Row', ($row - 1));
  244. // x:Column
  245. $objWriter->writeElement('x:Column', ($column - 1));
  246. $objWriter->endElement();
  247. $objWriter->endElement();
  248. }
  249. }