Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403] [Versions 400 and 403] [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              $setup = $this->spreadsheet->getActiveSheet()->getPageSetup();
  28  
  29              $attributes = $printInformation->Scale->attributes();
  30              if (isset($attributes['percentage'])) {
  31                  $setup->setScale((int) $attributes['percentage']);
  32              }
  33              $pageOrder = (string) $printInformation->order;
  34              if ($pageOrder === 'r_then_d') {
  35                  $setup->setPageOrder(WorksheetPageSetup::PAGEORDER_OVER_THEN_DOWN);
  36              } elseif ($pageOrder === 'd_then_r') {
  37                  $setup->setPageOrder(WorksheetPageSetup::PAGEORDER_DOWN_THEN_OVER);
  38              }
  39              $orientation = (string) $printInformation->orientation;
  40              if ($orientation !== '') {
  41                  $setup->setOrientation($orientation);
  42              }
  43              $attributes = $printInformation->hcenter->attributes();
  44              if (isset($attributes['value'])) {
  45                  $setup->setHorizontalCentered((bool) (string) $attributes['value']);
  46              }
  47              $attributes = $printInformation->vcenter->attributes();
  48              if (isset($attributes['value'])) {
  49                  $setup->setVerticalCentered((bool) (string) $attributes['value']);
  50              }
  51          }
  52  
  53          return $this;
  54      }
  55  
  56      public function sheetMargins(SimpleXMLElement $sheet): self
  57      {
  58          if (isset($sheet->PrintInformation, $sheet->PrintInformation->Margins)) {
  59              $marginSet = [
  60                  // Default Settings
  61                  'top' => 0.75,
  62                  'header' => 0.3,
  63                  'left' => 0.7,
  64                  'right' => 0.7,
  65                  'bottom' => 0.75,
  66                  'footer' => 0.3,
  67              ];
  68  
  69              $marginSet = $this->buildMarginSet($sheet, $marginSet);
  70              $this->adjustMargins($marginSet);
  71          }
  72  
  73          return $this;
  74      }
  75  
  76      private function buildMarginSet(SimpleXMLElement $sheet, array $marginSet): array
  77      {
  78          foreach ($sheet->PrintInformation->Margins->children(Gnumeric::NAMESPACE_GNM) as $key => $margin) {
  79              $marginAttributes = $margin->attributes();
  80              $marginSize = ($marginAttributes['Points']) ?? 72; //    Default is 72pt
  81              // Convert value in points to inches
  82              $marginSize = PageMargins::fromPoints((float) $marginSize);
  83              $marginSet[$key] = $marginSize;
  84          }
  85  
  86          return $marginSet;
  87      }
  88  
  89      private function adjustMargins(array $marginSet): void
  90      {
  91          foreach ($marginSet as $key => $marginSize) {
  92              // Gnumeric is quirky in the way it displays the header/footer values:
  93              //    header is actually the sum of top and header; footer is actually the sum of bottom and footer
  94              //    then top is actually the header value, and bottom is actually the footer value
  95              switch ($key) {
  96                  case 'left':
  97                  case 'right':
  98                      $this->sheetMargin($key, $marginSize);
  99  
 100                      break;
 101                  case 'top':
 102                      $this->sheetMargin($key, $marginSet['header'] ?? 0);
 103  
 104                      break;
 105                  case 'bottom':
 106                      $this->sheetMargin($key, $marginSet['footer'] ?? 0);
 107  
 108                      break;
 109                  case 'header':
 110                      $this->sheetMargin($key, ($marginSet['top'] ?? 0) - $marginSize);
 111  
 112                      break;
 113                  case 'footer':
 114                      $this->sheetMargin($key, ($marginSet['bottom'] ?? 0) - $marginSize);
 115  
 116                      break;
 117              }
 118          }
 119      }
 120  
 121      private function sheetMargin(string $key, float $marginSize): void
 122      {
 123          switch ($key) {
 124              case 'top':
 125                  $this->spreadsheet->getActiveSheet()->getPageMargins()->setTop($marginSize);
 126  
 127                  break;
 128              case 'bottom':
 129                  $this->spreadsheet->getActiveSheet()->getPageMargins()->setBottom($marginSize);
 130  
 131                  break;
 132              case 'left':
 133                  $this->spreadsheet->getActiveSheet()->getPageMargins()->setLeft($marginSize);
 134  
 135                  break;
 136              case 'right':
 137                  $this->spreadsheet->getActiveSheet()->getPageMargins()->setRight($marginSize);
 138  
 139                  break;
 140              case 'header':
 141                  $this->spreadsheet->getActiveSheet()->getPageMargins()->setHeader($marginSize);
 142  
 143                  break;
 144              case 'footer':
 145                  $this->spreadsheet->getActiveSheet()->getPageMargins()->setFooter($marginSize);
 146  
 147                  break;
 148          }
 149      }
 150  }