From d25dc965c9aa7f0fa9971facce0d73d9bd97db0f Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 26 Apr 2014 16:05:29 +0700 Subject: [PATCH] ODT Writer: Link writing --- CHANGELOG.md | 3 ++ docs/intro.rst | 2 +- docs/src/documentation.md | 2 +- src/PhpWord/Element/Link.php | 48 ++++++++++++++----- src/PhpWord/Writer/HTML/Element/Link.php | 13 +---- src/PhpWord/Writer/ODText/Element/Link.php | 38 +++++++++++++++ src/PhpWord/Writer/ODText/Element/TextRun.php | 5 +- src/PhpWord/Writer/Word2007/Element/Link.php | 6 +-- tests/PhpWord/Tests/Element/LinkTest.php | 8 ++-- tests/PhpWord/Tests/Element/TextRunTest.php | 6 +-- 10 files changed, 92 insertions(+), 39 deletions(-) create mode 100644 src/PhpWord/Writer/ODText/Element/Link.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 53d10a2c..908b0067 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ This release marked heavy refactorings on internal code structure with the creat - DOCX Writer: Change `docProps/app.xml` `Application` to `PHPWord` - @ivanlanin - DOCX Writer: Create `word/settings.xml` and `word/webSettings.xml` dynamically - @ivanlanin - ODT Writer: Basic image writing - @ivanlanin +- ODT Writer: Link writing - @ivanlanin ### Bugfixes @@ -55,6 +56,8 @@ This release marked heavy refactorings on internal code structure with the creat - `Footnote::addFootnoteLinkElement` replaced by `Media::addElement` - `Footnote::getFootnoteLinkElements` replaced by `Media::getElements` - All current methods on `Media` +- `Element\Link::getLinkSrc` replaced by `Element\Link::getTarget` +- `Element\Link::getLinkName` replaced by `Element\Link::getText` ### Miscellaneous diff --git a/docs/intro.rst b/docs/intro.rst index 48b4c092..362bd11f 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -77,7 +77,7 @@ Writers +---------------------------+----------------------+--------+-------+-------+--------+-------+ | | Title | ✓ | | | ✓ | ✓ | +---------------------------+----------------------+--------+-------+-------+--------+-------+ -| | Link | ✓ | | | ✓ | ✓ | +| | Link | ✓ | ✓ | | ✓ | ✓ | +---------------------------+----------------------+--------+-------+-------+--------+-------+ | | Preserve Text | ✓ | | | | | +---------------------------+----------------------+--------+-------+-------+--------+-------+ diff --git a/docs/src/documentation.md b/docs/src/documentation.md index 7766d66f..a96250ec 100644 --- a/docs/src/documentation.md +++ b/docs/src/documentation.md @@ -88,7 +88,7 @@ Below are the supported features for each file formats. | **Element Type** | Text | ✓ | ✓ | ✓ | ✓ | ✓ | | | Text Run | ✓ | ✓ | ✓ | ✓ | ✓ | | | Title | ✓ | | | ✓ | ✓ | -| | Link | ✓ | | | ✓ | ✓ | +| | Link | ✓ | ✓ | | ✓ | ✓ | | | Preserve Text | ✓ | | | | | | | Text Break | ✓ | ✓ | ✓ | ✓ | ✓ | | | Page Break | ✓ | | | | | diff --git a/src/PhpWord/Element/Link.php b/src/PhpWord/Element/Link.php index eabac932..d11be09d 100644 --- a/src/PhpWord/Element/Link.php +++ b/src/PhpWord/Element/Link.php @@ -18,18 +18,18 @@ use PhpOffice\PhpWord\Style\Paragraph; class Link extends AbstractElement { /** - * Link source + * Link target * * @var string */ - private $source; + private $target; /** - * Link name + * Link text * * @var string */ - private $name; + private $text; /** * Font style @@ -54,10 +54,10 @@ class Link extends AbstractElement * @param mixed $fontStyle * @param mixed $paragraphStyle */ - public function __construct($linkSrc, $linkName = null, $fontStyle = null, $paragraphStyle = null) + public function __construct($target, $text = null, $fontStyle = null, $paragraphStyle = null) { - $this->source = $linkSrc; - $this->name = $linkName; + $this->target = $target; + $this->text = is_null($text) ? $target : $text; $this->fontStyle = $this->setStyle(new Font('text'), $fontStyle); $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); @@ -65,23 +65,23 @@ class Link extends AbstractElement } /** - * Get Link source + * Get link target * * @return string */ - public function getLinkSrc() + public function getTarget() { - return $this->source; + return $this->target; } /** - * Get Link name + * Get link text * * @return string */ - public function getLinkName() + public function getText() { - return $this->name; + return $this->text; } /** @@ -103,4 +103,26 @@ class Link extends AbstractElement { return $this->paragraphStyle; } + + /** + * Get Link source + * + * @return string + * @deprecated 0.10.0 + */ + public function getLinkSrc() + { + return $this->getTarget(); + } + + /** + * Get Link name + * + * @return string + * @deprecated 0.10.0 + */ + public function getLinkName() + { + return $this->getText(); + } } diff --git a/src/PhpWord/Writer/HTML/Element/Link.php b/src/PhpWord/Writer/HTML/Element/Link.php index eab49e34..1680edfc 100644 --- a/src/PhpWord/Writer/HTML/Element/Link.php +++ b/src/PhpWord/Writer/HTML/Element/Link.php @@ -23,18 +23,9 @@ class Link extends Element */ public function write() { - $url = $this->element->getLinkSrc(); - $text = $this->element->getLinkName(); - if ($text == '') { - $text = $url; - } - $html = ''; + $html = "element->getTarget()}\">{$this->element->getText()}" . PHP_EOL; if (!$this->withoutP) { - $html .= "

