| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Reader\Ods; |
| 4 |
|
| 5 |
use DOMDocument; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 8 |
use stdClass; |
| 9 |
|
| 10 |
class PageSettings |
| 11 |
{ |
| 12 |
private string $officeNs = ''; |
| 13 |
|
| 14 |
private string $stylesNs = ''; |
| 15 |
|
| 16 |
private string $stylesFo = ''; |
| 17 |
|
| 18 |
private string $tableNs = ''; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var string[] |
| 22 |
*/ |
| 23 |
private array $tableStylesCrossReference = []; |
| 24 |
|
| 25 |
/** @var mixed[] */ |
| 26 |
private array $pageLayoutStyles = []; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var string[] |
| 30 |
*/ |
| 31 |
private array $masterStylesCrossReference = []; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var string[] |
| 35 |
*/ |
| 36 |
private array $masterPrintStylesCrossReference = []; |
| 37 |
|
| 38 |
public function __construct(DOMDocument $styleDom) |
| 39 |
{ |
| 40 |
$this->setDomNameSpaces($styleDom); |
| 41 |
$this->readPageSettingStyles($styleDom); |
| 42 |
$this->readStyleMasterLookup($styleDom); |
| 43 |
} |
| 44 |
|
| 45 |
private function setDomNameSpaces(DOMDocument $styleDom): void |
| 46 |
{ |
| 47 |
$this->officeNs = (string) $styleDom->lookupNamespaceUri('office'); |
| 48 |
$this->stylesNs = (string) $styleDom->lookupNamespaceUri('style'); |
| 49 |
$this->stylesFo = (string) $styleDom->lookupNamespaceUri('fo'); |
| 50 |
$this->tableNs = (string) $styleDom->lookupNamespaceUri('table'); |
| 51 |
} |
| 52 |
|
| 53 |
private function readPageSettingStyles(DOMDocument $styleDom): void |
| 54 |
{ |
| 55 |
$item0 = $styleDom->getElementsByTagNameNS($this->officeNs, 'automatic-styles')->item(0); |
| 56 |
$styles = ($item0 === null) ? [] : $item0->getElementsByTagNameNS($this->stylesNs, 'page-layout'); |
| 57 |
|
| 58 |
foreach ($styles as $styleSet) { |
| 59 |
$styleName = $styleSet->getAttributeNS($this->stylesNs, 'name'); |
| 60 |
$pageLayoutProperties = $styleSet->getElementsByTagNameNS($this->stylesNs, 'page-layout-properties')->item(0); |
| 61 |
$styleOrientation = ($nullsafeVariable1 = $pageLayoutProperties) ? $nullsafeVariable1->getAttributeNS($this->stylesNs, 'print-orientation') : null; |
| 62 |
$styleScale = ($nullsafeVariable2 = $pageLayoutProperties) ? $nullsafeVariable2->getAttributeNS($this->stylesNs, 'scale-to') : null; |
| 63 |
$stylePrintOrder = ($nullsafeVariable3 = $pageLayoutProperties) ? $nullsafeVariable3->getAttributeNS($this->stylesNs, 'print-page-order') : null; |
| 64 |
$centered = ($nullsafeVariable4 = $pageLayoutProperties) ? $nullsafeVariable4->getAttributeNS($this->stylesNs, 'table-centering') : null; |
| 65 |
|
| 66 |
$marginLeft = ($nullsafeVariable5 = $pageLayoutProperties) ? $nullsafeVariable5->getAttributeNS($this->stylesFo, 'margin-left') : null; |
| 67 |
$marginRight = ($nullsafeVariable6 = $pageLayoutProperties) ? $nullsafeVariable6->getAttributeNS($this->stylesFo, 'margin-right') : null; |
| 68 |
$marginTop = ($nullsafeVariable7 = $pageLayoutProperties) ? $nullsafeVariable7->getAttributeNS($this->stylesFo, 'margin-top') : null; |
| 69 |
$marginBottom = ($nullsafeVariable8 = $pageLayoutProperties) ? $nullsafeVariable8->getAttributeNS($this->stylesFo, 'margin-bottom') : null; |
| 70 |
$header = $styleSet->getElementsByTagNameNS($this->stylesNs, 'header-style')->item(0); |
| 71 |
$headerProperties = ($nullsafeVariable9 = ($nullsafeVariable10 = $header) ? $nullsafeVariable10->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties') : null) ? $nullsafeVariable9->item(0) : null; |
| 72 |
$marginHeader = ($nullsafeVariable11 = $headerProperties) ? $nullsafeVariable11->getAttributeNS($this->stylesFo, 'min-height') : null; |
| 73 |
$footer = $styleSet->getElementsByTagNameNS($this->stylesNs, 'footer-style')->item(0); |
| 74 |
$footerProperties = ($nullsafeVariable12 = ($nullsafeVariable13 = $footer) ? $nullsafeVariable13->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties') : null) ? $nullsafeVariable12->item(0) : null; |
| 75 |
$marginFooter = ($nullsafeVariable14 = $footerProperties) ? $nullsafeVariable14->getAttributeNS($this->stylesFo, 'min-height') : null; |
| 76 |
|
| 77 |
$this->pageLayoutStyles[$styleName] = (object) [ |
| 78 |
'orientation' => $styleOrientation ?: PageSetup::ORIENTATION_DEFAULT, |
| 79 |
'scale' => $styleScale ?: 100, |
| 80 |
'printOrder' => $stylePrintOrder, |
| 81 |
'horizontalCentered' => $centered === 'horizontal' || $centered === 'both', |
| 82 |
'verticalCentered' => $centered === 'vertical' || $centered === 'both', |
| 83 |
// margin size is already stored in inches, so no UOM conversion is required |
| 84 |
'marginLeft' => (float) ($marginLeft ?? 0.7), |
| 85 |
'marginRight' => (float) ($marginRight ?? 0.7), |
| 86 |
'marginTop' => (float) ($marginTop ?? 0.3), |
| 87 |
'marginBottom' => (float) ($marginBottom ?? 0.3), |
| 88 |
'marginHeader' => (float) ($marginHeader ?? 0.45), |
| 89 |
'marginFooter' => (float) ($marginFooter ?? 0.45), |
| 90 |
]; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
private function readStyleMasterLookup(DOMDocument $styleDom): void |
| 95 |
{ |
| 96 |
$item0 = $styleDom->getElementsByTagNameNS($this->officeNs, 'master-styles')->item(0); |
| 97 |
$styleMasterLookup = ($item0 === null) ? [] : $item0->getElementsByTagNameNS($this->stylesNs, 'master-page'); |
| 98 |
|
| 99 |
foreach ($styleMasterLookup as $styleMasterSet) { |
| 100 |
$styleMasterName = $styleMasterSet->getAttributeNS($this->stylesNs, 'name'); |
| 101 |
$pageLayoutName = $styleMasterSet->getAttributeNS($this->stylesNs, 'page-layout-name'); |
| 102 |
$this->masterPrintStylesCrossReference[$styleMasterName] = $pageLayoutName; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
public function readStyleCrossReferences(DOMDocument $contentDom): void |
| 107 |
{ |
| 108 |
$item0 = $contentDom->getElementsByTagNameNS($this->officeNs, 'automatic-styles')->item(0); |
| 109 |
$styleXReferences = ($item0 === null) ? [] : $item0->getElementsByTagNameNS($this->stylesNs, 'style'); |
| 110 |
|
| 111 |
foreach ($styleXReferences as $styleXreferenceSet) { |
| 112 |
$styleXRefName = $styleXreferenceSet->getAttributeNS($this->stylesNs, 'name'); |
| 113 |
$stylePageLayoutName = $styleXreferenceSet->getAttributeNS($this->stylesNs, 'master-page-name'); |
| 114 |
$styleFamilyName = $styleXreferenceSet->getAttributeNS($this->stylesNs, 'family'); |
| 115 |
if (!empty($styleFamilyName) && $styleFamilyName === 'table') { |
| 116 |
$styleVisibility = 'true'; |
| 117 |
foreach ($styleXreferenceSet->getElementsByTagNameNS($this->stylesNs, 'table-properties') as $tableProperties) { |
| 118 |
$styleVisibility = $tableProperties->getAttributeNS($this->tableNs, 'display'); |
| 119 |
} |
| 120 |
$this->tableStylesCrossReference[$styleXRefName] = $styleVisibility; |
| 121 |
} |
| 122 |
if (!empty($stylePageLayoutName)) { |
| 123 |
$this->masterStylesCrossReference[$styleXRefName] = $stylePageLayoutName; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
public function setVisibilityForWorksheet(Worksheet $worksheet, string $styleName): void |
| 129 |
{ |
| 130 |
if (!array_key_exists($styleName, $this->tableStylesCrossReference)) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
$worksheet->setSheetState( |
| 135 |
$this->tableStylesCrossReference[$styleName] === 'false' |
| 136 |
? Worksheet::SHEETSTATE_HIDDEN |
| 137 |
: Worksheet::SHEETSTATE_VISIBLE |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
public function setPrintSettingsForWorksheet(Worksheet $worksheet, string $styleName): void |
| 142 |
{ |
| 143 |
if (!array_key_exists($styleName, $this->masterStylesCrossReference)) { |
| 144 |
return; |
| 145 |
} |
| 146 |
$masterStyleName = $this->masterStylesCrossReference[$styleName]; |
| 147 |
|
| 148 |
if (!array_key_exists($masterStyleName, $this->masterPrintStylesCrossReference)) { |
| 149 |
return; |
| 150 |
} |
| 151 |
$printSettingsIndex = $this->masterPrintStylesCrossReference[$masterStyleName]; |
| 152 |
|
| 153 |
if (!array_key_exists($printSettingsIndex, $this->pageLayoutStyles)) { |
| 154 |
return; |
| 155 |
} |
| 156 |
/** @var (object{orientation: string, scale: int|string, printOrder: ?string, |
| 157 |
* horizontalCentered: bool, verticalCentered: bool, marginLeft: float, marginRight: float, marginTop: float, |
| 158 |
* marginBottom: float, marginHeader: float, marginFooter: float}&stdClass) */ |
| 159 |
$printSettings = $this->pageLayoutStyles[$printSettingsIndex]; |
| 160 |
|
| 161 |
$worksheet->getPageSetup() |
| 162 |
->setOrientation($printSettings->orientation ?? PageSetup::ORIENTATION_DEFAULT) |
| 163 |
->setPageOrder($printSettings->printOrder === 'ltr' ? PageSetup::PAGEORDER_OVER_THEN_DOWN : PageSetup::PAGEORDER_DOWN_THEN_OVER) |
| 164 |
->setScale((int) trim((string) $printSettings->scale, '%')) |
| 165 |
->setHorizontalCentered($printSettings->horizontalCentered) |
| 166 |
->setVerticalCentered($printSettings->verticalCentered); |
| 167 |
|
| 168 |
$worksheet->getPageMargins() |
| 169 |
->setLeft($printSettings->marginLeft) |
| 170 |
->setRight($printSettings->marginRight) |
| 171 |
->setTop($printSettings->marginTop) |
| 172 |
->setBottom($printSettings->marginBottom) |
| 173 |
->setHeader($printSettings->marginHeader) |
| 174 |
->setFooter($printSettings->marginFooter); |
| 175 |
} |
| 176 |
} |
| 177 |
|