Passed
Push — main ( 83c25f...829487 )
by Stefan
01:56
created

ExampleXPDF::col()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 27
nc 8
nop 3
dl 0
loc 36
rs 8.0555
c 0
b 0
f 0
1
<?php
2
use SKien\XFPDF\XPDF;
3
4
/**
5
 * example to use the XPDF - class:
6
 * first, define own class extending XPDF to define table coloumns und to handle
7
 * the output of dynamic content.
8
 * 
9
 * @author Stefanius
10
 */
11
class ExampleXPDF extends XPDF
12
{
13
    /** const for own column ID's */
14
    const MY_GRP_COL = 1;
15
    const MY_IMAGE_COL = 2;
16
    const MY_CALC_COL = 3;
17
    
18
    /** @var string remember month and year of the previous row */ 
19
    protected string $strMonth = '';
20
    
21
    /**
22
     * define table columns
23
     * @param string $orientation
24
     */
25
    public function __construct(string $orientation='P') 
26
    {
27
        // first call parent constructor for general initialization
28
        parent::__construct($orientation);
29
        
30
        // next we can specify the fonts and colors to use
31
        // This can be done by using the various font and color functions.
32
        // - SetXXXFont()
33
        // - SetXXXColors()
34
        //
35
        // However, it is faster and easier if all settings are made in a JSON file.
36
        // (and above reusable for multiple reports).
37
        // The file name have to be passed to the InitGrid() method.
38
        //
39
        // The structure of the JSON file is self-explanatory and can be seen in the example file
40
        // used here.
41
        $this->initGrid('xfpdf-sample.json');
42
        
43
        // set Logo printed in the page header
44
        $this->setLogo('images/elephpant.png');
45
        $this->setLogoHeight(9.0);
46
        $this->setPageFooter("Page: {PN}/{NP}\tAuthor: S.Kien\t{D} {T}");
47
        
48
        // now we define the columns of our report
49
        $this->addCol('Row',            10, 'R', XPDF::COL_ROW_NR,     XPDF::FLAG_TOTALS_TEXT);
50
        $this->addCol('Date',           35, 'C', 'date',               XPDF::FLAG_DATE);
51
        $this->addCol('Text',           -1, 'L', 'text');
52
        $this->addCol('Grp.',           12, 'C', self::MY_GRP_COL);
53
        $this->addCol('Weight',         20, 'R', 'weight',             XPDF::FLAG_TOTALS_CALC | XPDF::FLAG_NUMBER);
54
        $iImgCol = $this->addCol(-1,     8, 'C', self::MY_IMAGE_COL,   XPDF::FLAG_IMAGE | XPDF::FLAG_TOTALS_EMPTY);
55
        $this->addCol('Price',          25, 'R', 'price',              XPDF::FLAG_TOTALS_CALC | XPDF::FLAG_CUR_SYMBOL);
56
        $this->addCol('Cost per kg',    25, 'R', self::MY_CALC_COL,    XPDF::FLAG_TOTALS_EMPTY);
57
        
58
        // enable the totals/pagetotals and carry-over functionality
59
        $this->enableTotals(XPDF::TOTALS | XPDF::PAGE_TOTALS | XPDF::CARRY_OVER);
60
        $this->setTotalsText(
61
            "My Totals over all:",
62
            "Subtotal on Page {PN}:",
63
            "Carry over from Page {PN-1}:");
64
        
65
        // set date and number formating.
66
        $this->setDateFormat('%a, %d.%m.%Y');
67
        $this->setNumberFormat(1, '', ' kg');
68
        
69
        // and set meassuring for the image col
70
        $this->setColImageInfo($iImgCol, 1.5, 2.5, 3 );
71
    }
72
    
73
    /**
74
     * handle special content of cell 
75
     * - any calculation
76
     * - formating
77
     * - ...
78
     * not included in raw data 
79
     * 
80
     * !! Important !!
81
     * dont't forget to call parent::col($iCol, $row, $bFill) if data not processed... 
82
     * 
83
     * (non-PHPdoc)
84
     * @see XPDF::col()
85
     */
86
    protected function col(int $iCol, array $row, bool &$bFill) : string 
87
    {
88
        $strCol = '';
89
        switch ($iCol) {
90
            case self::MY_GRP_COL:
91
                $aValues = array( '', 'Grp. A', 'Grp. B', 'Grp. C', 'Grp. D');
92
                if ($row['grp_id'] > 0 && $row['grp_id'] <= 4) {
93
                    $strCol = $aValues[$row['grp_id']];
94
                }
95
                break;
96
            case self::MY_IMAGE_COL:
97
                $strCol = 'images/';
98
                $fltWeight = floatval($row['weight']);
99
                if ($fltWeight > 35.0) {
100
                    // ... to heavy
101
                    $strCol .= 'red.png';
102
                } else if ($fltWeight > 20.0) {
103
                    // ... just in the limit
104
                    $strCol .= 'yellow.png';
105
                } else {
106
                    $strCol .= 'green.png';
107
                }
108
                break;
109
            case self::MY_CALC_COL:
110
                $fltPricePerKg = 0.0;
111
                if (floatval($row['weight']) != 0) {
112
                    $fltPricePerKg = floatval($row['price']) / floatval($row['weight']);;
113
                }
114
                $strCol = $this->formatCurrency($fltPricePerKg, true);
115
                break;
116
            default:
117
                // very important to call parent class !!
118
                $strCol = parent::col($iCol, $row, $bFill);
119
                break;
120
        }
121
        return $strCol;
122
    }
123
    
124
    /**
125
     * the PreRow() method may be used to
126
     * - process internal state or properties  
127
     * - start of grouping
128
     * - manipulate data of next row
129
     * - insert some separator or subtitle within the grid
130
     * 
131
     * pay attention on $row param is passed as reference so any changes affects
132
     * further output.  
133
     * 
134
     * (non-PHPdoc)
135
     * @see XPDF::preRow()
136
     */
137
    protected function preRow(array &$row) : string
138
    {
139
        // for grouping
140
        $date = strtotime($row['date']);
141
        $strMonth = date('Y-m', $date);
142
        if ( $this->strMonth != $strMonth) {
143
            // first row we have no subtotals...
144
            if ($this->strMonth != '') {
145
                $this->endGroup();
146
            }
147
            $this->startGroup('Totals ' . strftime('%B %Y', $date) . ':', strftime('%B %Y', $date));
148
            $this->strMonth = $strMonth;
149
        }
150
        $strSubRow = '';
151
        if ($this->iRow == 47) {
152
            $strSubRow = '... next Row have been manipulated in ExampleXPDF::preRow(array &$row)!';
153
            $row['text'] = 'manipulated Rowdata!';
154
        }
155
        if ($this->iRow == 56) {
156
            $row['text'] = 'manipulated Rowdata without Subrow!';
157
        }
158
        return $strSubRow;
159
    }
160
    
161
    /**
162
     * Overwrite the EndGrid() method to close the last group.
163
     * {@inheritDoc}
164
     * @see \SKien\XFPDF\XPDF::endGrid()
165
     */
166
    public function endGrid() : void
167
    {
168
        // end last group for subtotals before we call the parent (!!! don't forget that!!)
169
        $this->endGroup();
170
        parent::endGrid();
171
    }
172
}
173