Skip to content

Commit

Permalink
Work item 1196 - Add a hyperlink to an image or textbox
Browse files Browse the repository at this point in the history
git-svn-id: https://phppowerpoint.svn.codeplex.com/svn/trunk@52160 ffd33b8c-2492-42e0-bdc5-587b920b7d6d
  • Loading branch information
maartenba committed Oct 11, 2010
1 parent df76f08 commit f737fa6
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 27 deletions.
44 changes: 44 additions & 0 deletions Classes/PHPPowerPoint/Shape.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ abstract class PHPPowerPoint_Shape implements PHPPowerPoint_IComparable
* @var PHPPowerPoint_Shape_Shadow
*/
protected $_shadow;

/**
* Hyperlink
*
* @var PHPPowerPoint_Shape_Hyperlink
*/
protected $_hyperlink;

/**
* Create a new PHPPowerPoint_Shape
Expand Down Expand Up @@ -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
Expand All @@ -329,6 +372,7 @@ public function getHashCode() {
. $this->_rotation
. $this->getFill()->getHashCode()
. $this->_shadow->getHashCode()
. (is_null($this->_hyperlink) ? '' : $this->_hyperlink->getHashCode())
. __CLASS__
);
}
Expand Down
44 changes: 44 additions & 0 deletions Classes/PHPPowerPoint/Shape/RichText/TextElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -88,6 +131,7 @@ public function getFont() {
public function getHashCode() {
return md5(
$this->_text
. (is_null($this->_hyperlink) ? '' : $this->_hyperlink->getHashCode())
. __CLASS__
);
}
Expand Down
9 changes: 2 additions & 7 deletions Classes/PHPPowerPoint/Writer/PowerPoint2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions Classes/PHPPowerPoint/Writer/PowerPoint2007/LayoutPack/Default.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"?>
Expand Down Expand Up @@ -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"?>
Expand Down Expand Up @@ -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"?>
Expand Down Expand Up @@ -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"?>
Expand Down Expand Up @@ -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"?>
Expand Down Expand Up @@ -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"?>
Expand Down Expand Up @@ -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"?>
Expand Down Expand Up @@ -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"?>
Expand Down Expand Up @@ -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"?>
Expand Down Expand Up @@ -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"?>
Expand Down Expand Up @@ -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"?>
Expand Down
74 changes: 71 additions & 3 deletions Classes/PHPPowerPoint/Writer/PowerPoint2007/Rels.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down
Loading

0 comments on commit f737fa6

Please sign in to comment.