Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Work item 1196 - Add a hyperlink to an image or textbox

git-svn-id: https://phppowerpoint.svn.codeplex.com/svn/trunk@52160 ffd33b8c-2492-42e0-bdc5-587b920b7d6d
  • Loading branch information...
commit f737fa68abac3ec76e12a30724c7e53a6852b666 1 parent df76f08
@maartenba maartenba authored
View
44 Classes/PHPPowerPoint/Shape.php
@@ -97,6 +97,13 @@
* @var PHPPowerPoint_Shape_Shadow
*/
protected $_shadow;
+
+ /**
+ * Hyperlink
+ *
+ * @var PHPPowerPoint_Shape_Hyperlink
+ */
+ protected $_hyperlink;
/**
* Create a new PHPPowerPoint_Shape
@@ -313,6 +320,42 @@ public function setShadow(PHPPowerPoint_Shape_Shadow $pValue = null) {
$this->_shadow = $pValue;
return $this;
}
+
+ /**
+ * Has Hyperlink?
+ *
+ * @return boolean
+ */
+ public function hasHyperlink()
+ {
+ return !is_null($this->_hyperlink);
+ }
+
+ /**
+ * Get Hyperlink
+ *
+ * @return PHPPowerPoint_Shape_Hyperlink
+ */
+ public function getHyperlink()
+ {
+ if (is_null($this->_hyperlink)) {
+ $this->_hyperlink = new PHPPowerPoint_Shape_Hyperlink();
+ }
+ return $this->_hyperlink;
+ }
+
+ /**
+ * Set Hyperlink
+ *
+ * @param PHPPowerPoint_Shape_Hyperlink $pHyperlink
+ * @throws Exception
+ * @return PHPPowerPoint_Shape
+ */
+ public function setHyperlink(PHPExcel_Cell_Hyperlink $pHyperlink = null)
+ {
+ $this->_hyperlink = $pHyperlink;
+ return $this;
+ }
/**
* Get hash code
@@ -329,6 +372,7 @@ public function getHashCode() {
. $this->_rotation
. $this->getFill()->getHashCode()
. $this->_shadow->getHashCode()
+ . (is_null($this->_hyperlink) ? '' : $this->_hyperlink->getHashCode())
. __CLASS__
);
}
View
44 Classes/PHPPowerPoint/Shape/RichText/TextElement.php
@@ -39,6 +39,13 @@ class PHPPowerPoint_Shape_RichText_TextElement implements PHPPowerPoint_Shape_Ri
* @var string
*/
private $_text;
+
+ /**
+ * Hyperlink
+ *
+ * @var PHPPowerPoint_Shape_Hyperlink
+ */
+ protected $_hyperlink;
/**
* Create a new PHPPowerPoint_Shape_RichText_TextElement instance
@@ -79,6 +86,42 @@ public function setText($pText = '') {
public function getFont() {
return null;
}
+
+ /**
+ * Has Hyperlink?
+ *
+ * @return boolean
+ */
+ public function hasHyperlink()
+ {
+ return !is_null($this->_hyperlink);
+ }
+
+ /**
+ * Get Hyperlink
+ *
+ * @return PHPPowerPoint_Shape_Hyperlink
+ */
+ public function getHyperlink()
+ {
+ if (is_null($this->_hyperlink)) {
+ $this->_hyperlink = new PHPPowerPoint_Shape_Hyperlink();
+ }
+ return $this->_hyperlink;
+ }
+
+ /**
+ * Set Hyperlink
+ *
+ * @param PHPPowerPoint_Shape_Hyperlink $pHyperlink
+ * @throws Exception
+ * @return PHPPowerPoint_Shape
+ */
+ public function setHyperlink(PHPExcel_Cell_Hyperlink $pHyperlink = null)
+ {
+ $this->_hyperlink = $pHyperlink;
+ return $this;
+ }
/**
* Get hash code
@@ -88,6 +131,7 @@ public function getFont() {
public function getHashCode() {
return md5(
$this->_text
+ . (is_null($this->_hyperlink) ? '' : $this->_hyperlink->getHashCode())
. __CLASS__
);
}
View
9 Classes/PHPPowerPoint/Writer/PowerPoint2007.php
@@ -222,16 +222,11 @@ public function save($pFilename = null)
// Add presentation to ZIP file
$objZip->addFromString('ppt/presentation.xml', $this->getWriterPart('Presentation')->writePresentation($this->_presentation));
- // Add slides (drawings, ...)
+ // Add slides (drawings, ...) and slide relationships (drawings, ...)
for ($i = 0; $i < $this->_presentation->getSlideCount(); ++$i) {
// Add slide
- $objZip->addFromString('ppt/slides/slide' . ($i + 1) . '.xml', $this->getWriterPart('Slide')->writeSlide($this->_presentation->getSlide($i)));
- }
-
- // Add slide relationships (drawings, ...)
- for ($i = 0; $i < $this->_presentation->getSlideCount(); ++$i) {
- // Add relationships
$objZip->addFromString('ppt/slides/_rels/slide' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeSlideRelationships($this->_presentation->getSlide($i), ($i + 1)));
+ $objZip->addFromString('ppt/slides/slide' . ($i + 1) . '.xml', $this->getWriterPart('Slide')->writeSlide($this->_presentation->getSlide($i)));
}
// Add media
View
22 Classes/PHPPowerPoint/Writer/PowerPoint2007/LayoutPack/Default.php
@@ -844,7 +844,7 @@ public function __construct() {
);
// Layouts - PHPPowerPoint_Slide_Layout::TITLE_SLIDE
- $this->_layouts[] = array(
+ $this->_layouts[1] = array(
'masterid' => 1,
'name' => PHPPowerPoint_Slide_Layout::TITLE_SLIDE,
'body' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -1084,7 +1084,7 @@ public function __construct() {
</p:sldLayout>');
// Layouts - PHPPowerPoint_Slide_Layout::TITLE_AND_CONTENT
- $this->_layouts[] = array(
+ $this->_layouts[2] = array(
'masterid' => 1,
'name' => PHPPowerPoint_Slide_Layout::TITLE_AND_CONTENT,
'body' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -1252,7 +1252,7 @@ public function __construct() {
</p:sldLayout>');
// Layouts - PHPPowerPoint_Slide_Layout::SECTION_HEADER
- $this->_layouts[] = array(
+ $this->_layouts[3] = array(
'masterid' => 1,
'name' => PHPPowerPoint_Slide_Layout::SECTION_HEADER,
'body' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -1496,7 +1496,7 @@ public function __construct() {
</p:sldLayout>');
// Layouts - PHPPowerPoint_Slide_Layout::TWO_CONTENT
- $this->_layouts[] = array(
+ $this->_layouts[4] = array(
'masterid' => 1,
'name' => PHPPowerPoint_Slide_Layout::TWO_CONTENT,
'body' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -1782,7 +1782,7 @@ public function __construct() {
</p:sldLayout>');
// Layouts - PHPPowerPoint_Slide_Layout::COMPARISON
- $this->_layouts[] = array(
+ $this->_layouts[5] = array(
'masterid' => 1,
'name' => PHPPowerPoint_Slide_Layout::COMPARISON,
'body' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -2202,7 +2202,7 @@ public function __construct() {
</p:sldLayout>');
// Layouts - PHPPowerPoint_Slide_Layout::TITLE_ONLY
- $this->_layouts[] = array(
+ $this->_layouts[6] = array(
'masterid' => 1,
'name' => PHPPowerPoint_Slide_Layout::TITLE_ONLY,
'body' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -2318,7 +2318,7 @@ public function __construct() {
</p:sldLayout>');
// Layouts - PHPPowerPoint_Slide_Layout::BLANK
- $this->_layouts[] = array(
+ $this->_layouts[7] = array(
'masterid' => 1,
'name' => PHPPowerPoint_Slide_Layout::BLANK,
'body' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -2411,7 +2411,7 @@ public function __construct() {
</p:sldLayout>');
// Layouts - PHPPowerPoint_Slide_Layout::CONTENT_WITH_CAPTION
- $this->_layouts[] = array(
+ $this->_layouts[8] = array(
'masterid' => 1,
'name' => PHPPowerPoint_Slide_Layout::CONTENT_WITH_CAPTION,
'body' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -2686,7 +2686,7 @@ public function __construct() {
</p:sldLayout>');
// Layouts - PHPPowerPoint_Slide_Layout::PICTURE_WITH_CAPTION
- $this->_layouts[] = array(
+ $this->_layouts[9] = array(
'masterid' => 1,
'name' => PHPPowerPoint_Slide_Layout::PICTURE_WITH_CAPTION,
'body' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -2937,7 +2937,7 @@ public function __construct() {
</p:sldLayout>');
// Layouts - PHPPowerPoint_Slide_Layout::TITLE_AND_VERTICAL_TEXT
- $this->_layouts[] = array(
+ $this->_layouts[10] = array(
'masterid' => 1,
'name' => PHPPowerPoint_Slide_Layout::TITLE_AND_VERTICAL_TEXT,
'body' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -3105,7 +3105,7 @@ public function __construct() {
</p:sldLayout>');
// Layouts - PHPPowerPoint_Slide_Layout::VERTICAL_TITLE_AND_TEXT
- $this->_layouts[] = array(
+ $this->_layouts[11] = array(
'masterid' => 1,
'name' => PHPPowerPoint_Slide_Layout::VERTICAL_TITLE_AND_TEXT,
'body' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
View
74 Classes/PHPPowerPoint/Writer/PowerPoint2007/Rels.php
@@ -334,9 +334,6 @@ public function writeThemeRelationships($masterId = 1)
/**
* Write slide relationships to XML format
*
- * Numbering is as follows:
- * rId1 - Drawings
- *
* @param PHPPowerPoint_Slide $pSlide
* @param int $pSlideId
* @return string XML Output
@@ -394,6 +391,77 @@ public function writeSlideRelationships(PHPPowerPoint_Slide $pSlide = null, $pSl
$iterator->next();
}
}
+
+ // Write hyperlink relationships?
+ if ($pSlide->getShapeCollection()->count() > 0) {
+ // Loop trough hyperlinks and write relationships
+ $iterator = $pSlide->getShapeCollection()->getIterator();
+ while ($iterator->valid()) {
+ // Hyperlink on shape
+ if ($iterator->current()->hasHyperlink()) {
+ // Write relationship for hyperlink
+ $hyperlink = $iterator->current()->getHyperlink();
+ $hyperlink->__relationId = 'rId' . $relId;
+
+ if (!$hyperlink->isInternal()) {
+ $this->_writeRelationship(
+ $objWriter,
+ $relId,
+ 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
+ $hyperlink->getUrl(),
+ 'External'
+ );
+ } else {
+ $this->_writeRelationship(
+ $objWriter,
+ $relId,
+ 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide',
+ 'slide' . $hyperlink->getSlideNumber() . '.xml'
+ );
+ }
+
+ ++$relId;
+ }
+
+ // Hyperlink on rich text run
+ if ($iterator->current() instanceof PHPPowerPoint_Shape_RichText) {
+ foreach ($iterator->current()->getParagraphs() as $paragraph) {
+ foreach ($paragraph->getRichTextElements() as $element) {
+ if ($element instanceof PHPPowerPoint_Shape_RichText_Run
+ || $element instanceof PHPPowerPoint_Shape_RichText_TextElement)
+ {
+ if ($element->hasHyperlink()) {
+ // Write relationship for hyperlink
+ $hyperlink = $element->getHyperlink();
+ $hyperlink->__relationId = 'rId' . $relId;
+
+ if (!$hyperlink->isInternal()) {
+ $this->_writeRelationship(
+ $objWriter,
+ $relId,
+ 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
+ $hyperlink->getUrl(),
+ 'External'
+ );
+ } else {
+ $this->_writeRelationship(
+ $objWriter,
+ $relId,
+ 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide',
+ 'slide' . $hyperlink->getSlideNumber() . '.xml'
+ );
+ }
+
+ ++$relId;
+ }
+ }
+ }
+ }
+ }
+
+ $iterator->next();
+ }
+ }
$objWriter->endElement();
View
57 Classes/PHPPowerPoint/Writer/PowerPoint2007/Slide.php
@@ -195,6 +195,12 @@ private function _writePic(PHPPowerPoint_Shared_XMLWriter $objWriter = null, PHP
$objWriter->writeAttribute('id', $shapeId);
$objWriter->writeAttribute('name', $shape->getName());
$objWriter->writeAttribute('descr', $shape->getDescription());
+
+ // a:hlinkClick
+ if ($shape->hasHyperlink()) {
+ $this->_writeHyperlink($objWriter, $shape);
+ }
+
$objWriter->endElement();
// p:cNvPicPr
@@ -208,7 +214,7 @@ private function _writePic(PHPPowerPoint_Shared_XMLWriter $objWriter = null, PHP
$objWriter->endElement();
// p:nvPr
- $objWriter->writeElement('p:nvPr', null);
+ $objWriter->writeElement('p:nvPr', null);
$objWriter->endElement();
@@ -314,6 +320,12 @@ private function _writeTxt(PHPPowerPoint_Shared_XMLWriter $objWriter = null, PHP
$objWriter->startElement('p:cNvPr');
$objWriter->writeAttribute('id', $shapeId);
$objWriter->writeAttribute('name', '');
+
+ // a:hlinkClick
+ if ($shape->hasHyperlink()) {
+ $this->_writeHyperlink($objWriter, $shape);
+ }
+
$objWriter->endElement();
// p:cNvSpPr
@@ -323,7 +335,7 @@ private function _writeTxt(PHPPowerPoint_Shared_XMLWriter $objWriter = null, PHP
// p:nvPr
$objWriter->writeElement('p:nvPr', null);
-
+
$objWriter->endElement();
// p:spPr
@@ -450,6 +462,12 @@ private function _writeTable(PHPPowerPoint_Shared_XMLWriter $objWriter = null, P
$objWriter->writeAttribute('id', $shapeId);
$objWriter->writeAttribute('name', $shape->getName());
$objWriter->writeAttribute('descr', $shape->getDescription());
+
+ // a:hlinkClick
+ if ($shape->hasHyperlink()) {
+ $this->_writeHyperlink($objWriter, $shape);
+ }
+
$objWriter->endElement();
// p:cNvGraphicFramePr
@@ -464,7 +482,7 @@ private function _writeTable(PHPPowerPoint_Shared_XMLWriter $objWriter = null, P
// p:nvPr
$objWriter->writeElement('p:nvPr', null);
-
+
$objWriter->endElement();
// p:xfrm
@@ -771,6 +789,11 @@ private function _writeParagraphs(PHPPowerPoint_Shared_XMLWriter $objWriter, $pa
$objWriter->startElement('a:latin');
$objWriter->writeAttribute('typeface', $element->getFont()->getName());
$objWriter->endElement();
+
+ // a:hlinkClick
+ if ($element->hasHyperlink()) {
+ $this->_writeHyperlink($objWriter, $element);
+ }
$objWriter->endElement();
}
@@ -808,13 +831,19 @@ private function _writeLineShape(PHPPowerPoint_Shared_XMLWriter $objWriter = nul
$objWriter->startElement('p:cNvPr');
$objWriter->writeAttribute('id', $shapeId);
$objWriter->writeAttribute('name', '');
+
+ // a:hlinkClick
+ if ($shape->hasHyperlink()) {
+ $this->_writeHyperlink($objWriter, $shape);
+ }
+
$objWriter->endElement();
// p:cNvCxnSpPr
$objWriter->writeElement('p:cNvCxnSpPr', null);
- // p:nvPr
- $objWriter->writeElement('p:nvPr', null);
+ // p:nvPr
+ $objWriter->writeElement('p:nvPr', null);
$objWriter->endElement();
@@ -1087,4 +1116,22 @@ private function _writePatternFill(PHPPowerPoint_Shared_XMLWriter $objWriter = n
$objWriter->endElement();
}
+
+ /**
+ * Write hyperlink
+ *
+ * @param PHPPowerPoint_Shared_XMLWriter $objWriter XML Writer
+ * @param PHPPowerPoint_Shape|PHPPowerPoint_Shape_RichText_TextElement $shape
+ */
+ private function _writeHyperlink(PHPPowerPoint_Shared_XMLWriter $objWriter = null, $shape = null)
+ {
+ // a:hlinkClick
+ $objWriter->startElement('a:hlinkClick');
+ $objWriter->writeAttribute('r:id', $shape->getHyperlink()->__relationId);
+ $objWriter->writeAttribute('tooltip', $shape->getHyperlink()->getTooltip());
+ if ($shape->getHyperlink()->isInternal()) {
+ $objWriter->writeAttribute('action', $shape->getHyperlink()->getUrl());
+ }
+ $objWriter->endElement();
+ }
}
View
4 Tests/02presentation.php
@@ -203,9 +203,11 @@
$shape->createBreak();
-$textRun = $shape->createTextRun(' http://phppowerpoint.codeplex.com');
+$textRun = $shape->createTextRun('http://phppowerpoint.codeplex.com');
$textRun->getFont()->setSize(36)
->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
+$textRun->getHyperlink()->setUrl("http://phppowerpoint.codeplex.com")
+ ->setTooltip('PHPPowerPoint');
// Save PowerPoint 2007 file
View
2  Tests/05templated.php
@@ -80,6 +80,8 @@
$textRun->getFont()->setBold(true)
->setSize(60)
->setColor( new PHPPowerPoint_Style_Color( 'FFC00000' ) );
+$shape->getHyperlink()->setUrl("http://phppowerpoint.codeplex.com")
+ ->setTooltip('PHPPowerPoint');
// Create a shape (line)
$shape = $currentSlide->createLineShape(170, 180, 770, 180);
View
1  changelog.txt
@@ -34,6 +34,7 @@ To be planned:
- Feature: (MB) Work item 1378 - Possibility to set borders on tables and table cells
- Feature: (MB) Work item 4921 - Access to additional properties of Text Boxes
- Feature: (MB) Applied patches 7010 and 7020
+- Feature: (MB) Work item 1196 - Add a hyperlink to an image or textbox
- Feature: (MBaker) Implement autoloader
- Bugfix: (MBaker) Allow solid colour fill
- Bugfix: (MB) Work item 3910 - Table width setting Office 2007
Please sign in to comment.
Something went wrong with that request. Please try again.