xref: /webtrees/app/Report/ReportHtmlCell.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Report;
21
22use function str_contains;
23use function str_replace;
24
25/**
26 * Class ReportHtmlCell
27 */
28class ReportHtmlCell extends ReportBaseCell
29{
30    /**
31     * HTML Cell renderer
32     *
33     * @param HtmlRenderer $renderer
34     *
35     * @return void
36     */
37    public function render($renderer): void
38    {
39        if (str_contains($this->text, '{{:ptp:}}')) {
40            return;
41        }
42        $temptext = str_replace('#PAGENUM#', (string) $renderer->pageNo(), $this->text);
43        // underline «title» part of Source item
44        $temptext = str_replace([
45            '«',
46            '»',
47        ], [
48            '<u>',
49            '</u>',
50        ], $temptext);
51
52        // Set up the text style
53        if ($renderer->getCurrentStyle() !== $this->styleName) {
54            $renderer->setCurrentStyle($this->styleName);
55        }
56
57        // If (Future-feature-enable/disable cell padding)
58        $cP = $renderer->cPadding;
59
60        // Adjust the positions
61        if ($this->left === ReportBaseElement::CURRENT_POSITION) {
62            $this->left = $renderer->getX();
63        } else {
64            $renderer->setX($this->left);
65        }
66
67        if ($this->top === ReportBaseElement::CURRENT_POSITION) {
68            $this->top = $renderer->getY();
69        } else {
70            $renderer->setY($this->top);
71        }
72
73        // Start collecting the HTML code
74        echo '<div class="', $this->styleName, '" style="position:absolute;top:', $this->top, 'pt;';
75        // Use Cell around padding to support RTL also
76        echo 'padding:', $cP, 'pt;';
77        // LTR (left) or RTL (right)
78        echo $renderer->alignRTL, ':', $this->left, 'pt;';
79
80        // Background color
81        if (!empty($this->bgcolor)) {
82            echo 'background-color:', $this->bgcolor, ';';
83        }
84
85        // Borders
86        $bpixX = 0;
87        $bpixY = 0;
88        if (!empty($this->border)) {
89            // Border all around
90            if ($this->border === '1') {
91                echo ' border:solid ', $this->bocolor ?: 'black', ' 1pt;';
92                $bpixX = 1;
93                $bpixY = 1;
94            } else {
95                if (str_contains($this->border, 'T')) {
96                    echo ' border-top:solid ', $this->bocolor ?: 'black', ' 1pt;';
97                    $bpixY = 1;
98                }
99                if (str_contains($this->border, 'B')) {
100                    echo ' border-bottom:solid ', $this->bocolor ?: 'black', ' 1pt;';
101                    $bpixY = 1;
102                }
103                if (str_contains($this->border, 'R')) {
104                    echo ' border-right:solid ', $this->bocolor ?: 'black', ' 1pt;';
105                    $bpixX = 1;
106                }
107                if (str_contains($this->border, 'L')) {
108                    echo ' border-left:solid ', $this->bocolor ?: 'black', ' 1pt;';
109                    $bpixX = 1;
110                }
111            }
112        }
113        // Check the width if set to page wide OR set by xml to larger then page wide
114        if ($this->width === 0.0 || $this->width > $renderer->getRemainingWidth()) {
115            $this->width = $renderer->getRemainingWidth();
116        }
117        // We have to calculate a different width for the padding, counting on both side
118        $cW = $this->width - $cP * 2.0;
119
120        // If there is any text
121        if (!empty($temptext)) {
122            // Wrap the text
123            $temptext = $renderer->textWrap($temptext, $cW);
124            $tmph     = $renderer->getTextCellHeight($temptext);
125            // Add some cell padding
126            $this->height += $cP;
127            if ($tmph > $this->height) {
128                $this->height = $tmph;
129            }
130        }
131        // Check the last cell height and adjust the current cell height if needed
132        if ($renderer->lastCellHeight > $this->height) {
133            $this->height = $renderer->lastCellHeight;
134        }
135        echo ' width:', $cW - $bpixX, 'pt;height:', $this->height - $bpixY, 'pt;';
136
137        // Text alignment
138        switch ($this->align) {
139            case 'C':
140                echo ' text-align:center;';
141                break;
142            case 'L':
143                echo ' text-align:left;';
144                break;
145            case 'R':
146                echo ' text-align:right;';
147                break;
148        }
149
150        // Print the collected HTML code
151        echo '">';
152
153        // Print URL
154        if (!empty($this->url)) {
155            echo '<a href="', $this->url, '">';
156        }
157        // Print any text if exists
158        if (!empty($temptext)) {
159            $renderer->write($temptext, $this->tcolor, false);
160        }
161        if (!empty($this->url)) {
162            echo '</a>';
163        }
164        // Finish the cell printing and start to clean up
165        echo "</div>\n";
166
167        // Where to place the next position
168        if ($this->newline === 0) {
169            // -> Next to this cell in the same line
170            $renderer->setXy($this->left + $this->width, $this->top);
171            $renderer->lastCellHeight = $this->height;
172        } elseif ($this->newline === 1) {
173            // -> On a new line at the margin - Default
174            $renderer->setXy(0, $renderer->getY() + $this->height + $cP * 2);
175            // Reset the last cell height for the next line
176            $renderer->lastCellHeight = 0;
177        } elseif ($this->newline === 2) {
178            // -> On a new line at the end of this cell
179            $renderer->setXy($renderer->getX() + $this->width, $renderer->getY() + $this->height + $cP * 2);
180            // Reset the last cell height for the next line
181            $renderer->lastCellHeight = 0;
182        }
183    }
184}
185