ODT Writer: Link writing

This commit is contained in:
Ivan Lanin 2014-04-26 16:05:29 +07:00
parent 8ace1c1d99
commit d25dc965c9
10 changed files with 92 additions and 39 deletions

View File

@ -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: Change `docProps/app.xml` `Application` to `PHPWord` - @ivanlanin
- DOCX Writer: Create `word/settings.xml` and `word/webSettings.xml` dynamically - @ivanlanin - DOCX Writer: Create `word/settings.xml` and `word/webSettings.xml` dynamically - @ivanlanin
- ODT Writer: Basic image writing - @ivanlanin - ODT Writer: Basic image writing - @ivanlanin
- ODT Writer: Link writing - @ivanlanin
### Bugfixes ### Bugfixes
@ -55,6 +56,8 @@ This release marked heavy refactorings on internal code structure with the creat
- `Footnote::addFootnoteLinkElement` replaced by `Media::addElement` - `Footnote::addFootnoteLinkElement` replaced by `Media::addElement`
- `Footnote::getFootnoteLinkElements` replaced by `Media::getElements` - `Footnote::getFootnoteLinkElements` replaced by `Media::getElements`
- All current methods on `Media` - All current methods on `Media`
- `Element\Link::getLinkSrc` replaced by `Element\Link::getTarget`
- `Element\Link::getLinkName` replaced by `Element\Link::getText`
### Miscellaneous ### Miscellaneous

View File

@ -77,7 +77,7 @@ Writers
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Title | ✓ | | | ✓ | ✓ | | | Title | ✓ | | | ✓ | ✓ |
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Link | ✓ | | | ✓ | ✓ | | | Link | ✓ | | | ✓ | ✓ |
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Preserve Text | ✓ | | | | | | | Preserve Text | ✓ | | | | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+

View File

@ -88,7 +88,7 @@ Below are the supported features for each file formats.
| **Element Type** | Text | ✓ | ✓ | ✓ | ✓ | ✓ | | **Element Type** | Text | ✓ | ✓ | ✓ | ✓ | ✓ |
| | Text Run | ✓ | ✓ | ✓ | ✓ | ✓ | | | Text Run | ✓ | ✓ | ✓ | ✓ | ✓ |
| | Title | ✓ | | | ✓ | ✓ | | | Title | ✓ | | | ✓ | ✓ |
| | Link | ✓ | | | ✓ | ✓ | | | Link | ✓ | | | ✓ | ✓ |
| | Preserve Text | ✓ | | | | | | | Preserve Text | ✓ | | | | |
| | Text Break | ✓ | ✓ | ✓ | ✓ | ✓ | | | Text Break | ✓ | ✓ | ✓ | ✓ | ✓ |
| | Page Break | ✓ | | | | | | | Page Break | ✓ | | | | |

View File

