Aucune description

ContentTypes.php 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer\Word2007\Part;
  18. use PhpOffice\PhpWord\Shared\XMLWriter;
  19. /**
  20. * Word2007 contenttypes part writer: [Content_Types].xml
  21. */
  22. class ContentTypes extends AbstractPart
  23. {
  24. /**
  25. * Write part
  26. *
  27. * @return string
  28. */
  29. public function write()
  30. {
  31. /** @var \PhpOffice\PhpWord\Writer\Word2007 $parentWriter Type hint */
  32. $parentWriter = $this->getParentWriter();
  33. $contentTypes = $parentWriter->getContentTypes();
  34. $openXMLPrefix = 'application/vnd.openxmlformats-';
  35. $wordMLPrefix = $openXMLPrefix . 'officedocument.wordprocessingml.';
  36. $drawingMLPrefix = $openXMLPrefix . 'officedocument.drawingml.';
  37. $overrides = array(
  38. '/docProps/core.xml' => $openXMLPrefix . 'package.core-properties+xml',
  39. '/docProps/app.xml' => $openXMLPrefix . 'officedocument.extended-properties+xml',
  40. '/docProps/custom.xml' => $openXMLPrefix . 'officedocument.custom-properties+xml',
  41. '/word/document.xml' => $wordMLPrefix . 'document.main+xml',
  42. '/word/styles.xml' => $wordMLPrefix . 'styles+xml',
  43. '/word/numbering.xml' => $wordMLPrefix . 'numbering+xml',
  44. '/word/settings.xml' => $wordMLPrefix . 'settings+xml',
  45. '/word/theme/theme1.xml' => $openXMLPrefix . 'officedocument.theme+xml',
  46. '/word/webSettings.xml' => $wordMLPrefix . 'webSettings+xml',
  47. '/word/fontTable.xml' => $wordMLPrefix . 'fontTable+xml',
  48. );
  49. $defaults = $contentTypes['default'];
  50. if (!empty($contentTypes['override'])) {
  51. foreach ($contentTypes['override'] as $key => $val) {
  52. if ($val == 'chart') {
  53. $overrides[$key] = $drawingMLPrefix . $val . '+xml';
  54. } else {
  55. $overrides[$key] = $wordMLPrefix . $val . '+xml';
  56. }
  57. }
  58. }
  59. $xmlWriter = $this->getXmlWriter();
  60. $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
  61. $xmlWriter->startElement('Types');
  62. $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
  63. $this->writeContentType($xmlWriter, $defaults, true);
  64. $this->writeContentType($xmlWriter, $overrides, false);
  65. $xmlWriter->endElement(); // Types
  66. return $xmlWriter->getData();
  67. }
  68. /**
  69. * Write content types element
  70. *
  71. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter XML Writer
  72. * @param array $parts
  73. * @param boolean $isDefault
  74. * @return void
  75. */
  76. private function writeContentType(XMLWriter $xmlWriter, $parts, $isDefault)
  77. {
  78. foreach ($parts as $partName => $contentType) {
  79. $partType = $isDefault ? 'Default' : 'Override';
  80. $partAttribute = $isDefault ? 'Extension' : 'PartName';
  81. $xmlWriter->startElement($partType);
  82. $xmlWriter->writeAttribute($partAttribute, $partName);
  83. $xmlWriter->writeAttribute('ContentType', $contentType);
  84. $xmlWriter->endElement();
  85. }
  86. }
  87. }