Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
   4  
   5  use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
   6  use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
   7  use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
   8  use SimpleXMLElement;
   9  
  10  class Hyperlinks
  11  {
  12      private $worksheet;
  13  
  14      private $hyperlinks = [];
  15  
  16      public function __construct(Worksheet $workSheet)
  17      {
  18          $this->worksheet = $workSheet;
  19      }
  20  
  21      public function readHyperlinks(SimpleXMLElement $relsWorksheet): void
  22      {
  23          foreach ($relsWorksheet->children(Namespaces::RELATIONSHIPS)->Relationship as $elementx) {
  24              $element = Xlsx::getAttributes($elementx);
  25              if ($element->Type == Namespaces::HYPERLINK) {
  26                  $this->hyperlinks[(string) $element->Id] = (string) $element->Target;
  27              }
  28          }
  29      }
  30  
  31      public function setHyperlinks(SimpleXMLElement $worksheetXml): void
  32      {
  33          foreach ($worksheetXml->children(Namespaces::MAIN)->hyperlink as $hyperlink) {
  34              if ($hyperlink !== null) {
  35                  $this->setHyperlink($hyperlink, $this->worksheet);
  36              }
  37          }
  38      }
  39  
  40      private function setHyperlink(SimpleXMLElement $hyperlink, Worksheet $worksheet): void
  41      {
  42          // Link url
  43          $linkRel = Xlsx::getAttributes($hyperlink, Namespaces::SCHEMA_OFFICE_DOCUMENT);
  44  
  45          $attributes = Xlsx::getAttributes($hyperlink);
  46          foreach (Coordinate::extractAllCellReferencesInRange($attributes->ref) as $cellReference) {
  47              $cell = $worksheet->getCell($cellReference);
  48              if (isset($linkRel['id'])) {
  49                  $hyperlinkUrl = $this->hyperlinks[(string) $linkRel['id']] ?? null;
  50                  if (isset($attributes['location'])) {
  51                      $hyperlinkUrl .= '#' . (string) $attributes['location'];
  52                  }
  53                  $cell->getHyperlink()->setUrl($hyperlinkUrl);
  54              } elseif (isset($attributes['location'])) {
  55                  $cell->getHyperlink()->setUrl('sheet://' . (string) $attributes['location']);
  56              }
  57  
  58              // Tooltip
  59              if (isset($attributes['tooltip'])) {
  60                  $cell->getHyperlink()->setTooltip((string) $attributes['tooltip']);
  61              }
  62          }
  63      }
  64  }