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\Xml;
4
 
5
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Namespaces;
6
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
8
use SimpleXMLElement;
9
use stdClass;
10
 
11
class PageSettings
12
{
13
    private stdClass $printSettings;
14
 
15
    public function __construct(SimpleXMLElement $xmlX)
16
    {
17
        $printSettings = $this->pageSetup($xmlX, $this->getPrintDefaults());
18
        $this->printSettings = $this->printSetup($xmlX, $printSettings);
19
    }
20
 
21
    public function loadPageSettings(Spreadsheet $spreadsheet): void
22
    {
23
        $spreadsheet->getActiveSheet()->getPageSetup()
24
            ->setPaperSize($this->printSettings->paperSize)
25
            ->setOrientation($this->printSettings->orientation)
26
            ->setScale($this->printSettings->scale)
27
            ->setVerticalCentered($this->printSettings->verticalCentered)
28
            ->setHorizontalCentered($this->printSettings->horizontalCentered)
29
            ->setPageOrder($this->printSettings->printOrder);
30
        $spreadsheet->getActiveSheet()->getPageMargins()
31
            ->setTop($this->printSettings->topMargin)
32
            ->setHeader($this->printSettings->headerMargin)
33
            ->setLeft($this->printSettings->leftMargin)
34
            ->setRight($this->printSettings->rightMargin)
35
            ->setBottom($this->printSettings->bottomMargin)
36
            ->setFooter($this->printSettings->footerMargin);
37
    }
38
 
39
    private function getPrintDefaults(): stdClass
40
    {
41
        return (object) [
42
            'paperSize' => 9,
43
            'orientation' => PageSetup::ORIENTATION_DEFAULT,
44
            'scale' => 100,
45
            'horizontalCentered' => false,
46
            'verticalCentered' => false,
47
            'printOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER,
48
            'topMargin' => 0.75,
49
            'headerMargin' => 0.3,
50
            'leftMargin' => 0.7,
51
            'rightMargin' => 0.7,
52
            'bottomMargin' => 0.75,
53
            'footerMargin' => 0.3,
54
        ];
55
    }
56
 
57
    private function pageSetup(SimpleXMLElement $xmlX, stdClass $printDefaults): stdClass
58
    {
59
        if (isset($xmlX->WorksheetOptions->PageSetup)) {
60
            foreach ($xmlX->WorksheetOptions->PageSetup as $pageSetupData) {
61
                foreach ($pageSetupData as $pageSetupKey => $pageSetupValue) {
62
                    $pageSetupAttributes = $pageSetupValue->attributes(Namespaces::URN_EXCEL);
63
                    if ($pageSetupAttributes !== null) {
64
                        switch ($pageSetupKey) {
65
                            case 'Layout':
66
                                $this->setLayout($printDefaults, $pageSetupAttributes);
67
 
68
                                break;
69
                            case 'Header':
70
                                $printDefaults->headerMargin = (float) $pageSetupAttributes->Margin ?: 1.0;
71
 
72
                                break;
73
                            case 'Footer':
74
                                $printDefaults->footerMargin = (float) $pageSetupAttributes->Margin ?: 1.0;
75
 
76
                                break;
77
                            case 'PageMargins':
78
                                $this->setMargins($printDefaults, $pageSetupAttributes);
79
 
80
                                break;
81
                        }
82
                    }
83
                }
84
            }
85
        }
86
 
87
        return $printDefaults;
88
    }
89
 
90
    private function printSetup(SimpleXMLElement $xmlX, stdClass $printDefaults): stdClass
91
    {
92
        if (isset($xmlX->WorksheetOptions->Print)) {
93
            foreach ($xmlX->WorksheetOptions->Print as $printData) {
94
                foreach ($printData as $printKey => $printValue) {
95
                    switch ($printKey) {
96
                        case 'LeftToRight':
97
                            $printDefaults->printOrder = PageSetup::PAGEORDER_OVER_THEN_DOWN;
98
 
99
                            break;
100
                        case 'PaperSizeIndex':
101
                            $printDefaults->paperSize = (int) $printValue ?: 9;
102
 
103
                            break;
104
                        case 'Scale':
105
                            $printDefaults->scale = (int) $printValue ?: 100;
106
 
107
                            break;
108
                    }
109
                }
110
            }
111
        }
112
 
113
        return $printDefaults;
114
    }
115
 
116
    private function setLayout(stdClass $printDefaults, SimpleXMLElement $pageSetupAttributes): void
117
    {
118
        $printDefaults->orientation = (string) strtolower($pageSetupAttributes->Orientation ?? '') ?: PageSetup::ORIENTATION_PORTRAIT;
119
        $printDefaults->horizontalCentered = (bool) $pageSetupAttributes->CenterHorizontal ?: false;
120
        $printDefaults->verticalCentered = (bool) $pageSetupAttributes->CenterVertical ?: false;
121
    }
122
 
123
    private function setMargins(stdClass $printDefaults, SimpleXMLElement $pageSetupAttributes): void
124
    {
125
        $printDefaults->leftMargin = (float) $pageSetupAttributes->Left ?: 1.0;
126
        $printDefaults->rightMargin = (float) $pageSetupAttributes->Right ?: 1.0;
127
        $printDefaults->topMargin = (float) $pageSetupAttributes->Top ?: 1.0;
128
        $printDefaults->bottomMargin = (float) $pageSetupAttributes->Bottom ?: 1.0;
129
    }
130
}