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.
   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Reader\Gnumeric;
   4  
   5  use PhpOffice\PhpSpreadsheet\Reader\Gnumeric;
   6  use PhpOffice\PhpSpreadsheet\Spreadsheet;
   7  use SimpleXMLElement;
   8  
   9  class Properties
  10  {
  11      /**
  12       * @var Spreadsheet
  13       */
  14      protected $spreadsheet;
  15  
  16      public function __construct(Spreadsheet $spreadsheet)
  17      {
  18          $this->spreadsheet = $spreadsheet;
  19      }
  20  
  21      private function docPropertiesOld(SimpleXMLElement $gnmXML): void
  22      {
  23          $docProps = $this->spreadsheet->getProperties();
  24          foreach ($gnmXML->Summary->Item as $summaryItem) {
  25              $propertyName = $summaryItem->name;
  26              $propertyValue = $summaryItem->{'val-string'};
  27              switch ($propertyName) {
  28                  case 'title':
  29                      $docProps->setTitle(trim($propertyValue));
  30  
  31                      break;
  32                  case 'comments':
  33                      $docProps->setDescription(trim($propertyValue));
  34  
  35                      break;
  36                  case 'keywords':
  37                      $docProps->setKeywords(trim($propertyValue));
  38  
  39                      break;
  40                  case 'category':
  41                      $docProps->setCategory(trim($propertyValue));
  42  
  43                      break;
  44                  case 'manager':
  45                      $docProps->setManager(trim($propertyValue));
  46  
  47                      break;
  48                  case 'author':
  49                      $docProps->setCreator(trim($propertyValue));
  50                      $docProps->setLastModifiedBy(trim($propertyValue));
  51  
  52                      break;
  53                  case 'company':
  54                      $docProps->setCompany(trim($propertyValue));
  55  
  56                      break;
  57              }
  58          }
  59      }
  60  
  61      private function docPropertiesDC(SimpleXMLElement $officePropertyDC): void
  62      {
  63          $docProps = $this->spreadsheet->getProperties();
  64          foreach ($officePropertyDC as $propertyName => $propertyValue) {
  65              $propertyValue = trim((string) $propertyValue);
  66              switch ($propertyName) {
  67                  case 'title':
  68                      $docProps->setTitle($propertyValue);
  69  
  70                      break;
  71                  case 'subject':
  72                      $docProps->setSubject($propertyValue);
  73  
  74                      break;
  75                  case 'creator':
  76                      $docProps->setCreator($propertyValue);
  77                      $docProps->setLastModifiedBy($propertyValue);
  78  
  79                      break;
  80                  case 'date':
  81                      $creationDate = $propertyValue;
  82                      $docProps->setModified($creationDate);
  83  
  84                      break;
  85                  case 'description':
  86                      $docProps->setDescription($propertyValue);
  87  
  88                      break;
  89              }
  90          }
  91      }
  92  
  93      private function docPropertiesMeta(SimpleXMLElement $officePropertyMeta): void
  94      {
  95          $docProps = $this->spreadsheet->getProperties();
  96          foreach ($officePropertyMeta as $propertyName => $propertyValue) {
  97              if ($propertyValue !== null) {
  98                  $attributes = $propertyValue->attributes(Gnumeric::NAMESPACE_META);
  99                  $propertyValue = trim((string) $propertyValue);
 100                  switch ($propertyName) {
 101                      case 'keyword':
 102                          $docProps->setKeywords($propertyValue);
 103  
 104                          break;
 105                      case 'initial-creator':
 106                          $docProps->setCreator($propertyValue);
 107                          $docProps->setLastModifiedBy($propertyValue);
 108  
 109                          break;
 110                      case 'creation-date':
 111                          $creationDate = $propertyValue;
 112                          $docProps->setCreated($creationDate);
 113  
 114                          break;
 115                      case 'user-defined':
 116                          if ($attributes) {
 117                              [, $attrName] = explode(':', (string) $attributes['name']);
 118                              $this->userDefinedProperties($attrName, $propertyValue);
 119                          }
 120  
 121                          break;
 122                  }
 123              }
 124          }
 125      }
 126  
 127      private function userDefinedProperties(string $attrName, string $propertyValue): void
 128      {
 129          $docProps = $this->spreadsheet->getProperties();
 130          switch ($attrName) {
 131              case 'publisher':
 132                  $docProps->setCompany($propertyValue);
 133  
 134                  break;
 135              case 'category':
 136                  $docProps->setCategory($propertyValue);
 137  
 138                  break;
 139              case 'manager':
 140                  $docProps->setManager($propertyValue);
 141  
 142                  break;
 143          }
 144      }
 145  
 146      public function readProperties(SimpleXMLElement $xml, SimpleXMLElement $gnmXML): void
 147      {
 148          $officeXML = $xml->children(Gnumeric::NAMESPACE_OFFICE);
 149          if (!empty($officeXML)) {
 150              $officeDocXML = $officeXML->{'document-meta'};
 151              $officeDocMetaXML = $officeDocXML->meta;
 152  
 153              foreach ($officeDocMetaXML as $officePropertyData) {
 154                  $officePropertyDC = $officePropertyData->children(Gnumeric::NAMESPACE_DC);
 155                  $this->docPropertiesDC($officePropertyDC);
 156  
 157                  $officePropertyMeta = $officePropertyData->children(Gnumeric::NAMESPACE_META);
 158                  $this->docPropertiesMeta($officePropertyMeta);
 159              }
 160          } elseif (isset($gnmXML->Summary)) {
 161              $this->docPropertiesOld($gnmXML);
 162          }
 163      }
 164  }