Passed
Pull Request — main (#5055)
by
unknown
07:04
created

ReportHtmlTextbox::render()   F

Complexity

Conditions 56
Paths > 20000

Size

Total Lines 277
Code Lines 165

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 56
eloc 165
nc 588349440
nop 1
dl 0
loc 277
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2023 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Report;
21
22
use function abs;
23
use function count;
24
use function is_object;
25
use function ksort;
26
use function str_replace;
27
use function trim;
28
29
/**
30
 * Class ReportHtmlTextbox
31
 */
32
class ReportHtmlTextbox extends ReportBaseTextbox
33
{
34
    /**
35
     * Render the elements.
36
     *
37
     * @param HtmlRenderer $renderer
38
     *
39
     * @return void
40
     */
41
    public function render($renderer): void
42
    {
43
        static $lastBoxYfinal;
44
        // checkFootnote
45
        $newelements      = [];
46
        $lastelement      = null;
47
        $footnote_element = [];
48
        // Element counter
49
        $cE = count($this->elements);
50
        //-- collapse duplicate elements
51
        for ($i = 0; $i < $cE; $i++) {
52
            $element = $this->elements[$i];
53
            if ($element instanceof ReportBaseElement) {
54
                if ($element instanceof ReportBaseText) {
55
                    if (!empty($footnote_element)) {
56
                        ksort($footnote_element);
57
                        foreach ($footnote_element as $links) {
58
                            $newelements[] = $links;
59
                        }
60
                        $footnote_element = [];
61
                    }
62
                    if (!isset($lastelement)) {
63
                        $lastelement = $element;
64
                    } elseif (($element instanceof ReportBaseText && $lastelement instanceof ReportBaseText) && ($element->getStyleName() === $lastelement->getStyleName())) {
65
                        $lastelement->addText(str_replace("\n", '<br>', $element->getValue()));
66
                    } else {
67
                        $newelements[] = $lastelement;
68
                        $lastelement   = $element;
69
                    }
70
                } elseif ($element instanceof ReportHtmlImage) {
71
                    $lastelement   = $element;
72
                } elseif ($element instanceof ReportHtmlFootnote) {
73
                    // Check if the Footnote has been set with it’s link number
74
                    $renderer->checkFootnote($element);
75
                    // Save first the last element if any
76
                    if (isset($lastelement)) {
77
                        $newelements[] = $lastelement;
78
                        $lastelement   = null;
79
                    }
80
                    // Save the Footnote with it’s link number as key for sorting later
81
                    $footnote_element[$element->num] = $element;
82
                } elseif (trim($element->getValue()) !== '') {
83
                    // Do not keep empty footnotes
84
                    if (!empty($footnote_element)) {
85
                        ksort($footnote_element);
86
                        foreach ($footnote_element as $links) {
87
                            $newelements[] = $links;
88
                        }
89
                        $footnote_element = [];
90
                    }
91
                    if (isset($lastelement)) {
92
                        $newelements[] = $lastelement;
93
                        $lastelement   = null;
94
                    }
95
                    $newelements[] = $element;
96
                }
97
            } else {
98
                if (isset($lastelement)) {
99
                    $newelements[] = $lastelement;
100
                    $lastelement   = null;
101
                }
102
                if (!empty($footnote_element)) {
103
                    ksort($footnote_element);
104
                    foreach ($footnote_element as $links) {
105
                        $newelements[] = $links;
106
                    }
107
                    $footnote_element = [];
108
                }
109
                $newelements[] = $element;
110
            }
111
        }
112
        if (isset($lastelement)) {
113
            $newelements[] = $lastelement;
114
        }
115
        if (!empty($footnote_element)) {
116
            ksort($footnote_element);
117
            foreach ($footnote_element as $links) {
118
                $newelements[] = $links;
119
            }
120
        }
121
        $this->elements = $newelements;
122
        unset($footnote_element, $lastelement, $newelements);
123
124
        $cP = 0; // Class Padding
125
126
        // Used with line breaks and cell height calculation within this box only
127
        $renderer->largestFontHeight = 0;
128
129
        // If current position (left)
130
        if ($this->left === ReportBaseElement::CURRENT_POSITION) {
131
            $cX = $renderer->getX();
132
        } else {
133
            $cX = $this->left;
134
            $renderer->setX($cX);
135
        }
136
        // If current position (top)
137
        $align_Y = false;
138
        $first_Y = $renderer->getY();
139
        $topstr = "";
0 ignored issues
show
Unused Code introduced by
The assignment to $topstr is dead and can be removed.
Loading history...
140
        if ($this->abs_position) {
141
            if ($this->top == ReportBaseElement::CURRENT_POSITION) {
142
                $this->top = $first_Y;
143
            }
144
        }
145
        if (!$this->abs_position) {
146
            if ($this->top_position !== ReportBaseElement::CURRENT_POSITION) {
147
                $this->top = $this->top_position;
148
            }
149
            $topstr = "top:" . $this->top . "pt;";
150
            $align_Y = true;
151
        }
152
        $renderer->setY($this->top);
153
154
        // Check the width if set to page wide OR set by xml to larger then page width (margin)
155
        if ($this->width === 0.0 || $this->width > $renderer->getRemainingWidth()) {
156
            $this->width = $renderer->getRemainingWidth();
157
        }
158
        // Setup the CellPadding
159
        if ($this->padding) {
160
            $cP = $renderer->cPadding;
161
        }
162
163
        // For padding, we have to use less wrap width
164
        $cW = $this->width - $cP * 2.0;
165
166
        //-- calculate the text box height
167
        // Number of lines, will be converted to height
168
        $cHT = 0;
169
        // Element height (except text)
170
        $eH = 0.0;
171
        // Footnote height (in points)
172
        $fH = 0;
173
        $w  = 0;
174
        //-- $lw is an array
175
        // 0 => last line width
176
        // 1 => 1 if text was wrapped, 0 if text did not wrap
177
        // 2 => number of LF
178
        $lw = [];
179
        // Element counter
180
        $cE = count($this->elements);
181
        for ($i = 0; $i < $cE; $i++) {
182
            if (is_object($this->elements[$i])) {
183
                $ew = $this->elements[$i]->setWrapWidth($cW - $w - 2, $cW);
184
                if ($ew === $cW) {
185
                    $w = 0;
186
                }
187
                $lw = $this->elements[$i]->getWidth($renderer);
188
                // Text is already gets the # LF
189
                $cHT += $lw[2];
190
                if ($lw[1] === 1) {
191
                    $w = $lw[0];
192
                } elseif ($lw[1] === 2) {
193
                    $w = 0;
194
                } else {
195
                    $w += $lw[0];
196
                }
197
                if ($w > $cW) {
198
                    $w = $lw[0];
199
                }
200
                // For anything else but text (images), get the height
201
                $eH += $this->elements[$i]->getHeight($renderer);
202
            } else {
203
                $fH += abs($renderer->getFootnotesHeight($cW));
204
            }
205
        }
206
207
        // Add up what’s the final height
208
        //$cH = $this->height;
209
        $cH = 0;
210
        // If any element exist
211
        if ($cE > 0) {
212
            // Check if this is text or some other element, like images
213
            if ($eH === 0.0) {
0 ignored issues
show
introduced by
The condition $eH === 0.0 is always true.
Loading history...
214
                // Number of LF but at least one line
215
                $cHT = ($cHT + 1) * $renderer->cellHeightRatio;
216
                // Calculate the cell height with the largest font size used
217
                $cHT *= $renderer->largestFontHeight;
218
                if ($cH < $cHT) {
219
                    $cH = $cHT;
220
                }
221
            } else {
222
                // This is any other element
223
                if ($cH < $eH) {
224
                    $cH = $eH;
225
                }
226
                // Add Footnote height to the rest of the height
227
                $cH += $fH;
228
            }
229
        }
230
231
        unset($lw, $cHT, $fH, $w);
232
233
        // Finally, check the last cells height
234
        if ($cH < $renderer->lastCellHeight) {
235
            $cH = $renderer->lastCellHeight;
236
        }
237
        // Update max Y in case of a pagebreak
238
        // We don't want to over write any images or other stuff
239
        $renderer->addMaxY($this->top + $cH);
240
241
        // Start to print HTML
242
        if (!$align_Y) {
243
            echo '<div style="position:absolute;top:', $this->top, 'pt;';
244
        } else {
245
            echo '<div style="position:relative;top:', $this->top, 'pt;';
246
        }
247
        //echo '<div style="position:relative;';
248
        // LTR (left) or RTL (right)
249
        echo $renderer->alignRTL, ':', $cX, 'pt;';
250
        // Background color
251
        if ($this->fill && $this->bgcolor !== '') {
252
            echo ' background-color:', $this->bgcolor, ';';
253
        }
254
        // Print padding only when it’s set
255
        if ($this->padding) {
256
            // Use Cell around padding to support RTL also
257
            echo 'padding:', $cP, 'pt;';
258
        }
259
        //if ($this->html_only_position) {    // allow a small margin between images if text is short
260
        //    echo "margin-bottom:5pt;";
261
        //}
262
        // Border setup
263
        if ($this->border) {
264
            echo ' border:solid black 1pt;';
265
            if (!$align_Y) {
266
                echo 'width:', $this->width - 1 - $cP * 2, 'pt;height:', $cH - 1, 'pt;';
267
            } else {
268
                echo 'width:', $this->width - 1 - $cP * 2, 'pt;height:auto;';
269
            } // height:',$this->height,'pt;'; //,$topstr;
270
        } else {
271
            if (!$align_Y) {
272
                echo 'width:', $this->width - $cP * 2, 'pt;height:', $cH, 'pt;';
273
            } else {
274
                echo 'width:', $this->width - $cP * 2, 'pt;height:auto;';
275
            } //height:',$this->height,'pt;'; //,$topstr;
276
        }
277
        echo '">';
278
279
        // Do a little "margin" trick before print
280
        // to get the correct current position => "."
281
        $cXT = $renderer->getX();
282
        $cYT = $renderer->getY();
283
        $renderer->setXy(0, 0);
284
285
        // Print the text elements
286
        foreach ($this->elements as $element) {
287
            if ($element instanceof ReportHtmlText) {
288
                $element->render($renderer, false);
289
            } elseif ($element instanceof ReportBaseElement) {
290
                $element->render($renderer);
291
            } elseif ($element === 'footnotetexts') {
292
                $renderer->footnotes();
293
            } elseif ($element === 'addpage') {
294
                $renderer->addPage();
295
            }
296
        }
297
        echo "</div>\n";
298
299
        // Reset "margins"
300
        $renderer->setXy($cXT, $cYT);
301
        // This will be mostly used to trick the multiple images last height
302
        if ($this->reseth) {
303
            $this->top = $first_Y;  //- $cH;
304
            $cH = 0;
305
        }
306
         // New line and some clean up
307
        if (!$this->newline) {
308
            $renderer->setXy($cX + $this->width, $this->top);
309
            $renderer->lastCellHeight = $cH;
310
        } else {
311
            //$renderer->setXy(0, $this->top + $cH + $cP * 2);
312
            $renderer->setXy(0, $first_Y + $cH + $cP * 2);
313
            $renderer->lastCellHeight = 0;
314
        }
315
        // This will make images in textboxes to ignore images in previous textboxes
316
        // Without this trick the $lastpicbottom in the previos textbox will be used in ReportHtmlImage
317
        $renderer->pageN++;
318
    }
319
}
320