ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Hyperlinks.php
Go to the documentation of this file.
1<?php
2
4
7use SimpleXMLElement;
8
10{
11 private $worksheet;
12
13 private $hyperlinks = [];
14
15 public function __construct(Worksheet $workSheet)
16 {
17 $this->worksheet = $workSheet;
18 }
19
20 public function readHyperlinks(SimpleXMLElement $relsWorksheet): void
21 {
22 foreach ($relsWorksheet->Relationship as $element) {
23 if ($element['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink') {
24 $this->hyperlinks[(string) $element['Id']] = (string) $element['Target'];
25 }
26 }
27 }
28
29 public function setHyperlinks(SimpleXMLElement $worksheetXml): void
30 {
31 foreach ($worksheetXml->hyperlink as $hyperlink) {
32 if ($hyperlink !== null) {
33 $this->setHyperlink($hyperlink, $this->worksheet);
34 }
35 }
36 }
37
38 private function setHyperlink(SimpleXMLElement $hyperlink, Worksheet $worksheet): void
39 {
40 // Link url
41 $linkRel = $hyperlink->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships');
42
43 foreach (Coordinate::extractAllCellReferencesInRange($hyperlink['ref']) as $cellReference) {
44 $cell = $worksheet->getCell($cellReference);
45 if (isset($linkRel['id'])) {
46 $hyperlinkUrl = $this->hyperlinks[(string) $linkRel['id']] ?? null;
47 if (isset($hyperlink['location'])) {
48 $hyperlinkUrl .= '#' . (string) $hyperlink['location'];
49 }
50 $cell->getHyperlink()->setUrl($hyperlinkUrl);
51 } elseif (isset($hyperlink['location'])) {
52 $cell->getHyperlink()->setUrl('sheet://' . (string) $hyperlink['location']);
53 }
54
55 // Tooltip
56 if (isset($hyperlink['tooltip'])) {
57 $cell->getHyperlink()->setTooltip((string) $hyperlink['tooltip']);
58 }
59 }
60 }
61}
An exception for terminatinating execution or to throw for unit testing.
Helper class to manipulate cell coordinates.
Definition: Coordinate.php:15
static extractAllCellReferencesInRange($cellRange)
Extract all cell references in range, which may be comprised of multiple cell ranges.
Definition: Coordinate.php:338