ExampleXPDF::preRow()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 12
nop 1
dl 0
loc 26
rs 9.3554
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/Time', 40, 'C', 'date', XPDF::FLAG_DATE_TIME);
51
        $this->addCol('Text', -1, 'L', 'text', XPDF::FLAG_ELIPSIS);
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 number formating.
66
        $this->setNumberFormat(1, '', ' kg');
67
68
        // and set meassuring for the image col
69
        $this->setColImageInfo($iImgCol, 1.5, 2.5, 3);
70
    }
71
72
    /**
73
     * handle special content of cell
74
     * - any calculation
75
     * - formating
76
     * - ...
77
     * not included in raw data
78
     *
79
     * !! Important !!
80
     * dont't forget to call parent::col($iCol, $row, $bFill) if data not processed...
81
     *
82
     * (non-PHPdoc)
83
     * @see XPDF::col()
84
     */
85
    protected function col(int $iCol, array $row, bool &$bFill) : string
86
    {
87
        $strCol = '';
88
        switch ($iCol) {
89
            case self::MY_GRP_COL:
90
                $aValues = array('', 'Grp. A', 'Grp. B', 'Grp. C', 'Grp. D');
91
                if ($row['grp_id'] > 0 && $row['grp_id'] <= 4) {
92
                    $strCol = $aValues[$row['grp_id']];
93
                }
94
                break;
95
            case self::MY_IMAGE_COL:
96
                $strCol = 'images/';
97
                $fltWeight = floatval($row['weight']);
98
                if ($fltWeight > 35.0) {
99
                    // ... to heavy
100
                    $strCol .= 'red.png';
101
                } else if ($fltWeight > 20.0) {
102
                    // ... just in the limit
103
                    $strCol .= 'yellow.png';
104
                } else {
105
                    $strCol .= 'green.png';
106
                }
107
                break;
108
            case self::MY_CALC_COL:
109
                $fltPricePerKg = 0.0;
110
                if (floatval($row['weight']) != 0) {
111
                    $fltPricePerKg = floatval($row['price']) / floatval($row['weight']);
112
                }
113
                $strCol = $this->formatCurrency($fltPricePerKg, true);
114
                break;
115
            default:
116
                // very important to call parent class !!
117
                $strCol = parent::col($iCol, $row, $bFill);
118
                break;
119
        }
120
        return $strCol;
121
    }
122
123
    /**
124
     * the preRow() method may be used to
125
     * - process internal state or properties
126
     * - start of grouping
127
     * - manipulate data of next row
128
     * - insert some separator or subtitle within the grid
129
     *
130
     * pay attention on $row param is passed as reference so any changes affects
131
     * further output.
132
     *
133
     * (non-PHPdoc)
134
     * @see XPDF::preRow()
135
     */
136
    protected function preRow(array &$row) : string
137
    {
138
        // for grouping
139
        $date = strtotime($row['date']);
140
        $strMonth = date('Y-m', $date);
141
        if ($this->strMonth != $strMonth) {
142
            // first row we have no subtotals...
143
            if ($this->strMonth != '') {
144
                $this->endGroup();
145
            }
146
            $dt = new DateTime();
147
            $dt->setTimestamp($date);
148
            $oDateFormatter = new IntlDateFormatter($this->strLocale, 0, 0, null, null, 'MMMM Y');
149
            $strDate = $oDateFormatter->format($dt);
150
            $this->startGroup('Totals ' . $strDate . ':', $strDate);
151
            $this->strMonth = $strMonth;
152
        }
153
        $strSubRow = '';
154
        if ($this->iRow == 47) {
155
            $strSubRow = '... next Row have been manipulated in ExampleXPDF::preRow(array &$row)!';
156
            $row['text'] = 'manipulated Rowdata!';
157
        }
158
        if ($this->iRow == 56) {
159
            $row['text'] = 'manipulated Rowdata without Subrow!';
160
        }
161
        return $strSubRow;
162
    }
163
164
    /**
165
     * Overwrite the endGrid() method to close the last group.
166
     * {@inheritDoc}
167
     * @see \SKien\XFPDF\XPDF::endGrid()
168
     */
169
    public function endGrid() : void
170
    {
171
        // end last group for subtotals before we call the parent (!!! don't forget that!!)
172
        $this->endGroup();
173
        parent::endGrid();
174
    }
175
}
176