Passed
Push — 4.1 ( 971190...c5cc9d )
by Andrea
13:01
created

TabellaPdf::stampa()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 55
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 55
ccs 0
cts 21
cp 0
rs 9.504
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 12

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Fi\CoreBundle\Utils\Export;
4
5
//use Fi\CoreBundle\Utils\GrigliaFiltriUtils;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
6
//use TCPDF;
7
8
class TabellaPdf
9
{
10
    /*public function stampa($parametri = array())
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...
11
    {
12
        $testata = $parametri['testata'];
13
        $request = $parametri['request'];
14
        $nometabella = $request->get('nometabella');
15
16
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
17
18
        //echo PDF_HEADER_LOGO;
19
        $pdftitle = isset($testata['titolo']) && ($testata['titolo'] != '') ? $testata['titolo'] : 'Elenco ' . $nometabella;
20
        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'FiFree2', $pdftitle, array(0, 0, 0), array(0, 0, 0));
21
        $pdf->setFooterData(array(0, 0, 0), array(0, 0, 0));
22
23
        $pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
24
        $pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
25
26
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
27
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
28
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
29
        $pdf->SetFillColor(220, 220, 220);
30
31
        $pdf->AddPage('L');
32
        $arraystampaparm = array(
33
            "larghezzaform" => 900,
34
            "h" => 6,
35
            "border" => 1,
36
            "align" => 'L',
37
            "fill" => 0,
38
            "ln" => 0
39
        );
40
        $parametristampa = array_merge($parametri, $arraystampaparm);
41
42
        $this->stampaTestata($pdf, $parametristampa);
43
44
        $this->stampaDettaglio($pdf, $parametristampa);
45
46
47
//          I: send the file inline to the browser (default). The plug-in is used if available.
48
//          The name given by name is used when one selects the “Save as” option on the link generating the PDF.
49
//          D: send to the browser and force a file download with the name given by name.
50
//          F: save to a local server file with the name given by name.
51
//          S: return the document as a string (name is ignored).
52
//          FI: equivalent to F + I option
53
//          FD: equivalent to F + D option
54
//          E: return the document as base64 mime multi-part email attachment (RFC 2045)
55
//
56
//
57
//         In caso il pdf stampato nel browser resti fisso a caricare la pagina,
58
//          impostare 'D' per forzare lo scarico del file, oppure
59
//          mettere exit al posto di return 0; questo opzione però non è accettata da gli strumenti di controllo del codice che non si
60
//          aspettano exit nel codice
61
62
        $pdf->Output($request->get('nometabella') . '.pdf', 'I');
63
64
        return 0;
65
    }
66
    private function stampaTestata($pdf, $parametri)
67
    {
68
        $ln = $parametri['ln'];
69
        $fill = $parametri['fill'];
70
        $align = $parametri['align'];
71
        $border = $parametri['border'];
72
        $h = $parametri['h'];
73
        $larghezzaform = $parametri['larghezzaform'];
74
        $testata = $parametri['testata'];
75
        $nomicolonne = $testata['nomicolonne'];
76
        $modellicolonne = $testata['modellocolonne'];
77
78
        // Testata
79
        $pdf->SetFont('helvetica', 'B', 9);
80
        $arr_heights = array();
81
        // store current object
82
        $pdf->startTransaction();
83
        foreach ($nomicolonne as $posizione => $nomecolonna) {
84
            $width = $this->getWidthColumn($modellicolonne, $posizione, $larghezzaform);
85
            // get the number of lines
86
            $arr_heights[] = $pdf->MultiCell($width, 0, $nomecolonna, $border, $align, $fill, 0, '', '', true, 0, false, true, 0);
87
        }
88
        // restore previous object
89
        $pdf->rollbackTransaction(true);
90
        //work out the number of lines required
91
        $rowcount = max($arr_heights);
92
        //now draw it
93
        foreach ($nomicolonne as $posizione => $nomecolonna) {
94
            $width = $this->getWidthColumn($modellicolonne, $posizione, $larghezzaform);
95
            $pdf->MultiCell($width, $rowcount * $h, $nomecolonna, $border, $align, $fill, $ln);
96
        }
97
        $pdf->SetFont('helvetica', '', 9);
98
        $pdf->Ln();
99
    }
100
    private function stampaDettaglio($pdf, $parametri)
101
    {
102
103
        $ln = $parametri['ln'];
104
        $fill = $parametri['fill'];
105
        $align = $parametri['align'];
106
        $border = $parametri['border'];
107
        $h = $parametri['h'];
108
        $larghezzaform = $parametri['larghezzaform'];
109
        $testata = $parametri['testata'];
110
        $modellicolonne = $testata['modellocolonne'];
111
112
        $rispostaj = $parametri['griglia'];
113
        // Dati
114
        $risposta = json_decode($rispostaj);
115
        $dimensions = $pdf->getPageDimensions();
116
        $righe = $risposta->rows;
117
        $pdf->SetFont('helvetica', '', 9);
118
        foreach ($righe as $riga) {
119
            $fill = !$fill;
120
            $vettorecelle = $riga->cell;
121
122
            $arr_heights = array();
123
            // store current object
124
            $pdf->startTransaction();
125
            foreach ($vettorecelle as $posizione => $valore) {
126
                if (!is_object($valore)) {
127
                    $width = $this->getWidthColumn($modellicolonne, $posizione, $larghezzaform);
128
                    // get the number of lines
129
                    $arr_heights[] = $pdf->MultiCell($width, 0, $valore, $border, $align, $fill, 0, '', '', true, 0, false, true, 0);
130
                }
131
            }
132
            // restore previous object
133
            $pdf->rollbackTransaction(true);
134
            //work out the number of lines required
135
            $rowcount = max($arr_heights);
136
            $startY = $pdf->GetY();
137
            if (($startY + $rowcount * $h) + $dimensions['bm'] > ($dimensions['hk'])) {
138
                // page break
139
                $pdf->AddPage('L');
140
                // stampa testata
141
                $this->stampaTestata($pdf, $parametri);
142
            }
143
            //now draw it
144
            foreach ($vettorecelle as $posizione => $valore) {
145
                if (!is_object($valore)) {
146
                    $width = $this->getWidthColumn($modellicolonne, $posizione, $larghezzaform);
147
                    $pdf->MultiCell($width, $rowcount * $h, $valore, $border, $align, $fill, $ln);
148
                }
149
            }
150
            $pdf->Ln();
151
        }
152
        $pdf->Cell(0, 10, GrigliaFiltriUtils::traduciFiltri(array('filtri' => $risposta->filtri)), 0, false, 'L', 0, '', 0, false, 'T', 'M');
153
    }
154
    private function getWidthColumn($modellicolonne, $posizione, $larghezzaform)
155
    {
156
        if (isset($modellicolonne[$posizione])) {
157
            $width = ((297 * $modellicolonne[$posizione]['width']) / $larghezzaform) / 2;
158
        } else {
159
            $width = ((297 * 100) / $larghezzaform) / 2;
160
        }
161
        return $width;
162
    }*/
163
}
164