Comments.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel2007_Comments
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel2007
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_Excel2007_Comments extends PHPExcel_Writer_Excel2007_WriterPart
  35. {
  36. /**
  37. * Write comments to XML format
  38. *
  39. * @param PHPExcel_Worksheet $pWorksheet
  40. * @return string XML Output
  41. * @throws PHPExcel_Writer_Exception
  42. */
  43. public function writeComments(PHPExcel_Worksheet $pWorksheet = null)
  44. {
  45. // Create XML writer
  46. $objWriter = null;
  47. if ($this->getParentWriter()->getUseDiskCaching()) {
  48. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  49. } else {
  50. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  51. }
  52. // XML header
  53. $objWriter->startDocument('1.0','UTF-8','yes');
  54. // Comments cache
  55. $comments = $pWorksheet->getComments();
  56. // Authors cache
  57. $authors = array();
  58. $authorId = 0;
  59. foreach ($comments as $comment) {
  60. if (!isset($authors[$comment->getAuthor()])) {
  61. $authors[$comment->getAuthor()] = $authorId++;
  62. }
  63. }
  64. // comments
  65. $objWriter->startElement('comments');
  66. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
  67. // Loop through authors
  68. $objWriter->startElement('authors');
  69. foreach ($authors as $author => $index) {
  70. $objWriter->writeElement('author', $author);
  71. }
  72. $objWriter->endElement();
  73. // Loop through comments
  74. $objWriter->startElement('commentList');
  75. foreach ($comments as $key => $value) {
  76. $this->_writeComment($objWriter, $key, $value, $authors);
  77. }
  78. $objWriter->endElement();
  79. $objWriter->endElement();
  80. // Return
  81. return $objWriter->getData();
  82. }
  83. /**
  84. * Write comment to XML format
  85. *
  86. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  87. * @param string $pCellReference Cell reference
  88. * @param PHPExcel_Comment $pComment Comment
  89. * @param array $pAuthors Array of authors
  90. * @throws PHPExcel_Writer_Exception
  91. */
  92. public function _writeComment(PHPExcel_Shared_XMLWriter $objWriter = null, $pCellReference = 'A1', PHPExcel_Comment $pComment = null, $pAuthors = null)
  93. {
  94. // comment
  95. $objWriter->startElement('comment');
  96. $objWriter->writeAttribute('ref', $pCellReference);
  97. $objWriter->writeAttribute('authorId', $pAuthors[$pComment->getAuthor()]);
  98. // text
  99. $objWriter->startElement('text');
  100. $this->getParentWriter()->getWriterPart('stringtable')->writeRichText($objWriter, $pComment->getText());
  101. $objWriter->endElement();
  102. $objWriter->endElement();
  103. }
  104. /**
  105. * Write VML comments to XML format
  106. *
  107. * @param PHPExcel_Worksheet $pWorksheet
  108. * @return string XML Output
  109. * @throws PHPExcel_Writer_Exception
  110. */
  111. public function writeVMLComments(PHPExcel_Worksheet $pWorksheet = null)
  112. {
  113. // Create XML writer
  114. $objWriter = null;
  115. if ($this->getParentWriter()->getUseDiskCaching()) {
  116. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  117. } else {
  118. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  119. }
  120. // XML header
  121. $objWriter->startDocument('1.0','UTF-8','yes');
  122. // Comments cache
  123. $comments = $pWorksheet->getComments();
  124. // xml
  125. $objWriter->startElement('xml');
  126. $objWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
  127. $objWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
  128. $objWriter->writeAttribute('xmlns:x', 'urn:schemas-microsoft-com:office:excel');
  129. // o:shapelayout
  130. $objWriter->startElement('o:shapelayout');
  131. $objWriter->writeAttribute('v:ext', 'edit');
  132. // o:idmap
  133. $objWriter->startElement('o:idmap');
  134. $objWriter->writeAttribute('v:ext', 'edit');
  135. $objWriter->writeAttribute('data', '1');
  136. $objWriter->endElement();
  137. $objWriter->endElement();
  138. // v:shapetype
  139. $objWriter->startElement('v:shapetype');
  140. $objWriter->writeAttribute('id', '_x0000_t202');
  141. $objWriter->writeAttribute('coordsize', '21600,21600');
  142. $objWriter->writeAttribute('o:spt', '202');
  143. $objWriter->writeAttribute('path', 'm,l,21600r21600,l21600,xe');
  144. // v:stroke
  145. $objWriter->startElement('v:stroke');
  146. $objWriter->writeAttribute('joinstyle', 'miter');
  147. $objWriter->endElement();
  148. // v:path
  149. $objWriter->startElement('v:path');
  150. $objWriter->writeAttribute('gradientshapeok', 't');
  151. $objWriter->writeAttribute('o:connecttype', 'rect');
  152. $objWriter->endElement();
  153. $objWriter->endElement();
  154. // Loop through comments
  155. foreach ($comments as $key => $value) {
  156. $this->_writeVMLComment($objWriter, $key, $value);
  157. }
  158. $objWriter->endElement();
  159. // Return
  160. return $objWriter->getData();
  161. }
  162. /**
  163. * Write VML comment to XML format
  164. *
  165. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  166. * @param string $pCellReference Cell reference
  167. * @param PHPExcel_Comment $pComment Comment
  168. * @throws PHPExcel_Writer_Exception
  169. */
  170. public function _writeVMLComment(PHPExcel_Shared_XMLWriter $objWriter = null, $pCellReference = 'A1', PHPExcel_Comment $pComment = null)
  171. {
  172. // Metadata
  173. list($column, $row) = PHPExcel_Cell::coordinateFromString($pCellReference);
  174. $column = PHPExcel_Cell::columnIndexFromString($column);
  175. $id = 1024 + $column + $row;
  176. $id = substr($id, 0, 4);
  177. // v:shape
  178. $objWriter->startElement('v:shape');
  179. $objWriter->writeAttribute('id', '_x0000_s' . $id);
  180. $objWriter->writeAttribute('type', '#_x0000_t202');
  181. $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'));
  182. $objWriter->writeAttribute('fillcolor', '#' . $pComment->getFillColor()->getRGB());
  183. $objWriter->writeAttribute('o:insetmode', 'auto');
  184. // v:fill
  185. $objWriter->startElement('v:fill');
  186. $objWriter->writeAttribute('color2', '#' . $pComment->getFillColor()->getRGB());
  187. $objWriter->endElement();
  188. // v:shadow
  189. $objWriter->startElement('v:shadow');
  190. $objWriter->writeAttribute('on', 't');
  191. $objWriter->writeAttribute('color', 'black');
  192. $objWriter->writeAttribute('obscured', 't');
  193. $objWriter->endElement();
  194. // v:path
  195. $objWriter->startElement('v:path');
  196. $objWriter->writeAttribute('o:connecttype', 'none');
  197. $objWriter->endElement();
  198. // v:textbox
  199. $objWriter->startElement('v:textbox');
  200. $objWriter->writeAttribute('style', 'mso-direction-alt:auto');
  201. // div
  202. $objWriter->startElement('div');
  203. $objWriter->writeAttribute('style', 'text-align:left');
  204. $objWriter->endElement();
  205. $objWriter->endElement();
  206. // x:ClientData
  207. $objWriter->startElement('x:ClientData');
  208. $objWriter->writeAttribute('ObjectType', 'Note');
  209. // x:MoveWithCells
  210. $objWriter->writeElement('x:MoveWithCells', '');
  211. // x:SizeWithCells
  212. $objWriter->writeElement('x:SizeWithCells', '');
  213. // x:Anchor
  214. //$objWriter->writeElement('x:Anchor', $column . ', 15, ' . ($row - 2) . ', 10, ' . ($column + 4) . ', 15, ' . ($row + 5) . ', 18');
  215. // x:AutoFill
  216. $objWriter->writeElement('x:AutoFill', 'False');
  217. // x:Row
  218. $objWriter->writeElement('x:Row', ($row - 1));
  219. // x:Column
  220. $objWriter->writeElement('x:Column', ($column - 1));
  221. $objWriter->endElement();
  222. $objWriter->endElement();
  223. }
  224. }