protected function writeSlideRelationships(Slide $pSlide)
{
//@todo Group all getShapeCollection()->getIterator
// Create XML writer
$objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
// XML header
$objWriter->startDocument('1.0', 'UTF-8', 'yes');
// Relationships
$objWriter->startElement('Relationships');
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
// Starting relation id
$relId = 1;
$idxSlide = $pSlide->getParent()->getIndex($pSlide);
// Write slideLayout relationship
if ($pSlide->getSlideLayout()) {
$layoutId = $pSlide->getSlideLayout()->layoutNr;
$this->writeRelationship($objWriter, $relId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout', '../slideLayouts/slideLayout' . $layoutId . '.xml');
}
// Write drawing relationships?
if ($pSlide->getShapeCollection()->count() > 0) {
// Loop trough images and write relationships
$iterator = $pSlide->getShapeCollection()->getIterator();
while ($iterator->valid()) {
if ($iterator->current() instanceof Media) {
// Write relationship for image drawing
$this->writeRelationship($objWriter, $relId, 'http://schemas.microsoft.com/office/2007/relationships/media', '../media/' . $iterator->current()->getIndexedFilename());
$iterator->current()->relationId = 'rId' . $relId;
++$relId;
} elseif ($iterator->current() instanceof ShapeDrawing\AbstractDrawingAdapter) {
// Write relationship for image drawing
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', '../media/' . $iterator->current()->getIndexedFilename());
$iterator->current()->relationId = 'rId' . $relId;
++$relId;
} elseif ($iterator->current() instanceof ShapeChart) {
// Write relationship for chart drawing
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart', '../charts/' . $iterator->current()->getIndexedFilename());
$iterator->current()->relationId = 'rId' . $relId;
++$relId;
} elseif ($iterator->current() instanceof Group) {
$iterator2 = $iterator->current()->getShapeCollection()->getIterator();
while ($iterator2->valid()) {
if ($iterator->current() instanceof Media) {
// Write relationship for image drawing
$this->writeRelationship($objWriter, $relId, 'http://schemas.microsoft.com/office/2007/relationships/media', '../media/' . $iterator->current()->getIndexedFilename());
$iterator->current()->relationId = 'rId' . $relId;
++$relId;
} elseif ($iterator->current() instanceof ShapeDrawing\AbstractDrawingAdapter) {
// Write relationship for image drawing
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', '../media/' . $iterator2->current()->getIndexedFilename());
$iterator2->current()->relationId = 'rId' . $relId;
++$relId;
} elseif ($iterator2->current() instanceof ShapeChart) {
// Write relationship for chart drawing
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart', '../charts/' . $iterator2->current()->getIndexedFilename());
$iterator2->current()->relationId = 'rId' . $relId;
++$relId;
}
$iterator2->next();
}
}
$iterator->next();
}
}
// Write background relationships?
$oBackground = $pSlide->getBackground();
if ($oBackground instanceof Image) {
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', '../media/' . $oBackground->getIndexedFilename($idxSlide));
$oBackground->relationId = 'rId' . $relId;
++$relId;
}
// 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 RichText) {
foreach ($iterator->current()->getParagraphs() as $paragraph) {
foreach ($paragraph->getRichTextElements() as $element) {
if ($element instanceof Run || $element instanceof RunTextElement) {
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;
}
}
}
}
}
// Hyperlink in table
if ($iterator->current() instanceof ShapeTable) {
// Rows
$countRows = count($iterator->current()->getRows());
for ($row = 0; $row < $countRows; $row++) {
// Cells in rows
$countCells = count($iterator->current()->getRow($row)->getCells());
for ($cell = 0; $cell < $countCells; $cell++) {
$currentCell = $iterator->current()->getRow($row)->getCell($cell);
// Paragraphs in cell
foreach ($currentCell->getParagraphs() as $paragraph) {
// RichText in paragraph
foreach ($paragraph->getRichTextElements() as $element) {
// Run or Text in RichText
if ($element instanceof Run || $element instanceof 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;
}
}
}
}
}
}
}
if ($iterator->current() instanceof Group) {
$iterator2 = $pSlide->getShapeCollection()->getIterator();
while ($iterator2->valid()) {
// Hyperlink on shape
if ($iterator2->current()->hasHyperlink()) {
// Write relationship for hyperlink
$hyperlink = $iterator2->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 ($iterator2->current() instanceof RichText) {
foreach ($iterator2->current()->getParagraphs() as $paragraph) {
foreach ($paragraph->getRichTextElements() as $element) {
if ($element instanceof Run || $element instanceof 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;
}
}
}
}
}
// Hyperlink in table
if ($iterator2->current() instanceof ShapeTable) {
// Rows
$countRows = count($iterator2->current()->getRows());
for ($row = 0; $row < $countRows; $row++) {
// Cells in rows
$countCells = count($iterator2->current()->getRow($row)->getCells());
for ($cell = 0; $cell < $countCells; $cell++) {
$currentCell = $iterator2->current()->getRow($row)->getCell($cell);
// Paragraphs in cell
foreach ($currentCell->getParagraphs() as $paragraph) {
// RichText in paragraph
foreach ($paragraph->getRichTextElements() as $element) {
// Run or Text in RichText
if ($element instanceof Run || $element instanceof 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;
}
}
}
}
}
}
}
$iterator2->next();
}
}
$iterator->next();
}
}
// Write comment relationships
if ($pSlide->getShapeCollection()->count() > 0) {
$hasSlideComment = false;
// Loop trough images and write relationships
$iterator = $pSlide->getShapeCollection()->getIterator();
while ($iterator->valid()) {
if ($iterator->current() instanceof Comment) {
$hasSlideComment = true;
break;
} elseif ($iterator->current() instanceof Group) {
$iterator2 = $iterator->current()->getShapeCollection()->getIterator();
while ($iterator2->valid()) {
if ($iterator2->current() instanceof Comment) {
$hasSlideComment = true;
break 2;
}
$iterator2->next();
}
}
$iterator->next();
}
if ($hasSlideComment) {
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments', '../comments/comment' . ($idxSlide + 1) . '.xml');
++$relId;
}
}
if ($pSlide->getNote()->getShapeCollection()->count() > 0) {
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide', '../notesSlides/notesSlide' . ($idxSlide + 1) . '.xml');
}
$objWriter->endElement();
// Return
return $objWriter->getData();
}