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 311 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Reader\Gnumeric;
   4  
   5  use PhpOffice\PhpSpreadsheet\Reader\Gnumeric;
   6  use PhpOffice\PhpSpreadsheet\Spreadsheet;
   7  use PhpOffice\PhpSpreadsheet\Worksheet\PageMargins;
   8  use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup as WorksheetPageSetup;
   9  use SimpleXMLElement;
  10  
  11  class PageSetup
  12  {
  13      /**
  14       * @var Spreadsheet
  15       */
  16      private $spreadsheet;
  17  
  18      public function __construct(Spreadsheet $spreadsheet)
  19      {
  20          $this->spreadsheet = $spreadsheet;
  21      }
  22  
  23      public function printInformation(SimpleXMLElement $sheet): self
  24      {
  25          if (isset($sheet->PrintInformation, $sheet->PrintInformation[0])) {
  26              $printInformation = $sheet->PrintInformation[0];
  27  
  28              $scale = (string) $printInformation->Scale->attributes()['percentage'];
  29              $pageOrder = (string) $printInformation->order;
  30              $orientation = (string) $printInformation->orientation;
  31              $horizontalCentered = (string) $printInformation->hcenter->attributes()['value'];
  32              $verticalCentered = (string) $printInformation->vcenter->attributes()['value'];
  33  
  34              $this->spreadsheet->getActiveSheet()->getPageSetup()
  35                  ->setPageOrder($pageOrder === 'r_then_d' ? WorksheetPageSetup::PAGEORDER_OVER_THEN_DOWN : WorksheetPageSetup::PAGEORDER_DOWN_THEN_OVER)
  36                  ->setScale((int) $scale)
  37                  ->setOrientation($orientation ?? WorksheetPageSetup::ORIENTATION_DEFAULT)
  38                  ->setHorizontalCentered((bool) $horizontalCentered)
  39                  ->setVerticalCentered((bool) $verticalCentered);
  40          }
  41  
  42          return $this;
  43      }
  44  
  45      public function sheetMargins(SimpleXMLElement $sheet): self
  46      {
  47          if (isset($sheet->PrintInformation, $sheet->PrintInformation->Margins)) {
  48              $marginSet = [
  49                  // Default Settings
  50                  'top' => 0.75,
  51                  'header' => 0.3,
  52                  'left' => 0.7,
  53                  'right' => 0.7,
  54                  'bottom' => 0.75,
  55                  'footer' => 0.3,
  56              ];
  57  
  58              $marginSet = $this->buildMarginSet($sheet, $marginSet);
  59              $this->adjustMargins($marginSet);
  60          }
  61  
  62          return $this;
  63      }
  64  
  65      private function buildMarginSet(SimpleXMLElement $sheet, array $marginSet): array
  66      {
  67          foreach ($sheet->PrintInformation->Margins->children(Gnumeric::NAMESPACE_GNM) as $key => $margin) {
  68              $marginAttributes = $margin->attributes();
  69              $marginSize = ($marginAttributes['Points']) ?? 72; //    Default is 72pt
  70              // Convert value in points to inches
  71              $marginSize = PageMargins::fromPoints((float) $marginSize);
  72              $marginSet[$key] = $marginSize;
  73          }
  74  
  75          return $marginSet;
  76      }
  77  
  78      private function adjustMargins(array $marginSet): void
  79      {
  80          foreach ($marginSet as $key => $marginSize) {
  81              // Gnumeric is quirky in the way it displays the header/footer values:
  82              //    header is actually the sum of top and header; footer is actually the sum of bottom and footer
  83              //    then top is actually the header value, and bottom is actually the footer value
  84              switch ($key) {
  85                  case 'left':
  86                  case 'right':
  87                      $this->sheetMargin($key, $marginSize);
  88  
  89                      break;
  90                  case 'top':
  91                      $this->sheetMargin($key, $marginSet['header'] ?? 0);
  92  
  93                      break;
  94                  case 'bottom':
  95                      $this->sheetMargin($key, $marginSet['footer'] ?? 0);
  96  
  97                      break;
  98                  case 'header':
  99                      $this->sheetMargin($key, ($marginSet['top'] ?? 0) - $marginSize);
 100  
 101                      break;
 102                  case 'footer':
 103                      $this->sheetMargin($key, ($marginSet['bottom'] ?? 0) - $marginSize);
 104  
 105                      break;
 106              }
 107          }
 108      }
 109  
 110      private function sheetMargin(string $key, float $marginSize): void
 111      {
 112          switch ($key) {
 113              case 'top':
 114                  $this->spreadsheet->getActiveSheet()->getPageMargins()->setTop($marginSize);
 115  
 116                  break;
 117              case 'bottom':
 118                  $this->spreadsheet->getActiveSheet()->getPageMargins()->setBottom($marginSize);
 119  
 120                  break;
 121              case 'left':
 122                  $this->spreadsheet->getActiveSheet()->getPageMargins()->setLeft($marginSize);
 123  
 124                  break;
 125              case 'right':
 126                  $this->spreadsheet->getActiveSheet()->getPageMargins()->setRight($marginSize);
 127  
 128                  break;
 129              case 'header':
 130                  $this->spreadsheet->getActiveSheet()->getPageMargins()->setHeader($marginSize);
 131  
 132                  break;
 133              case 'footer':
 134                  $this->spreadsheet->getActiveSheet()->getPageMargins()->setFooter($marginSize);
 135  
 136                  break;
 137          }
 138      }
 139  }