Nessuna descrizione

DocPropsCustom.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. /**
  19. * Word2007 custom document properties part writer: docProps/custom.xml
  20. *
  21. * @since 0.11.0
  22. */
  23. class DocPropsCustom extends AbstractPart
  24. {
  25. /**
  26. * Write part
  27. *
  28. * @return string
  29. */
  30. public function write()
  31. {
  32. $phpWord = $this->getParentWriter()->getPhpWord();
  33. $xmlWriter = $this->getXmlWriter();
  34. $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
  35. $xmlWriter->startElement('Properties');
  36. $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/custom-properties');
  37. $xmlWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
  38. $docProps = $phpWord->getDocInfo();
  39. $properties = $docProps->getCustomProperties();
  40. foreach ($properties as $key => $property) {
  41. $propertyValue = $docProps->getCustomPropertyValue($property);
  42. $propertyType = $docProps->getCustomPropertyType($property);
  43. $xmlWriter->startElement('property');
  44. $xmlWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
  45. $xmlWriter->writeAttribute('pid', $key + 2);
  46. $xmlWriter->writeAttribute('name', $property);
  47. switch ($propertyType) {
  48. case 'i':
  49. $xmlWriter->writeElement('vt:i4', $propertyValue);
  50. break;
  51. case 'f':
  52. $xmlWriter->writeElement('vt:r8', $propertyValue);
  53. break;
  54. case 'b':
  55. $xmlWriter->writeElement('vt:bool', ($propertyValue) ? 'true' : 'false');
  56. break;
  57. case 'd':
  58. $xmlWriter->startElement('vt:filetime');
  59. $xmlWriter->writeRaw(date($this->dateFormat, $propertyValue));
  60. $xmlWriter->endElement();
  61. break;
  62. default:
  63. $xmlWriter->writeElement('vt:lpwstr', $propertyValue);
  64. break;
  65. }
  66. $xmlWriter->endElement(); // property
  67. }
  68. $xmlWriter->endElement(); // Properties
  69. return $xmlWriter->getData();
  70. }
  71. }