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