Bez popisu

Document.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\RTF\Part;
  18. use PhpOffice\PhpWord\Settings;
  19. use PhpOffice\PhpWord\Writer\RTF\Element\Container;
  20. use PhpOffice\PhpWord\Writer\RTF\Style\Section as SectionStyleWriter;
  21. /**
  22. * RTF document part writer
  23. *
  24. * @since 0.11.0
  25. * @link http://www.biblioscape.com/rtf15_spec.htm#Heading24
  26. */
  27. class Document extends AbstractPart
  28. {
  29. /**
  30. * Write part
  31. *
  32. * @return string
  33. */
  34. public function write()
  35. {
  36. $content = '';
  37. $content .= $this->writeInfo();
  38. $content .= $this->writeFormatting();
  39. $content .= $this->writeSections();
  40. return $content;
  41. }
  42. /**
  43. * Write document information
  44. *
  45. * @return string
  46. */
  47. private function writeInfo()
  48. {
  49. $docProps = $this->getParentWriter()->getPhpWord()->getDocInfo();
  50. $properties = array('title', 'subject', 'category', 'keywords', 'comment',
  51. 'author', 'operator', 'creatim', 'revtim', 'company', 'manager');
  52. $mapping = array('comment' => 'description', 'author' => 'creator', 'operator' => 'lastModifiedBy',
  53. 'creatim' => 'created', 'revtim' => 'modified');
  54. $dateFields = array('creatim', 'revtim');
  55. $content = '';
  56. $content .= '{';
  57. $content .= '\info';
  58. foreach ($properties as $property) {
  59. $method = 'get' . (isset($mapping[$property]) ? $mapping[$property] : $property);
  60. $value = $docProps->$method();
  61. $value = in_array($property, $dateFields) ? $this->getDateValue($value) : $value;
  62. $content .= "{\\{$property} {$value}}";
  63. }
  64. $content .= '}';
  65. $content .= PHP_EOL;
  66. return $content;
  67. }
  68. /**
  69. * Write document formatting properties
  70. *
  71. * @return string
  72. */
  73. private function writeFormatting()
  74. {
  75. $content = '';
  76. $content .= '\deftab720'; // Set the default tab size (720 twips)
  77. $content .= '\viewkind1'; // Set the view mode of the document
  78. $content .= '\uc1'; // Set the numberof bytes that follows a unicode character
  79. $content .= '\pard'; // Resets to default paragraph properties.
  80. $content .= '\nowidctlpar'; // No widow/orphan control
  81. $content .= '\lang1036'; // Applies a language to a text run (1036 : French (France))
  82. $content .= '\kerning1'; // Point size (in half-points) above which to kern character pairs
  83. $content .= '\fs' . (Settings::getDefaultFontSize() * 2); // Set the font size in half-points
  84. $content .= PHP_EOL;
  85. return $content;
  86. }
  87. /**
  88. * Write sections
  89. *
  90. * @return string
  91. */
  92. private function writeSections()
  93. {
  94. $content = '';
  95. $sections = $this->getParentWriter()->getPhpWord()->getSections();
  96. foreach ($sections as $section) {
  97. $styleWriter = new SectionStyleWriter($section->getStyle());
  98. $styleWriter->setParentWriter($this->getParentWriter());
  99. $content .= $styleWriter->write();
  100. $elementWriter = new Container($this->getParentWriter(), $section);
  101. $content .= $elementWriter->write();
  102. $content .= '\sect' . PHP_EOL;
  103. }
  104. return $content;
  105. }
  106. /**
  107. * Get date value
  108. *
  109. * The format of date value is `\yr?\mo?\dy?\hr?\min?\sec?`
  110. *
  111. * @param int $value
  112. * @return string
  113. */
  114. private function getDateValue($value)
  115. {
  116. $dateParts = array(
  117. 'Y' => 'yr',
  118. 'm' => 'mo',
  119. 'd' => 'dy',
  120. 'H' => 'hr',
  121. 'i' => 'min',
  122. 's' => 'sec',
  123. );
  124. $result = '';
  125. foreach ($dateParts as $dateFormat => $controlWord) {
  126. $result .= '\\' . $controlWord . date($dateFormat, $value);
  127. }
  128. return $result;
  129. }
  130. }