Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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