/**
  * Set Slide
  *
  * @param  \PhpOffice\PhpPowerpoint\Slide $pValue
  * @param  bool                $pOverrideOld If a Slide has already been assigned, overwrite it and remove image from old Slide?
  * @throws \Exception
  * @return self
  */
 public function setSlide(Slide $pValue = null, $pOverrideOld = false)
 {
     if (is_null($this->slide)) {
         // Add drawing to \PhpOffice\PhpPowerpoint\Slide
         $this->slide = $pValue;
         if (!is_null($this->slide)) {
             $this->slide->getShapeCollection()->append($this);
         }
     } else {
         if ($pOverrideOld) {
             // Remove drawing from old \PhpOffice\PhpPowerpoint\Slide
             $iterator = $this->slide->getShapeCollection()->getIterator();
             while ($iterator->valid()) {
                 if ($iterator->current()->getHashCode() == $this->getHashCode()) {
                     $this->slide->getShapeCollection()->offsetUnset($iterator->key());
                     $this->slide = null;
                     break;
                 }
             }
             // Set new \PhpOffice\PhpPowerpoint\Slide
             $this->setSlide($pValue);
         } else {
             throw new \Exception("A \\PhpOffice\\PhpPowerpoint\\Slide has already been assigned. Shapes can only exist on one \\PhpOffice\\PhpPowerpoint\\Slide.");
         }
     }
     return $this;
 }
Example #2
0
 /**
  * Write slide relationships to XML format
  *
  * @param  \PhpOffice\PhpPowerpoint\Slide $pSlide
  * @return string              XML Output
  * @throws \Exception
  */
 public function writeSlideRelationships(SlideElement $pSlide)
 {
     // Create XML writer
     $objWriter = $this->getXMLWriter();
     // 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;
     // Write slideLayout relationship
     $parentWriter = $this->getParentWriter();
     if ($parentWriter instanceof PowerPoint2007) {
         $layoutPack = $parentWriter->getLayoutPack();
         $layoutIndex = $layoutPack->findlayoutIndex($pSlide->getSlideLayout(), $pSlide->getSlideMasterId());
         $this->writeRelationship($objWriter, $relId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout', '../slideLayouts/slideLayout' . ($layoutIndex + 1) . '.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 ShapeDrawing || $iterator->current() instanceof MemoryDrawing) {
                 // Write relationship for image drawing
                 $this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', '../media/' . str_replace(' ', '_', $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;
             }
             $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 RichText) {
                 foreach ($iterator->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;
                             }
                         }
                     }
                 }
             }
             $iterator->next();
         }
     }
     $objWriter->endElement();
     // Return
     return $objWriter->getData();
 }