ODText.php 2.7 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;
  18. use PhpOffice\PhpWord\Media;
  19. use PhpOffice\PhpWord\PhpWord;
  20. /**
  21. * ODText writer
  22. *
  23. * @since 0.7.0
  24. */
  25. class ODText extends AbstractWriter implements WriterInterface
  26. {
  27. /**
  28. * Create new ODText writer
  29. *
  30. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  31. */
  32. public function __construct(PhpWord $phpWord = null)
  33. {
  34. // Assign PhpWord
  35. $this->setPhpWord($phpWord);
  36. // Create parts
  37. $this->parts = array(
  38. 'Mimetype' => 'mimetype',
  39. 'Content' => 'content.xml',
  40. 'Meta' => 'meta.xml',
  41. 'Styles' => 'styles.xml',
  42. 'Manifest' => 'META-INF/manifest.xml',
  43. );
  44. foreach (array_keys($this->parts) as $partName) {
  45. $partClass = get_class($this) . '\\Part\\' . $partName;
  46. if (class_exists($partClass)) {
  47. /** @var $partObject \PhpOffice\PhpWord\Writer\ODText\Part\AbstractPart Type hint */
  48. $partObject = new $partClass();
  49. $partObject->setParentWriter($this);
  50. $this->writerParts[strtolower($partName)] = $partObject;
  51. }
  52. }
  53. // Set package paths
  54. $this->mediaPaths = array('image' => 'Pictures/');
  55. }
  56. /**
  57. * Save PhpWord to file.
  58. *
  59. * @param string $filename
  60. */
  61. public function save($filename = null)
  62. {
  63. $filename = $this->getTempFile($filename);
  64. $zip = $this->getZipArchive($filename);
  65. // Add section media files
  66. $sectionMedia = Media::getElements('section');
  67. if (!empty($sectionMedia)) {
  68. $this->addFilesToPackage($zip, $sectionMedia);
  69. }
  70. // Write parts
  71. foreach ($this->parts as $partName => $fileName) {
  72. if ($fileName != '') {
  73. $zip->addFromString($fileName, $this->getWriterPart($partName)->write());
  74. }
  75. }
  76. // Close zip archive and cleanup temp file
  77. $zip->close();
  78. $this->cleanupTempFile();
  79. }
  80. }