Completed
Push — 4.1 ( 1244ae...15ef68 )
by Andrea
62:52
created

TabellaXls::printBodyXls()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 20
ccs 0
cts 0
cp 0
rs 9.2222
c 0
b 0
f 0
cc 6
nc 5
nop 3
crap 42
1
<?php
2
3
namespace Fi\CoreBundle\Utils\Export;
4
5
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6
use PhpOffice\PhpSpreadsheet\Writer\Xls;
7
use PhpOffice\PhpSpreadsheet\Style\Fill;
8
use PhpOffice\PhpSpreadsheet\Style\Border;
9
use Fi\CoreBundle\Utils\Entity\DoctrineFieldReader;
10
11
class TabellaXls
12
{
13
14
    public function esportaexcel($parametri = array())
15
    {
16
        set_time_limit(960);
17
        ini_set('memory_limit', '2048M');
18
19
        //Creare un nuovo file
20
        $spreadsheet = new Spreadsheet();
21
        $objPHPExcel = new Xls($spreadsheet);
22
        $spreadsheet->setActiveSheetIndex(0);
23
24
        // Set properties
25
        $spreadsheet->getProperties()->setCreator('Comune di Firenze');
26
        $spreadsheet->getProperties()->setLastModifiedBy('Comune di Firenze');
27
28
29
        $header = $parametri['parametritabella'];
30
        $rows = $parametri['recordstabella'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
31
32
        //Scrittura su file
33
        $sheet = $spreadsheet->getActiveSheet();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
34
        $titolosheet = 'Esportazione ' . $parametri['nomecontroller'];
35
        $sheet->setTitle(substr($titolosheet, 0, 30));
36
        $sheet->getParent()->getDefaultStyle()->getFont()->setName('Verdana');
37
38
        $this->printHeaderXls($header, $sheet);
39
40
        $this->printBodyXls($header, $rows, $sheet);
41
42
        //Si crea un oggetto
43
        $todaydate = date('d-m-y');
44
45
        $filename = 'Exportazione';
46
        $filename = $filename . '-' . $todaydate . '-' . strtoupper(md5(uniqid(rand(), true)));
47
        $filename = $filename . '.xls';
48
        $filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename;
49
50
        if (file_exists($filename)) {
51
            unlink($filename);
52
        }
53
54
        $objPHPExcel->save($filename);
55
56
        return $filename;
57
    }
58
59
    private function printHeaderXls($testata, $sheet)
60
    {
61
        $indicecolonnaheader = 1;
62
        $letteracolonna = 0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
63
        foreach ($testata as $modellocolonna) {
64
            if ($modellocolonna["escluso"] === false) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal escluso does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
65
                //Si imposta la larghezza delle colonne
66
                $letteracolonna = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($indicecolonnaheader);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
67
                $width = (int) $modellocolonna['larghezza'] / 7;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 20 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
68
                $indicecolonnaheadertitle = $modellocolonna['etichetta'];
69
                $coltitle = strtoupper($indicecolonnaheadertitle);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 17 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
70
                $sheet->setCellValueByColumnAndRow($indicecolonnaheader, 1, $coltitle);
71
                $sheet->getColumnDimension($letteracolonna)->setWidth($width);
72
73
                ++$indicecolonnaheader;
74
            }
75
        }
76
77
        if ($indicecolonnaheader > 0) {
78
            //Si imposta il colore dello sfondo delle celle
79
            //Colore header
80
            $style_header = array(
81
                'fill' => array(
82
                    'type' => Fill::FILL_SOLID,
83
                    'color' => array('rgb' => 'E5E4E2')
84
                ),
85
                'font' => array(
86
                    'bold' => true
87
                )
88
            );
89
            $sheet->getStyle('A1:' . $letteracolonna . '1')->applyFromArray($style_header);
90
        }
91
92
        $sheet->getRowDimension('1')->setRowHeight(20);
93
    }
94
95
    private function getValueCell($tipocampo, $vettorecella)
96
    {
97
        $valore = null;
98
        switch ($tipocampo) {
99
            case 'date':
100
                $d = (int) substr($vettorecella, 0, 2);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
101
                $m = (int) substr($vettorecella, 3, 2);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
102
                $y = (int) substr($vettorecella, 6, 4);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
103
                $t_date = \PhpOffice\PhpSpreadsheet\Shared\Date::formattedPHPToExcel($y, $m, $d);
104
                $valore = $t_date;
105
                break;
106
            case 'boolean':
107
                $valore = ($vettorecella == 1) ? 'SI' : 'NO';
108
                break;
109
            default:
110
                $valore = $vettorecella;
111
                break;
112
        }
113
        return $valore;
114
    }
115
116
    private function printBodyXls($header, $rows, $sheet)
117
    {
118
        $row = 2;
119
        foreach ($rows as $riga) {
120
            $col = 1;
121
            foreach ($header as $colonnatestata => $valorecolonnatestata) {
122
                if ($valorecolonnatestata["escluso"] === false) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal escluso does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
123
                    $dfr = new DoctrineFieldReader($riga);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
124
                    $oggetto = $dfr->getField2Object($colonnatestata, $riga);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
125
                    $valorecampo = $dfr->object2view($oggetto, $valorecolonnatestata["tipocampo"]);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal tipocampo does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
126
                    if ($valorecampo === '' || $valorecampo === null) {
127
                        $col = $col + 1;
0 ignored issues
show
Coding Style introduced by
Increment operators should be used where possible; found "$col = $col + 1;" but expected "$col++"
Loading history...
128
                        continue;
129
                    }
130
                    $sheet->setCellValueByColumnAndRow($col, $row, $valorecampo);
131
                    $col = $col + 1;
0 ignored issues
show
Coding Style introduced by
Increment operators should be used where possible; found "$col = $col + 1;" but expected "$col++"
Loading history...
132
                }
133
            }
134
                $sheet->getRowDimension($row)->setRowHeight(18);
135
                ++$row;
136
        }
137
138
//        $indicecolonna = 0;
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
139
//        foreach ($modellicolonne as $modellocolonna) {
140
//            $letteracolonna = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($indicecolonna);
141
//            switch ($modellocolonna['tipocampo']) {
142
//                case 'text':
143
//                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
144
//                            ->getNumberFormat()
145
//                            ->setFormatCode("@");
146
//                    break;
147
//                case 'string':
148
//                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
149
//                            ->getNumberFormat()
150
//                            ->setFormatCode("@");
151
//                    break;
152
//                case 'integer':
153
//                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
154
//                            ->getNumberFormat()
155
//                            ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER);
156
//                    break;
157
//                case 'float':
158
//                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
159
//                            ->getNumberFormat()
160
//                            ->setFormatCode('#,##0.00');
161
//                    break;
162
//                case 'number':
163
//                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
164
//                            ->getNumberFormat()
165
//                            ->setFormatCode('#,##0.00');
166
//                    break;
167
//                case 'datetime':
168
//                    //\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DDMMYYYYSLASH
169
//                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
170
//                            ->getNumberFormat()
171
//                            ->setFormatCode("dd/mm/yyyy");
172
//                    break;
173
//                case 'date':
174
//                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
175
//                            ->getNumberFormat()
176
//                            ->setFormatCode("dd/mm/yyyy");
177
//                    break;
178
//                default:
179
//                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
180
//                            ->getNumberFormat()
181
//                            ->setFormatCode("@");
182
//                    break;
183
//            }
184
//
185
//            ++$indicecolonna;
186
//        }
187
    }
188
}
189