説明なし

Meta.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\ODText\Part;
  18. use PhpOffice\PhpWord\Shared\XMLWriter;
  19. /**
  20. * ODText meta part writer: meta.xml
  21. *
  22. * @since 0.11.0
  23. */
  24. class Meta extends AbstractPart
  25. {
  26. /**
  27. * Write part
  28. *
  29. * @return string
  30. */
  31. public function write()
  32. {
  33. $phpWord = $this->getParentWriter()->getPhpWord();
  34. $docProps = $phpWord->getDocInfo();
  35. $xmlWriter = $this->getXmlWriter();
  36. $xmlWriter->startDocument('1.0', 'UTF-8');
  37. $xmlWriter->startElement('office:document-meta');
  38. $xmlWriter->writeAttribute('office:version', '1.2');
  39. $xmlWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0');
  40. $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
  41. $xmlWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
  42. $xmlWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0');
  43. $xmlWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office');
  44. $xmlWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#');
  45. $xmlWriter->startElement('office:meta');
  46. // Core properties
  47. $xmlWriter->writeElement('dc:title', $docProps->getTitle());
  48. $xmlWriter->writeElement('dc:subject', $docProps->getSubject());
  49. $xmlWriter->writeElement('dc:description', $docProps->getDescription());
  50. $xmlWriter->writeElement('dc:creator', $docProps->getLastModifiedBy());
  51. $xmlWriter->writeElement('dc:date', gmdate($this->dateFormat, $docProps->getModified()));
  52. // Extended properties
  53. $xmlWriter->writeElement('meta:generator', 'PHPWord');
  54. $xmlWriter->writeElement('meta:initial-creator', $docProps->getCreator());
  55. $xmlWriter->writeElement('meta:creation-date', gmdate($this->dateFormat, $docProps->getCreated()));
  56. $xmlWriter->writeElement('meta:keyword', $docProps->getKeywords());
  57. // Category, company, and manager are put in meta namespace
  58. $properties = array('Category', 'Company', 'Manager');
  59. foreach ($properties as $property) {
  60. $method = "get{$property}";
  61. if ($docProps->$method() !== null) {
  62. $this->writeCustomProperty($xmlWriter, $property, $docProps->$method());
  63. }
  64. }
  65. // Other custom properties
  66. // @todo Check type. Currently all assumed as string
  67. foreach ($docProps->getCustomProperties() as $property) {
  68. $value = $docProps->getCustomPropertyValue($property);
  69. $this->writeCustomProperty($xmlWriter, $property, $value);
  70. }
  71. $xmlWriter->endElement(); // office:meta
  72. $xmlWriter->endElement(); // office:document-meta
  73. return $xmlWriter->getData();
  74. }
  75. /**
  76. * Write individual property
  77. *
  78. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  79. * @param string $property
  80. * @param string $value
  81. * @return void
  82. *
  83. * @todo Handle other `$type`: double|date|dateTime|duration|boolean (4th arguments)
  84. */
  85. private function writeCustomProperty(XMLWriter $xmlWriter, $property, $value)
  86. {
  87. $xmlWriter->startElement('meta:user-defined');
  88. $xmlWriter->writeAttribute('meta:name', $property);
  89. // if ($type !== null) {
  90. // $xmlWriter->writeAttribute('meta:value-type', $type);
  91. // }
  92. $xmlWriter->writeRaw($value);
  93. $xmlWriter->endElement(); // meta:user-defined
  94. }
  95. }