diff --git a/.gitignore b/.gitignore index 23e11e8e..b318565f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,8 @@ vendor /.buildpath /.project /docs +*.odt +*.docx +*.rtf +*.txt +*.xml \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 2cca49ae..5796deca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,14 +26,14 @@ before_script: script: ## PHP_CodeSniffer - - phpcs --standard=PSR1 src/ - - phpcs --standard=PSR2 src/ + - phpcs --standard=PSR1 Classes/ + - phpcs --standard=PSR2 Classes/ ## PHP Copy/Paste Detector - - php phpcpd.phar --verbose src/ + - php phpcpd.phar --verbose Classes/ ## PHP Mess Detector - - phpmd src/ text codesize,unusedcode,naming,design + - phpmd Classes/ text codesize,unusedcode,naming,design ## PHPLOC - - php phploc.phar src/ + - php phploc.phar Classes/ notifications: email: diff --git a/Classes/PHPWord.php b/Classes/PHPWord.php new file mode 100644 index 00000000..feda8761 --- /dev/null +++ b/Classes/PHPWord.php @@ -0,0 +1,229 @@ +_properties = new PHPWord_DocumentProperties(); + $this->_defaultFontName = 'Arial'; + $this->_defaultFontSize = 20; + } + + /** + * Get properties + * @return PHPWord_DocumentProperties + */ + public function getProperties() { + return $this->_properties; + } + + /** + * Set properties + * + * @param PHPWord_DocumentProperties $value + * @return PHPWord + */ + public function setProperties(PHPWord_DocumentProperties $value) { + $this->_properties = $value; + return $this; + } + + /** + * Create a new Section + * + * @param PHPWord_Section_Settings $settings + * @return PHPWord_Section + */ + public function createSection($settings = null) { + $sectionCount = $this->_countSections() + 1; + + $section = new PHPWord_Section($sectionCount, $settings); + $this->_sectionCollection[] = $section; + return $section; + } + + /** + * Get default Font name + * @return string + */ + public function getDefaultFontName() { + return $this->_defaultFontName; + } + + /** + * Set default Font name + * @param string $pValue + */ + public function setDefaultFontName($pValue) { + $this->_defaultFontName = $pValue; + } + + /** + * Get default Font size + * @return string + */ + public function getDefaultFontSize() { + return $this->_defaultFontSize; + } + + /** + * Set default Font size + * @param int $pValue + */ + public function setDefaultFontSize($pValue) { + $pValue = $pValue * 2; + $this->_defaultFontSize = $pValue; + } + + /** + * Adds a paragraph style definition to styles.xml + * + * @param $styleName string + * @param $styles array + */ + public function addParagraphStyle($styleName, $styles) { + PHPWord_Style::addParagraphStyle($styleName, $styles); + } + + /** + * Adds a font style definition to styles.xml + * + * @param $styleName string + * @param $styles array + */ + public function addFontStyle($styleName, $styleFont, $styleParagraph = null) { + PHPWord_Style::addFontStyle($styleName, $styleFont, $styleParagraph); + } + + /** + * Adds a table style definition to styles.xml + * + * @param $styleName string + * @param $styles array + */ + public function addTableStyle($styleName, $styleTable, $styleFirstRow = null) { + PHPWord_Style::addTableStyle($styleName, $styleTable, $styleFirstRow); + } + + /** + * Adds a heading style definition to styles.xml + * + * @param $titleCount int + * @param $styles array + */ + public function addTitleStyle($titleCount, $styleFont, $styleParagraph = null) { + PHPWord_Style::addTitleStyle($titleCount, $styleFont, $styleParagraph); + } + + /** + * Adds a hyperlink style to styles.xml + * + * @param $styleName string + * @param $styles array + */ + public function addLinkStyle($styleName, $styles) { + PHPWord_Style::addLinkStyle($styleName, $styles); + } + + /** + * Get sections + * @return PHPWord_Section[] + */ + public function getSections() { + return $this->_sectionCollection; + } + + /** + * Get section count + * @return int + */ + private function _countSections() { + return count($this->_sectionCollection); + } + + /** + * Load a Template File + * + * @param string $strFilename + * @return PHPWord_Template + */ + public function loadTemplate($strFilename) { + if(file_exists($strFilename)) { + $template = new PHPWord_Template($strFilename); + return $template; + } else { + trigger_error('Template file '.$strFilename.' not found.', E_USER_ERROR); + } + } +} \ No newline at end of file diff --git a/src/PHPWord/Autoloader.php b/Classes/PHPWord/Autoloader.php similarity index 100% rename from src/PHPWord/Autoloader.php rename to Classes/PHPWord/Autoloader.php diff --git a/src/PHPWord/DocumentProperties.php b/Classes/PHPWord/DocumentProperties.php similarity index 100% rename from src/PHPWord/DocumentProperties.php rename to Classes/PHPWord/DocumentProperties.php diff --git a/Classes/PHPWord/Exception.php b/Classes/PHPWord/Exception.php new file mode 100644 index 00000000..1cf4f050 --- /dev/null +++ b/Classes/PHPWord/Exception.php @@ -0,0 +1,52 @@ +line = $line; + $e->file = $file; + throw $e; + } +} \ No newline at end of file diff --git a/src/PHPWord/HashTable.php b/Classes/PHPWord/HashTable.php similarity index 100% rename from src/PHPWord/HashTable.php rename to Classes/PHPWord/HashTable.php diff --git a/src/PHPWord/IOFactory.php b/Classes/PHPWord/IOFactory.php similarity index 100% rename from src/PHPWord/IOFactory.php rename to Classes/PHPWord/IOFactory.php diff --git a/src/PHPWord/Media.php b/Classes/PHPWord/Media.php similarity index 100% rename from src/PHPWord/Media.php rename to Classes/PHPWord/Media.php diff --git a/Classes/PHPWord/Section.php b/Classes/PHPWord/Section.php new file mode 100644 index 00000000..d5f5618d --- /dev/null +++ b/Classes/PHPWord/Section.php @@ -0,0 +1,397 @@ +_sectionCount = $sectionCount; + $this->_settings = new PHPWord_Section_Settings(); + + if(!is_null($settings) && is_array($settings)) { + foreach($settings as $key => $value) { + if(substr($key, 0, 1) != '_') { + $key = '_'.$key; + } + $this->_settings->setSettingValue($key, $value); + } + } + } + + /** + * Get Section Settings + * + * @return PHPWord_Section_Settings + */ + public function getSettings() { + return $this->_settings; + } + + /** + * Add a Text Element + * + * @param string $text + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return PHPWord_Section_Text + */ + public function addText($text, $styleFont = null, $styleParagraph = null) { + if(!PHPWord_Shared_String::IsUTF8($text)){ + $text = utf8_encode($text); + } + $text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph); + $this->_elementCollection[] = $text; + return $text; + } + + /** + * Add a Link Element + * + * @param string $linkSrc + * @param string $linkName + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return PHPWord_Section_Link + */ + public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) { + if(!PHPWord_Shared_String::IsUTF8($linkSrc)){ + $linkSrc = utf8_encode($linkSrc); + } + if(!is_null($linkName)) { + if(!PHPWord_Shared_String::IsUTF8($linkName)){ + $linkName = utf8_encode($linkName); + } + } + + $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont, $styleParagraph); + $rID = PHPWord_Media::addSectionLinkElement($linkSrc); + $link->setRelationId($rID); + + $this->_elementCollection[] = $link; + return $link; + } + + /** + * Add a TextBreak Element + * + * @param int $count + */ + public function addTextBreak($count = 1) { + for($i=1; $i<=$count; $i++) { + $this->_elementCollection[] = new PHPWord_Section_TextBreak(); + } + } + + /** + * Add a PageBreak Element + */ + public function addPageBreak() { + $this->_elementCollection[] = new PHPWord_Section_PageBreak(); + } + + /** + * Add a Table Element + * + * @param mixed $style + * @return PHPWord_Section_Table + */ + public function addTable($style = null) { + $table = new PHPWord_Section_Table('section', $this->_sectionCount, $style); + $this->_elementCollection[] = $table; + return $table; + } + + /** + * Add a ListItem Element + * + * @param string $text + * @param int $depth + * @param mixed $styleFont + * @param mixed $styleList + * @param mixed $styleParagraph + * @return PHPWord_Section_ListItem + */ + public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) { + if(!PHPWord_Shared_String::IsUTF8($text)){ + $text = utf8_encode($text); + } + $listItem = new PHPWord_Section_ListItem($text, $depth, $styleFont, $styleList, $styleParagraph); + $this->_elementCollection[] = $listItem; + return $listItem; + } + + /** + * Add a OLE-Object Element + * + * @param string $src + * @param mixed $style + * @return PHPWord_Section_Object + */ + public function addObject($src, $style = null) { + $object = new PHPWord_Section_Object($src, $style); + + if(!is_null($object->getSource())) { + $inf = pathinfo($src); + $ext = $inf['extension']; + if(strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { + $ext = substr($ext, 0, -1); + } + + $iconSrc = PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/'; + if(!file_exists($iconSrc.'_'.$ext.'.png')) { + $iconSrc = $iconSrc.'_default.png'; + } else { + $iconSrc .= '_'.$ext.'.png'; + } + + $rIDimg = PHPWord_Media::addSectionMediaElement($iconSrc, 'image'); + $data = PHPWord_Media::addSectionMediaElement($src, 'oleObject'); + $rID = $data[0]; + $objectId = $data[1]; + + $object->setRelationId($rID); + $object->setObjectId($objectId); + $object->setImageRelationId($rIDimg); + + $this->_elementCollection[] = $object; + return $object; + } else { + trigger_error('Source does not exist or unsupported object type.'); + } + } + + /** + * Add a Image Element + * + * @param string $src + * @param mixed $style + * @return PHPWord_Section_Image + */ + public function addImage($src, $style = null) { + $image = new PHPWord_Section_Image($src, $style); + + if(!is_null($image->getSource())) { + $rID = PHPWord_Media::addSectionMediaElement($src, 'image'); + $image->setRelationId($rID); + + $this->_elementCollection[] = $image; + return $image; + } else { + trigger_error('Source does not exist or unsupported image type.'); + } + } + + /** + * Add a by PHP created Image Element + * + * @param string $link + * @param mixed $style + * @return PHPWord_Section_MemoryImage + */ + public function addMemoryImage($link, $style = null) { + $memoryImage = new PHPWord_Section_MemoryImage($link, $style); + if(!is_null($memoryImage->getSource())) { + $rID = PHPWord_Media::addSectionMediaElement($link, 'image', $memoryImage); + $memoryImage->setRelationId($rID); + + $this->_elementCollection[] = $memoryImage; + return $memoryImage; + } else { + trigger_error('Unsupported image type.'); + } + } + + /** + * Add a Table-of-Contents Element + * + * @param mixed $styleFont + * @param mixed $styleTOC + * @return PHPWord_TOC + */ + public function addTOC($styleFont = null, $styleTOC = null) { + $toc = new PHPWord_TOC($styleFont, $styleTOC); + $this->_elementCollection[] = $toc; + return $toc; + } + + /** + * Add a Title Element + * + * @param string $text + * @param int $depth + * @return PHPWord_Section_Title + */ + public function addTitle($text, $depth = 1) { + if(!PHPWord_Shared_String::IsUTF8($text)){ + $text = utf8_encode($text); + } + $styles = PHPWord_Style::getStyles(); + if(array_key_exists('Heading_'.$depth, $styles)) { + $style = 'Heading'.$depth; + } else { + $style = null; + } + + $title = new PHPWord_Section_Title($text, $depth, $style); + + $data = PHPWord_TOC::addTitle($text, $depth); + $anchor = $data[0]; + $bookmarkId = $data[1]; + + $title->setAnchor($anchor); + $title->setBookmarkId($bookmarkId); + + $this->_elementCollection[] = $title; + return $title; + } + + /** + * Create a new TextRun + * + * @return PHPWord_Section_TextRun + */ + public function createTextRun($styleParagraph = null) { + $textRun = new PHPWord_Section_TextRun($styleParagraph); + $this->_elementCollection[] = $textRun; + return $textRun; + } + + /** + * Get all Elements + * + * @return array + */ + public function getElements() { + return $this->_elementCollection; + } + + /** + * Create a new Header + * + * @return PHPWord_Section_Header + */ + public function createHeader() { + $header = new PHPWord_Section_Header($this->_sectionCount); + $this->_headers[] = $header; + return $header; + } + + /** + * Get Headers + * + * @return array + */ + public function getHeaders() { + return $this->_headers; + } + + /** + * Is there a header for this section that is for the first page only? + * + * If any of the PHPWord_Section_Header instances have a type of + * PHPWord_Section_Header::FIRST then this method returns true. False + * otherwise. + * + * @return Boolean + */ + public function hasDifferentFirstPage() { + $value = array_filter($this->_headers, function(PHPWord_Section_Header &$header) { + return $header->getType() == PHPWord_Section_Header::FIRST; + }); + return count($value) > 0; + } + + /** + * Create a new Footer + * + * @return PHPWord_Section_Footer + */ + public function createFooter() { + $footer = new PHPWord_Section_Footer($this->_sectionCount); + $this->_footer = $footer; + return $footer; + } + + /** + * Get Footer + * + * @return PHPWord_Section_Footer + */ + public function getFooter() { + return $this->_footer; + } +} +?> \ No newline at end of file diff --git a/Classes/PHPWord/Section/Footer.php b/Classes/PHPWord/Section/Footer.php new file mode 100644 index 00000000..e54d1ea5 --- /dev/null +++ b/Classes/PHPWord/Section/Footer.php @@ -0,0 +1,205 @@ +_footerCount = $sectionCount; + } + + /** + * Add a Text Element + * + * @param string $text + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return PHPWord_Section_Text + */ + public function addText($text, $styleFont = null, $styleParagraph = null) { + if(!PHPWord_Shared_String::IsUTF8($text)){ + $text = utf8_encode($text); + } + $text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph); + $this->_elementCollection[] = $text; + return $text; + } + + /** + * Add a TextBreak Element + * + * @param int $count + */ + public function addTextBreak($count = 1) { + for($i=1; $i<=$count; $i++) { + $this->_elementCollection[] = new PHPWord_Section_TextBreak(); + } + } + + /** + * Create a new TextRun + * + * @return PHPWord_Section_TextRun + */ + public function createTextRun($styleParagraph = null) { + $textRun = new PHPWord_Section_TextRun($styleParagraph); + $this->_elementCollection[] = $textRun; + return $textRun; + } + + /** + * Add a Table Element + * + * @param mixed $style + * @return PHPWord_Section_Table + */ + public function addTable($style = null) { + $table = new PHPWord_Section_Table('footer', $this->_footerCount, $style); + $this->_elementCollection[] = $table; + return $table; + } + + /** + * Add a Image Element + * + * @param string $src + * @param mixed $style + * @return PHPWord_Section_Image + */ + public function addImage($src, $style = null) { + $image = new PHPWord_Section_Image($src, $style); + + if(!is_null($image->getSource())) { + $rID = PHPWord_Media::addFooterMediaElement($this->_footerCount, $src); + $image->setRelationId($rID); + + $this->_elementCollection[] = $image; + return $image; + } else { + trigger_error('Src does not exist or invalid image type.', E_USER_ERROR); + } + } + + /** + * Add a by PHP created Image Element + * + * @param string $link + * @param mixed $style + * @return PHPWord_Section_MemoryImage + */ + public function addMemoryImage($link, $style = null) { + $memoryImage = new PHPWord_Section_MemoryImage($link, $style); + if(!is_null($memoryImage->getSource())) { + $rID = PHPWord_Media::addFooterMediaElement($this->_footerCount, $link, $memoryImage); + $memoryImage->setRelationId($rID); + + $this->_elementCollection[] = $memoryImage; + return $memoryImage; + } else { + trigger_error('Unsupported image type.'); + } + } + + /** + * Add a PreserveText Element + * + * @param string $text + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return PHPWord_Section_Footer_PreserveText + */ + public function addPreserveText($text, $styleFont = null, $styleParagraph = null) { + if(!PHPWord_Shared_String::IsUTF8($text)){ + $text = utf8_encode($text); + } + $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph); + $this->_elementCollection[] = $ptext; + return $ptext; + } + + /** + * Get Footer Relation ID + */ + public function getRelationId() { + return $this->_rId; + } + + /** + * Set Footer Relation ID + * + * @param int $rId + */ + public function setRelationId($rId) { + $this->_rId = $rId; + } + + /** + * Get all Footer Elements + */ + public function getElements() { + return $this->_elementCollection; + } + + /** + * Get Footer Count + */ + public function getFooterCount() { + return $this->_footerCount; + } +} +?> \ No newline at end of file diff --git a/src/PHPWord/Section/Footer/PreserveText.php b/Classes/PHPWord/Section/Footer/PreserveText.php similarity index 100% rename from src/PHPWord/Section/Footer/PreserveText.php rename to Classes/PHPWord/Section/Footer/PreserveText.php diff --git a/Classes/PHPWord/Section/Header.php b/Classes/PHPWord/Section/Header.php new file mode 100644 index 00000000..65ad3971 --- /dev/null +++ b/Classes/PHPWord/Section/Header.php @@ -0,0 +1,284 @@ +_headerCount = $sectionCount; + } + + /** + * Add a Text Element + * + * @param string $text + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return PHPWord_Section_Text + */ + public function addText($text, $styleFont = null, $styleParagraph = null) { + if(!PHPWord_Shared_String::IsUTF8($text)){ + $text = utf8_encode($text); + } + $text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph); + $this->_elementCollection[] = $text; + return $text; + } + + /** + * Add a TextBreak Element + * + * @param int $count + */ + public function addTextBreak($count = 1) { + for($i=1; $i<=$count; $i++) { + $this->_elementCollection[] = new PHPWord_Section_TextBreak(); + } + } + + /** + * Create a new TextRun + * + * @return PHPWord_Section_TextRun + */ + public function createTextRun($styleParagraph = null) { + $textRun = new PHPWord_Section_TextRun($styleParagraph); + $this->_elementCollection[] = $textRun; + return $textRun; + } + + /** + * Add a Table Element + * + * @param mixed $style + * @return PHPWord_Section_Table + */ + public function addTable($style = null) { + $table = new PHPWord_Section_Table('header', $this->_headerCount, $style); + $this->_elementCollection[] = $table; + return $table; + } + + /** + * Add a Image Element + * + * @param string $src + * @param mixed $style + * @return PHPWord_Section_Image + */ + public function addImage($src, $style = null) { + $image = new PHPWord_Section_Image($src, $style); + + if(!is_null($image->getSource())) { + $rID = PHPWord_Media::addHeaderMediaElement($this->_headerCount, $src); + $image->setRelationId($rID); + + $this->_elementCollection[] = $image; + return $image; + } else { + trigger_error('Src does not exist or invalid image type.', E_USER_ERROR); + } + } + + /** + * Add a by PHP created Image Element + * + * @param string $link + * @param mixed $style + * @return PHPWord_Section_MemoryImage + */ + public function addMemoryImage($link, $style = null) { + $memoryImage = new PHPWord_Section_MemoryImage($link, $style); + if(!is_null($memoryImage->getSource())) { + $rID = PHPWord_Media::addHeaderMediaElement($this->_headerCount, $link, $memoryImage); + $memoryImage->setRelationId($rID); + + $this->_elementCollection[] = $memoryImage; + return $memoryImage; + } else { + trigger_error('Unsupported image type.'); + } + } + + /** + * Add a PreserveText Element + * + * @param string $text + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return PHPWord_Section_Footer_PreserveText + */ + public function addPreserveText($text, $styleFont = null, $styleParagraph = null) { + if(!PHPWord_Shared_String::IsUTF8($text)){ + $text = utf8_encode($text); + } + $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph); + $this->_elementCollection[] = $ptext; + return $ptext; + } + + /** + * Add a Watermark Element + * + * @param string $src + * @param mixed $style + * @return PHPWord_Section_Image + */ + public function addWatermark($src, $style = null) { + $image = new PHPWord_Section_Image($src, $style, true); + + if(!is_null($image->getSource())) { + $rID = PHPWord_Media::addHeaderMediaElement($this->_headerCount, $src); + $image->setRelationId($rID); + + $this->_elementCollection[] = $image; + return $image; + } else { + trigger_error('Src does not exist or invalid image type.', E_USER_ERROR); + } + } + + /** + * Get Header Relation ID + */ + public function getRelationId() { + return $this->_rId; + } + + /** + * Set Header Relation ID + * + * @param int $rId + */ + public function setRelationId($rId) { + $this->_rId = $rId; + } + + /** + * Get all Header Elements + */ + public function getElements() { + return $this->_elementCollection; + } + + /** + * Get Header Count + */ + public function getHeaderCount() { + return $this->_headerCount; + } + + /** + * Get Header Type + */ + public function getType() { + return $this->_type; + } + + /** + * Reset back to default + */ + public function resetType() { + return $this->_type = PHPWord_Section_Header::AUTO; + } + + /** + * First page only header + */ + public function firstPage() { + return $this->_type = PHPWord_Section_Header::FIRST; + } + + /** + * Even numbered Pages only + */ + public function evenPage() { + return $this->_type = PHPWord_Section_Header::EVEN; + } + +} +?> diff --git a/src/PHPWord/Section/Image.php b/Classes/PHPWord/Section/Image.php similarity index 100% rename from src/PHPWord/Section/Image.php rename to Classes/PHPWord/Section/Image.php diff --git a/src/PHPWord/Section/Link.php b/Classes/PHPWord/Section/Link.php similarity index 100% rename from src/PHPWord/Section/Link.php rename to Classes/PHPWord/Section/Link.php diff --git a/src/PHPWord/Section/ListItem.php b/Classes/PHPWord/Section/ListItem.php similarity index 100% rename from src/PHPWord/Section/ListItem.php rename to Classes/PHPWord/Section/ListItem.php diff --git a/src/PHPWord/Section/MemoryImage.php b/Classes/PHPWord/Section/MemoryImage.php similarity index 100% rename from src/PHPWord/Section/MemoryImage.php rename to Classes/PHPWord/Section/MemoryImage.php diff --git a/src/PHPWord/Section/Object.php b/Classes/PHPWord/Section/Object.php similarity index 100% rename from src/PHPWord/Section/Object.php rename to Classes/PHPWord/Section/Object.php diff --git a/src/PHPWord/Section/PageBreak.php b/Classes/PHPWord/Section/PageBreak.php similarity index 100% rename from src/PHPWord/Section/PageBreak.php rename to Classes/PHPWord/Section/PageBreak.php diff --git a/src/PHPWord/Section/Settings.php b/Classes/PHPWord/Section/Settings.php similarity index 100% rename from src/PHPWord/Section/Settings.php rename to Classes/PHPWord/Section/Settings.php diff --git a/src/PHPWord/Section/Table.php b/Classes/PHPWord/Section/Table.php similarity index 100% rename from src/PHPWord/Section/Table.php rename to Classes/PHPWord/Section/Table.php diff --git a/Classes/PHPWord/Section/Table/Cell.php b/Classes/PHPWord/Section/Table/Cell.php new file mode 100644 index 00000000..989042fe --- /dev/null +++ b/Classes/PHPWord/Section/Table/Cell.php @@ -0,0 +1,329 @@ +_insideOf = $insideOf; + $this->_pCount = $pCount; + $this->_width = $width; + + if(!is_null($style)) { + if(is_array($style)) { + $this->_style = new PHPWord_Style_Cell(); + + foreach($style as $key => $value) { + if(substr($key, 0, 1) != '_') { + $key = '_'.$key; + } + $this->_style->setStyleValue($key, $value); + } + } else { + $this->_style = $style; + } + } + } + + /** + * Add a Text Element + * + * @param string $text + * @param mixed $style + * @return PHPWord_Section_Text + */ + public function addText($text, $styleFont = null, $styleParagraph = null) { + if(!PHPWord_Shared_String::IsUTF8($text)){ + $text = utf8_encode($text); + } + $text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph); + $this->_elementCollection[] = $text; + return $text; + } + + /** + * Add a Link Element + * + * @param string $linkSrc + * @param string $linkName + * @param mixed $style + * @return PHPWord_Section_Link + */ + public function addLink($linkSrc, $linkName = null, $style = null) { + if($this->_insideOf == 'section') { + if(!PHPWord_Shared_String::IsUTF8($linkSrc)){ + $linkSrc = utf8_encode($linkSrc); + } + if(!is_null($linkName)) { + if(!PHPWord_Shared_String::IsUTF8($linkName)){ + $linkName = utf8_encode($linkName); + } + } + + $link = new PHPWord_Section_Link($linkSrc, $linkName, $style); + $rID = PHPWord_Media::addSectionLinkElement($linkSrc); + $link->setRelationId($rID); + + $this->_elementCollection[] = $link; + return $link; + } else { + trigger_error('Unsupported Link header / footer reference'); + return false; + } + } + + /** + * Add a TextBreak Element + * + * @param int $count + */ + public function addTextBreak() { + $this->_elementCollection[] = new PHPWord_Section_TextBreak(); + } + + /** + * Add a ListItem Element + * + * @param string $text + * @param int $depth + * @param mixed $styleText + * @param mixed $styleList + * @return PHPWord_Section_ListItem + */ + public function addListItem($text, $depth = 0, $styleText = null, $styleList = null) { + if(!PHPWord_Shared_String::IsUTF8($text)){ + $text = utf8_encode($text); + } + $listItem = new PHPWord_Section_ListItem($text, $depth, $styleText, $styleList); + $this->_elementCollection[] = $listItem; + return $listItem; + } + + /** + * Add a Image Element + * + * @param string $src + * @param mixed $style + * @return PHPWord_Section_Image + */ + public function addImage($src, $style = null) { + $image = new PHPWord_Section_Image($src, $style); + + if(!is_null($image->getSource())) { + if($this->_insideOf == 'section') { + $rID = PHPWord_Media::addSectionMediaElement($src, 'image'); + } elseif($this->_insideOf == 'header') { + $rID = PHPWord_Media::addHeaderMediaElement($this->_pCount, $src); + } elseif($this->_insideOf == 'footer') { + $rID = PHPWord_Media::addFooterMediaElement($this->_pCount, $src); + } + $image->setRelationId($rID); + + $this->_elementCollection[] = $image; + return $image; + } else { + trigger_error('Source does not exist or unsupported image type.'); + } + } + + /** + * Add a by PHP created Image Element + * + * @param string $link + * @param mixed $style + * @return PHPWord_Section_MemoryImage + */ + public function addMemoryImage($link, $style = null) { + $memoryImage = new PHPWord_Section_MemoryImage($link, $style); + if(!is_null($memoryImage->getSource())) { + if($this->_insideOf == 'section') { + $rID = PHPWord_Media::addSectionMediaElement($link, 'image', $memoryImage); + } elseif($this->_insideOf == 'header') { + $rID = PHPWord_Media::addHeaderMediaElement($this->_pCount, $link, $memoryImage); + } elseif($this->_insideOf == 'footer') { + $rID = PHPWord_Media::addFooterMediaElement($this->_pCount, $link, $memoryImage); + } + $memoryImage->setRelationId($rID); + + $this->_elementCollection[] = $memoryImage; + return $memoryImage; + } else { + trigger_error('Unsupported image type.'); + } + } + + /** + * Add a OLE-Object Element + * + * @param string $src + * @param mixed $style + * @return PHPWord_Section_Object + */ + public function addObject($src, $style = null) { + $object = new PHPWord_Section_Object($src, $style); + + if(!is_null($object->getSource())) { + $inf = pathinfo($src); + $ext = $inf['extension']; + if(strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { + $ext = substr($ext, 0, -1); + } + + $iconSrc = PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/'; + if(!file_exists($iconSrc.'_'.$ext.'.png')) { + $iconSrc = $iconSrc.'_default.png'; + } else { + $iconSrc .= '_'.$ext.'.png'; + } + + $rIDimg = PHPWord_Media::addSectionMediaElement($iconSrc, 'image'); + $data = PHPWord_Media::addSectionMediaElement($src, 'oleObject'); + $rID = $data[0]; + $objectId = $data[1]; + + $object->setRelationId($rID); + $object->setObjectId($objectId); + $object->setImageRelationId($rIDimg); + + $this->_elementCollection[] = $object; + return $object; + } else { + trigger_error('Source does not exist or unsupported object type.'); + } + } + + /** + * Add a PreserveText Element + * + * @param string $text + * @param mixed $styleFont + * @param mixed $styleParagraph + * @return PHPWord_Section_Footer_PreserveText + */ + public function addPreserveText($text, $styleFont = null, $styleParagraph = null) { + if($this->_insideOf == 'footer' || $this->_insideOf == 'header') { + if(!PHPWord_Shared_String::IsUTF8($text)){ + $text = utf8_encode($text); + } + $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph); + $this->_elementCollection[] = $ptext; + return $ptext; + } else { + trigger_error('addPreserveText only supported in footer/header.'); + } + } + + /** + * Create a new TextRun + * + * @return PHPWord_Section_TextRun + */ + public function createTextRun($styleParagraph = null) { + $textRun = new PHPWord_Section_TextRun($styleParagraph); + $this->_elementCollection[] = $textRun; + return $textRun; + } + + /** + * Get all Elements + * + * @return array + */ + public function getElements() { + return $this->_elementCollection; + } + + /** + * Get Cell Style + * + * @return PHPWord_Style_Cell + */ + public function getStyle() { + return $this->_style; + } + + /** + * Get Cell width + * + * @return int + */ + public function getWidth() { + return $this->_width; + } +} +?> diff --git a/src/PHPWord/Section/Text.php b/Classes/PHPWord/Section/Text.php similarity index 100% rename from src/PHPWord/Section/Text.php rename to Classes/PHPWord/Section/Text.php diff --git a/src/PHPWord/Section/TextBreak.php b/Classes/PHPWord/Section/TextBreak.php similarity index 100% rename from src/PHPWord/Section/TextBreak.php rename to Classes/PHPWord/Section/TextBreak.php diff --git a/Classes/PHPWord/Section/TextRun.php b/Classes/PHPWord/Section/TextRun.php new file mode 100644 index 00000000..6a9bfaca --- /dev/null +++ b/Classes/PHPWord/Section/TextRun.php @@ -0,0 +1,131 @@ +_elementCollection = array(); + + // Set paragraph style + if(is_array($styleParagraph)) { + $this->_styleParagraph = new PHPWord_Style_Paragraph(); + + foreach($styleParagraph as $key => $value) { + if(substr($key, 0, 1) != '_') { + $key = '_'.$key; + } + $this->_styleParagraph->setStyleValue($key, $value); + } + } else { + $this->_styleParagraph = $styleParagraph; + } + } + + + /** + * Add a Text Element + * + * @var string $text + * @var mixed $styleFont + * @return PHPWord_Section_Text + */ + public function addText($text = null, $styleFont = null) { + if(!PHPWord_Shared_String::IsUTF8($text)){ + $text = utf8_encode($text); + } + $text = new PHPWord_Section_Text($text, $styleFont); + $this->_elementCollection[] = $text; + return $text; + } + + /** + * Add a Link Element + * + * @param string $linkSrc + * @param string $linkName + * @param mixed $styleFont + * @return PHPWord_Section_Link + */ + public function addLink($linkSrc, $linkName = null, $styleFont = null) { + $linkSrc = utf8_encode($linkSrc); + if(!is_null($linkName)) { + $linkName = utf8_encode($linkName); + } + + $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont); + $rID = PHPWord_Media::addSectionLinkElement($linkSrc); + $link->setRelationId($rID); + + $this->_elementCollection[] = $link; + return $link; + } + + /** + * Get TextRun content + * + * @return string + */ + public function getElements() { + return $this->_elementCollection; + } + + /** + * Get Paragraph style + * + * @return PHPWord_Style_Paragraph + */ + public function getParagraphStyle() { + return $this->_styleParagraph; + } +} +?> \ No newline at end of file diff --git a/src/PHPWord/Section/Title.php b/Classes/PHPWord/Section/Title.php similarity index 100% rename from src/PHPWord/Section/Title.php rename to Classes/PHPWord/Section/Title.php diff --git a/src/PHPWord/Shared/Drawing.php b/Classes/PHPWord/Shared/Drawing.php similarity index 100% rename from src/PHPWord/Shared/Drawing.php rename to Classes/PHPWord/Shared/Drawing.php diff --git a/src/PHPWord/Shared/File.php b/Classes/PHPWord/Shared/File.php similarity index 100% rename from src/PHPWord/Shared/File.php rename to Classes/PHPWord/Shared/File.php diff --git a/src/PHPWord/Shared/Font.php b/Classes/PHPWord/Shared/Font.php similarity index 100% rename from src/PHPWord/Shared/Font.php rename to Classes/PHPWord/Shared/Font.php diff --git a/Classes/PHPWord/Shared/String.php b/Classes/PHPWord/Shared/String.php new file mode 100644 index 00000000..3dcdc80e --- /dev/null +++ b/Classes/PHPWord/Shared/String.php @@ -0,0 +1,263 @@ +) + * element or in the shared string element. + * + * @param string $value Value to unescape + * @return string + */ + public static function ControlCharacterOOXML2PHP($value = '') { + if(empty(self::$_controlCharacters)) { + self::_buildControlCharacters(); + } + + return str_replace( array_keys(self::$_controlCharacters), array_values(self::$_controlCharacters), $value ); + } + + /** + * Convert from PHP control character to OpenXML escaped control character + * + * Excel 2007 team: + * ---------------- + * That's correct, control characters are stored directly in the shared-strings table. + * We do encode characters that cannot be represented in XML using the following escape sequence: + * _xHHHH_ where H represents a hexadecimal character in the character's value... + * So you could end up with something like _x0008_ in a string (either in a cell value () + * element or in the shared string element. + * + * @param string $value Value to escape + * @return string + */ + public static function ControlCharacterPHP2OOXML($value = '') { + if(empty(self::$_controlCharacters)) { + self::_buildControlCharacters(); + } + + return str_replace( array_values(self::$_controlCharacters), array_keys(self::$_controlCharacters), $value ); + } + + /** + * Check if a string contains UTF-8 data + * + * @param string $value + * @return boolean + */ + public static function IsUTF8($value = '') { + return $value === '' || preg_match('/^./su', $value) === 1; + } + + /** + * Formats a numeric value as a string for output in various output writers + * + * @param mixed $value + * @return string + */ + public static function FormatNumber($value) { + return number_format($value, 2, '.', ''); + } + + /** + * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length) + * Writes the string using uncompressed notation, no rich text, no Asian phonetics + * If mbstring extension is not available, ASCII is assumed, and compressed notation is used + * although this will give wrong results for non-ASCII strings + * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3 + * + * @param string $value UTF-8 encoded string + * @return string + */ + public static function UTF8toBIFF8UnicodeShort($value) + { + // character count + $ln = self::CountCharacters($value, 'UTF-8'); + + // option flags + $opt = (self::getIsMbstringEnabled() || self::getIsIconvEnabled()) ? + 0x0001 : 0x0000; + + // characters + $chars = self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8'); + + $data = pack('CC', $ln, $opt) . $chars; + return $data; + } + + /** + * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length) + * Writes the string using uncompressed notation, no rich text, no Asian phonetics + * If mbstring extension is not available, ASCII is assumed, and compressed notation is used + * although this will give wrong results for non-ASCII strings + * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3 + * + * @param string $value UTF-8 encoded string + * @return string + */ + public static function UTF8toBIFF8UnicodeLong($value) + { + // character count + $ln = self::CountCharacters($value, 'UTF-8'); + + // option flags + $opt = (self::getIsMbstringEnabled() || self::getIsIconvEnabled()) ? + 0x0001 : 0x0000; + + // characters + $chars = self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8'); + + $data = pack('vC', $ln, $opt) . $chars; + return $data; + } + + /** + * Convert string from one encoding to another. First try mbstring, then iconv, or no convertion + * + * @param string $value + * @param string $to Encoding to convert to, e.g. 'UTF-8' + * @param string $from Encoding to convert from, e.g. 'UTF-16LE' + * @return string + */ + public static function ConvertEncoding($value, $to, $from) + { + if (self::getIsMbstringEnabled()) { + $value = mb_convert_encoding($value, $to, $from); + return $value; + } + + if (self::getIsIconvEnabled()) { + $value = iconv($from, $to, $value); + return $value; + } + + // else, no conversion + return $value; + } + + /** + * Get character count. First try mbstring, then iconv, finally strlen + * + * @param string $value + * @param string $enc Encoding + * @return int Character count + */ + public static function CountCharacters($value, $enc = 'UTF-8') + { + if (self::getIsMbstringEnabled()) { + $count = mb_strlen($value, $enc); + return $count; + } + + if (self::getIsIconvEnabled()) { + $count = iconv_strlen($value, $enc); + return $count; + } + + // else strlen + $count = strlen($value); + return $count; + } + +} diff --git a/src/PHPWord/Shared/XMLWriter.php b/Classes/PHPWord/Shared/XMLWriter.php similarity index 100% rename from src/PHPWord/Shared/XMLWriter.php rename to Classes/PHPWord/Shared/XMLWriter.php diff --git a/src/PHPWord/Shared/ZipStreamWrapper.php b/Classes/PHPWord/Shared/ZipStreamWrapper.php similarity index 100% rename from src/PHPWord/Shared/ZipStreamWrapper.php rename to Classes/PHPWord/Shared/ZipStreamWrapper.php diff --git a/src/PHPWord/Style.php b/Classes/PHPWord/Style.php similarity index 100% rename from src/PHPWord/Style.php rename to Classes/PHPWord/Style.php diff --git a/Classes/PHPWord/Style/Cell.php b/Classes/PHPWord/Style/Cell.php new file mode 100644 index 00000000..090ff7b4 --- /dev/null +++ b/Classes/PHPWord/Style/Cell.php @@ -0,0 +1,318 @@ +_valign = null; + $this->_textDirection = null; + $this->_bgColor = null; + $this->_borderTopSize = null; + $this->_borderTopColor = null; + $this->_borderLeftSize = null; + $this->_borderLeftColor = null; + $this->_borderRightSize = null; + $this->_borderRightColor = null; + $this->_borderBottomSize = null; + $this->_borderBottomColor = null; + $this->_defaultBorderColor = '000000'; + } + + /** + * Set style value + * + * @var string $key + * @var mixed $value + */ + public function setStyleValue($key, $value) { + if($key == '_borderSize') { + $this->setBorderSize($value); + } elseif($key == '_borderColor') { + $this->setBorderColor($value); + } else { + $this->$key = $value; + } + } + + public function getVAlign() { + return $this->_valign; + } + + public function setVAlign($pValue = null) { + $this->_valign = $pValue; + } + + public function getTextDirection() { + return $this->_textDirection; + } + + public function setTextDirection($pValue = null) { + $this->_textDirection = $pValue; + } + + public function getBgColor() { + return $this->_bgColor; + } + + public function setBgColor($pValue = null) { + $this->_bgColor = $pValue; + } + + public function setHeight($pValue = null) { + $this->_height = $pValue; + } + + public function setBorderSize($pValue = null) { + $this->_borderTopSize = $pValue; + $this->_borderLeftSize = $pValue; + $this->_borderRightSize = $pValue; + $this->_borderBottomSize = $pValue; + } + + public function getBorderSize() { + $t = $this->getBorderTopSize(); + $l = $this->getBorderLeftSize(); + $r = $this->getBorderRightSize(); + $b = $this->getBorderBottomSize(); + + return array($t, $l, $r, $b); + } + + public function setBorderColor($pValue = null) { + $this->_borderTopColor = $pValue; + $this->_borderLeftColor = $pValue; + $this->_borderRightColor = $pValue; + $this->_borderBottomColor = $pValue; + } + + public function getBorderColor() { + $t = $this->getBorderTopColor(); + $l = $this->getBorderLeftColor(); + $r = $this->getBorderRightColor(); + $b = $this->getBorderBottomColor(); + + return array($t, $l, $r, $b); + } + + public function setBorderTopSize($pValue = null) { + $this->_borderTopSize = $pValue; + } + + public function getBorderTopSize() { + return $this->_borderTopSize; + } + + public function setBorderTopColor($pValue = null) { + $this->_borderTopColor = $pValue; + } + + public function getBorderTopColor() { + return $this->_borderTopColor; + } + + public function setBorderLeftSize($pValue = null) { + $this->_borderLeftSize = $pValue; + } + + public function getBorderLeftSize() { + return $this->_borderLeftSize; + } + + public function setBorderLeftColor($pValue = null) { + $this->_borderLeftColor = $pValue; + } + + public function getBorderLeftColor() { + return $this->_borderLeftColor; + } + + public function setBorderRightSize($pValue = null) { + $this->_borderRightSize = $pValue; + } + + public function getBorderRightSize() { + return $this->_borderRightSize; + } + + public function setBorderRightColor($pValue = null) { + $this->_borderRightColor = $pValue; + } + + public function getBorderRightColor() { + return $this->_borderRightColor; + } + + + public function setBorderBottomSize($pValue = null) { + $this->_borderBottomSize = $pValue; + } + + public function getBorderBottomSize() { + return $this->_borderBottomSize; + } + + public function setBorderBottomColor($pValue = null) { + $this->_borderBottomColor = $pValue; + } + + public function getBorderBottomColor() { + return $this->_borderBottomColor; + } + + public function getDefaultBorderColor() { + return $this->_defaultBorderColor; + } + + public function setGridSpan($pValue = null) { + $this->_gridSpan = $pValue; + } + + public function getGridSpan() { + return $this->_gridSpan; + } + + public function setVMerge($pValue = null) { + $this->_vMerge = $pValue; + } + + public function getVMerge() { + return $this->_vMerge; + } +} +?> \ No newline at end of file diff --git a/src/PHPWord/Style/Font.php b/Classes/PHPWord/Style/Font.php similarity index 100% rename from src/PHPWord/Style/Font.php rename to Classes/PHPWord/Style/Font.php diff --git a/src/PHPWord/Style/Image.php b/Classes/PHPWord/Style/Image.php similarity index 100% rename from src/PHPWord/Style/Image.php rename to Classes/PHPWord/Style/Image.php diff --git a/src/PHPWord/Style/ListItem.php b/Classes/PHPWord/Style/ListItem.php similarity index 100% rename from src/PHPWord/Style/ListItem.php rename to Classes/PHPWord/Style/ListItem.php diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php new file mode 100644 index 00000000..937d8650 --- /dev/null +++ b/Classes/PHPWord/Style/Paragraph.php @@ -0,0 +1,225 @@ +_align = null; + $this->_spaceBefore = null; + $this->_spaceAfter = null; + $this->_spacing = null; + $this->_tabs = null; + $this->_indent = null; + } + + /** + * Set Style value + * + * @param string $key + * @param mixed $value + */ + public function setStyleValue($key, $value) { + if($key == '_indent') { + $value = (int)$value * 720; // 720 twips per indent + } + if($key == '_spacing') { + $value += 240; // because line height of 1 matches 240 twips + } + if($key === '_tabs') { + $value = new PHPWord_Style_Tabs($value); + } + $this->$key = $value; + } + + /** + * Get Paragraph Alignment + * + * @return string + */ + public function getAlign() { + return $this->_align; + } + + /** + * Set Paragraph Alignment + * + * @param string $pValue + * @return PHPWord_Style_Paragraph + */ + public function setAlign($pValue = null) { + if(strtolower($pValue) == 'justify') { + // justify becames both + $pValue = 'both'; + } + $this->_align = $pValue; + return $this; + } + + /** + * Get Space before Paragraph + * + * @return string + */ + public function getSpaceBefore() { + return $this->_spaceBefore; + } + + /** + * Set Space before Paragraph + * + * @param int $pValue + * @return PHPWord_Style_Paragraph + */ + public function setSpaceBefore($pValue = null) { + $this->_spaceBefore = $pValue; + return $this; + } + + /** + * Get Space after Paragraph + * + * @return string + */ + public function getSpaceAfter() { + return $this->_spaceAfter; + } + + /** + * Set Space after Paragraph + * + * @param int $pValue + * @return PHPWord_Style_Paragraph + */ + public function setSpaceAfter($pValue = null) { + $this->_spaceAfter = $pValue; + return $this; + } + + /** + * Get Spacing between breaks + * + * @return int + */ + public function getSpacing() { + return $this->_spacing; + } + + /** + * Set Spacing between breaks + * + * @param int $pValue + * @return PHPWord_Style_Paragraph + */ + public function setSpacing($pValue = null) { + $this->_spacing = $pValue; + return $this; + } + + /** + * Get indentation + * + * @return int + */ + public function getIndent() { + return $this->_indent; + } + + /** + * Set indentation + * + * @param int $pValue + * @return PHPWord_Style_Paragraph + */ + public function setIndent($pValue = null) { + $this->_indent = $pValue; + return $this; + } + + /** + * Get tabs + * + * @return PHPWord_Style_Tabs + */ + public function getTabs() { + return $this->_tabs; + } +} +?> \ No newline at end of file diff --git a/src/PHPWord/Style/TOC.php b/Classes/PHPWord/Style/TOC.php similarity index 100% rename from src/PHPWord/Style/TOC.php rename to Classes/PHPWord/Style/TOC.php diff --git a/Classes/PHPWord/Style/Tab.php b/Classes/PHPWord/Style/Tab.php new file mode 100644 index 00000000..cd0882c1 --- /dev/null +++ b/Classes/PHPWord/Style/Tab.php @@ -0,0 +1,147 @@ +_val = (self::isStopType($val)) ? $val : 'clear'; + + // Default to 0 if the position is non-numeric + $this->_position = (is_numeric($position)) ? intval($position) : 0; + + // Default to NULL if no tab leader + $this->_leader = (self::isLeaderType($leader)) ? $leader : NULL; + } + + /** + * Creates the XML DOM for the instance of PHPWord_Style_Tab. + * + * @param PHPWord_Shared_XMLWriter $objWriter + */ + public function toXml(PHPWord_Shared_XMLWriter &$objWriter = NULL) { + if(isset($objWriter)) { + $objWriter->startElement("w:tab"); + $objWriter->writeAttribute("w:val", $this->_val); + if(!is_null($this->_leader)) { + $objWriter->writeAttribute("w:leader", $this->_leader); + } + $objWriter->writeAttribute("w:pos", $this->_position); + $objWriter->endElement(); + } + } + + /** + * Test if attribute is a valid stop type. + * + * @param string $attribute + * @return bool True if it is; false otherwise. + */ + private static function isStopType($attribute) { + return in_array($attribute, self::$_possibleStopTypes); + } + + /** + * Test if attribute is a valid leader type. + * + * @param string $attribute + * @return bool True if it is; false otherwise. + */ + private static function isLeaderType($attribute) { + return in_array($attribute, self::$_possibleLeaders); + } +} +?> \ No newline at end of file diff --git a/src/PHPWord/Style/Table.php b/Classes/PHPWord/Style/Table.php similarity index 100% rename from src/PHPWord/Style/Table.php rename to Classes/PHPWord/Style/Table.php diff --git a/src/PHPWord/Style/TableFull.php b/Classes/PHPWord/Style/TableFull.php similarity index 100% rename from src/PHPWord/Style/TableFull.php rename to Classes/PHPWord/Style/TableFull.php diff --git a/Classes/PHPWord/Style/Tabs.php b/Classes/PHPWord/Style/Tabs.php new file mode 100644 index 00000000..096c0b6f --- /dev/null +++ b/Classes/PHPWord/Style/Tabs.php @@ -0,0 +1,67 @@ +_tabs = $tabs; + } + + /** + * + * @param PHPWord_Shared_XMLWriter $objWriter + */ + public function toXml(PHPWord_Shared_XMLWriter &$objWriter = NULL) { + if(isset($objWriter)) { + $objWriter->startElement("w:tabs"); + foreach ($this->_tabs as &$tab) { + $tab->toXml($objWriter); + } + $objWriter->endElement(); + } + } +} +?> \ No newline at end of file diff --git a/src/PHPWord/TOC.php b/Classes/PHPWord/TOC.php similarity index 100% rename from src/PHPWord/TOC.php rename to Classes/PHPWord/TOC.php diff --git a/src/PHPWord/Template.php b/Classes/PHPWord/Template.php similarity index 50% rename from src/PHPWord/Template.php rename to Classes/PHPWord/Template.php index 37044312..1d2765d0 100644 --- a/src/PHPWord/Template.php +++ b/Classes/PHPWord/Template.php @@ -2,7 +2,7 @@ /** * PHPWord * - * Copyright (c) 2013 PHPWord + * Copyright (c) 2011 PHPWord * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,73 +20,90 @@ * * @category PHPWord * @package PHPWord - * @copyright Copyright (c) 2013 PHPWord (http://www.codeplex.com/PHPWord) + * @copyright Copyright (c) 010 PHPWord * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL - * @version 0.7.0 + * @version Beta 0.6.3, 08.07.2011 */ + /** - * Class PHPWord_Template + * PHPWord_DocumentProperties + * + * @category PHPWord + * @package PHPWord + * @copyright Copyright (c) 2009 - 2011 PHPWord (http://www.codeplex.com/PHPWord) */ -class PHPWord_Template -{ - +class PHPWord_Template { + /** * ZipArchive - * + * * @var ZipArchive */ private $_objZip; - + /** * Temporary Filename - * + * * @var string */ private $_tempFileName; - + /** * Document XML - * + * * @var string */ private $_documentXML; - - + + /** * Create a new Template Object - * + * * @param string $strFilename */ - public function __construct($strFilename) - { - $path = dirname($strFilename); - $this->_tempFileName = $path . DIRECTORY_SEPARATOR . time() . '.docx'; + public function __construct($strFilename) { + $this->_tempFileName = tempnam(sys_get_temp_dir(), ''); + if ($this->_tempFileName !== false) { + // Copy the source File to the temp File + if(!copy($strFilename, $this->_tempFileName)){ + throw new PHPWord_Exception('Could not copy the template from '.$strFilename.' to '.$this->_tempFileName.'.'); + } - copy($strFilename, $this->_tempFileName); // Copy the source File to the temp File + $this->_objZip = new ZipArchive(); + $this->_objZip->open($this->_tempFileName); - $this->_objZip = new ZipArchive(); - $this->_objZip->open($this->_tempFileName); - - $this->_documentXML = $this->_objZip->getFromName('word/document.xml'); + $this->_documentXML = $this->_objZip->getFromName('word/document.xml'); + } else { + throw new PHPWord_Exception('Could not create temporary file with unique name in the default temporary directory.'); + } } - + /** * Set a Template value - * + * * @param mixed $search * @param mixed $replace */ - public function setValue($search, $replace) - { - if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') { - $search = '${' . $search . '}'; + public function setValue($search, $replace) { + $pattern = '|\$\{([^\}]+)\}|U'; + preg_match_all($pattern, $this->_documentXML, $matches); + foreach ($matches[0] as $value) { + $valueCleaned = preg_replace('/<[^>]+>/', '', $value); + $valueCleaned = preg_replace('/<\/[^>]+>/', '', $valueCleaned); + $this->_documentXML = str_replace($value, $valueCleaned, $this->_documentXML); } - if (!is_array($replace)) { - $replace = utf8_encode($replace); + if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') { + $search = '${'.$search.'}'; } - + + if(!is_array($replace)) { + if(!PHPWord_Shared_String::IsUTF8($replace)){ + $replace = utf8_encode($replace); + } + } + $this->_documentXML = str_replace($search, $replace, $this->_documentXML); } @@ -101,22 +118,22 @@ class PHPWord_Template /** * Save Template - * + * * @param string $strFilename */ - public function save($strFilename) - { - if (file_exists($strFilename)) { + public function save($strFilename) { + if(file_exists($strFilename)) { unlink($strFilename); } - + $this->_objZip->addFromString('word/document.xml', $this->_documentXML); - + // Close zip file - if ($this->_objZip->close() === false) { + if($this->_objZip->close() === false) { throw new Exception('Could not close zip file.'); } - + rename($this->_tempFileName, $strFilename); } } +?> \ No newline at end of file diff --git a/src/PHPWord/Writer/IWriter.php b/Classes/PHPWord/Writer/IWriter.php similarity index 100% rename from src/PHPWord/Writer/IWriter.php rename to Classes/PHPWord/Writer/IWriter.php diff --git a/src/PHPWord/Writer/ODText.php b/Classes/PHPWord/Writer/ODText.php similarity index 100% rename from src/PHPWord/Writer/ODText.php rename to Classes/PHPWord/Writer/ODText.php diff --git a/src/PHPWord/Writer/ODText/Content.php b/Classes/PHPWord/Writer/ODText/Content.php similarity index 100% rename from src/PHPWord/Writer/ODText/Content.php rename to Classes/PHPWord/Writer/ODText/Content.php diff --git a/src/PHPWord/Writer/ODText/Manifest.php b/Classes/PHPWord/Writer/ODText/Manifest.php similarity index 100% rename from src/PHPWord/Writer/ODText/Manifest.php rename to Classes/PHPWord/Writer/ODText/Manifest.php diff --git a/src/PHPWord/Writer/ODText/Meta.php b/Classes/PHPWord/Writer/ODText/Meta.php similarity index 100% rename from src/PHPWord/Writer/ODText/Meta.php rename to Classes/PHPWord/Writer/ODText/Meta.php diff --git a/src/PHPWord/Writer/ODText/Mimetype.php b/Classes/PHPWord/Writer/ODText/Mimetype.php similarity index 100% rename from src/PHPWord/Writer/ODText/Mimetype.php rename to Classes/PHPWord/Writer/ODText/Mimetype.php diff --git a/src/PHPWord/Writer/ODText/Styles.php b/Classes/PHPWord/Writer/ODText/Styles.php similarity index 100% rename from src/PHPWord/Writer/ODText/Styles.php rename to Classes/PHPWord/Writer/ODText/Styles.php diff --git a/src/PHPWord/Writer/ODText/WriterPart.php b/Classes/PHPWord/Writer/ODText/WriterPart.php similarity index 100% rename from src/PHPWord/Writer/ODText/WriterPart.php rename to Classes/PHPWord/Writer/ODText/WriterPart.php diff --git a/src/PHPWord/Writer/RTF.php b/Classes/PHPWord/Writer/RTF.php similarity index 100% rename from src/PHPWord/Writer/RTF.php rename to Classes/PHPWord/Writer/RTF.php diff --git a/Classes/PHPWord/Writer/Word2007.php b/Classes/PHPWord/Writer/Word2007.php new file mode 100644 index 00000000..c9bf9870 --- /dev/null +++ b/Classes/PHPWord/Writer/Word2007.php @@ -0,0 +1,240 @@ +_document = $PHPWord; + + $this->_diskCachingDirectory = './'; + + $this->_writerParts['contenttypes'] = new PHPWord_Writer_Word2007_ContentTypes(); + $this->_writerParts['rels'] = new PHPWord_Writer_Word2007_Rels(); + $this->_writerParts['docprops'] = new PHPWord_Writer_Word2007_DocProps(); + $this->_writerParts['documentrels'] = new PHPWord_Writer_Word2007_DocumentRels(); + $this->_writerParts['document'] = new PHPWord_Writer_Word2007_Document(); + $this->_writerParts['styles'] = new PHPWord_Writer_Word2007_Styles(); + $this->_writerParts['header'] = new PHPWord_Writer_Word2007_Header(); + $this->_writerParts['footer'] = new PHPWord_Writer_Word2007_Footer(); + + foreach($this->_writerParts as $writer) { + $writer->setParentWriter($this); + } + } + + public function save($pFilename = null) { + if(!is_null($this->_document)) { + + // If $pFilename is php://output or php://stdout, make it a temporary file... + $originalFilename = $pFilename; + if(strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { + $pFilename = @tempnam('./', 'phppttmp'); + if($pFilename == '') { + $pFilename = $originalFilename; + } + } + + // Create new ZIP file and open it for writing + $objZip = new ZipArchive(); + + // Try opening the ZIP file + if($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) { + if($objZip->open($pFilename, ZIPARCHIVE::CREATE) !== true) { + throw new Exception("Could not open " . $pFilename . " for writing."); + } + } + + + $sectionElements = array(); + $_secElements = PHPWord_Media::getSectionMediaElements(); + foreach($_secElements as $element) { // loop through section media elements + if($element['type'] != 'hyperlink') { + $this->_addFileToPackage($objZip, $element); + } + $sectionElements[] = $element; + } + + $_hdrElements = PHPWord_Media::getHeaderMediaElements(); + foreach($_hdrElements as $_headerFile => $_hdrMedia) { // loop through headers + if(count($_hdrMedia) > 0) { + $objZip->addFromString('word/_rels/'.$_headerFile.'.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_hdrMedia)); + foreach($_hdrMedia as $element) { // loop through header media elements + $this->_addFileToPackage($objZip, $element); + } + } + } + + $_ftrElements = PHPWord_Media::getFooterMediaElements(); + foreach($_ftrElements as $_footerFile => $_ftrMedia) { // loop through footers + if(count($_ftrMedia) > 0) { + $objZip->addFromString('word/_rels/'.$_footerFile.'.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_ftrMedia)); + foreach($_ftrMedia as $element) { // loop through footers media elements + $this->_addFileToPackage($objZip, $element); + } + } + } + + + + $_cHdrs = 0; + $_cFtrs = 0; + $rID = PHPWord_Media::countSectionMediaElements() + 6; + $_sections = $this->_document->getSections(); + + foreach($_sections as $section) { + $_headers = $section->getHeaders(); + foreach ($_headers as $index => &$_header) { + $_cHdrs++; + $_header->setRelationId(++$rID); + $_headerFile = 'header'.$_cHdrs.'.xml'; + $sectionElements[] = array('target'=>$_headerFile, 'type'=>'header', 'rID'=>$rID); + $objZip->addFromString('word/'.$_headerFile, $this->getWriterPart('header')->writeHeader($_header)); + } + + $_footer = $section->getFooter(); + if(!is_null($_footer)) { + $_cFtrs++; + $_footer->setRelationId(++$rID); + $_footerCount = $_footer->getFooterCount(); + $_footerFile = 'footer'.$_footerCount.'.xml'; + $sectionElements[] = array('target'=>$_footerFile, 'type'=>'footer', 'rID'=>$rID); + $objZip->addFromString('word/'.$_footerFile, $this->getWriterPart('footer')->writeFooter($_footer)); + } + } + + // build docx file + // Write dynamic files + $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes($this->_imageTypes, $this->_objectTypes, $_cHdrs, $_cFtrs)); + $objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeRelationships($this->_document)); + $objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->_document)); + $objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->_document)); + $objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->_document)); + $objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('documentrels')->writeDocumentRels($sectionElements)); + $objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->_document)); + + // Write static files + $objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/numbering.xml', 'word/numbering.xml'); + $objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/settings.xml', 'word/settings.xml'); + $objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/theme1.xml', 'word/theme/theme1.xml'); + $objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/webSettings.xml', 'word/webSettings.xml'); + $objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/fontTable.xml', 'word/fontTable.xml'); + + + // Close file + if($objZip->close() === false) { + throw new Exception("Could not close zip file $pFilename."); + } + + // If a temporary file was used, copy it to the correct file stream + if($originalFilename != $pFilename) { + if (copy($pFilename, $originalFilename) === false) { + throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename."); + } + @unlink($pFilename); + } + } else { + throw new Exception("PHPWord object unassigned."); + } + } + + private function _chkContentTypes($src) { + $srcInfo = pathinfo($src); + $extension = strtolower($srcInfo['extension']); + if(substr($extension, 0, 3) == 'php') { + $extension = 'php'; + } + $_supportedImageTypes = array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'tif', 'tiff', 'php'); + + if(in_array($extension, $_supportedImageTypes)) { + $imagedata = getimagesize($src); + $imagetype = image_type_to_mime_type($imagedata[2]); + $imageext = image_type_to_extension($imagedata[2]); + $imageext = str_replace('.', '', $imageext); + if($imageext == 'jpeg') $imageext = 'jpg'; + + if(!in_array($imagetype, $this->_imageTypes)) { + $this->_imageTypes[$imageext] = $imagetype; + } + } else { + if(!in_array($extension, $this->_objectTypes)) { + $this->_objectTypes[] = $extension; + } + } + } + + public function getWriterPart($pPartName = '') { + if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) { + return $this->_writerParts[strtolower($pPartName)]; + } else { + return null; + } + } + + public function getUseDiskCaching() { + return $this->_useDiskCaching; + } + + public function setUseDiskCaching($pValue = false, $pDirectory = null) { + $this->_useDiskCaching = $pValue; + + if (!is_null($pDirectory)) { + if (is_dir($pDirectory)) { + $this->_diskCachingDirectory = $pDirectory; + } else { + throw new Exception("Directory does not exist: $pDirectory"); + } + } + + return $this; + } + + private function _addFileToPackage($objZip, $element) { + if(isset($element['isMemImage']) && $element['isMemImage']) { + $image = call_user_func($element['createfunction'], $element['source']); + ob_start(); + call_user_func($element['imagefunction'], $image); + $imageContents = ob_get_contents(); + ob_end_clean(); + $objZip->addFromString('word/'.$element['target'], $imageContents); + imagedestroy($image); + + $this->_chkContentTypes($element['source']); + } else { + $objZip->addFile($element['source'], 'word/'.$element['target']); + $this->_chkContentTypes($element['source']); + } + } +} +?> \ No newline at end of file diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php new file mode 100644 index 00000000..549f1253 --- /dev/null +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -0,0 +1,727 @@ +getFontStyle(); + + $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; + + if(!$withoutP) { + $objWriter->startElement('w:p'); + + $styleParagraph = $text->getParagraphStyle(); + $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false; + + if($SpIsObject) { + $this->_writeParagraphStyle($objWriter, $styleParagraph); + } elseif(!$SpIsObject && !is_null($styleParagraph)) { + $objWriter->startElement('w:pPr'); + $objWriter->startElement('w:pStyle'); + $objWriter->writeAttribute('w:val', $styleParagraph); + $objWriter->endElement(); + $objWriter->endElement(); + } + } + + $strText = htmlspecialchars($text->getText()); + $strText = PHPWord_Shared_String::ControlCharacterPHP2OOXML($strText); + + $objWriter->startElement('w:r'); + + if($SfIsObject) { + $this->_writeTextStyle($objWriter, $styleFont); + } elseif(!$SfIsObject && !is_null($styleFont)) { + $objWriter->startElement('w:rPr'); + $objWriter->startElement('w:rStyle'); + $objWriter->writeAttribute('w:val', $styleFont); + $objWriter->endElement(); + $objWriter->endElement(); + } + + $objWriter->startElement('w:t'); + $objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text + $objWriter->writeRaw($strText); + $objWriter->endElement(); + + $objWriter->endElement(); // w:r + + if(!$withoutP) { + $objWriter->endElement(); // w:p + } + } + + protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun) { + $elements = $textrun->getElements(); + $styleParagraph = $textrun->getParagraphStyle(); + + $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false; + + $objWriter->startElement('w:p'); + + if($SpIsObject) { + $this->_writeParagraphStyle($objWriter, $styleParagraph); + } elseif(!$SpIsObject && !is_null($styleParagraph)) { + $objWriter->startElement('w:pPr'); + $objWriter->startElement('w:pStyle'); + $objWriter->writeAttribute('w:val', $styleParagraph); + $objWriter->endElement(); + $objWriter->endElement(); + } + + if(count($elements) > 0) { + foreach($elements as $element) { + if($element instanceof PHPWord_Section_Text) { + $this->_writeText($objWriter, $element, true); + } elseif($element instanceof PHPWord_Section_Link) { + $this->_writeLink($objWriter, $element, true); + } + } + } + + $objWriter->endElement(); + } + + protected function _writeParagraphStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Paragraph $style, $withoutPPR = false) { + $align = $style->getAlign(); + $spaceBefore = $style->getSpaceBefore(); + $spaceAfter = $style->getSpaceAfter(); + $spacing = $style->getSpacing(); + $indent = $style->getIndent(); + $tabs = $style->getTabs(); + + if(!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($indent) || !is_null($tabs)) { + if(!$withoutPPR) { + $objWriter->startElement('w:pPr'); + } + + if(!is_null($align)) { + $objWriter->startElement('w:jc'); + $objWriter->writeAttribute('w:val', $align); + $objWriter->endElement(); + } + + if(!is_null($indent)) { + $objWriter->startElement('w:ind'); + $objWriter->writeAttribute('w:firstLine', 0); + $objWriter->writeAttribute('w:left', $indent); + $objWriter->endElement(); + } + + if(!is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($spacing)) { + $objWriter->startElement('w:spacing'); + if(!is_null($spaceBefore)) { + $objWriter->writeAttribute('w:before', $spaceBefore); + } + if(!is_null($spaceAfter)) { + $objWriter->writeAttribute('w:after', $spaceAfter); + } + if(!is_null($spacing)) { + $objWriter->writeAttribute('w:line', $spacing); + $objWriter->writeAttribute('w:lineRule', 'auto'); + } + $objWriter->endElement(); + } + + if(!is_null($tabs)) { + $tabs->toXml($objWriter); + } + + if(!$withoutPPR) { + $objWriter->endElement(); // w:pPr + } + } + } + + protected function _writeLink(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Link $link, $withoutP = false) { + $rID = $link->getRelationId(); + $linkName = $link->getLinkName(); + if(is_null($linkName)) { + $linkName = $link->getLinkSrc(); + } + + $styleFont = $link->getFontStyle(); + $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; + + if(!$withoutP) { + $objWriter->startElement('w:p'); + + $styleParagraph = $link->getParagraphStyle(); + $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false; + + if($SpIsObject) { + $this->_writeParagraphStyle($objWriter, $styleParagraph); + } elseif(!$SpIsObject && !is_null($styleParagraph)) { + $objWriter->startElement('w:pPr'); + $objWriter->startElement('w:pStyle'); + $objWriter->writeAttribute('w:val', $styleParagraph); + $objWriter->endElement(); + $objWriter->endElement(); + } + } + + $objWriter->startElement('w:hyperlink'); + $objWriter->writeAttribute('r:id', 'rId'.$rID); + $objWriter->writeAttribute('w:history', '1'); + + $objWriter->startElement('w:r'); + if($SfIsObject) { + $this->_writeTextStyle($objWriter, $styleFont); + } elseif(!$SfIsObject && !is_null($styleFont)) { + $objWriter->startElement('w:rPr'); + $objWriter->startElement('w:rStyle'); + $objWriter->writeAttribute('w:val', $styleFont); + $objWriter->endElement(); + $objWriter->endElement(); + } + + $objWriter->startElement('w:t'); + $objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text + $objWriter->writeRaw($linkName); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->endElement(); + + if(!$withoutP) { + $objWriter->endElement(); // w:p + } + } + + protected function _writePreserveText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footer_PreserveText $textrun) { + $styleFont = $textrun->getFontStyle(); + $styleParagraph = $textrun->getParagraphStyle(); + + $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; + $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false; + + $arrText = $textrun->getText(); + + $objWriter->startElement('w:p'); + + if($SpIsObject) { + $this->_writeParagraphStyle($objWriter, $styleParagraph); + } elseif(!$SpIsObject && !is_null($styleParagraph)) { + $objWriter->startElement('w:pPr'); + $objWriter->startElement('w:pStyle'); + $objWriter->writeAttribute('w:val', $styleParagraph); + $objWriter->endElement(); + $objWriter->endElement(); + } + + foreach($arrText as $text) { + + if(substr($text, 0, 1) == '{') { + $text = substr($text, 1, -1); + + $objWriter->startElement('w:r'); + $objWriter->startElement('w:fldChar'); + $objWriter->writeAttribute('w:fldCharType', 'begin'); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->startElement('w:r'); + + if($SfIsObject) { + $this->_writeTextStyle($objWriter, $styleFont); + } elseif(!$SfIsObject && !is_null($styleFont)) { + $objWriter->startElement('w:rPr'); + $objWriter->startElement('w:rStyle'); + $objWriter->writeAttribute('w:val', $styleFont); + $objWriter->endElement(); + $objWriter->endElement(); + } + + $objWriter->startElement('w:instrText'); + $objWriter->writeAttribute('xml:space', 'preserve'); + $objWriter->writeRaw($text); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->startElement('w:r'); + $objWriter->startElement('w:fldChar'); + $objWriter->writeAttribute('w:fldCharType', 'separate'); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->startElement('w:r'); + $objWriter->startElement('w:fldChar'); + $objWriter->writeAttribute('w:fldCharType', 'end'); + $objWriter->endElement(); + $objWriter->endElement(); + } else { + $text = htmlspecialchars($text); + $text = PHPWord_Shared_String::ControlCharacterPHP2OOXML($text); + + $objWriter->startElement('w:r'); + + if($SfIsObject) { + $this->_writeTextStyle($objWriter, $styleFont); + } elseif(!$SfIsObject && !is_null($styleFont)) { + $objWriter->startElement('w:rPr'); + $objWriter->startElement('w:rStyle'); + $objWriter->writeAttribute('w:val', $styleFont); + $objWriter->endElement(); + $objWriter->endElement(); + } + + $objWriter->startElement('w:t'); + $objWriter->writeAttribute('xml:space', 'preserve'); + $objWriter->writeRaw($text); + $objWriter->endElement(); + $objWriter->endElement(); + } + } + + $objWriter->endElement(); // p + } + + protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Font $style) { + $font = $style->getName(); + $bold = $style->getBold(); + $italic = $style->getItalic(); + $color = $style->getColor(); + $size = $style->getSize(); + $fgColor = $style->getFgColor(); + $striketrough = $style->getStrikethrough(); + $underline = $style->getUnderline(); + + $objWriter->startElement('w:rPr'); + + // Font + if($font != 'Arial') { + $objWriter->startElement('w:rFonts'); + $objWriter->writeAttribute('w:ascii', $font); + $objWriter->writeAttribute('w:hAnsi', $font); + $objWriter->writeAttribute('w:cs', $font); + $objWriter->endElement(); + } + + // Color + if($color != '000000') { + $objWriter->startElement('w:color'); + $objWriter->writeAttribute('w:val', $color); + $objWriter->endElement(); + } + + // Size + if($size != 20) { + $objWriter->startElement('w:sz'); + $objWriter->writeAttribute('w:val', $size); + $objWriter->endElement(); + $objWriter->startElement('w:szCs'); + $objWriter->writeAttribute('w:val', $size); + $objWriter->endElement(); + } + + // Bold + if($bold) { + $objWriter->writeElement('w:b', null); + } + + // Italic + if($italic) { + $objWriter->writeElement('w:i', null); + $objWriter->writeElement('w:iCs', null); + } + + // Underline + if(!is_null($underline) && $underline != 'none') { + $objWriter->startElement('w:u'); + $objWriter->writeAttribute('w:val', $underline); + $objWriter->endElement(); + } + + // Striketrough + if($striketrough) { + $objWriter->writeElement('w:strike', null); + } + + // Foreground-Color + if(!is_null($fgColor)) { + $objWriter->startElement('w:highlight'); + $objWriter->writeAttribute('w:val', $fgColor); + $objWriter->endElement(); + } + + $objWriter->endElement(); + } + + protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null) { + $objWriter->writeElement('w:p', null); + } + + protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table) { + $_rows = $table->getRows(); + $_cRows = count($_rows); + + if($_cRows > 0) { + $objWriter->startElement('w:tbl'); + $tblStyle = $table->getStyle(); + if($tblStyle instanceof PHPWord_Style_Table) { + $this->_writeTableStyle($objWriter, $tblStyle); + } else { + if(!empty($tblStyle)) { + $objWriter->startElement('w:tblPr'); + $objWriter->startElement('w:tblStyle'); + $objWriter->writeAttribute('w:val', $tblStyle); + $objWriter->endElement(); + $objWriter->endElement(); + } + } + + $_heights = $table->getRowHeights(); + for($i=0; $i<$_cRows; $i++) { + $row = $_rows[$i]; + $height = $_heights[$i]; + + $objWriter->startElement('w:tr'); + + if(!is_null($height)) { + $objWriter->startElement('w:trPr'); + $objWriter->startElement('w:trHeight'); + $objWriter->writeAttribute('w:val', $height); + $objWriter->endElement(); + $objWriter->endElement(); + } + + foreach($row as $cell) { + $objWriter->startElement('w:tc'); + + $cellStyle = $cell->getStyle(); + $width = $cell->getWidth(); + + $objWriter->startElement('w:tcPr'); + $objWriter->startElement('w:tcW'); + $objWriter->writeAttribute('w:w', $width); + $objWriter->writeAttribute('w:type', 'dxa'); + $objWriter->endElement(); + + if($cellStyle instanceof PHPWord_Style_Cell) { + $this->_writeCellStyle($objWriter, $cellStyle); + } + + $objWriter->endElement(); + + $_elements = $cell->getElements(); + if(count($_elements) > 0) { + foreach($_elements as $element) { + if($element instanceof PHPWord_Section_Text) { + $this->_writeText($objWriter, $element); + } elseif($element instanceof PHPWord_Section_TextRun) { + $this->_writeTextRun($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Link) { + $this->_writeLink($objWriter, $element); + } elseif($element instanceof PHPWord_Section_TextBreak) { + $this->_writeTextBreak($objWriter); + } elseif($element instanceof PHPWord_Section_ListItem) { + $this->_writeListItem($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Image || + $element instanceof PHPWord_Section_MemoryImage) { + $this->_writeImage($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Object) { + $this->_writeObject($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Footer_PreserveText) { + $this->_writePreserveText($objWriter, $element); + } + } + } else { + $this->_writeTextBreak($objWriter); + } + + $objWriter->endElement(); + } + $objWriter->endElement(); + } + $objWriter->endElement(); + } + } + + protected function _writeTableStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Table $style = null) { + $margins = $style->getCellMargin(); + $mTop = (!is_null($margins[0])) ? true : false; + $mLeft = (!is_null($margins[1])) ? true : false; + $mRight = (!is_null($margins[2])) ? true : false; + $mBottom = (!is_null($margins[3])) ? true : false; + + if($mTop || $mLeft || $mRight || $mBottom) { + $objWriter->startElement('w:tblPr'); + $objWriter->startElement('w:tblCellMar'); + + if($mTop) { + $objWriter->startElement('w:top'); + $objWriter->writeAttribute('w:w', $margins[0]); + $objWriter->writeAttribute('w:type', 'dxa'); + $objWriter->endElement(); + } + + if($mLeft) { + $objWriter->startElement('w:left'); + $objWriter->writeAttribute('w:w', $margins[1]); + $objWriter->writeAttribute('w:type', 'dxa'); + $objWriter->endElement(); + } + + if($mRight) { + $objWriter->startElement('w:right'); + $objWriter->writeAttribute('w:w', $margins[2]); + $objWriter->writeAttribute('w:type', 'dxa'); + $objWriter->endElement(); + } + + if($mBottom) { + $objWriter->startElement('w:bottom'); + $objWriter->writeAttribute('w:w', $margins[3]); + $objWriter->writeAttribute('w:type', 'dxa'); + $objWriter->endElement(); + } + + $objWriter->endElement(); + $objWriter->endElement(); + } + } + + protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null) { + $bgColor = $style->getBgColor(); + $valign = $style->getVAlign(); + $textDir = $style->getTextDirection(); + $brdSz = $style->getBorderSize(); + $brdCol = $style->getBorderColor(); + + $bTop = (!is_null($brdSz[0])) ? true : false; + $bLeft = (!is_null($brdSz[1])) ? true : false; + $bRight = (!is_null($brdSz[2])) ? true : false; + $bBottom = (!is_null($brdSz[3])) ? true : false; + $borders = ($bTop || $bLeft || $bRight || $bBottom) ? true : false; + + $styles = (!is_null($bgColor) || !is_null($valign) || !is_null($textDir) || $borders) ? true : false; + + if($styles) { + if(!is_null($textDir)) { + $objWriter->startElement('w:textDirection'); + $objWriter->writeAttribute('w:val', $textDir); + $objWriter->endElement(); + } + + if(!is_null($bgColor)) { + $objWriter->startElement('w:shd'); + $objWriter->writeAttribute('w:val', 'clear'); + $objWriter->writeAttribute('w:color', 'auto'); + $objWriter->writeAttribute('w:fill', $bgColor); + $objWriter->endElement(); + } + + if(!is_null($valign)) { + $objWriter->startElement('w:vAlign'); + $objWriter->writeAttribute('w:val', $valign); + $objWriter->endElement(); + } + + if($borders) { + $_defaultColor = $style->getDefaultBorderColor(); + + $objWriter->startElement('w:tcBorders'); + if($bTop) { + if(is_null($brdCol[0])) { $brdCol[0] = $_defaultColor; } + $objWriter->startElement('w:top'); + $objWriter->writeAttribute('w:val', 'single'); + $objWriter->writeAttribute('w:sz', $brdSz[0]); + $objWriter->writeAttribute('w:color', $brdCol[0]); + $objWriter->endElement(); + } + + if($bLeft) { + if(is_null($brdCol[1])) { $brdCol[1] = $_defaultColor; } + $objWriter->startElement('w:left'); + $objWriter->writeAttribute('w:val', 'single'); + $objWriter->writeAttribute('w:sz', $brdSz[1]); + $objWriter->writeAttribute('w:color', $brdCol[1]); + $objWriter->endElement(); + } + + if($bRight) { + if(is_null($brdCol[2])) { $brdCol[2] = $_defaultColor; } + $objWriter->startElement('w:right'); + $objWriter->writeAttribute('w:val', 'single'); + $objWriter->writeAttribute('w:sz', $brdSz[2]); + $objWriter->writeAttribute('w:color', $brdCol[2]); + $objWriter->endElement(); + } + + if($bBottom) { + if(is_null($brdCol[3])) { $brdCol[3] = $_defaultColor; } + $objWriter->startElement('w:bottom'); + $objWriter->writeAttribute('w:val', 'single'); + $objWriter->writeAttribute('w:sz', $brdSz[3]); + $objWriter->writeAttribute('w:color', $brdCol[3]); + $objWriter->endElement(); + } + + $objWriter->endElement(); + } + } + $gridSpan = $style->getGridSpan(); + if(!is_null($gridSpan)) { + $objWriter->startElement('w:gridSpan'); + $objWriter->writeAttribute('w:val', $gridSpan); + $objWriter->endElement(); + } + + $vMerge = $style->getVMerge(); + if(!is_null($vMerge)) { + $objWriter->startElement('w:vMerge'); + $objWriter->writeAttribute('w:val', $vMerge); + $objWriter->endElement(); + } + } + + protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, $image) { + $rId = $image->getRelationId(); + + $style = $image->getStyle(); + $width = $style->getWidth(); + $height = $style->getHeight(); + $align = $style->getAlign(); + + $objWriter->startElement('w:p'); + + if(!is_null($align)) { + $objWriter->startElement('w:pPr'); + $objWriter->startElement('w:jc'); + $objWriter->writeAttribute('w:val', $align); + $objWriter->endElement(); + $objWriter->endElement(); + } + + $objWriter->startElement('w:r'); + + $objWriter->startElement('w:pict'); + + $objWriter->startElement('v:shape'); + $objWriter->writeAttribute('type', '#_x0000_t75'); + $objWriter->writeAttribute('style', 'width:'.$width.'px;height:'.$height.'px'); + + $objWriter->startElement('v:imagedata'); + $objWriter->writeAttribute('r:id', 'rId'.$rId); + $objWriter->writeAttribute('o:title', ''); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->endElement(); + + $objWriter->endElement(); + + $objWriter->endElement(); + } + + protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image) { + $rId = $image->getRelationId(); + + $style = $image->getStyle(); + $width = $style->getWidth(); + $height = $style->getHeight(); + $marginLeft = $style->getMarginLeft(); + $marginTop = $style->getMarginTop(); + + $objWriter->startElement('w:p'); + + $objWriter->startElement('w:r'); + + $objWriter->startElement('w:pict'); + + $objWriter->startElement('v:shape'); + $objWriter->writeAttribute('type', '#_x0000_t75'); + + $strStyle = 'position:absolute;'; + $strStyle .= ' width:'.$width.'px;'; + $strStyle .= ' height:'.$height.'px;'; + if(!is_null($marginTop)) { + $strStyle .= ' margin-top:'.$marginTop.'px;'; + } + if(!is_null($marginLeft)) { + $strStyle .= ' margin-left:'.$marginLeft.'px;'; + } + + $objWriter->writeAttribute('style', $strStyle); + + $objWriter->startElement('v:imagedata'); + $objWriter->writeAttribute('r:id', 'rId'.$rId); + $objWriter->writeAttribute('o:title', ''); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->endElement(); + + $objWriter->endElement(); + + $objWriter->endElement(); + } + + protected function _writeTitle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Title $title) { + $text = htmlspecialchars($title->getText()); + $text = PHPWord_Shared_String::ControlCharacterPHP2OOXML($text); + $anchor = $title->getAnchor(); + $bookmarkId = $title->getBookmarkId(); + $style = $title->getStyle(); + + $objWriter->startElement('w:p'); + + if(!empty($style)) { + $objWriter->startElement('w:pPr'); + $objWriter->startElement('w:pStyle'); + $objWriter->writeAttribute('w:val', $style); + $objWriter->endElement(); + $objWriter->endElement(); + } + + $objWriter->startElement('w:r'); + $objWriter->startElement('w:fldChar'); + $objWriter->writeAttribute('w:fldCharType', 'end'); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->startElement('w:bookmarkStart'); + $objWriter->writeAttribute('w:id', $bookmarkId); + $objWriter->writeAttribute('w:name', $anchor); + $objWriter->endElement(); + + $objWriter->startElement('w:r'); + $objWriter->startElement('w:t'); + $objWriter->writeRaw($text); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->startElement('w:bookmarkEnd'); + $objWriter->writeAttribute('w:id', $bookmarkId); + $objWriter->endElement(); + + $objWriter->endElement(); + } +} +?> \ No newline at end of file diff --git a/src/PHPWord/Writer/Word2007/ContentTypes.php b/Classes/PHPWord/Writer/Word2007/ContentTypes.php similarity index 100% rename from src/PHPWord/Writer/Word2007/ContentTypes.php rename to Classes/PHPWord/Writer/Word2007/ContentTypes.php diff --git a/src/PHPWord/Writer/Word2007/DocProps.php b/Classes/PHPWord/Writer/Word2007/DocProps.php similarity index 100% rename from src/PHPWord/Writer/Word2007/DocProps.php rename to Classes/PHPWord/Writer/Word2007/DocProps.php diff --git a/Classes/PHPWord/Writer/Word2007/Document.php b/Classes/PHPWord/Writer/Word2007/Document.php new file mode 100644 index 00000000..4543aeae --- /dev/null +++ b/Classes/PHPWord/Writer/Word2007/Document.php @@ -0,0 +1,458 @@ +getParentWriter()->getUseDiskCaching()) { + $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); + } else { + $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY); + } + + // XML header + $objWriter->startDocument('1.0','UTF-8','yes'); + + // w:document + $objWriter->startElement('w:document'); + + $objWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006'); + $objWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office'); + $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'); + $objWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math'); + $objWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml'); + $objWriter->writeAttribute('xmlns:wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'); + $objWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word'); + $objWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); + $objWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); + + $objWriter->startElement('w:body'); + + $_sections = $pPHPWord->getSections(); + $countSections = count($_sections); + $pSection = 0; + + if($countSections > 0) { + foreach($_sections as $section) { + $pSection++; + + $_elements = $section->getElements(); + + foreach($_elements as $element) { + if($element instanceof PHPWord_Section_Text) { + $this->_writeText($objWriter, $element); + } elseif($element instanceof PHPWord_Section_TextRun) { + $this->_writeTextRun($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Link) { + $this->_writeLink($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Title) { + $this->_writeTitle($objWriter, $element); + } elseif($element instanceof PHPWord_Section_TextBreak) { + $this->_writeTextBreak($objWriter); + } elseif($element instanceof PHPWord_Section_PageBreak) { + $this->_writePageBreak($objWriter); + } elseif($element instanceof PHPWord_Section_Table) { + $this->_writeTable($objWriter, $element); + } elseif($element instanceof PHPWord_Section_ListItem) { + $this->_writeListItem($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Image || + $element instanceof PHPWord_Section_MemoryImage) { + $this->_writeImage($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Object) { + $this->_writeObject($objWriter, $element); + } elseif($element instanceof PHPWord_TOC) { + $this->_writeTOC($objWriter); + } + } + + if($pSection == $countSections) { + $this->_writeEndSection($objWriter, $section); + } else { + $this->_writeSection($objWriter, $section); + } + } + } + + $objWriter->endElement(); // End w:body + $objWriter->endElement(); // End w:document + + // Return + return $objWriter->getData(); + } + + private function _writeSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section) { + $objWriter->startElement('w:p'); + $objWriter->startElement('w:pPr'); + $this->_writeEndSection($objWriter, $section, 3); + $objWriter->endElement(); + $objWriter->endElement(); + } + + private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section) { + $_settings = $section->getSettings(); + $_headers = $section->getHeaders(); + $_footer = $section->getFooter(); + $pgSzW = $_settings->getPageSizeW(); + $pgSzH = $_settings->getPageSizeH(); + $orientation = $_settings->getOrientation(); + + $marginTop = $_settings->getMarginTop(); + $marginLeft = $_settings->getMarginLeft(); + $marginRight = $_settings->getMarginRight(); + $marginBottom = $_settings->getMarginBottom(); + + $borders = $_settings->getBorderSize(); + + $objWriter->startElement('w:sectPr'); + + foreach ($_headers as &$_header) { + $rId = $_header->getRelationId(); + $objWriter->startElement('w:headerReference'); + $objWriter->writeAttribute('w:type', $_header->getType()); + $objWriter->writeAttribute('r:id', 'rId'.$rId); + $objWriter->endElement(); + } + + if($section->hasDifferentFirstPage()) { + $objWriter->startElement('w:titlePg'); + $objWriter->endElement(); + } + + if(!is_null($_footer)) { + $rId = $_footer->getRelationId(); + $objWriter->startElement('w:footerReference'); + $objWriter->writeAttribute('w:type', 'default'); + $objWriter->writeAttribute('r:id', 'rId'.$rId); + $objWriter->endElement(); + } + + $objWriter->startElement('w:pgSz'); + $objWriter->writeAttribute('w:w', $pgSzW); + $objWriter->writeAttribute('w:h', $pgSzH); + + if(!is_null($orientation) && strtolower($orientation) != 'portrait') { + $objWriter->writeAttribute('w:orient', $orientation); + } + + $objWriter->endElement(); + + $objWriter->startElement('w:pgMar'); + $objWriter->writeAttribute('w:top', $marginTop); + $objWriter->writeAttribute('w:right', $marginRight); + $objWriter->writeAttribute('w:bottom', $marginBottom); + $objWriter->writeAttribute('w:left', $marginLeft); + $objWriter->writeAttribute('w:header', '720'); + $objWriter->writeAttribute('w:footer', '720'); + $objWriter->writeAttribute('w:gutter', '0'); + $objWriter->endElement(); + + + if(!is_null($borders[0]) || !is_null($borders[1]) || !is_null($borders[2]) || !is_null($borders[3])) { + $borderColor = $_settings->getBorderColor(); + + $objWriter->startElement('w:pgBorders'); + $objWriter->writeAttribute('w:offsetFrom', 'page'); + + if(!is_null($borders[0])) { + $objWriter->startElement('w:top'); + $objWriter->writeAttribute('w:val', 'single'); + $objWriter->writeAttribute('w:sz', $borders[0]); + $objWriter->writeAttribute('w:space', '24'); + $objWriter->writeAttribute('w:color', $borderColor[0]); + $objWriter->endElement(); + } + + if(!is_null($borders[1])) { + $objWriter->startElement('w:left'); + $objWriter->writeAttribute('w:val', 'single'); + $objWriter->writeAttribute('w:sz', $borders[1]); + $objWriter->writeAttribute('w:space', '24'); + $objWriter->writeAttribute('w:color', $borderColor[1]); + $objWriter->endElement(); + } + + if(!is_null($borders[2])) { + $objWriter->startElement('w:right'); + $objWriter->writeAttribute('w:val', 'single'); + $objWriter->writeAttribute('w:sz', $borders[2]); + $objWriter->writeAttribute('w:space', '24'); + $objWriter->writeAttribute('w:color', $borderColor[2]); + $objWriter->endElement(); + } + + if(!is_null($borders[3])) { + $objWriter->startElement('w:bottom'); + $objWriter->writeAttribute('w:val', 'single'); + $objWriter->writeAttribute('w:sz', $borders[3]); + $objWriter->writeAttribute('w:space', '24'); + $objWriter->writeAttribute('w:color', $borderColor[3]); + $objWriter->endElement(); + } + $objWriter->endElement(); + } + + + $objWriter->startElement('w:cols'); + $objWriter->writeAttribute('w:space', '720'); + $objWriter->endElement(); + + + $objWriter->endElement(); + } + + private function _writePageBreak(PHPWord_Shared_XMLWriter $objWriter = null) { + $objWriter->startElement('w:p'); + $objWriter->startElement('w:r'); + $objWriter->startElement('w:br'); + $objWriter->writeAttribute('w:type', 'page'); + $objWriter->endElement(); + $objWriter->endElement(); + $objWriter->endElement(); + } + + private function _writeListItem(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_ListItem $listItem) { + $textObject = $listItem->getTextObject(); + $text = $textObject->getText(); + $styleParagraph = $textObject->getParagraphStyle(); + $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false; + + $depth = $listItem->getDepth(); + $listType = $listItem->getStyle()->getListType(); + + $objWriter->startElement('w:p'); + $objWriter->startElement('w:pPr'); + + if($SpIsObject) { + $this->_writeParagraphStyle($objWriter, $styleParagraph, true); + } elseif(!$SpIsObject && !is_null($styleParagraph)) { + $objWriter->startElement('w:pStyle'); + $objWriter->writeAttribute('w:val', $styleParagraph); + $objWriter->endElement(); + } + + $objWriter->startElement('w:numPr'); + + $objWriter->startElement('w:ilvl'); + $objWriter->writeAttribute('w:val', $depth); + $objWriter->endElement(); + + $objWriter->startElement('w:numId'); + $objWriter->writeAttribute('w:val', $listType); + $objWriter->endElement(); + + $objWriter->endElement(); + $objWriter->endElement(); + + $this->_writeText($objWriter, $textObject, true); + + $objWriter->endElement(); + } + + protected function _writeObject(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Object $object) { + $rIdObject = $object->getRelationId(); + $rIdImage = $object->getImageRelationId(); + $shapeId = md5($rIdObject.'_'.$rIdImage); + + $objectId = $object->getObjectId(); + + $style = $object->getStyle(); + $width = $style->getWidth(); + $height = $style->getHeight(); + $align = $style->getAlign(); + + + $objWriter->startElement('w:p'); + + if(!is_null($align)) { + $objWriter->startElement('w:pPr'); + $objWriter->startElement('w:jc'); + $objWriter->writeAttribute('w:val', $align); + $objWriter->endElement(); + $objWriter->endElement(); + } + + $objWriter->startElement('w:r'); + + $objWriter->startElement('w:object'); + $objWriter->writeAttribute('w:dxaOrig', '249'); + $objWriter->writeAttribute('w:dyaOrig', '160'); + + $objWriter->startElement('v:shape'); + $objWriter->writeAttribute('id', $shapeId); + $objWriter->writeAttribute('type', '#_x0000_t75'); + $objWriter->writeAttribute('style', 'width:104px;height:67px'); + $objWriter->writeAttribute('o:ole', ''); + + $objWriter->startElement('v:imagedata'); + $objWriter->writeAttribute('r:id', 'rId'.$rIdImage); + $objWriter->writeAttribute('o:title', ''); + $objWriter->endElement(); + + $objWriter->endElement(); + + $objWriter->startElement('o:OLEObject'); + $objWriter->writeAttribute('Type', 'Embed'); + $objWriter->writeAttribute('ProgID', 'Package'); + $objWriter->writeAttribute('ShapeID', $shapeId); + $objWriter->writeAttribute('DrawAspect', 'Icon'); + $objWriter->writeAttribute('ObjectID', '_'.$objectId); + $objWriter->writeAttribute('r:id', 'rId'.$rIdObject); + $objWriter->endElement(); + + $objWriter->endElement(); + + $objWriter->endElement(); // w:r + + $objWriter->endElement(); // w:p + } + + private function _writeTOC(PHPWord_Shared_XMLWriter $objWriter = null) { + $titles = PHPWord_TOC::getTitles(); + $styleFont = PHPWord_TOC::getStyleFont(); + + $styleTOC = PHPWord_TOC::getStyleTOC(); + $fIndent = $styleTOC->getIndent(); + $tabLeader = $styleTOC->getTabLeader(); + $tabPos = $styleTOC->getTabPos(); + + $isObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; + + for($i=0; $istartElement('w:p'); + + $objWriter->startElement('w:pPr'); + + if($isObject && !is_null($styleFont->getParagraphStyle())) { + $this->_writeParagraphStyle($objWriter, $styleFont->getParagraphStyle()); + } + + if($indent > 0) { + $objWriter->startElement('w:ind'); + $objWriter->writeAttribute('w:left', $indent); + $objWriter->endElement(); + } + + if(!empty($styleFont) && !$isObject) { + $objWriter->startElement('w:pPr'); + $objWriter->startElement('w:pStyle'); + $objWriter->writeAttribute('w:val', $styleFont); + $objWriter->endElement(); + $objWriter->endElement(); + } + + $objWriter->startElement('w:tabs'); + $objWriter->startElement('w:tab'); + $objWriter->writeAttribute('w:val', 'right'); + if(!empty($tabLeader)) { + $objWriter->writeAttribute('w:leader', $tabLeader); + } + $objWriter->writeAttribute('w:pos', $tabPos); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->endElement(); // w:pPr + + + if($i == 0) { + $objWriter->startElement('w:r'); + $objWriter->startElement('w:fldChar'); + $objWriter->writeAttribute('w:fldCharType', 'begin'); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->startElement('w:r'); + $objWriter->startElement('w:instrText'); + $objWriter->writeAttribute('xml:space', 'preserve'); + $objWriter->writeRaw('TOC \o "1-9" \h \z \u'); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->startElement('w:r'); + $objWriter->startElement('w:fldChar'); + $objWriter->writeAttribute('w:fldCharType', 'separate'); + $objWriter->endElement(); + $objWriter->endElement(); + } + + $objWriter->startElement('w:hyperlink'); + $objWriter->writeAttribute('w:anchor', $title['anchor']); + $objWriter->writeAttribute('w:history', '1'); + + $objWriter->startElement('w:r'); + + if($isObject) { + $this->_writeTextStyle($objWriter, $styleFont); + } + + $objWriter->startElement('w:t'); + $objWriter->writeRaw($title['text']); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->startElement('w:r'); + $objWriter->writeElement('w:tab', null); + $objWriter->endElement(); + + $objWriter->startElement('w:r'); + $objWriter->startElement('w:fldChar'); + $objWriter->writeAttribute('w:fldCharType', 'begin'); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->startElement('w:r'); + $objWriter->startElement('w:instrText'); + $objWriter->writeAttribute('xml:space', 'preserve'); + $objWriter->writeRaw('PAGEREF '.$title['anchor'].' \h'); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->startElement('w:r'); + $objWriter->startElement('w:fldChar'); + $objWriter->writeAttribute('w:fldCharType', 'end'); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->endElement(); // w:hyperlink + + $objWriter->endElement(); // w:p + } + + $objWriter->startElement('w:p'); + $objWriter->startElement('w:r'); + $objWriter->startElement('w:fldChar'); + $objWriter->writeAttribute('w:fldCharType', 'end'); + $objWriter->endElement(); + $objWriter->endElement(); + $objWriter->endElement(); + } +} diff --git a/src/PHPWord/Writer/Word2007/DocumentRels.php b/Classes/PHPWord/Writer/Word2007/DocumentRels.php similarity index 100% rename from src/PHPWord/Writer/Word2007/DocumentRels.php rename to Classes/PHPWord/Writer/Word2007/DocumentRels.php diff --git a/src/PHPWord/Writer/Word2007/Footer.php b/Classes/PHPWord/Writer/Word2007/Footer.php similarity index 100% rename from src/PHPWord/Writer/Word2007/Footer.php rename to Classes/PHPWord/Writer/Word2007/Footer.php diff --git a/src/PHPWord/Writer/Word2007/Header.php b/Classes/PHPWord/Writer/Word2007/Header.php similarity index 100% rename from src/PHPWord/Writer/Word2007/Header.php rename to Classes/PHPWord/Writer/Word2007/Header.php diff --git a/src/PHPWord/Writer/Word2007/Rels.php b/Classes/PHPWord/Writer/Word2007/Rels.php similarity index 100% rename from src/PHPWord/Writer/Word2007/Rels.php rename to Classes/PHPWord/Writer/Word2007/Rels.php diff --git a/src/PHPWord/Writer/Word2007/Styles.php b/Classes/PHPWord/Writer/Word2007/Styles.php similarity index 100% rename from src/PHPWord/Writer/Word2007/Styles.php rename to Classes/PHPWord/Writer/Word2007/Styles.php diff --git a/src/PHPWord/Writer/Word2007/WriterPart.php b/Classes/PHPWord/Writer/Word2007/WriterPart.php similarity index 100% rename from src/PHPWord/Writer/Word2007/WriterPart.php rename to Classes/PHPWord/Writer/Word2007/WriterPart.php diff --git a/src/PHPWord/_staticDocParts/_doc.png b/Classes/PHPWord/_staticDocParts/_doc.png similarity index 100% rename from src/PHPWord/_staticDocParts/_doc.png rename to Classes/PHPWord/_staticDocParts/_doc.png diff --git a/src/PHPWord/_staticDocParts/_ppt.png b/Classes/PHPWord/_staticDocParts/_ppt.png similarity index 100% rename from src/PHPWord/_staticDocParts/_ppt.png rename to Classes/PHPWord/_staticDocParts/_ppt.png diff --git a/src/PHPWord/_staticDocParts/_xls.png b/Classes/PHPWord/_staticDocParts/_xls.png similarity index 100% rename from src/PHPWord/_staticDocParts/_xls.png rename to Classes/PHPWord/_staticDocParts/_xls.png diff --git a/src/PHPWord/_staticDocParts/fontTable.xml b/Classes/PHPWord/_staticDocParts/fontTable.xml similarity index 100% rename from src/PHPWord/_staticDocParts/fontTable.xml rename to Classes/PHPWord/_staticDocParts/fontTable.xml diff --git a/src/PHPWord/_staticDocParts/numbering.xml b/Classes/PHPWord/_staticDocParts/numbering.xml similarity index 100% rename from src/PHPWord/_staticDocParts/numbering.xml rename to Classes/PHPWord/_staticDocParts/numbering.xml diff --git a/src/PHPWord/_staticDocParts/settings.xml b/Classes/PHPWord/_staticDocParts/settings.xml similarity index 100% rename from src/PHPWord/_staticDocParts/settings.xml rename to Classes/PHPWord/_staticDocParts/settings.xml diff --git a/src/PHPWord/_staticDocParts/theme1.xml b/Classes/PHPWord/_staticDocParts/theme1.xml similarity index 100% rename from src/PHPWord/_staticDocParts/theme1.xml rename to Classes/PHPWord/_staticDocParts/theme1.xml diff --git a/src/PHPWord/_staticDocParts/webSettings.xml b/Classes/PHPWord/_staticDocParts/webSettings.xml similarity index 100% rename from src/PHPWord/_staticDocParts/webSettings.xml rename to Classes/PHPWord/_staticDocParts/webSettings.xml diff --git a/Examples/HeaderFooter.php b/Examples/HeaderFooter.php deleted file mode 100644 index 918280cd..00000000 --- a/Examples/HeaderFooter.php +++ /dev/null @@ -1,28 +0,0 @@ -createSection(); - -// Add header -$header = $section->createHeader(); -$table = $header->addTable(); -$table->addRow(); -$table->addCell(4500)->addText('This is the header.'); -$table->addCell(4500)->addImage('_earth.jpg', array('width'=>50, 'height'=>50, 'align'=>'right')); - -// Add footer -$footer = $section->createFooter(); -$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', array('align'=>'center')); - -// Write some text -$section->addTextBreak(); -$section->addText('Some text...'); - -// Save File -$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); -$objWriter->save('HeaderFooter.docx'); -?> \ No newline at end of file diff --git a/changelog.txt b/changelog.txt index a632a34f..9770653f 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,42 @@ -2013-12-11 (v0.7.0): +************************************************************************************** +* PHPWord +* +* Copyright (c) 2011 - 2013 PHPWord +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +* +* @copyright Copyright (c) 2011 - 2013 PHPWord (https://github.com/PHPOffice/PHPWord/) +* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL +* @version ##VERSION##, ##DATE## +************************************************************************************** + +Fixed in branch for release 0.7 : +- Bugfix: (RomanSyroeshko) GH-32 - "Warning: Invalid error type specified in ...\PHPWord.php on line 226" is thrown when the specified template file is not found +- Bugfix: (RomanSyroeshko) GH-34 - PHPWord_Shared_String.IsUTF8 returns FALSE for Cyrillic UTF-8 input +- Bugfix: (RomanSyroeshko) GH-38 - Temporary files naming logic in PHPWord_Template can lead to a collision +- Feature: (Progi1984) GH-1 - Implement RTF Writer +- Feature: (Progi1984) GH-2 - Implement ODT Writer +- Feature: (kaystrobach) - Word2007 : Add rowspan and colspan to cells +- Feature: (RLovelett) - Word2007 : Support for tab stops +- Feature: (RLovelett) - Word2007 : Support Multiple headers +- General: (MarkBaker) - Add superscript/subscript styling in Excel2007 Writer +- General: (deds) - add indentation support to paragraphs +- General: (Progi1984) GH-27 - Support for Composer +- General: (Progi1984) - Basic CI with Travis +- General: (Progi1984) - Added PHPWord_Exception and exception when could not copy the template +- General: (Progi1984) - IMPROVED : Moved examples out of Classes directory +- General: (Esmeraldo) CP-49 - IMPROVED : Advanced string replace in setValue for Template - Feature: (gavroche) Added composer file -- Feature: (gavroche) Added support for image wrapping style \ No newline at end of file +- Feature: (gavroche) Added support for image wrapping style diff --git a/composer.json b/composer.json index a954a074..f080748b 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ }, "autoload": { "psr-0": { - "PHPWord": "src/" + "PHPWord": "Classes/" } } } diff --git a/samples/.gitignore b/samples/.gitignore deleted file mode 100644 index f3b243cb..00000000 --- a/samples/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ - -*.odt -*.docx -*.rtf -*.txt \ No newline at end of file diff --git a/samples/Sample_01_SimpleText.php b/samples/Sample_01_SimpleText.php index 4bf9d343..d421c37d 100644 --- a/samples/Sample_01_SimpleText.php +++ b/samples/Sample_01_SimpleText.php @@ -9,7 +9,7 @@ else { define('EOL', '
'); } -require_once '../src/PHPWord.php'; +require_once '../Classes/PHPWord.php'; // New Word Document echo date('H:i:s') , " Create new PHPWord object" , EOL; diff --git a/samples/Sample_02_TabStops.php b/samples/Sample_02_TabStops.php new file mode 100644 index 00000000..3c4d3841 --- /dev/null +++ b/samples/Sample_02_TabStops.php @@ -0,0 +1,64 @@ +'); +} + +require_once '../Classes/PHPWord.php'; + +// New Word Document +echo date('H:i:s') , ' Create new PHPWord object' , EOL; +$PHPWord = new PHPWord(); + +// Ads styles +$PHPWord->addParagraphStyle('multipleTab', array( + 'tabs' => array( + new PHPWord_Style_Tab('left', 1550), + new PHPWord_Style_Tab('center', 3200), + new PHPWord_Style_Tab('right', 5300) + ) +)); +$PHPWord->addParagraphStyle('rightTab', array( + 'tabs' => array( + new PHPWord_Style_Tab('right', 9090) + ) +)); +$PHPWord->addParagraphStyle('centerTab', array( + 'tabs' => array( + new PHPWord_Style_Tab('center', 4680) + ) +)); + +// New portrait section +$section = $PHPWord->createSection(); + +// Add listitem elements +$section->addText("Multiple Tabs:\tOne\tTwo\tThree", NULL, 'multipleTab'); +$section->addText("Left Aligned\tRight Aligned", NULL, 'rightTab'); +$section->addText("\tCenter Aligned", NULL, 'centerTab'); + +// Save File +echo date('H:i:s') , ' Write to Word2007 format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); +$objWriter->save(str_replace('.php', '.docx', __FILE__)); + +echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); +$objWriter->save(str_replace('.php', '.odt', __FILE__)); + +echo date('H:i:s') , ' Write to RTF format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); +$objWriter->save(str_replace('.php', '.rtf', __FILE__)); + + +// Echo memory peak usage +echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL; + +// Echo done +echo date('H:i:s') , ' Done writing file' , EOL; +?> \ No newline at end of file diff --git a/Examples/AdvancedTable.php b/samples/old/AdvancedTable.php similarity index 97% rename from Examples/AdvancedTable.php rename to samples/old/AdvancedTable.php index 5a2088ff..e83c4e98 100644 --- a/Examples/AdvancedTable.php +++ b/samples/old/AdvancedTable.php @@ -1,5 +1,5 @@ createSection(); + +// Add first page header +$header = $section->createHeader(); +$header->firstPage(); +$table = $header->addTable(); +$table->addRow(); +$table->addCell(4500)->addText('This is the header.'); +$table->addCell(4500)->addImage('_earth.jpg', array('width'=>50, 'height'=>50, 'align'=>'right')); + +// Add header for all other pages +$subsequent = $section->createHeader(); +$subsequent->addText("Subsequent pages in Section 1 will Have this!"); + +// Add footer +$footer = $section->createFooter(); +$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', array('align'=>'center')); + +// Write some text +$section->addTextBreak(); +$section->addText('Some text...'); + +// Create a second page +$section->addPageBreak(); + +// Write some text +$section->addTextBreak(); +$section->addText('Some text...'); + +// Create a third page +$section->addPageBreak(); + +// Write some text +$section->addTextBreak(); +$section->addText('Some text...'); + +// New portrait section +$section2 = $PHPWord->createSection(); + +$sec2Header = $section2->createHeader(); +$sec2Header->addText("All pages in Section 2 will Have this!"); + +// Write some text +$section2->addTextBreak(); +$section2->addText('Some text...'); + +// Save File +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); +$objWriter->save('HeaderFooter.docx'); +?> \ No newline at end of file diff --git a/samples/old/Image.php b/samples/old/Image.php new file mode 100644 index 00000000..6316783f --- /dev/null +++ b/samples/old/Image.php @@ -0,0 +1,24 @@ +createSection(); + +// Add image elements +$section->addImage('_mars.jpg'); +$section->addTextBreak(2); + +$section->addImage('_earth.jpg', array('width'=>210, 'height'=>210, 'align'=>'center')); +$section->addTextBreak(2); + +$section->addImage('_mars.jpg', array('width'=>100, 'height'=>100, 'align'=>'right')); + + + +// Save File +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); +$objWriter->save('Image.docx'); +?> \ No newline at end of file diff --git a/Examples/Link.php b/samples/old/Link.php similarity index 93% rename from Examples/Link.php rename to samples/old/Link.php index 7cebaece..5cd98bb8 100644 --- a/Examples/Link.php +++ b/samples/old/Link.php @@ -1,5 +1,5 @@ _properties = new PHPWord_DocumentProperties(); - $this->_defaultFontName = 'Arial'; - $this->_defaultFontSize = 20; - } - - /** - * Get properties - * @return PHPWord_DocumentProperties - */ - public function getProperties() - { - return $this->_properties; - } - - /** - * Set properties - * - * @param PHPWord_DocumentProperties $value - * @return PHPWord - */ - public function setProperties(PHPWord_DocumentProperties $value) - { - $this->_properties = $value; - return $this; - } - - /** - * Create a new Section - * - * @param PHPWord_Section_Settings $settings - * @return PHPWord_Section - */ - public function createSection($settings = null) - { - $sectionCount = $this->_countSections() + 1; - - $section = new PHPWord_Section($sectionCount, $settings); - $this->_sectionCollection[] = $section; - return $section; - } - - /** - * Get default Font name - * @return string - */ - public function getDefaultFontName() - { - return $this->_defaultFontName; - } - - /** - * Set default Font name - * @param string $pValue - */ - public function setDefaultFontName($pValue) - { - $this->_defaultFontName = $pValue; - } - - /** - * Get default Font size - * @return string - */ - public function getDefaultFontSize() - { - return $this->_defaultFontSize; - } - - /** - * Set default Font size - * @param int $pValue - */ - public function setDefaultFontSize($pValue) - { - $pValue = $pValue * 2; - $this->_defaultFontSize = $pValue; - } - - /** - * Adds a paragraph style definition to styles.xml - * - * @param $styleName string - * @param $styles array - */ - public function addParagraphStyle($styleName, $styles) - { - PHPWord_Style::addParagraphStyle($styleName, $styles); - } - - /** - * Adds a font style definition to styles.xml - * - * @param $styleName string - * @param $styles array - */ - public function addFontStyle($styleName, $styleFont, $styleParagraph = null) - { - PHPWord_Style::addFontStyle($styleName, $styleFont, $styleParagraph); - } - - /** - * Adds a table style definition to styles.xml - * - * @param $styleName string - * @param $styles array - */ - public function addTableStyle($styleName, $styleTable, $styleFirstRow = null) - { - PHPWord_Style::addTableStyle($styleName, $styleTable, $styleFirstRow); - } - - /** - * Adds a heading style definition to styles.xml - * - * @param $titleCount int - * @param $styles array - */ - public function addTitleStyle($titleCount, $styleFont, $styleParagraph = null) - { - PHPWord_Style::addTitleStyle($titleCount, $styleFont, $styleParagraph); - } - - /** - * Adds a hyperlink style to styles.xml - * - * @param $styleName string - * @param $styles array - */ - public function addLinkStyle($styleName, $styles) - { - PHPWord_Style::addLinkStyle($styleName, $styles); - } - - /** - * Get sections - * @return PHPWord_Section[] - */ - public function getSections() - { - return $this->_sectionCollection; - } - - /** - * Get section count - * @return int - */ - private function _countSections() - { - return count($this->_sectionCollection); - } - - /** - * Load a Template File - * - * @param string $strFilename - * @return PHPWord_Template - */ - public function loadTemplate($strFilename) - { - if (file_exists($strFilename)) { - $template = new PHPWord_Template($strFilename); - return $template; - } else { - trigger_error('Template file ' . $strFilename . ' not found.', E_ERROR); - } - } -} \ No newline at end of file diff --git a/src/PHPWord/Section.php b/src/PHPWord/Section.php deleted file mode 100644 index 38bb63d5..00000000 --- a/src/PHPWord/Section.php +++ /dev/null @@ -1,385 +0,0 @@ -_sectionCount = $sectionCount; - $this->_settings = new PHPWord_Section_Settings(); - - if (!is_null($settings) && is_array($settings)) { - foreach ($settings as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->_settings->setSettingValue($key, $value); - } - } - } - - /** - * Get Section Settings - * - * @return PHPWord_Section_Settings - */ - public function getSettings() - { - return $this->_settings; - } - - /** - * Add a Text Element - * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return PHPWord_Section_Text - */ - public function addText($text, $styleFont = null, $styleParagraph = null) - { - $givenText = utf8_encode($text); - $text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add a Link Element - * - * @param string $linkSrc - * @param string $linkName - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return PHPWord_Section_Link - */ - public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) - { - $linkSrc = utf8_encode($linkSrc); - if (!is_null($linkName)) { - $linkName = utf8_encode($linkName); - } - - $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont, $styleParagraph); - $rID = PHPWord_Media::addSectionLinkElement($linkSrc); - $link->setRelationId($rID); - - $this->_elementCollection[] = $link; - return $link; - } - - /** - * Add a TextBreak Element - * - * @param int $count - */ - public function addTextBreak($count = 1) - { - for ($i = 1; $i <= $count; $i++) { - $this->_elementCollection[] = new PHPWord_Section_TextBreak(); - } - } - - /** - * Add a PageBreak Element - */ - public function addPageBreak() - { - $this->_elementCollection[] = new PHPWord_Section_PageBreak(); - } - - /** - * Add a Table Element - * - * @param mixed $style - * @return PHPWord_Section_Table - */ - public function addTable($style = null) - { - $table = new PHPWord_Section_Table('section', $this->_sectionCount, $style); - $this->_elementCollection[] = $table; - return $table; - } - - /** - * Add a ListItem Element - * - * @param string $text - * @param int $depth - * @param mixed $styleFont - * @param mixed $styleList - * @param mixed $styleParagraph - * @return PHPWord_Section_ListItem - */ - public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) - { - $text = utf8_encode($text); - $listItem = new PHPWord_Section_ListItem($text, $depth, $styleFont, $styleList, $styleParagraph); - $this->_elementCollection[] = $listItem; - return $listItem; - } - - /** - * Add a OLE-Object Element - * - * @param string $src - * @param mixed $style - * @return PHPWord_Section_Object - */ - public function addObject($src, $style = null) - { - $object = new PHPWord_Section_Object($src, $style); - - if (!is_null($object->getSource())) { - $inf = pathinfo($src); - $ext = $inf['extension']; - if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { - $ext = substr($ext, 0, -1); - } - - $iconSrc = PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/'; - if (!file_exists($iconSrc . '_' . $ext . '.png')) { - $iconSrc = $iconSrc . '_default.png'; - } else { - $iconSrc .= '_' . $ext . '.png'; - } - - $rIDimg = PHPWord_Media::addSectionMediaElement($iconSrc, 'image'); - $data = PHPWord_Media::addSectionMediaElement($src, 'oleObject'); - $rID = $data[0]; - $objectId = $data[1]; - - $object->setRelationId($rID); - $object->setObjectId($objectId); - $object->setImageRelationId($rIDimg); - - $this->_elementCollection[] = $object; - return $object; - } else { - trigger_error('Source does not exist or unsupported object type.'); - } - } - - /** - * Add a Image Element - * - * @param string $src - * @param mixed $style - * @return PHPWord_Section_Image - */ - public function addImage($src, $style = null) - { - $image = new PHPWord_Section_Image($src, $style); - - if (!is_null($image->getSource())) { - $rID = PHPWord_Media::addSectionMediaElement($src, 'image'); - $image->setRelationId($rID); - - $this->_elementCollection[] = $image; - return $image; - } else { - trigger_error('Source does not exist or unsupported image type.'); - } - } - - /** - * Add a by PHP created Image Element - * - * @param string $link - * @param mixed $style - * @return PHPWord_Section_MemoryImage - */ - public function addMemoryImage($link, $style = null) - { - $memoryImage = new PHPWord_Section_MemoryImage($link, $style); - if (!is_null($memoryImage->getSource())) { - $rID = PHPWord_Media::addSectionMediaElement($link, 'image', $memoryImage); - $memoryImage->setRelationId($rID); - - $this->_elementCollection[] = $memoryImage; - return $memoryImage; - } else { - trigger_error('Unsupported image type.'); - } - } - - /** - * Add a Table-of-Contents Element - * - * @param mixed $styleFont - * @param mixed $styleTOC - * @return PHPWord_TOC - */ - public function addTOC($styleFont = null, $styleTOC = null) - { - $toc = new PHPWord_TOC($styleFont, $styleTOC); - $this->_elementCollection[] = $toc; - return $toc; - } - - /** - * Add a Title Element - * - * @param string $text - * @param int $depth - * @return PHPWord_Section_Title - */ - public function addTitle($text, $depth = 1) - { - $text = utf8_encode($text); - $styles = PHPWord_Style::getStyles(); - if (array_key_exists('Heading_' . $depth, $styles)) { - $style = 'Heading' . $depth; - } else { - $style = null; - } - - $title = new PHPWord_Section_Title($text, $depth, $style); - - $data = PHPWord_TOC::addTitle($text, $depth); - $anchor = $data[0]; - $bookmarkId = $data[1]; - - $title->setAnchor($anchor); - $title->setBookmarkId($bookmarkId); - - $this->_elementCollection[] = $title; - return $title; - } - - /** - * Create a new TextRun - * - * @return PHPWord_Section_TextRun - */ - public function createTextRun($styleParagraph = null) - { - $textRun = new PHPWord_Section_TextRun($styleParagraph); - $this->_elementCollection[] = $textRun; - return $textRun; - } - - /** - * Get all Elements - * - * @return array - */ - public function getElements() - { - return $this->_elementCollection; - } - - /** - * Create a new Header - * - * @return PHPWord_Section_Header - */ - public function createHeader() - { - $header = new PHPWord_Section_Header($this->_sectionCount); - $this->_header = $header; - return $header; - } - - /** - * Get Header - * - * @return PHPWord_Section_Header - */ - public function getHeader() - { - return $this->_header; - } - - /** - * Create a new Footer - * - * @return PHPWord_Section_Footer - */ - public function createFooter() - { - $footer = new PHPWord_Section_Footer($this->_sectionCount); - $this->_footer = $footer; - return $footer; - } - - /** - * Get Footer - * - * @return PHPWord_Section_Footer - */ - public function getFooter() - { - return $this->_footer; - } -} diff --git a/src/PHPWord/Section/Footer.php b/src/PHPWord/Section/Footer.php deleted file mode 100644 index a21031fc..00000000 --- a/src/PHPWord/Section/Footer.php +++ /dev/null @@ -1,208 +0,0 @@ -_footerCount = $sectionCount; - } - - /** - * Add a Text Element - * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return PHPWord_Section_Text - */ - public function addText($text, $styleFont = null, $styleParagraph = null) - { - $givenText = utf8_encode($text); - $text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add a TextBreak Element - * - * @param int $count - */ - public function addTextBreak($count = 1) - { - for ($i = 1; $i <= $count; $i++) { - $this->_elementCollection[] = new PHPWord_Section_TextBreak(); - } - } - - /** - * Create a new TextRun - * - * @return PHPWord_Section_TextRun - */ - public function createTextRun($styleParagraph = null) - { - $textRun = new PHPWord_Section_TextRun($styleParagraph); - $this->_elementCollection[] = $textRun; - return $textRun; - } - - /** - * Add a Table Element - * - * @param mixed $style - * @return PHPWord_Section_Table - */ - public function addTable($style = null) - { - $table = new PHPWord_Section_Table('footer', $this->_footerCount, $style); - $this->_elementCollection[] = $table; - return $table; - } - - /** - * Add a Image Element - * - * @param string $src - * @param mixed $style - * @return PHPWord_Section_Image - */ - public function addImage($src, $style = null) - { - $image = new PHPWord_Section_Image($src, $style); - - if (!is_null($image->getSource())) { - $rID = PHPWord_Media::addFooterMediaElement($this->_footerCount, $src); - $image->setRelationId($rID); - - $this->_elementCollection[] = $image; - return $image; - } else { - trigger_error('Src does not exist or invalid image type.', E_ERROR); - } - } - - /** - * Add a by PHP created Image Element - * - * @param string $link - * @param mixed $style - * @return PHPWord_Section_MemoryImage - */ - public function addMemoryImage($link, $style = null) - { - $memoryImage = new PHPWord_Section_MemoryImage($link, $style); - if (!is_null($memoryImage->getSource())) { - $rID = PHPWord_Media::addFooterMediaElement($this->_footerCount, $link, $memoryImage); - $memoryImage->setRelationId($rID); - - $this->_elementCollection[] = $memoryImage; - return $memoryImage; - } else { - trigger_error('Unsupported image type.'); - } - } - - /** - * Add a PreserveText Element - * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return PHPWord_Section_Footer_PreserveText - */ - public function addPreserveText($text, $styleFont = null, $styleParagraph = null) - { - $text = utf8_encode($text); - $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $ptext; - return $ptext; - } - - /** - * Get Footer Relation ID - */ - public function getRelationId() - { - return $this->_rId; - } - - /** - * Set Footer Relation ID - * - * @param int $rId - */ - public function setRelationId($rId) - { - $this->_rId = $rId; - } - - /** - * Get all Footer Elements - */ - public function getElements() - { - return $this->_elementCollection; - } - - /** - * Get Footer Count - */ - public function getFooterCount() - { - return $this->_footerCount; - } -} diff --git a/src/PHPWord/Section/Header.php b/src/PHPWord/Section/Header.php deleted file mode 100644 index fff730fd..00000000 --- a/src/PHPWord/Section/Header.php +++ /dev/null @@ -1,230 +0,0 @@ -_headerCount = $sectionCount; - } - - /** - * Add a Text Element - * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return PHPWord_Section_Text - */ - public function addText($text, $styleFont = null, $styleParagraph = null) - { - $givenText = utf8_encode($text); - $text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add a TextBreak Element - * - * @param int $count - */ - public function addTextBreak($count = 1) - { - for ($i = 1; $i <= $count; $i++) { - $this->_elementCollection[] = new PHPWord_Section_TextBreak(); - } - } - - /** - * Create a new TextRun - * - * @return PHPWord_Section_TextRun - */ - public function createTextRun($styleParagraph = null) - { - $textRun = new PHPWord_Section_TextRun($styleParagraph); - $this->_elementCollection[] = $textRun; - return $textRun; - } - - /** - * Add a Table Element - * - * @param mixed $style - * @return PHPWord_Section_Table - */ - public function addTable($style = null) - { - $table = new PHPWord_Section_Table('header', $this->_headerCount, $style); - $this->_elementCollection[] = $table; - return $table; - } - - /** - * Add a Image Element - * - * @param string $src - * @param mixed $style - * @return PHPWord_Section_Image - */ - public function addImage($src, $style = null) - { - $image = new PHPWord_Section_Image($src, $style); - - if (!is_null($image->getSource())) { - $rID = PHPWord_Media::addHeaderMediaElement($this->_headerCount, $src); - $image->setRelationId($rID); - - $this->_elementCollection[] = $image; - return $image; - } else { - trigger_error('Src does not exist or invalid image type.', E_ERROR); - } - } - - /** - * Add a by PHP created Image Element - * - * @param string $link - * @param mixed $style - * @return PHPWord_Section_MemoryImage - */ - public function addMemoryImage($link, $style = null) - { - $memoryImage = new PHPWord_Section_MemoryImage($link, $style); - if (!is_null($memoryImage->getSource())) { - $rID = PHPWord_Media::addHeaderMediaElement($this->_headerCount, $link, $memoryImage); - $memoryImage->setRelationId($rID); - - $this->_elementCollection[] = $memoryImage; - return $memoryImage; - } else { - trigger_error('Unsupported image type.'); - } - } - - /** - * Add a PreserveText Element - * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return PHPWord_Section_Footer_PreserveText - */ - public function addPreserveText($text, $styleFont = null, $styleParagraph = null) - { - $text = utf8_encode($text); - $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $ptext; - return $ptext; - } - - /** - * Add a Watermark Element - * - * @param string $src - * @param mixed $style - * @return PHPWord_Section_Image - */ - public function addWatermark($src, $style = null) - { - $image = new PHPWord_Section_Image($src, $style, true); - - if (!is_null($image->getSource())) { - $rID = PHPWord_Media::addHeaderMediaElement($this->_headerCount, $src); - $image->setRelationId($rID); - - $this->_elementCollection[] = $image; - return $image; - } else { - trigger_error('Src does not exist or invalid image type.', E_ERROR); - } - } - - /** - * Get Header Relation ID - */ - public function getRelationId() - { - return $this->_rId; - } - - /** - * Set Header Relation ID - * - * @param int $rId - */ - public function setRelationId($rId) - { - $this->_rId = $rId; - } - - /** - * Get all Header Elements - */ - public function getElements() - { - return $this->_elementCollection; - } - - /** - * Get Header Count - */ - public function getHeaderCount() - { - return $this->_headerCount; - } -} diff --git a/src/PHPWord/Section/Table/Cell.php b/src/PHPWord/Section/Table/Cell.php deleted file mode 100644 index 48c9de81..00000000 --- a/src/PHPWord/Section/Table/Cell.php +++ /dev/null @@ -1,327 +0,0 @@ -_insideOf = $insideOf; - $this->_pCount = $pCount; - $this->_width = $width; - - if (!is_null($style)) { - if (is_array($style)) { - $this->_style = new PHPWord_Style_Cell(); - - foreach ($style as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->_style->setStyleValue($key, $value); - } - } else { - $this->_style = $style; - } - } - } - - /** - * Add a Text Element - * - * @param string $text - * @param mixed $style - * @return PHPWord_Section_Text - */ - public function addText($text, $styleFont = null, $styleParagraph = null) - { - $text = utf8_encode($text); - $text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add a Link Element - * - * @param string $linkSrc - * @param string $linkName - * @param mixed $style - * @return PHPWord_Section_Link - */ - public function addLink($linkSrc, $linkName = null, $style = null) - { - if ($this->_insideOf == 'section') { - $linkSrc = utf8_encode($linkSrc); - if (!is_null($linkName)) { - $linkName = utf8_encode($linkName); - } - - $link = new PHPWord_Section_Link($linkSrc, $linkName, $style); - $rID = PHPWord_Media::addSectionLinkElement($linkSrc); - $link->setRelationId($rID); - - $this->_elementCollection[] = $link; - return $link; - } else { - trigger_error('Unsupported Link header / footer reference'); - return false; - } - } - - /** - * Add a TextBreak Element - * - * @param int $count - */ - public function addTextBreak() - { - $this->_elementCollection[] = new PHPWord_Section_TextBreak(); - } - - /** - * Add a ListItem Element - * - * @param string $text - * @param int $depth - * @param mixed $styleText - * @param mixed $styleList - * @return PHPWord_Section_ListItem - */ - public function addListItem($text, $depth = 0, $styleText = null, $styleList = null) - { - $text = utf8_encode($text); - $listItem = new PHPWord_Section_ListItem($text, $depth, $styleText, $styleList); - $this->_elementCollection[] = $listItem; - return $listItem; - } - - /** - * Add a Image Element - * - * @param string $src - * @param mixed $style - * @return PHPWord_Section_Image - */ - public function addImage($src, $style = null) - { - $image = new PHPWord_Section_Image($src, $style); - - if (!is_null($image->getSource())) { - if ($this->_insideOf == 'section') { - $rID = PHPWord_Media::addSectionMediaElement($src, 'image'); - } elseif ($this->_insideOf == 'header') { - $rID = PHPWord_Media::addHeaderMediaElement($this->_pCount, $src); - } elseif ($this->_insideOf == 'footer') { - $rID = PHPWord_Media::addFooterMediaElement($this->_pCount, $src); - } - $image->setRelationId($rID); - - $this->_elementCollection[] = $image; - return $image; - } else { - trigger_error('Source does not exist or unsupported image type.'); - } - } - - /** - * Add a by PHP created Image Element - * - * @param string $link - * @param mixed $style - * @return PHPWord_Section_MemoryImage - */ - public function addMemoryImage($link, $style = null) - { - $memoryImage = new PHPWord_Section_MemoryImage($link, $style); - if (!is_null($memoryImage->getSource())) { - if ($this->_insideOf == 'section') { - $rID = PHPWord_Media::addSectionMediaElement($link, 'image', $memoryImage); - } elseif ($this->_insideOf == 'header') { - $rID = PHPWord_Media::addHeaderMediaElement($this->_pCount, $link, $memoryImage); - } elseif ($this->_insideOf == 'footer') { - $rID = PHPWord_Media::addFooterMediaElement($this->_pCount, $link, $memoryImage); - } - $memoryImage->setRelationId($rID); - - $this->_elementCollection[] = $memoryImage; - return $memoryImage; - } else { - trigger_error('Unsupported image type.'); - } - } - - /** - * Add a OLE-Object Element - * - * @param string $src - * @param mixed $style - * @return PHPWord_Section_Object - */ - public function addObject($src, $style = null) - { - $object = new PHPWord_Section_Object($src, $style); - - if (!is_null($object->getSource())) { - $inf = pathinfo($src); - $ext = $inf['extension']; - if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { - $ext = substr($ext, 0, -1); - } - - $iconSrc = PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/'; - if (!file_exists($iconSrc . '_' . $ext . '.png')) { - $iconSrc = $iconSrc . '_default.png'; - } else { - $iconSrc .= '_' . $ext . '.png'; - } - - $rIDimg = PHPWord_Media::addSectionMediaElement($iconSrc, 'image'); - $data = PHPWord_Media::addSectionMediaElement($src, 'oleObject'); - $rID = $data[0]; - $objectId = $data[1]; - - $object->setRelationId($rID); - $object->setObjectId($objectId); - $object->setImageRelationId($rIDimg); - - $this->_elementCollection[] = $object; - return $object; - } else { - trigger_error('Source does not exist or unsupported object type.'); - } - } - - /** - * Add a PreserveText Element - * - * @param string $text - * @param mixed $styleFont - * @param mixed $styleParagraph - * @return PHPWord_Section_Footer_PreserveText - */ - public function addPreserveText($text, $styleFont = null, $styleParagraph = null) - { - if ($this->_insideOf == 'footer' || $this->_insideOf == 'header') { - $text = utf8_encode($text); - $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph); - $this->_elementCollection[] = $ptext; - return $ptext; - } else { - trigger_error('addPreserveText only supported in footer/header.'); - } - } - - /** - * Create a new TextRun - * - * @return PHPWord_Section_TextRun - */ - public function createTextRun($styleParagraph = null) - { - $textRun = new PHPWord_Section_TextRun($styleParagraph); - $this->_elementCollection[] = $textRun; - return $textRun; - } - - /** - * Get all Elements - * - * @return array - */ - public function getElements() - { - return $this->_elementCollection; - } - - /** - * Get Cell Style - * - * @return PHPWord_Style_Cell - */ - public function getStyle() - { - return $this->_style; - } - - /** - * Get Cell width - * - * @return int - */ - public function getWidth() - { - return $this->_width; - } -} \ No newline at end of file diff --git a/src/PHPWord/Section/TextRun.php b/src/PHPWord/Section/TextRun.php deleted file mode 100644 index 2ff89044..00000000 --- a/src/PHPWord/Section/TextRun.php +++ /dev/null @@ -1,129 +0,0 @@ -_elementCollection = array(); - - // Set paragraph style - if (is_array($styleParagraph)) { - $this->_styleParagraph = new PHPWord_Style_Paragraph(); - - foreach ($styleParagraph as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->_styleParagraph->setStyleValue($key, $value); - } - } else { - $this->_styleParagraph = $styleParagraph; - } - } - - - /** - * Add a Text Element - * - * @var string $text - * @var mixed $styleFont - * @return PHPWord_Section_Text - */ - public function addText($text = null, $styleFont = null) - { - $givenText = utf8_encode($text); - $text = new PHPWord_Section_Text($givenText, $styleFont); - $this->_elementCollection[] = $text; - return $text; - } - - /** - * Add a Link Element - * - * @param string $linkSrc - * @param string $linkName - * @param mixed $styleFont - * @return PHPWord_Section_Link - */ - public function addLink($linkSrc, $linkName = null, $styleFont = null) - { - $linkSrc = utf8_encode($linkSrc); - if (!is_null($linkName)) { - $linkName = utf8_encode($linkName); - } - - $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont); - $rID = PHPWord_Media::addSectionLinkElement($linkSrc); - $link->setRelationId($rID); - - $this->_elementCollection[] = $link; - return $link; - } - - /** - * Get TextRun content - * - * @return string - */ - public function getElements() - { - return $this->_elementCollection; - } - - /** - * Get Paragraph style - * - * @return PHPWord_Style_Paragraph - */ - public function getParagraphStyle() - { - return $this->_styleParagraph; - } -} diff --git a/src/PHPWord/Shared/String.php b/src/PHPWord/Shared/String.php deleted file mode 100644 index 1e703893..00000000 --- a/src/PHPWord/Shared/String.php +++ /dev/null @@ -1,270 +0,0 @@ -) - * element or in the shared string element. - * - * @param string $value Value to unescape - * @return string - */ - public static function ControlCharacterOOXML2PHP($value = '') - { - if (empty(self::$_controlCharacters)) { - self::_buildControlCharacters(); - } - - return str_replace(array_keys(self::$_controlCharacters), array_values(self::$_controlCharacters), $value); - } - - /** - * Convert from PHP control character to OpenXML escaped control character - * - * Excel 2007 team: - * ---------------- - * That's correct, control characters are stored directly in the shared-strings table. - * We do encode characters that cannot be represented in XML using the following escape sequence: - * _xHHHH_ where H represents a hexadecimal character in the character's value... - * So you could end up with something like _x0008_ in a string (either in a cell value () - * element or in the shared string element. - * - * @param string $value Value to escape - * @return string - */ - public static function ControlCharacterPHP2OOXML($value = '') - { - if (empty(self::$_controlCharacters)) { - self::_buildControlCharacters(); - } - - return str_replace(array_values(self::$_controlCharacters), array_keys(self::$_controlCharacters), $value); - } - - /** - * Check if a string contains UTF8 data - * - * @param string $value - * @return boolean - */ - public static function IsUTF8($value = '') - { - return utf8_encode(utf8_decode($value)) === $value; - } - - /** - * Formats a numeric value as a string for output in various output writers - * - * @param mixed $value - * @return string - */ - public static function FormatNumber($value) - { - return number_format($value, 2, '.', ''); - } - - /** - * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length) - * Writes the string using uncompressed notation, no rich text, no Asian phonetics - * If mbstring extension is not available, ASCII is assumed, and compressed notation is used - * although this will give wrong results for non-ASCII strings - * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3 - * - * @param string $value UTF-8 encoded string - * @return string - */ - public static function UTF8toBIFF8UnicodeShort($value) - { - // character count - $ln = self::CountCharacters($value, 'UTF-8'); - - // option flags - $opt = (self::getIsMbstringEnabled() || self::getIsIconvEnabled()) ? - 0x0001 : 0x0000; - - // characters - $chars = self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8'); - - $data = pack('CC', $ln, $opt) . $chars; - return $data; - } - - /** - * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length) - * Writes the string using uncompressed notation, no rich text, no Asian phonetics - * If mbstring extension is not available, ASCII is assumed, and compressed notation is used - * although this will give wrong results for non-ASCII strings - * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3 - * - * @param string $value UTF-8 encoded string - * @return string - */ - public static function UTF8toBIFF8UnicodeLong($value) - { - // character count - $ln = self::CountCharacters($value, 'UTF-8'); - - // option flags - $opt = (self::getIsMbstringEnabled() || self::getIsIconvEnabled()) ? - 0x0001 : 0x0000; - - // characters - $chars = self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8'); - - $data = pack('vC', $ln, $opt) . $chars; - return $data; - } - - /** - * Convert string from one encoding to another. First try mbstring, then iconv, or no convertion - * - * @param string $value - * @param string $to Encoding to convert to, e.g. 'UTF-8' - * @param string $from Encoding to convert from, e.g. 'UTF-16LE' - * @return string - */ - public static function ConvertEncoding($value, $to, $from) - { - if (self::getIsMbstringEnabled()) { - $value = mb_convert_encoding($value, $to, $from); - return $value; - } - - if (self::getIsIconvEnabled()) { - $value = iconv($from, $to, $value); - return $value; - } - - // else, no conversion - return $value; - } - - /** - * Get character count. First try mbstring, then iconv, finally strlen - * - * @param string $value - * @param string $enc Encoding - * @return int Character count - */ - public static function CountCharacters($value, $enc = 'UTF-8') - { - if (self::getIsMbstringEnabled()) { - $count = mb_strlen($value, $enc); - return $count; - } - - if (self::getIsIconvEnabled()) { - $count = iconv_strlen($value, $enc); - return $count; - } - - // else strlen - $count = strlen($value); - return $count; - } - -} diff --git a/src/PHPWord/Style/Cell.php b/src/PHPWord/Style/Cell.php deleted file mode 100644 index 0c294f29..00000000 --- a/src/PHPWord/Style/Cell.php +++ /dev/null @@ -1,316 +0,0 @@ -_valign = null; - $this->_textDirection = null; - $this->_bgColor = null; - $this->_borderTopSize = null; - $this->_borderTopColor = null; - $this->_borderLeftSize = null; - $this->_borderLeftColor = null; - $this->_borderRightSize = null; - $this->_borderRightColor = null; - $this->_borderBottomSize = null; - $this->_borderBottomColor = null; - $this->_defaultBorderColor = '000000'; - } - - /** - * Set style value - * - * @var string $key - * @var mixed $value - */ - public function setStyleValue($key, $value) - { - if ($key == '_borderSize') { - $this->setBorderSize($value); - } elseif ($key == '_borderColor') { - $this->setBorderColor($value); - } else { - $this->$key = $value; - } - } - - public function getVAlign() - { - return $this->_valign; - } - - public function setVAlign($pValue = null) - { - $this->_valign = $pValue; - } - - public function getTextDirection() - { - return $this->_textDirection; - } - - public function setTextDirection($pValue = null) - { - $this->_textDirection = $pValue; - } - - public function getBgColor() - { - return $this->_bgColor; - } - - public function setBgColor($pValue = null) - { - $this->_bgColor = $pValue; - } - - public function setHeight($pValue = null) - { - $this->_height = $pValue; - } - - public function setBorderSize($pValue = null) - { - $this->_borderTopSize = $pValue; - $this->_borderLeftSize = $pValue; - $this->_borderRightSize = $pValue; - $this->_borderBottomSize = $pValue; - } - - public function getBorderSize() - { - $t = $this->getBorderTopSize(); - $l = $this->getBorderLeftSize(); - $r = $this->getBorderRightSize(); - $b = $this->getBorderBottomSize(); - - return array($t, $l, $r, $b); - } - - public function setBorderColor($pValue = null) - { - $this->_borderTopColor = $pValue; - $this->_borderLeftColor = $pValue; - $this->_borderRightColor = $pValue; - $this->_borderBottomColor = $pValue; - } - - public function getBorderColor() - { - $t = $this->getBorderTopColor(); - $l = $this->getBorderLeftColor(); - $r = $this->getBorderRightColor(); - $b = $this->getBorderBottomColor(); - - return array($t, $l, $r, $b); - } - - public function setBorderTopSize($pValue = null) - { - $this->_borderTopSize = $pValue; - } - - public function getBorderTopSize() - { - return $this->_borderTopSize; - } - - public function setBorderTopColor($pValue = null) - { - $this->_borderTopColor = $pValue; - } - - public function getBorderTopColor() - { - return $this->_borderTopColor; - } - - - public function setBorderLeftSize($pValue = null) - { - $this->_borderLeftSize = $pValue; - } - - public function getBorderLeftSize() - { - return $this->_borderLeftSize; - } - - public function setBorderLeftColor($pValue = null) - { - $this->_borderLeftColor = $pValue; - } - - public function getBorderLeftColor() - { - return $this->_borderLeftColor; - } - - - public function setBorderRightSize($pValue = null) - { - $this->_borderRightSize = $pValue; - } - - public function getBorderRightSize() - { - return $this->_borderRightSize; - } - - public function setBorderRightColor($pValue = null) - { - $this->_borderRightColor = $pValue; - } - - public function getBorderRightColor() - { - return $this->_borderRightColor; - } - - - public function setBorderBottomSize($pValue = null) - { - $this->_borderBottomSize = $pValue; - } - - public function getBorderBottomSize() - { - return $this->_borderBottomSize; - } - - public function setBorderBottomColor($pValue = null) - { - $this->_borderBottomColor = $pValue; - } - - public function getBorderBottomColor() - { - return $this->_borderBottomColor; - } - - public function getDefaultBorderColor() - { - return $this->_defaultBorderColor; - } -} diff --git a/src/PHPWord/Style/Paragraph.php b/src/PHPWord/Style/Paragraph.php deleted file mode 100644 index c92933d2..00000000 --- a/src/PHPWord/Style/Paragraph.php +++ /dev/null @@ -1,213 +0,0 @@ -_align = null; - $this->_spaceBefore = null; - $this->_spaceAfter = null; - $this->_spacing = null; - $this->_indent = null; - } - - /** - * Set Style value - * - * @param string $key - * @param mixed $value - */ - public function setStyleValue($key, $value) - { - if ($key == '_spacing') { - $value += 240; // because line height of 1 matches 240 twips - } - if ($key == '_indent') { - $value = (int)$value * 720; // 720 twips per indent - } - $this->$key = $value; - } - - /** - * Get Paragraph Alignment - * - * @return string - */ - public function getAlign() - { - return $this->_align; - } - - /** - * Set Paragraph Alignment - * - * @param string $pValue - * @return PHPWord_Style_Paragraph - */ - public function setAlign($pValue = null) - { - if (strtolower($pValue) == 'justify') { - // justify becames both - $pValue = 'both'; - } - $this->_align = $pValue; - return $this; - } - - /** - * Get Space before Paragraph - * - * @return string - */ - public function getSpaceBefore() - { - return $this->_spaceBefore; - } - - /** - * Set Space before Paragraph - * - * @param int $pValue - * @return PHPWord_Style_Paragraph - */ - public function setSpaceBefore($pValue = null) - { - $this->_spaceBefore = $pValue; - return $this; - } - - /** - * Get Space after Paragraph - * - * @return string - */ - public function getSpaceAfter() - { - return $this->_spaceAfter; - } - - /** - * Set Space after Paragraph - * - * @param int $pValue - * @return PHPWord_Style_Paragraph - */ - public function setSpaceAfter($pValue = null) - { - $this->_spaceAfter = $pValue; - return $this; - } - - /** - * Get Spacing between breaks - * - * @return int - */ - public function getSpacing() - { - return $this->_spacing; - } - - /** - * Set Spacing between breaks - * - * @param int $pValue - * @return PHPWord_Style_Paragraph - */ - public function setSpacing($pValue = null) - { - $this->_spacing = $pValue; - return $this; - } - - /** - * Get indentation - * - * @return int - */ - public function getIndent() - { - return $this->_indent; - } - - /** - * Set indentation - * - * @param int $pValue - * @return PHPWord_Style_Paragraph - */ - public function setIndent($pValue = null) - { - $this->_indent = $pValue; - return $this; - } -} diff --git a/src/PHPWord/Writer/Word2007.php b/src/PHPWord/Writer/Word2007.php deleted file mode 100644 index bb9b9da6..00000000 --- a/src/PHPWord/Writer/Word2007.php +++ /dev/null @@ -1,249 +0,0 @@ -_document = $PHPWord; - - $this->_diskCachingDirectory = './'; - - $this->_writerParts['contenttypes'] = new PHPWord_Writer_Word2007_ContentTypes(); - $this->_writerParts['rels'] = new PHPWord_Writer_Word2007_Rels(); - $this->_writerParts['docprops'] = new PHPWord_Writer_Word2007_DocProps(); - $this->_writerParts['documentrels'] = new PHPWord_Writer_Word2007_DocumentRels(); - $this->_writerParts['document'] = new PHPWord_Writer_Word2007_Document(); - $this->_writerParts['styles'] = new PHPWord_Writer_Word2007_Styles(); - $this->_writerParts['header'] = new PHPWord_Writer_Word2007_Header(); - $this->_writerParts['footer'] = new PHPWord_Writer_Word2007_Footer(); - - foreach ($this->_writerParts as $writer) { - $writer->setParentWriter($this); - } - } - - public function save($pFilename = null) - { - if (!is_null($this->_document)) { - - // If $pFilename is php://output or php://stdout, make it a temporary file... - $originalFilename = $pFilename; - if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { - $pFilename = @tempnam('./', 'phppttmp'); - if ($pFilename == '') { - $pFilename = $originalFilename; - } - } - - // Create new ZIP file and open it for writing - $objZip = new ZipArchive(); - - // Try opening the ZIP file - if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) { - if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !== true) { - throw new Exception("Could not open " . $pFilename . " for writing."); - } - } - - - $sectionElements = array(); - $_secElements = PHPWord_Media::getSectionMediaElements(); - foreach ($_secElements as $element) { // loop through section media elements - if ($element['type'] != 'hyperlink') { - $this->_addFileToPackage($objZip, $element); - } - $sectionElements[] = $element; - } - - $_hdrElements = PHPWord_Media::getHeaderMediaElements(); - foreach ($_hdrElements as $_headerFile => $_hdrMedia) { // loop through headers - if (count($_hdrMedia) > 0) { - $objZip->addFromString('word/_rels/' . $_headerFile . '.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_hdrMedia)); - foreach ($_hdrMedia as $element) { // loop through header media elements - $this->_addFileToPackage($objZip, $element); - } - } - } - - $_ftrElements = PHPWord_Media::getFooterMediaElements(); - foreach ($_ftrElements as $_footerFile => $_ftrMedia) { // loop through footers - if (count($_ftrMedia) > 0) { - $objZip->addFromString('word/_rels/' . $_footerFile . '.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_ftrMedia)); - foreach ($_ftrMedia as $element) { // loop through footers media elements - $this->_addFileToPackage($objZip, $element); - } - } - } - - - $_cHdrs = 0; - $_cFtrs = 0; - $rID = PHPWord_Media::countSectionMediaElements() + 6; - $_sections = $this->_document->getSections(); - - foreach ($_sections as $section) { - $_header = $section->getHeader(); - if (!is_null($_header)) { - $_cHdrs++; - $_header->setRelationId(++$rID); - $_headerCount = $_header->getHeaderCount(); - $_headerFile = 'header' . $_headerCount . '.xml'; - $sectionElements[] = array('target' => $_headerFile, 'type' => 'header', 'rID' => $rID); - $objZip->addFromString('word/' . $_headerFile, $this->getWriterPart('header')->writeHeader($_header)); - } - - $_footer = $section->getFooter(); - if (!is_null($_footer)) { - $_cFtrs++; - $_footer->setRelationId(++$rID); - $_footerCount = $_footer->getFooterCount(); - $_footerFile = 'footer' . $_footerCount . '.xml'; - $sectionElements[] = array('target' => $_footerFile, 'type' => 'footer', 'rID' => $rID); - $objZip->addFromString('word/' . $_footerFile, $this->getWriterPart('footer')->writeFooter($_footer)); - } - } - - // build docx file - // Write dynamic files - $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes($this->_imageTypes, $this->_objectTypes, $_cHdrs, $_cFtrs)); - $objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeRelationships($this->_document)); - $objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->_document)); - $objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->_document)); - $objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->_document)); - $objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('documentrels')->writeDocumentRels($sectionElements)); - $objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->_document)); - - // Write static files - $objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/numbering.xml', 'word/numbering.xml'); - $objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/settings.xml', 'word/settings.xml'); - $objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/theme1.xml', 'word/theme/theme1.xml'); - $objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/webSettings.xml', 'word/webSettings.xml'); - $objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/fontTable.xml', 'word/fontTable.xml'); - - - // Close file - if ($objZip->close() === false) { - throw new Exception("Could not close zip file $pFilename."); - } - - // If a temporary file was used, copy it to the correct file stream - if ($originalFilename != $pFilename) { - if (copy($pFilename, $originalFilename) === false) { - throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename."); - } - @unlink($pFilename); - } - } else { - throw new Exception("PHPWord object unassigned."); - } - } - - private function _chkContentTypes($src) - { - $srcInfo = pathinfo($src); - $extension = strtolower($srcInfo['extension']); - if (substr($extension, 0, 3) == 'php') { - $extension = 'php'; - } - $_supportedImageTypes = array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'tif', 'tiff', 'php'); - - if (in_array($extension, $_supportedImageTypes)) { - $imagedata = getimagesize($src); - $imagetype = image_type_to_mime_type($imagedata[2]); - $imageext = image_type_to_extension($imagedata[2]); - $imageext = str_replace('.', '', $imageext); - if ($imageext == 'jpeg') $imageext = 'jpg'; - - if (!in_array($imagetype, $this->_imageTypes)) { - $this->_imageTypes[$imageext] = $imagetype; - } - } else { - if (!in_array($extension, $this->_objectTypes)) { - $this->_objectTypes[] = $extension; - } - } - } - - public function getWriterPart($pPartName = '') - { - if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) { - return $this->_writerParts[strtolower($pPartName)]; - } else { - return null; - } - } - - public function getUseDiskCaching() - { - return $this->_useDiskCaching; - } - - public function setUseDiskCaching($pValue = false, $pDirectory = null) - { - $this->_useDiskCaching = $pValue; - - if (!is_null($pDirectory)) { - if (is_dir($pDirectory)) { - $this->_diskCachingDirectory = $pDirectory; - } else { - throw new Exception("Directory does not exist: $pDirectory"); - } - } - - return $this; - } - - private function _addFileToPackage($objZip, $element) - { - if (isset($element['isMemImage']) && $element['isMemImage']) { - $image = call_user_func($element['createfunction'], $element['source']); - ob_start(); - call_user_func($element['imagefunction'], $image); - $imageContents = ob_get_contents(); - ob_end_clean(); - $objZip->addFromString('word/' . $element['target'], $imageContents); - imagedestroy($image); - - $this->_chkContentTypes($element['source']); - } else { - $objZip->addFile($element['source'], 'word/' . $element['target']); - $this->_chkContentTypes($element['source']); - } - } -} diff --git a/src/PHPWord/Writer/Word2007/Base.php b/src/PHPWord/Writer/Word2007/Base.php deleted file mode 100644 index be65c762..00000000 --- a/src/PHPWord/Writer/Word2007/Base.php +++ /dev/null @@ -1,782 +0,0 @@ -getFontStyle(); - - $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; - - if (!$withoutP) { - $objWriter->startElement('w:p'); - - $styleParagraph = $text->getParagraphStyle(); - $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false; - - if ($SpIsObject) { - $this->_writeParagraphStyle($objWriter, $styleParagraph); - } elseif (!$SpIsObject && !is_null($styleParagraph)) { - $objWriter->startElement('w:pPr'); - $objWriter->startElement('w:pStyle'); - $objWriter->writeAttribute('w:val', $styleParagraph); - $objWriter->endElement(); - $objWriter->endElement(); - } - } - - $strText = htmlspecialchars($text->getText()); - $strText = PHPWord_Shared_String::ControlCharacterPHP2OOXML($strText); - - $objWriter->startElement('w:r'); - - if ($SfIsObject) { - $this->_writeTextStyle($objWriter, $styleFont); - } elseif (!$SfIsObject && !is_null($styleFont)) { - $objWriter->startElement('w:rPr'); - $objWriter->startElement('w:rStyle'); - $objWriter->writeAttribute('w:val', $styleFont); - $objWriter->endElement(); - $objWriter->endElement(); - } - - $objWriter->startElement('w:t'); - $objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text - $objWriter->writeRaw($strText); - $objWriter->endElement(); - - $objWriter->endElement(); // w:r - - if (!$withoutP) { - $objWriter->endElement(); // w:p - } - } - - protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun) - { - $elements = $textrun->getElements(); - $styleParagraph = $textrun->getParagraphStyle(); - - $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false; - - $objWriter->startElement('w:p'); - - if ($SpIsObject) { - $this->_writeParagraphStyle($objWriter, $styleParagraph); - } elseif (!$SpIsObject && !is_null($styleParagraph)) { - $objWriter->startElement('w:pPr'); - $objWriter->startElement('w:pStyle'); - $objWriter->writeAttribute('w:val', $styleParagraph); - $objWriter->endElement(); - $objWriter->endElement(); - } - - if (count($elements) > 0) { - foreach ($elements as $element) { - if ($element instanceof PHPWord_Section_Text) { - $this->_writeText($objWriter, $element, true); - } elseif ($element instanceof PHPWord_Section_Link) { - $this->_writeLink($objWriter, $element, true); - } - } - } - - $objWriter->endElement(); - } - - protected function _writeParagraphStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Paragraph $style, $withoutPPR = false) - { - $align = $style->getAlign(); - $spaceBefore = $style->getSpaceBefore(); - $spaceAfter = $style->getSpaceAfter(); - $spacing = $style->getSpacing(); - - - if (!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter)) { - - if (!$withoutPPR) { - $objWriter->startElement('w:pPr'); - } - - if (!is_null($align)) { - $objWriter->startElement('w:jc'); - $objWriter->writeAttribute('w:val', $align); - $objWriter->endElement(); - } - - if (!is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($spacing)) { - - $objWriter->startElement('w:spacing'); - - if (!is_null($spaceBefore)) { - $objWriter->writeAttribute('w:before', $spaceBefore); - } - if (!is_null($spaceAfter)) { - $objWriter->writeAttribute('w:after', $spaceAfter); - } - if (!is_null($spacing)) { - $objWriter->writeAttribute('w:line', $spacing); - $objWriter->writeAttribute('w:lineRule', 'auto'); - } - - $objWriter->endElement(); - } - - if (!$withoutPPR) { - $objWriter->endElement(); // w:pPr - } - } - } - - protected function _writeLink(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Link $link, $withoutP = false) - { - $rID = $link->getRelationId(); - $linkName = $link->getLinkName(); - if (is_null($linkName)) { - $linkName = $link->getLinkSrc(); - } - - $styleFont = $link->getFontStyle(); - $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; - - if (!$withoutP) { - $objWriter->startElement('w:p'); - - $styleParagraph = $link->getParagraphStyle(); - $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false; - - if ($SpIsObject) { - $this->_writeParagraphStyle($objWriter, $styleParagraph); - } elseif (!$SpIsObject && !is_null($styleParagraph)) { - $objWriter->startElement('w:pPr'); - $objWriter->startElement('w:pStyle'); - $objWriter->writeAttribute('w:val', $styleParagraph); - $objWriter->endElement(); - $objWriter->endElement(); - } - } - - $objWriter->startElement('w:hyperlink'); - $objWriter->writeAttribute('r:id', 'rId' . $rID); - $objWriter->writeAttribute('w:history', '1'); - - $objWriter->startElement('w:r'); - if ($SfIsObject) { - $this->_writeTextStyle($objWriter, $styleFont); - } elseif (!$SfIsObject && !is_null($styleFont)) { - $objWriter->startElement('w:rPr'); - $objWriter->startElement('w:rStyle'); - $objWriter->writeAttribute('w:val', $styleFont); - $objWriter->endElement(); - $objWriter->endElement(); - } - - $objWriter->startElement('w:t'); - $objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text - $objWriter->writeRaw($linkName); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->endElement(); - - if (!$withoutP) { - $objWriter->endElement(); // w:p - } - } - - protected function _writePreserveText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footer_PreserveText $textrun) - { - $styleFont = $textrun->getFontStyle(); - $styleParagraph = $textrun->getParagraphStyle(); - - $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; - $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false; - - $arrText = $textrun->getText(); - - $objWriter->startElement('w:p'); - - if ($SpIsObject) { - $this->_writeParagraphStyle($objWriter, $styleParagraph); - } elseif (!$SpIsObject && !is_null($styleParagraph)) { - $objWriter->startElement('w:pPr'); - $objWriter->startElement('w:pStyle'); - $objWriter->writeAttribute('w:val', $styleParagraph); - $objWriter->endElement(); - $objWriter->endElement(); - } - - foreach ($arrText as $text) { - - if (substr($text, 0, 1) == '{') { - $text = substr($text, 1, -1); - - $objWriter->startElement('w:r'); - $objWriter->startElement('w:fldChar'); - $objWriter->writeAttribute('w:fldCharType', 'begin'); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->startElement('w:r'); - - if ($SfIsObject) { - $this->_writeTextStyle($objWriter, $styleFont); - } elseif (!$SfIsObject && !is_null($styleFont)) { - $objWriter->startElement('w:rPr'); - $objWriter->startElement('w:rStyle'); - $objWriter->writeAttribute('w:val', $styleFont); - $objWriter->endElement(); - $objWriter->endElement(); - } - - $objWriter->startElement('w:instrText'); - $objWriter->writeAttribute('xml:space', 'preserve'); - $objWriter->writeRaw($text); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->startElement('w:r'); - $objWriter->startElement('w:fldChar'); - $objWriter->writeAttribute('w:fldCharType', 'separate'); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->startElement('w:r'); - $objWriter->startElement('w:fldChar'); - $objWriter->writeAttribute('w:fldCharType', 'end'); - $objWriter->endElement(); - $objWriter->endElement(); - } else { - $text = htmlspecialchars($text); - $text = PHPWord_Shared_String::ControlCharacterPHP2OOXML($text); - - $objWriter->startElement('w:r'); - - if ($SfIsObject) { - $this->_writeTextStyle($objWriter, $styleFont); - } elseif (!$SfIsObject && !is_null($styleFont)) { - $objWriter->startElement('w:rPr'); - $objWriter->startElement('w:rStyle'); - $objWriter->writeAttribute('w:val', $styleFont); - $objWriter->endElement(); - $objWriter->endElement(); - } - - $objWriter->startElement('w:t'); - $objWriter->writeAttribute('xml:space', 'preserve'); - $objWriter->writeRaw($text); - $objWriter->endElement(); - $objWriter->endElement(); - } - } - - $objWriter->endElement(); // p - } - - protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Font $style) - { - $font = $style->getName(); - $bold = $style->getBold(); - $italic = $style->getItalic(); - $color = $style->getColor(); - $size = $style->getSize(); - $fgColor = $style->getFgColor(); - $striketrough = $style->getStrikethrough(); - $underline = $style->getUnderline(); - $superscript = $style->getSuperScript(); - $subscript = $style->getSubScript(); - - $objWriter->startElement('w:rPr'); - - // Font - if ($font != 'Arial') { - $objWriter->startElement('w:rFonts'); - $objWriter->writeAttribute('w:ascii', $font); - $objWriter->writeAttribute('w:hAnsi', $font); - $objWriter->writeAttribute('w:cs', $font); - $objWriter->endElement(); - } - - // Color - if ($color != '000000') { - $objWriter->startElement('w:color'); - $objWriter->writeAttribute('w:val', $color); - $objWriter->endElement(); - } - - // Size - if ($size != 20) { - $objWriter->startElement('w:sz'); - $objWriter->writeAttribute('w:val', $size); - $objWriter->endElement(); - $objWriter->startElement('w:szCs'); - $objWriter->writeAttribute('w:val', $size); - $objWriter->endElement(); - } - - // Bold - if ($bold) { - $objWriter->writeElement('w:b', null); - } - - // Superscript - if ($superscript) { - $objWriter->startElement('w:vertAlign'); - $objWriter->writeAttribute('w:val', 'superscript'); - $objWriter->endElement(); - } - - // Subscript - if ($subscript) { - $objWriter->startElement('w:vertAlign'); - $objWriter->writeAttribute('w:val', 'subscript'); - $objWriter->endElement(); - } - - // Italic - if ($italic) { - $objWriter->writeElement('w:i', null); - $objWriter->writeElement('w:iCs', null); - } - - // Underline - if (!is_null($underline) && $underline != 'none') { - $objWriter->startElement('w:u'); - $objWriter->writeAttribute('w:val', $underline); - $objWriter->endElement(); - } - - // Striketrough - if ($striketrough) { - $objWriter->writeElement('w:strike', null); - } - - // Foreground-Color - if (!is_null($fgColor)) { - $objWriter->startElement('w:highlight'); - $objWriter->writeAttribute('w:val', $fgColor); - $objWriter->endElement(); - } - - $objWriter->endElement(); - } - - protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null) - { - $objWriter->writeElement('w:p', null); - } - - protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table) - { - $_rows = $table->getRows(); - $_cRows = count($_rows); - - if ($_cRows > 0) { - $objWriter->startElement('w:tbl'); - $tblStyle = $table->getStyle(); - if ($tblStyle instanceof PHPWord_Style_Table) { - $this->_writeTableStyle($objWriter, $tblStyle); - } else { - if (!empty($tblStyle)) { - $objWriter->startElement('w:tblPr'); - $objWriter->startElement('w:tblStyle'); - $objWriter->writeAttribute('w:val', $tblStyle); - $objWriter->endElement(); - $objWriter->endElement(); - } - } - - $_heights = $table->getRowHeights(); - for ($i = 0; $i < $_cRows; $i++) { - $row = $_rows[$i]; - $height = $_heights[$i]; - - $objWriter->startElement('w:tr'); - - if (!is_null($height)) { - $objWriter->startElement('w:trPr'); - $objWriter->startElement('w:trHeight'); - $objWriter->writeAttribute('w:val', $height); - $objWriter->endElement(); - $objWriter->endElement(); - } - - foreach ($row as $cell) { - $objWriter->startElement('w:tc'); - - $cellStyle = $cell->getStyle(); - $width = $cell->getWidth(); - - $objWriter->startElement('w:tcPr'); - $objWriter->startElement('w:tcW'); - $objWriter->writeAttribute('w:w', $width); - $objWriter->writeAttribute('w:type', 'dxa'); - $objWriter->endElement(); - - if ($cellStyle instanceof PHPWord_Style_Cell) { - $this->_writeCellStyle($objWriter, $cellStyle); - } - - $objWriter->endElement(); - - $_elements = $cell->getElements(); - if (count($_elements) > 0) { - foreach ($_elements as $element) { - if ($element instanceof PHPWord_Section_Text) { - $this->_writeText($objWriter, $element); - } elseif ($element instanceof PHPWord_Section_TextRun) { - $this->_writeTextRun($objWriter, $element); - } elseif ($element instanceof PHPWord_Section_Link) { - $this->_writeLink($objWriter, $element); - } elseif ($element instanceof PHPWord_Section_TextBreak) { - $this->_writeTextBreak($objWriter); - } elseif ($element instanceof PHPWord_Section_ListItem) { - $this->_writeListItem($objWriter, $element); - } elseif ($element instanceof PHPWord_Section_Image || - $element instanceof PHPWord_Section_MemoryImage - ) { - $this->_writeImage($objWriter, $element); - } elseif ($element instanceof PHPWord_Section_Object) { - $this->_writeObject($objWriter, $element); - } elseif ($element instanceof PHPWord_Section_Footer_PreserveText) { - $this->_writePreserveText($objWriter, $element); - } - } - } else { - $this->_writeTextBreak($objWriter); - } - - $objWriter->endElement(); - } - $objWriter->endElement(); - } - $objWriter->endElement(); - } - } - - protected function _writeTableStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Table $style = null) - { - $margins = $style->getCellMargin(); - $mTop = (!is_null($margins[0])) ? true : false; - $mLeft = (!is_null($margins[1])) ? true : false; - $mRight = (!is_null($margins[2])) ? true : false; - $mBottom = (!is_null($margins[3])) ? true : false; - - if ($mTop || $mLeft || $mRight || $mBottom) { - $objWriter->startElement('w:tblPr'); - $objWriter->startElement('w:tblCellMar'); - - if ($mTop) { - $objWriter->startElement('w:top'); - $objWriter->writeAttribute('w:w', $margins[0]); - $objWriter->writeAttribute('w:type', 'dxa'); - $objWriter->endElement(); - } - - if ($mLeft) { - $objWriter->startElement('w:left'); - $objWriter->writeAttribute('w:w', $margins[1]); - $objWriter->writeAttribute('w:type', 'dxa'); - $objWriter->endElement(); - } - - if ($mRight) { - $objWriter->startElement('w:right'); - $objWriter->writeAttribute('w:w', $margins[2]); - $objWriter->writeAttribute('w:type', 'dxa'); - $objWriter->endElement(); - } - - if ($mBottom) { - $objWriter->startElement('w:bottom'); - $objWriter->writeAttribute('w:w', $margins[3]); - $objWriter->writeAttribute('w:type', 'dxa'); - $objWriter->endElement(); - } - - $objWriter->endElement(); - $objWriter->endElement(); - } - } - - protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null) - { - $bgColor = $style->getBgColor(); - $valign = $style->getVAlign(); - $textDir = $style->getTextDirection(); - $brdSz = $style->getBorderSize(); - $brdCol = $style->getBorderColor(); - - $bTop = (!is_null($brdSz[0])) ? true : false; - $bLeft = (!is_null($brdSz[1])) ? true : false; - $bRight = (!is_null($brdSz[2])) ? true : false; - $bBottom = (!is_null($brdSz[3])) ? true : false; - $borders = ($bTop || $bLeft || $bRight || $bBottom) ? true : false; - - $styles = (!is_null($bgColor) || !is_null($valign) || !is_null($textDir) || $borders) ? true : false; - - if ($styles) { - if (!is_null($textDir)) { - $objWriter->startElement('w:textDirection'); - $objWriter->writeAttribute('w:val', $textDir); - $objWriter->endElement(); - } - - if (!is_null($bgColor)) { - $objWriter->startElement('w:shd'); - $objWriter->writeAttribute('w:val', 'clear'); - $objWriter->writeAttribute('w:color', 'auto'); - $objWriter->writeAttribute('w:fill', $bgColor); - $objWriter->endElement(); - } - - if (!is_null($valign)) { - $objWriter->startElement('w:vAlign'); - $objWriter->writeAttribute('w:val', $valign); - $objWriter->endElement(); - } - - if ($borders) { - $_defaultColor = $style->getDefaultBorderColor(); - - $objWriter->startElement('w:tcBorders'); - if ($bTop) { - if (is_null($brdCol[0])) { - $brdCol[0] = $_defaultColor; - } - $objWriter->startElement('w:top'); - $objWriter->writeAttribute('w:val', 'single'); - $objWriter->writeAttribute('w:sz', $brdSz[0]); - $objWriter->writeAttribute('w:color', $brdCol[0]); - $objWriter->endElement(); - } - - if ($bLeft) { - if (is_null($brdCol[1])) { - $brdCol[1] = $_defaultColor; - } - $objWriter->startElement('w:left'); - $objWriter->writeAttribute('w:val', 'single'); - $objWriter->writeAttribute('w:sz', $brdSz[1]); - $objWriter->writeAttribute('w:color', $brdCol[1]); - $objWriter->endElement(); - } - - if ($bRight) { - if (is_null($brdCol[2])) { - $brdCol[2] = $_defaultColor; - } - $objWriter->startElement('w:right'); - $objWriter->writeAttribute('w:val', 'single'); - $objWriter->writeAttribute('w:sz', $brdSz[2]); - $objWriter->writeAttribute('w:color', $brdCol[2]); - $objWriter->endElement(); - } - - if ($bBottom) { - if (is_null($brdCol[3])) { - $brdCol[3] = $_defaultColor; - } - $objWriter->startElement('w:bottom'); - $objWriter->writeAttribute('w:val', 'single'); - $objWriter->writeAttribute('w:sz', $brdSz[3]); - $objWriter->writeAttribute('w:color', $brdCol[3]); - $objWriter->endElement(); - } - - $objWriter->endElement(); - } - } - } - /** - * @param \PHPWord_Shared_XMLWriter $objWriter - * @param \PHPWord_Section_Image $image - */ - protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image) - { - $rId = $image->getRelationId(); - - $style = $image->getStyle(); - $width = $style->getWidth(); - $height = $style->getHeight(); - $align = $style->getAlign(); - $marginTop = $style->getMarginTop(); - $marginLeft = $style->getMarginLeft(); - $wrappingStyle = $style->getWrappingStyle(); - - $objWriter->startElement('w:p'); - - if (!is_null($align)) { - $objWriter->startElement('w:pPr'); - $objWriter->startElement('w:jc'); - $objWriter->writeAttribute('w:val', $align); - $objWriter->endElement(); - $objWriter->endElement(); - } - - $objWriter->startElement('w:r'); - - $objWriter->startElement('w:pict'); - - $objWriter->startElement('v:shape'); - $objWriter->writeAttribute('type', '#_x0000_t75'); - - $imgStyle = ''; - if (null !== $width) { - $imgStyle .= 'width:' . $width . 'px;'; - } - if (null !== $height) { - $imgStyle .= 'height:' . $height . 'px;'; - } - if (null !== $marginTop) { - $imgStyle .= 'margin-top:' . $marginTop . 'in;'; - } - if (null !== $marginLeft) { - $imgStyle .= 'margin-left:' . $marginLeft . 'in;'; - } - - switch ($wrappingStyle) { - case PHPWord_Style_Image::WRAPPING_STYLE_BEHIND: - $imgStyle .= 'position:absolute;z-index:-251658752;'; - break; - case PHPWord_Style_Image::WRAPPING_STYLE_SQUARE: - $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; - break; - case PHPWord_Style_Image::WRAPPING_STYLE_TIGHT: - $imgStyle .= 'position:absolute;z-index:251659264;mso-wrap-edited:f;mso-position-horizontal:absolute;mso-position-vertical:absolute'; - break; - case PHPWord_Style_Image::WRAPPING_STYLE_INFRONT: - $imgStyle .= 'position:absolute;zz-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; - break; - } - - $objWriter->writeAttribute('style', $imgStyle); - - $objWriter->startElement('v:imagedata'); - $objWriter->writeAttribute('r:id', 'rId' . $rId); - $objWriter->writeAttribute('o:title', ''); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->endElement(); - - $objWriter->endElement(); - - $objWriter->endElement(); - } - - protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image) - { - $rId = $image->getRelationId(); - - $style = $image->getStyle(); - $width = $style->getWidth(); - $height = $style->getHeight(); - $marginLeft = $style->getMarginLeft(); - $marginTop = $style->getMarginTop(); - - $objWriter->startElement('w:p'); - - $objWriter->startElement('w:r'); - - $objWriter->startElement('w:pict'); - - $objWriter->startElement('v:shape'); - $objWriter->writeAttribute('type', '#_x0000_t75'); - - $strStyle = 'position:absolute;'; - $strStyle .= ' width:' . $width . 'px;'; - $strStyle .= ' height:' . $height . 'px;'; - if (!is_null($marginTop)) { - $strStyle .= ' margin-top:' . $marginTop . 'px;'; - } - if (!is_null($marginLeft)) { - $strStyle .= ' margin-left:' . $marginLeft . 'px;'; - } - - $objWriter->writeAttribute('style', $strStyle); - - $objWriter->startElement('v:imagedata'); - $objWriter->writeAttribute('r:id', 'rId' . $rId); - $objWriter->writeAttribute('o:title', ''); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->endElement(); - - $objWriter->endElement(); - - $objWriter->endElement(); - } - - protected function _writeTitle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Title $title) - { - $text = htmlspecialchars($title->getText()); - $text = PHPWord_Shared_String::ControlCharacterPHP2OOXML($text); - $anchor = $title->getAnchor(); - $bookmarkId = $title->getBookmarkId(); - $style = $title->getStyle(); - - $objWriter->startElement('w:p'); - - if (!empty($style)) { - $objWriter->startElement('w:pPr'); - $objWriter->startElement('w:pStyle'); - $objWriter->writeAttribute('w:val', $style); - $objWriter->endElement(); - $objWriter->endElement(); - } - - $objWriter->startElement('w:r'); - $objWriter->startElement('w:fldChar'); - $objWriter->writeAttribute('w:fldCharType', 'end'); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->startElement('w:bookmarkStart'); - $objWriter->writeAttribute('w:id', $bookmarkId); - $objWriter->writeAttribute('w:name', $anchor); - $objWriter->endElement(); - - $objWriter->startElement('w:r'); - $objWriter->startElement('w:t'); - $objWriter->writeRaw($text); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->startElement('w:bookmarkEnd'); - $objWriter->writeAttribute('w:id', $bookmarkId); - $objWriter->endElement(); - - $objWriter->endElement(); - } -} \ No newline at end of file diff --git a/src/PHPWord/Writer/Word2007/Document.php b/src/PHPWord/Writer/Word2007/Document.php deleted file mode 100644 index e3ecd46e..00000000 --- a/src/PHPWord/Writer/Word2007/Document.php +++ /dev/null @@ -1,464 +0,0 @@ -getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY); - } - - // XML header - $objWriter->startDocument('1.0', 'UTF-8', 'yes'); - - // w:document - $objWriter->startElement('w:document'); - - $objWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006'); - $objWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office'); - $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'); - $objWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math'); - $objWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml'); - $objWriter->writeAttribute('xmlns:wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'); - $objWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word'); - $objWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); - $objWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); - - $objWriter->startElement('w:body'); - - $_sections = $pPHPWord->getSections(); - $countSections = count($_sections); - $pSection = 0; - - if ($countSections > 0) { - foreach ($_sections as $section) { - $pSection++; - - $_elements = $section->getElements(); - - foreach ($_elements as $element) { - if ($element instanceof PHPWord_Section_Text) { - $this->_writeText($objWriter, $element); - } elseif ($element instanceof PHPWord_Section_TextRun) { - $this->_writeTextRun($objWriter, $element); - } elseif ($element instanceof PHPWord_Section_Link) { - $this->_writeLink($objWriter, $element); - } elseif ($element instanceof PHPWord_Section_Title) { - $this->_writeTitle($objWriter, $element); - } elseif ($element instanceof PHPWord_Section_TextBreak) { - $this->_writeTextBreak($objWriter); - } elseif ($element instanceof PHPWord_Section_PageBreak) { - $this->_writePageBreak($objWriter); - } elseif ($element instanceof PHPWord_Section_Table) { - $this->_writeTable($objWriter, $element); - } elseif ($element instanceof PHPWord_Section_ListItem) { - $this->_writeListItem($objWriter, $element); - } elseif ($element instanceof PHPWord_Section_Image || - $element instanceof PHPWord_Section_MemoryImage - ) { - $this->_writeImage($objWriter, $element); - } elseif ($element instanceof PHPWord_Section_Object) { - $this->_writeObject($objWriter, $element); - } elseif ($element instanceof PHPWord_TOC) { - $this->_writeTOC($objWriter); - } - } - - if ($pSection == $countSections) { - $this->_writeEndSection($objWriter, $section); - } else { - $this->_writeSection($objWriter, $section); - } - } - } - - $objWriter->endElement(); // End w:body - $objWriter->endElement(); // End w:document - - // Return - return $objWriter->getData(); - } - - private function _writeSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section) - { - $objWriter->startElement('w:p'); - $objWriter->startElement('w:pPr'); - $this->_writeEndSection($objWriter, $section, 3); - $objWriter->endElement(); - $objWriter->endElement(); - } - - private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section) - { - $_settings = $section->getSettings(); - $_header = $section->getHeader(); - $_footer = $section->getFooter(); - $pgSzW = $_settings->getPageSizeW(); - $pgSzH = $_settings->getPageSizeH(); - $orientation = $_settings->getOrientation(); - - $marginTop = $_settings->getMarginTop(); - $marginLeft = $_settings->getMarginLeft(); - $marginRight = $_settings->getMarginRight(); - $marginBottom = $_settings->getMarginBottom(); - - $borders = $_settings->getBorderSize(); - - $objWriter->startElement('w:sectPr'); - - if (!is_null($_header)) { - $rId = $_header->getRelationId(); - $objWriter->startElement('w:headerReference'); - $objWriter->writeAttribute('w:type', 'default'); - $objWriter->writeAttribute('r:id', 'rId' . $rId); - $objWriter->endElement(); - } - - if (!is_null($_footer)) { - $rId = $_footer->getRelationId(); - $objWriter->startElement('w:footerReference'); - $objWriter->writeAttribute('w:type', 'default'); - $objWriter->writeAttribute('r:id', 'rId' . $rId); - $objWriter->endElement(); - } - - $objWriter->startElement('w:pgSz'); - $objWriter->writeAttribute('w:w', $pgSzW); - $objWriter->writeAttribute('w:h', $pgSzH); - - if (!is_null($orientation) && strtolower($orientation) != 'portrait') { - $objWriter->writeAttribute('w:orient', $orientation); - } - - $objWriter->endElement(); - - $objWriter->startElement('w:pgMar'); - $objWriter->writeAttribute('w:top', $marginTop); - $objWriter->writeAttribute('w:right', $marginRight); - $objWriter->writeAttribute('w:bottom', $marginBottom); - $objWriter->writeAttribute('w:left', $marginLeft); - $objWriter->writeAttribute('w:header', '720'); - $objWriter->writeAttribute('w:footer', '720'); - $objWriter->writeAttribute('w:gutter', '0'); - $objWriter->endElement(); - - - if (!is_null($borders[0]) || !is_null($borders[1]) || !is_null($borders[2]) || !is_null($borders[3])) { - $borderColor = $_settings->getBorderColor(); - - $objWriter->startElement('w:pgBorders'); - $objWriter->writeAttribute('w:offsetFrom', 'page'); - - if (!is_null($borders[0])) { - $objWriter->startElement('w:top'); - $objWriter->writeAttribute('w:val', 'single'); - $objWriter->writeAttribute('w:sz', $borders[0]); - $objWriter->writeAttribute('w:space', '24'); - $objWriter->writeAttribute('w:color', $borderColor[0]); - $objWriter->endElement(); - } - - if (!is_null($borders[1])) { - $objWriter->startElement('w:left'); - $objWriter->writeAttribute('w:val', 'single'); - $objWriter->writeAttribute('w:sz', $borders[1]); - $objWriter->writeAttribute('w:space', '24'); - $objWriter->writeAttribute('w:color', $borderColor[1]); - $objWriter->endElement(); - } - - if (!is_null($borders[2])) { - $objWriter->startElement('w:right'); - $objWriter->writeAttribute('w:val', 'single'); - $objWriter->writeAttribute('w:sz', $borders[2]); - $objWriter->writeAttribute('w:space', '24'); - $objWriter->writeAttribute('w:color', $borderColor[2]); - $objWriter->endElement(); - } - - if (!is_null($borders[3])) { - $objWriter->startElement('w:bottom'); - $objWriter->writeAttribute('w:val', 'single'); - $objWriter->writeAttribute('w:sz', $borders[3]); - $objWriter->writeAttribute('w:space', '24'); - $objWriter->writeAttribute('w:color', $borderColor[3]); - $objWriter->endElement(); - } - $objWriter->endElement(); - } - - - $objWriter->startElement('w:cols'); - $objWriter->writeAttribute('w:space', '720'); - $objWriter->endElement(); - - - $objWriter->endElement(); - } - - private function _writePageBreak(PHPWord_Shared_XMLWriter $objWriter = null) - { - $objWriter->startElement('w:p'); - $objWriter->startElement('w:r'); - $objWriter->startElement('w:br'); - $objWriter->writeAttribute('w:type', 'page'); - $objWriter->endElement(); - $objWriter->endElement(); - $objWriter->endElement(); - } - - private function _writeListItem(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_ListItem $listItem) - { - $textObject = $listItem->getTextObject(); - $text = $textObject->getText(); - $styleParagraph = $textObject->getParagraphStyle(); - $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false; - - $depth = $listItem->getDepth(); - $listType = $listItem->getStyle()->getListType(); - - $objWriter->startElement('w:p'); - $objWriter->startElement('w:pPr'); - - if ($SpIsObject) { - $this->_writeParagraphStyle($objWriter, $styleParagraph, true); - } elseif (!$SpIsObject && !is_null($styleParagraph)) { - $objWriter->startElement('w:pStyle'); - $objWriter->writeAttribute('w:val', $styleParagraph); - $objWriter->endElement(); - } - - $objWriter->startElement('w:numPr'); - - $objWriter->startElement('w:ilvl'); - $objWriter->writeAttribute('w:val', $depth); - $objWriter->endElement(); - - $objWriter->startElement('w:numId'); - $objWriter->writeAttribute('w:val', $listType); - $objWriter->endElement(); - - $objWriter->endElement(); - $objWriter->endElement(); - - $this->_writeText($objWriter, $textObject, true); - - $objWriter->endElement(); - } - - protected function _writeObject(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Object $object) - { - $rIdObject = $object->getRelationId(); - $rIdImage = $object->getImageRelationId(); - $shapeId = md5($rIdObject . '_' . $rIdImage); - - $objectId = $object->getObjectId(); - - $style = $object->getStyle(); - $width = $style->getWidth(); - $height = $style->getHeight(); - $align = $style->getAlign(); - - - $objWriter->startElement('w:p'); - - if (!is_null($align)) { - $objWriter->startElement('w:pPr'); - $objWriter->startElement('w:jc'); - $objWriter->writeAttribute('w:val', $align); - $objWriter->endElement(); - $objWriter->endElement(); - } - - $objWriter->startElement('w:r'); - - $objWriter->startElement('w:object'); - $objWriter->writeAttribute('w:dxaOrig', '249'); - $objWriter->writeAttribute('w:dyaOrig', '160'); - - $objWriter->startElement('v:shape'); - $objWriter->writeAttribute('id', $shapeId); - $objWriter->writeAttribute('type', '#_x0000_t75'); - $objWriter->writeAttribute('style', 'width:104px;height:67px'); - $objWriter->writeAttribute('o:ole', ''); - - $objWriter->startElement('v:imagedata'); - $objWriter->writeAttribute('r:id', 'rId' . $rIdImage); - $objWriter->writeAttribute('o:title', ''); - $objWriter->endElement(); - - $objWriter->endElement(); - - $objWriter->startElement('o:OLEObject'); - $objWriter->writeAttribute('Type', 'Embed'); - $objWriter->writeAttribute('ProgID', 'Package'); - $objWriter->writeAttribute('ShapeID', $shapeId); - $objWriter->writeAttribute('DrawAspect', 'Icon'); - $objWriter->writeAttribute('ObjectID', '_' . $objectId); - $objWriter->writeAttribute('r:id', 'rId' . $rIdObject); - $objWriter->endElement(); - - $objWriter->endElement(); - - $objWriter->endElement(); // w:r - - $objWriter->endElement(); // w:p - } - - private function _writeTOC(PHPWord_Shared_XMLWriter $objWriter = null) - { - $titles = PHPWord_TOC::getTitles(); - $styleFont = PHPWord_TOC::getStyleFont(); - - $styleTOC = PHPWord_TOC::getStyleTOC(); - $fIndent = $styleTOC->getIndent(); - $tabLeader = $styleTOC->getTabLeader(); - $tabPos = $styleTOC->getTabPos(); - - $isObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; - - for ($i = 0; $i < count($titles); $i++) { - $title = $titles[$i]; - $indent = ($title['depth'] - 1) * $fIndent; - - $objWriter->startElement('w:p'); - - $objWriter->startElement('w:pPr'); - - if ($isObject && !is_null($styleFont->getParagraphStyle())) { - $this->_writeParagraphStyle($objWriter, $styleFont->getParagraphStyle()); - } - - if ($indent > 0) { - $objWriter->startElement('w:ind'); - $objWriter->writeAttribute('w:left', $indent); - $objWriter->endElement(); - } - - if (!empty($styleFont) && !$isObject) { - $objWriter->startElement('w:pPr'); - $objWriter->startElement('w:pStyle'); - $objWriter->writeAttribute('w:val', $styleFont); - $objWriter->endElement(); - $objWriter->endElement(); - } - - $objWriter->startElement('w:tabs'); - $objWriter->startElement('w:tab'); - $objWriter->writeAttribute('w:val', 'right'); - if (!empty($tabLeader)) { - $objWriter->writeAttribute('w:leader', $tabLeader); - } - $objWriter->writeAttribute('w:pos', $tabPos); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->endElement(); // w:pPr - - - if ($i == 0) { - $objWriter->startElement('w:r'); - $objWriter->startElement('w:fldChar'); - $objWriter->writeAttribute('w:fldCharType', 'begin'); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->startElement('w:r'); - $objWriter->startElement('w:instrText'); - $objWriter->writeAttribute('xml:space', 'preserve'); - $objWriter->writeRaw('TOC \o "1-9" \h \z \u'); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->startElement('w:r'); - $objWriter->startElement('w:fldChar'); - $objWriter->writeAttribute('w:fldCharType', 'separate'); - $objWriter->endElement(); - $objWriter->endElement(); - } - - $objWriter->startElement('w:hyperlink'); - $objWriter->writeAttribute('w:anchor', $title['anchor']); - $objWriter->writeAttribute('w:history', '1'); - - $objWriter->startElement('w:r'); - - if ($isObject) { - $this->_writeTextStyle($objWriter, $styleFont); - } - - $objWriter->startElement('w:t'); - $objWriter->writeRaw($title['text']); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->startElement('w:r'); - $objWriter->writeElement('w:tab', null); - $objWriter->endElement(); - - $objWriter->startElement('w:r'); - $objWriter->startElement('w:fldChar'); - $objWriter->writeAttribute('w:fldCharType', 'begin'); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->startElement('w:r'); - $objWriter->startElement('w:instrText'); - $objWriter->writeAttribute('xml:space', 'preserve'); - $objWriter->writeRaw('PAGEREF ' . $title['anchor'] . ' \h'); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->startElement('w:r'); - $objWriter->startElement('w:fldChar'); - $objWriter->writeAttribute('w:fldCharType', 'end'); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->endElement(); // w:hyperlink - - $objWriter->endElement(); // w:p - } - - $objWriter->startElement('w:p'); - $objWriter->startElement('w:r'); - $objWriter->startElement('w:fldChar'); - $objWriter->writeAttribute('w:fldCharType', 'end'); - $objWriter->endElement(); - $objWriter->endElement(); - $objWriter->endElement(); - } -}