Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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