" . PHP_EOL; - } - $html .= "{$text}" . PHP_EOL; - if (!$this->withoutP) { - $html .= "

" . PHP_EOL; + $html = '

' . $html . '

' . PHP_EOL; } return $html; diff --git a/src/PhpWord/Writer/ODText/Element/Link.php b/src/PhpWord/Writer/ODText/Element/Link.php new file mode 100644 index 00000000..08235519 --- /dev/null +++ b/src/PhpWord/Writer/ODText/Element/Link.php @@ -0,0 +1,38 @@ +withoutP) { + $this->xmlWriter->startElement('text:p'); // text:p + } + + $this->xmlWriter->startElement('text:a'); + $this->xmlWriter->writeAttribute('xlink:type', 'simple'); + $this->xmlWriter->writeAttribute('xlink:href', $this->element->getTarget()); + $this->xmlWriter->writeRaw($this->element->getText()); + $this->xmlWriter->endElement(); // text:a + + if (!$this->withoutP) { + $this->xmlWriter->endElement(); // text:p + } + } +} diff --git a/src/PhpWord/Writer/ODText/Element/TextRun.php b/src/PhpWord/Writer/ODText/Element/TextRun.php index f4982fad..59187583 100644 --- a/src/PhpWord/Writer/ODText/Element/TextRun.php +++ b/src/PhpWord/Writer/ODText/Element/TextRun.php @@ -29,7 +29,10 @@ class TextRun extends Element if (count($elements) > 0) { foreach ($elements as $element) { if ($element instanceof TextElement) { - $elementWriter = new ElementWriter($this->xmlWriter, $this->parentWriter, $element, true); + $elementWriter = new Text($this->xmlWriter, $this->parentWriter, $element, true); + $elementWriter->write(); + } elseif ($element instanceof Link) { + $elementWriter = new Link($this->xmlWriter, $this->parentWriter, $element, true); $elementWriter->write(); } } diff --git a/src/PhpWord/Writer/Word2007/Element/Link.php b/src/PhpWord/Writer/Word2007/Element/Link.php index 4169f687..7fcdffb8 100644 --- a/src/PhpWord/Writer/Word2007/Element/Link.php +++ b/src/PhpWord/Writer/Word2007/Element/Link.php @@ -25,10 +25,6 @@ class Link extends Element public function write() { $rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0); - $linkName = $this->element->getLinkName(); - if (is_null($linkName)) { - $linkName = $this->element->getLinkSrc(); - } $fStyle = $this->element->getFontStyle(); $pStyle = $this->element->getParagraphStyle(); @@ -50,7 +46,7 @@ class Link extends Element $styleWriter->write(); $this->xmlWriter->startElement('w:t'); $this->xmlWriter->writeAttribute('xml:space', 'preserve'); - $this->xmlWriter->writeRaw($linkName); + $this->xmlWriter->writeRaw($this->element->getText()); $this->xmlWriter->endElement(); // w:t $this->xmlWriter->endElement(); // w:r $this->xmlWriter->endElement(); // w:hyperlink diff --git a/tests/PhpWord/Tests/Element/LinkTest.php b/tests/PhpWord/Tests/Element/LinkTest.php index 9f89b21c..3840ede8 100644 --- a/tests/PhpWord/Tests/Element/LinkTest.php +++ b/tests/PhpWord/Tests/Element/LinkTest.php @@ -28,8 +28,8 @@ class LinkTest extends \PHPUnit_Framework_TestCase $oLink = new Link('http://www.google.com'); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $oLink); - $this->assertEquals($oLink->getLinkSrc(), 'http://www.google.com'); - $this->assertEquals($oLink->getLinkName(), null); + $this->assertEquals($oLink->getTarget(), 'http://www.google.com'); + $this->assertEquals($oLink->getText(), $oLink->getTarget()); $this->assertEquals($oLink->getFontStyle(), null); $this->assertEquals($oLink->getParagraphStyle(), null); } @@ -47,8 +47,8 @@ class LinkTest extends \PHPUnit_Framework_TestCase ); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $oLink); - $this->assertEquals($oLink->getLinkSrc(), 'http://www.google.com'); - $this->assertEquals($oLink->getLinkName(), 'Search Engine'); + $this->assertEquals($oLink->getTarget(), 'http://www.google.com'); + $this->assertEquals($oLink->getText(), 'Search Engine'); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oLink->getFontStyle()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oLink->getParagraphStyle()); } diff --git a/tests/PhpWord/Tests/Element/TextRunTest.php b/tests/PhpWord/Tests/Element/TextRunTest.php index 6f277e82..0916e40b 100644 --- a/tests/PhpWord/Tests/Element/TextRunTest.php +++ b/tests/PhpWord/Tests/Element/TextRunTest.php @@ -90,7 +90,7 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element); $this->assertCount(1, $oTextRun->getElements()); - $this->assertEquals($element->getLinkSrc(), 'http://www.google.fr'); + $this->assertEquals($element->getTarget(), 'http://www.google.fr'); } /** @@ -103,8 +103,8 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element); $this->assertCount(1, $oTextRun->getElements()); - $this->assertEquals($element->getLinkSrc(), 'http://www.google.fr'); - $this->assertEquals($element->getLinkName(), 'ééé'); + $this->assertEquals($element->getTarget(), 'http://www.google.fr'); + $this->assertEquals($element->getText(), 'ééé'); } /**