From d54a674b97a60af84b27f4e2555ad6d3d462e3cd Mon Sep 17 00:00:00 2001 From: jogo Date: Fri, 4 Jul 2014 13:07:08 +0200 Subject: [PATCH 1/8] add element bookmark and allow links to internal bookmarks in Word2007 --- src/PhpWord/Element/AbstractContainer.php | 4 +- src/PhpWord/Element/Bookmark.php | 64 +++++++++++++++++++ src/PhpWord/Element/Link.php | 21 +++++- src/PhpWord/PhpWord.php | 11 ++-- .../Writer/Word2007/Element/Bookmark.php | 49 ++++++++++++++ src/PhpWord/Writer/Word2007/Element/Link.php | 10 +-- src/PhpWord/Writer/Word2007/Element/Title.php | 9 ++- 7 files changed, 150 insertions(+), 18 deletions(-) create mode 100644 src/PhpWord/Element/Bookmark.php create mode 100644 src/PhpWord/Writer/Word2007/Element/Bookmark.php diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 686644eb..9b598906 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -22,6 +22,7 @@ namespace PhpOffice\PhpWord\Element; * * @method Text addText(string $text, mixed $fStyle = null, mixed $pStyle = null) * @method TextRun addTextRun(mixed $pStyle = null) + * @method Bookmark addBookmark(string $name) * @method Link addLink(string $target, string $text = null, mixed $fStyle = null, mixed $pStyle = null) * @method PreserveText addPreserveText(string $text, mixed $fStyle = null, mixed $pStyle = null) * @method void addTextBreak(int $count = 1, mixed $fStyle = null, mixed $pStyle = null) @@ -78,7 +79,7 @@ abstract class AbstractContainer extends AbstractElement public function __call($function, $args) { $elements = array( - 'Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak', + 'Text', 'TextRun', 'Bookmark', 'Link', 'PreserveText', 'TextBreak', 'ListItem', 'ListItemRun', 'Table', 'Image', 'Object', 'Footnote', 'Endnote', 'CheckBox', 'TextBox', 'Field', 'Line', 'Shape', 'Title', 'TOC', 'PageBreak', @@ -189,6 +190,7 @@ abstract class AbstractContainer extends AbstractElement ); $validContainers = array( 'Text' => $allContainers, + 'Bookmark' => $allContainers, 'Link' => $allContainers, 'TextBreak' => $allContainers, 'Image' => $allContainers, diff --git a/src/PhpWord/Element/Bookmark.php b/src/PhpWord/Element/Bookmark.php new file mode 100644 index 00000000..39b6d69a --- /dev/null +++ b/src/PhpWord/Element/Bookmark.php @@ -0,0 +1,64 @@ +name = String::toUTF8($name); + return $this; + } + + /** + * Get Bookmark name + * + * @return string + */ + public function getName() + { + return $this->name; + } + +} diff --git a/src/PhpWord/Element/Link.php b/src/PhpWord/Element/Link.php index 4b00dc90..81349b5d 100644 --- a/src/PhpWord/Element/Link.php +++ b/src/PhpWord/Element/Link.php @@ -61,6 +61,13 @@ class Link extends AbstractElement */ protected $mediaRelation = true; + /** + * Has internal flag - anchor to internal bookmark + * + * @var bool + */ + protected $internal = false; + /** * Create a new Link Element * @@ -69,13 +76,13 @@ class Link extends AbstractElement * @param mixed $fontStyle * @param mixed $paragraphStyle */ - public function __construct($source, $text = null, $fontStyle = null, $paragraphStyle = null) + public function __construct($source, $text = null, $fontStyle = null, $paragraphStyle = null, $internal = false) { $this->source = String::toUTF8($source); $this->text = is_null($text) ? $this->source : String::toUTF8($text); $this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle); $this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle); - + $this->internal = $internal; return $this; } @@ -154,4 +161,14 @@ class Link extends AbstractElement { return $this->getText(); } + + /** + * is internal + * + * @return bool + */ + public function isInternal() + { + return $this->internal; + } } diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 3b991e23..93835d91 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -27,6 +27,7 @@ use PhpOffice\PhpWord\Exception\Exception; * @method Collection\Footnotes getFootnotes() * @method Collection\Endnotes getEndnotes() * @method Collection\Charts getCharts() + * @method int addBookmark(Element\Bookmark $bookmark) * @method int addTitle(Element\Title $title) * @method int addFootnote(Element\Footnote $footnote) * @method int addEndnote(Element\Endnote $endnote) @@ -82,7 +83,7 @@ class PhpWord public function __construct() { // Collection - $collections = array('Titles', 'Footnotes', 'Endnotes', 'Charts'); + $collections = array('Bookmarks', 'Titles', 'Footnotes', 'Endnotes', 'Charts'); foreach ($collections as $collection) { $class = 'PhpOffice\\PhpWord\\Collection\\' . $collection; $this->collections[$collection] = new $class(); @@ -113,7 +114,7 @@ class PhpWord $addCollection = array(); $addStyle = array(); - $collections = array('Title', 'Footnote', 'Endnote', 'Chart'); + $collections = array('Bookmark', 'Title', 'Footnote', 'Endnote', 'Chart'); foreach ($collections as $collection) { $getCollection[] = strtolower("get{$collection}s"); $addCollection[] = strtolower("add{$collection}"); @@ -218,10 +219,9 @@ class PhpWord } /** - * Set default font name. + * Set default font name * * @param string $fontName - * @return void */ public function setDefaultFontName($fontName) { @@ -239,10 +239,9 @@ class PhpWord } /** - * Set default font size. + * Set default font size * * @param int $fontSize - * @return void */ public function setDefaultFontSize($fontSize) { diff --git a/src/PhpWord/Writer/Word2007/Element/Bookmark.php b/src/PhpWord/Writer/Word2007/Element/Bookmark.php new file mode 100644 index 00000000..df5a104a --- /dev/null +++ b/src/PhpWord/Writer/Word2007/Element/Bookmark.php @@ -0,0 +1,49 @@ +getXmlWriter(); + $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Bookmark) { + return; + } + + $rId = $element->getRelationId(); + + $xmlWriter->startElement('w:bookmarkStart'); + $xmlWriter->writeAttribute('w:id', $rId); + $xmlWriter->writeAttribute('w:name', $element->getName()); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:bookmarkEnd'); + $xmlWriter->writeAttribute('w:id', $rId); + $xmlWriter->endElement(); + } +} diff --git a/src/PhpWord/Writer/Word2007/Element/Link.php b/src/PhpWord/Writer/Word2007/Element/Link.php index 2a5a5ae1..31bfa3fd 100644 --- a/src/PhpWord/Writer/Word2007/Element/Link.php +++ b/src/PhpWord/Writer/Word2007/Element/Link.php @@ -25,9 +25,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; class Link extends Text { /** - * Write link element. - * - * @return void + * Write link element */ public function write() { @@ -42,7 +40,11 @@ class Link extends Text $this->startElementP(); $xmlWriter->startElement('w:hyperlink'); - $xmlWriter->writeAttribute('r:id', 'rId' . $rId); + if($element->isInternal()) { + $xmlWriter->writeAttribute('w:anchor', $element->getSource()); + }else { + $xmlWriter->writeAttribute('r:id', 'rId' . $rId); + } $xmlWriter->writeAttribute('w:history', '1'); $xmlWriter->startElement('w:r'); diff --git a/src/PhpWord/Writer/Word2007/Element/Title.php b/src/PhpWord/Writer/Word2007/Element/Title.php index f48e932a..2c2fe0b0 100644 --- a/src/PhpWord/Writer/Word2007/Element/Title.php +++ b/src/PhpWord/Writer/Word2007/Element/Title.php @@ -25,9 +25,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; class Title extends AbstractElement { /** - * Write title element. - * - * @return void + * Write title element */ public function write() { @@ -50,10 +48,11 @@ class Title extends AbstractElement } $rId = $element->getRelationId(); + $bookmarkRId = $element->getPhpWord()->addBookmark(); // Bookmark start for TOC $xmlWriter->startElement('w:bookmarkStart'); - $xmlWriter->writeAttribute('w:id', $rId); + $xmlWriter->writeAttribute('w:id', $bookmarkRId); $xmlWriter->writeAttribute('w:name', "_Toc{$rId}"); $xmlWriter->endElement(); @@ -66,7 +65,7 @@ class Title extends AbstractElement // Bookmark end $xmlWriter->startElement('w:bookmarkEnd'); - $xmlWriter->writeAttribute('w:id', $rId); + $xmlWriter->writeAttribute('w:id', $bookmarkRId); $xmlWriter->endElement(); $xmlWriter->endElement(); From fdeefb77d95eb4d17c355ba42f816e38e48a7585 Mon Sep 17 00:00:00 2001 From: jogo Date: Fri, 4 Jul 2014 13:24:14 +0200 Subject: [PATCH 2/8] include minor changes from develop branch --- src/PhpWord/PhpWord.php | 6 ++++-- src/PhpWord/Writer/Word2007/Element/Link.php | 2 ++ src/PhpWord/Writer/Word2007/Element/Title.php | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 93835d91..e83ce08f 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -219,9 +219,10 @@ class PhpWord } /** - * Set default font name + * Set default font name. * * @param string $fontName + * @return void */ public function setDefaultFontName($fontName) { @@ -239,9 +240,10 @@ class PhpWord } /** - * Set default font size + * Set default font size. * * @param int $fontSize + * @return void */ public function setDefaultFontSize($fontSize) { diff --git a/src/PhpWord/Writer/Word2007/Element/Link.php b/src/PhpWord/Writer/Word2007/Element/Link.php index 31bfa3fd..50f6d131 100644 --- a/src/PhpWord/Writer/Word2007/Element/Link.php +++ b/src/PhpWord/Writer/Word2007/Element/Link.php @@ -26,6 +26,8 @@ class Link extends Text { /** * Write link element + * + * @return void */ public function write() { diff --git a/src/PhpWord/Writer/Word2007/Element/Title.php b/src/PhpWord/Writer/Word2007/Element/Title.php index 2c2fe0b0..a53af3fa 100644 --- a/src/PhpWord/Writer/Word2007/Element/Title.php +++ b/src/PhpWord/Writer/Word2007/Element/Title.php @@ -26,6 +26,8 @@ class Title extends AbstractElement { /** * Write title element + * + * @return void */ public function write() { From 475c76dd7907a534dc642dbbbd5400d8bce49e07 Mon Sep 17 00:00:00 2001 From: jogo Date: Fri, 4 Jul 2014 13:30:07 +0200 Subject: [PATCH 3/8] convert tabs to spaces --- src/PhpWord/PhpWord.php | 4 ++-- src/PhpWord/Writer/Word2007/Element/Link.php | 4 ++-- src/PhpWord/Writer/Word2007/Element/Title.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index e83ce08f..419af4be 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -222,7 +222,7 @@ class PhpWord * Set default font name. * * @param string $fontName - * @return void + * @return void */ public function setDefaultFontName($fontName) { @@ -243,7 +243,7 @@ class PhpWord * Set default font size. * * @param int $fontSize - * @return void + * @return void */ public function setDefaultFontSize($fontSize) { diff --git a/src/PhpWord/Writer/Word2007/Element/Link.php b/src/PhpWord/Writer/Word2007/Element/Link.php index 50f6d131..9f53fc7d 100644 --- a/src/PhpWord/Writer/Word2007/Element/Link.php +++ b/src/PhpWord/Writer/Word2007/Element/Link.php @@ -26,8 +26,8 @@ class Link extends Text { /** * Write link element - * - * @return void + * + * @return void */ public function write() { diff --git a/src/PhpWord/Writer/Word2007/Element/Title.php b/src/PhpWord/Writer/Word2007/Element/Title.php index a53af3fa..abdfc944 100644 --- a/src/PhpWord/Writer/Word2007/Element/Title.php +++ b/src/PhpWord/Writer/Word2007/Element/Title.php @@ -26,8 +26,8 @@ class Title extends AbstractElement { /** * Write title element - * - * @return void + * + * @return void */ public function write() { From 4d060b3f07f3058e245b07f93ea3965fc674c774 Mon Sep 17 00:00:00 2001 From: jogo Date: Fri, 4 Jul 2014 13:32:47 +0200 Subject: [PATCH 4/8] sync with develop-branch --- src/PhpWord/Writer/Word2007/Element/Link.php | 2 +- src/PhpWord/Writer/Word2007/Element/Title.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Writer/Word2007/Element/Link.php b/src/PhpWord/Writer/Word2007/Element/Link.php index 9f53fc7d..58156ebc 100644 --- a/src/PhpWord/Writer/Word2007/Element/Link.php +++ b/src/PhpWord/Writer/Word2007/Element/Link.php @@ -25,7 +25,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; class Link extends Text { /** - * Write link element + * Write link element. * * @return void */ diff --git a/src/PhpWord/Writer/Word2007/Element/Title.php b/src/PhpWord/Writer/Word2007/Element/Title.php index abdfc944..ce9aeea5 100644 --- a/src/PhpWord/Writer/Word2007/Element/Title.php +++ b/src/PhpWord/Writer/Word2007/Element/Title.php @@ -25,7 +25,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; class Title extends AbstractElement { /** - * Write title element + * Write title element. * * @return void */ From 87c3df55361bed6204e158de16980d015c5ecbf2 Mon Sep 17 00:00:00 2001 From: jogo Date: Fri, 4 Jul 2014 14:10:20 +0200 Subject: [PATCH 5/8] add missing file apply formatting to CGL --- src/PhpWord/Collection/Bookmarks.php | 27 ++++++++++++++++++++ src/PhpWord/Element/Bookmark.php | 1 - src/PhpWord/Element/Link.php | 2 +- src/PhpWord/Writer/Word2007/Element/Link.php | 8 +++--- 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 src/PhpWord/Collection/Bookmarks.php diff --git a/src/PhpWord/Collection/Bookmarks.php b/src/PhpWord/Collection/Bookmarks.php new file mode 100644 index 00000000..cb9d74d5 --- /dev/null +++ b/src/PhpWord/Collection/Bookmarks.php @@ -0,0 +1,27 @@ +name; } - } diff --git a/src/PhpWord/Element/Link.php b/src/PhpWord/Element/Link.php index 81349b5d..9d0eb766 100644 --- a/src/PhpWord/Element/Link.php +++ b/src/PhpWord/Element/Link.php @@ -82,7 +82,7 @@ class Link extends AbstractElement $this->text = is_null($text) ? $this->source : String::toUTF8($text); $this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle); $this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle); - $this->internal = $internal; + $this->internal = $internal; return $this; } diff --git a/src/PhpWord/Writer/Word2007/Element/Link.php b/src/PhpWord/Writer/Word2007/Element/Link.php index 58156ebc..2cb8407f 100644 --- a/src/PhpWord/Writer/Word2007/Element/Link.php +++ b/src/PhpWord/Writer/Word2007/Element/Link.php @@ -42,10 +42,10 @@ class Link extends Text $this->startElementP(); $xmlWriter->startElement('w:hyperlink'); - if($element->isInternal()) { - $xmlWriter->writeAttribute('w:anchor', $element->getSource()); - }else { - $xmlWriter->writeAttribute('r:id', 'rId' . $rId); + if ($element->isInternal()) { + $xmlWriter->writeAttribute('w:anchor', $element->getSource()); + } else { + $xmlWriter->writeAttribute('r:id', 'rId' . $rId); } $xmlWriter->writeAttribute('w:history', '1'); $xmlWriter->startElement('w:r'); From 7c8ff2884bcbd0e075929e221580d3e266d97a29 Mon Sep 17 00:00:00 2001 From: jogo Date: Fri, 4 Jul 2014 15:31:48 +0200 Subject: [PATCH 6/8] add sample fix undefined Index --- samples/Sample_35_InternalLink.php | 22 ++++++++++++++++++++++ src/PhpWord/PhpWord.php | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 samples/Sample_35_InternalLink.php diff --git a/samples/Sample_35_InternalLink.php b/samples/Sample_35_InternalLink.php new file mode 100644 index 00000000..3b6aae51 --- /dev/null +++ b/samples/Sample_35_InternalLink.php @@ -0,0 +1,22 @@ +addSection(); +$section->addTitle( 'This is page 1', 1 ); +$linkIsInternal = true; +$section->addLink('MyBookmark', 'Take me to page 3', null ,null,$linkIsInternal); +$section->addPageBreak(); +$section->addTitle( 'This is page 2', 1 ); +$section->addPageBreak(); +$section->addTitle( 'This is page 3', 1 ); +$section->addBookmark('MyBookmark'); + +// Save file +echo write($phpWord, basename(__FILE__, '.php'), $writers); +if (!CLI) { + include_once 'Sample_Footer.php'; +} diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 419af4be..cde4351e 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -139,7 +139,7 @@ class PhpWord /** @var \PhpOffice\PhpWord\Collection\AbstractCollection $collectionObject */ $collectionObject = $this->collections[$key]; - return $collectionObject->addItem($args[0]); + return $collectionObject->addItem(array_key_exists(0,$args) ? $args[0] : null); } // Run add style method From 6c9cffd83b8d40ebf82663722389f596d3b5c945 Mon Sep 17 00:00:00 2001 From: jogo Date: Fri, 4 Jul 2014 15:40:48 +0200 Subject: [PATCH 7/8] updated documentation for link-Element. added documentation for bookmark-Element. --- docs/elements.rst | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/elements.rst b/docs/elements.rst index 2f2fb91c..46db6e40 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -53,6 +53,8 @@ column shows the containers while the rows lists the elements. +-------+-----------------+-----------+----------+----------+---------+------------+------------+ | 22 | Form fields | v | v | v | v | v | v | +-------+-----------------+-----------+----------+----------+---------+------------+------------+ +| 23 | Bookmarks | v | - | - | v | v | - | ++-------+-----------------+-----------+----------+----------+---------+------------+------------+ Legend: @@ -129,12 +131,13 @@ You can add Hyperlinks to the document by using the function addLink: .. code-block:: php - $section->addLink($linkSrc, [$linkName], [$fontStyle], [$paragraphStyle]); + $section->addLink($linkSrc, [$linkName], [$fontStyle], [$paragraphStyle], [$isInternal]); - ``$linkSrc`` The URL of the link. - ``$linkName`` Placeholder of the URL that appears in the document. - ``$fontStyle`` See "Font style" section. - ``$paragraphStyle`` See "Paragraph style" section. +- ``$isInternal`` Set to true, if the link points to a bookmark inside the document Preserve texts ~~~~~~~~~~~~~~ @@ -429,3 +432,14 @@ Form fields ----------- To be completed. + +Bookmarks +~~~~~ + +You can add Bookmarks to the document by using the function addBookmark: + +.. code-block:: php + + $section->addBookmark($name); + +- ``$name`` The name of the bookmark which can be referenced in the addLink-Function as target. Should obviously be unique throughout the document. \ No newline at end of file From 72026345730a0548b55664ebe218a8398cd8ad92 Mon Sep 17 00:00:00 2001 From: jogo Date: Fri, 4 Jul 2014 15:54:20 +0200 Subject: [PATCH 8/8] fix wrong formatting --- src/PhpWord/PhpWord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index cde4351e..083f9d37 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -139,7 +139,7 @@ class PhpWord /** @var \PhpOffice\PhpWord\Collection\AbstractCollection $collectionObject */ $collectionObject = $this->collections[$key]; - return $collectionObject->addItem(array_key_exists(0,$args) ? $args[0] : null); + return $collectionObject->addItem(array_key_exists(0, $args) ? $args[0] : null); } // Run add style method