@ -18,18 +18,18 @@ use PhpOffice\PhpWord\Style\Paragraph;
class Link extends AbstractElement class Link extends AbstractElement
{ {
/** /**
* Link source * Link target
* *
* @var string * @var string
*/ */
private $source; private $target;
/** /**
* Link name * Link text
* *
* @var string * @var string
*/ */
private $name; private $text;
/** /**
* Font style * Font style
@ -54,10 +54,10 @@ class Link extends AbstractElement
* @param mixed $fontStyle * @param mixed $fontStyle
* @param mixed $paragraphStyle * @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->target = $target;
$this->name = $linkName; $this->text = is_null($text) ? $target : $text;
$this->fontStyle = $this->setStyle(new Font('text'), $fontStyle); $this->fontStyle = $this->setStyle(new Font('text'), $fontStyle);
$this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle);
@ -65,23 +65,23 @@ class Link extends AbstractElement
} }
/** /**
* Get Link source * Get link target
* *
* @return string * @return string
*/ */
public function getLinkSrc() public function getTarget()
{ {
return $this->source; return $this->target;
} }
/** /**
* Get Link name * Get link text
* *
* @return string * @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; 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();
}
} }

View File

@ -23,18 +23,9 @@ class Link extends Element
*/ */
public function write() public function write()
{ {
$url = $this->element->getLinkSrc(); $html = "<a href=\"{$this->element->getTarget()}\">{$this->element->getText()}</a>" . PHP_EOL;
$text = $this->element->getLinkName();
if ($text == '') {
$text = $url;
}
$html = '';
if (!$this->withoutP) { if (!$this->withoutP) {
$html .= "<p>" . PHP_EOL; $html = '<p>' . $html . '</p>' . PHP_EOL;
}
$html .= "<a href=\"{$url}\">{$text}</a>" . PHP_EOL;
if (!$this->withoutP) {
$html .= "</p>" . PHP_EOL;
} }
return $html; return $html;

View File

@ -0,0 +1,38 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\ODText\Element;
/**
* Text element writer
*
* @since 0.10.0
*/
class Link extends Element
{
/**
* Write element
*/
public function write()
{
if (!$this->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
}
}
}

View File

@ -29,7 +29,10 @@ class TextRun extends Element
if (count($elements) > 0) { if (count($elements) > 0) {
foreach ($elements as $element) { foreach ($elements as $element) {
if ($element instanceof TextElement) { 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(); $elementWriter->write();
} }
} }

View File

@ -25,10 +25,6 @@ class Link extends Element
public function write() public function write()
{ {
$rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0); $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(); $fStyle = $this->element->getFontStyle();
$pStyle = $this->element->getParagraphStyle(); $pStyle = $this->element->getParagraphStyle();
@ -50,7 +46,7 @@ class Link extends Element
$styleWriter->write(); $styleWriter->write();
$this->xmlWriter->startElement('w:t'); $this->xmlWriter->startElement('w:t');
$this->xmlWriter->writeAttribute('xml:space', 'preserve'); $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:t
$this->xmlWriter->endElement(); // w:r $this->xmlWriter->endElement(); // w:r
$this->xmlWriter->endElement(); // w:hyperlink $this->xmlWriter->endElement(); // w:hyperlink

View File

@ -28,8 +28,8 @@ class LinkTest extends \PHPUnit_Framework_TestCase
$oLink = new Link('http://www.google.com'); $oLink = new Link('http://www.google.com');
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $oLink); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $oLink);
$this->assertEquals($oLink->getLinkSrc(), 'http://www.google.com'); $this->assertEquals($oLink->getTarget(), 'http://www.google.com');
$this->assertEquals($oLink->getLinkName(), null); $this->assertEquals($oLink->getText(), $oLink->getTarget());
$this->assertEquals($oLink->getFontStyle(), null); $this->assertEquals($oLink->getFontStyle(), null);
$this->assertEquals($oLink->getParagraphStyle(), null); $this->assertEquals($oLink->getParagraphStyle(), null);
} }
@ -47,8 +47,8 @@ class LinkTest extends \PHPUnit_Framework_TestCase
); );
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $oLink); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $oLink);
$this->assertEquals($oLink->getLinkSrc(), 'http://www.google.com'); $this->assertEquals($oLink->getTarget(), 'http://www.google.com');
$this->assertEquals($oLink->getLinkName(), 'Search Engine'); $this->assertEquals($oLink->getText(), 'Search Engine');
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oLink->getFontStyle()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oLink->getFontStyle());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oLink->getParagraphStyle()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oLink->getParagraphStyle());
} }

View File

@ -90,7 +90,7 @@ class TextRunTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
$this->assertCount(1, $oTextRun->getElements()); $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->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
$this->assertCount(1, $oTextRun->getElements()); $this->assertCount(1, $oTextRun->getElements());
$this->assertEquals($element->getLinkSrc(), 'http://www.google.fr'); $this->assertEquals($element->getTarget(), 'http://www.google.fr');
$this->assertEquals($element->getLinkName(), 'ééé'); $this->assertEquals($element->getText(), 'ééé');
} }
/** /**