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