Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

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