Body.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer\HTML\Part;
  18. use PhpOffice\PhpWord\Writer\HTML\Element\Container;
  19. use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter;
  20. /**
  21. * RTF body part writer
  22. *
  23. * @since 0.11.0
  24. */
  25. class Body extends AbstractPart
  26. {
  27. /**
  28. * Write part
  29. *
  30. * @return string
  31. */
  32. public function write()
  33. {
  34. $phpWord = $this->getParentWriter()->getPhpWord();
  35. $content = '';
  36. $content .= '<body>' . PHP_EOL;
  37. $sections = $phpWord->getSections();
  38. foreach ($sections as $section) {
  39. $writer = new Container($this->getParentWriter(), $section);
  40. $content .= $writer->write();
  41. }
  42. $content .= $this->writeNotes();
  43. $content .= '</body>' . PHP_EOL;
  44. return $content;
  45. }
  46. /**
  47. * Write footnote/endnote contents as textruns
  48. *
  49. * @return string
  50. */
  51. private function writeNotes()
  52. {
  53. /** @var \PhpOffice\PhpWord\Writer\HTML $parentWriter Type hint */
  54. $parentWriter = $this->getParentWriter();
  55. $phpWord = $parentWriter->getPhpWord();
  56. $notes = $parentWriter->getNotes();
  57. $content = '';
  58. if (!empty($notes)) {
  59. $content .= '<hr />' . PHP_EOL;
  60. foreach ($notes as $noteId => $noteMark) {
  61. list($noteType, $noteTypeId) = explode('-', $noteMark);
  62. $method = 'get' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes');
  63. $collection = $phpWord->$method()->getItems();
  64. if (isset($collection[$noteTypeId])) {
  65. $element = $collection[$noteTypeId];
  66. $noteAnchor = "<a name=\"note-{$noteId}\" />";
  67. $noteAnchor .= "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
  68. $writer = new TextRunWriter($this->getParentWriter(), $element);
  69. $writer->setOpeningText($noteAnchor);
  70. $content .= $writer->write();
  71. }
  72. }
  73. }
  74. return $content;
  75. }
  76. }