xref: /webtrees/app/Report/ReportHtmlCell.php (revision d11be7027e34e3121be11cc025421873364403f9)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
21a25f0a04SGreg Roach
22dec352c1SGreg Roachuse function str_contains;
23b6f35a76SGreg Roachuse function str_replace;
24b6f35a76SGreg Roach
25a25f0a04SGreg Roach/**
26a25f0a04SGreg Roach * Class ReportHtmlCell
27a25f0a04SGreg Roach */
28c1010edaSGreg Roachclass ReportHtmlCell extends ReportBaseCell
29c1010edaSGreg Roach{
30a25f0a04SGreg Roach    /**
31a25f0a04SGreg Roach     * HTML Cell renderer
32a25f0a04SGreg Roach     *
33b6f35a76SGreg Roach     * @param HtmlRenderer $renderer
34c7ff4153SGreg Roach     *
35c7ff4153SGreg Roach     * @return void
36a25f0a04SGreg Roach     */
3777bab461SGreg Roach    public function render($renderer): void
38c1010edaSGreg Roach    {
39dec352c1SGreg Roach        if (str_contains($this->text, '{{:ptp:}}')) {
40a25f0a04SGreg Roach            return;
41a25f0a04SGreg Roach        }
428a4ee39cSGreg Roach        $temptext = str_replace('#PAGENUM#', (string) $renderer->pageNo(), $this->text);
43a25f0a04SGreg Roach        // underline «title» part of Source item
44c1010edaSGreg Roach        $temptext = str_replace([
45c1010edaSGreg Roach            '«',
46c1010edaSGreg Roach            '»',
47c1010edaSGreg Roach        ], [
48c1010edaSGreg Roach            '<u>',
49c1010edaSGreg Roach            '</u>',
50c1010edaSGreg Roach        ], $temptext);
51a25f0a04SGreg Roach
5267c69ce5SGreg Roach        // Set up the text style
5367c69ce5SGreg Roach        if ($renderer->getCurrentStyle() !== $this->styleName) {
54a25f0a04SGreg Roach            $renderer->setCurrentStyle($this->styleName);
55a25f0a04SGreg Roach        }
56a25f0a04SGreg Roach
57a25f0a04SGreg Roach        // If (Future-feature-enable/disable cell padding)
58a25f0a04SGreg Roach        $cP = $renderer->cPadding;
59a25f0a04SGreg Roach
60a25f0a04SGreg Roach        // Adjust the positions
61c21bdddcSGreg Roach        if ($this->left === ReportBaseElement::CURRENT_POSITION) {
62a25f0a04SGreg Roach            $this->left = $renderer->getX();
63a25f0a04SGreg Roach        } else {
64a25f0a04SGreg Roach            $renderer->setX($this->left);
65a25f0a04SGreg Roach        }
66a25f0a04SGreg Roach
67c21bdddcSGreg Roach        if ($this->top === ReportBaseElement::CURRENT_POSITION) {
68a25f0a04SGreg Roach            $this->top = $renderer->getY();
69a25f0a04SGreg Roach        } else {
70a25f0a04SGreg Roach            $renderer->setY($this->top);
71a25f0a04SGreg Roach        }
72a25f0a04SGreg Roach
73a25f0a04SGreg Roach        // Start collecting the HTML code
747a6ee1acSGreg Roach        echo '<div class="', $this->styleName, '" style="position:absolute;top:', $this->top, 'pt;';
75a25f0a04SGreg Roach        // Use Cell around padding to support RTL also
767a6ee1acSGreg Roach        echo 'padding:', $cP, 'pt;';
77a25f0a04SGreg Roach        // LTR (left) or RTL (right)
787a6ee1acSGreg Roach        echo $renderer->alignRTL, ':', $this->left, 'pt;';
7967c69ce5SGreg Roach
80a25f0a04SGreg Roach        // Background color
81a25f0a04SGreg Roach        if (!empty($this->bgcolor)) {
827a6ee1acSGreg Roach            echo 'background-color:', $this->bgcolor, ';';
83a25f0a04SGreg Roach        }
8467c69ce5SGreg Roach
8567c69ce5SGreg Roach        // Borders
86a25f0a04SGreg Roach        $bpixX = 0;
87a25f0a04SGreg Roach        $bpixY = 0;
88a25f0a04SGreg Roach        if (!empty($this->border)) {
89a25f0a04SGreg Roach            // Border all around
9077bab461SGreg Roach            if ($this->border === '1') {
9177bab461SGreg Roach                echo ' border:solid ', $this->bocolor ?: 'black', ' 1pt;';
92a25f0a04SGreg Roach                $bpixX = 1;
93a25f0a04SGreg Roach                $bpixY = 1;
94a25f0a04SGreg Roach            } else {
9577bab461SGreg Roach                if (str_contains($this->border, 'T')) {
9677bab461SGreg Roach                    echo ' border-top:solid ', $this->bocolor ?: 'black', ' 1pt;';
97a25f0a04SGreg Roach                    $bpixY = 1;
98a25f0a04SGreg Roach                }
9977bab461SGreg Roach                if (str_contains($this->border, 'B')) {
10077bab461SGreg Roach                    echo ' border-bottom:solid ', $this->bocolor ?: 'black', ' 1pt;';
101a25f0a04SGreg Roach                    $bpixY = 1;
102a25f0a04SGreg Roach                }
10377bab461SGreg Roach                if (str_contains($this->border, 'R')) {
10477bab461SGreg Roach                    echo ' border-right:solid ', $this->bocolor ?: 'black', ' 1pt;';
105a25f0a04SGreg Roach                    $bpixX = 1;
106a25f0a04SGreg Roach                }
10777bab461SGreg Roach                if (str_contains($this->border, 'L')) {
10877bab461SGreg Roach                    echo ' border-left:solid ', $this->bocolor ?: 'black', ' 1pt;';
109a25f0a04SGreg Roach                    $bpixX = 1;
110a25f0a04SGreg Roach                }
111a25f0a04SGreg Roach            }
112a25f0a04SGreg Roach        }
113a25f0a04SGreg Roach        // Check the width if set to page wide OR set by xml to larger then page wide
11477bab461SGreg Roach        if ($this->width === 0.0 || $this->width > $renderer->getRemainingWidth()) {
115a25f0a04SGreg Roach            $this->width = $renderer->getRemainingWidth();
116a25f0a04SGreg Roach        }
117a25f0a04SGreg Roach        // We have to calculate a different width for the padding, counting on both side
11877bab461SGreg Roach        $cW = $this->width - $cP * 2.0;
119a25f0a04SGreg Roach
120a25f0a04SGreg Roach        // If there is any text
121a25f0a04SGreg Roach        if (!empty($temptext)) {
122a25f0a04SGreg Roach            // Wrap the text
123a25f0a04SGreg Roach            $temptext = $renderer->textWrap($temptext, $cW);
124a25f0a04SGreg Roach            $tmph     = $renderer->getTextCellHeight($temptext);
125a25f0a04SGreg Roach            // Add some cell padding
126a25f0a04SGreg Roach            $this->height += $cP;
127a25f0a04SGreg Roach            if ($tmph > $this->height) {
128a25f0a04SGreg Roach                $this->height = $tmph;
129a25f0a04SGreg Roach            }
130a25f0a04SGreg Roach        }
13167c69ce5SGreg Roach        // Check the last cell height and adjust the current cell height if needed
132a25f0a04SGreg Roach        if ($renderer->lastCellHeight > $this->height) {
133a25f0a04SGreg Roach            $this->height = $renderer->lastCellHeight;
134a25f0a04SGreg Roach        }
1357a6ee1acSGreg Roach        echo ' width:', $cW - $bpixX, 'pt;height:', $this->height - $bpixY, 'pt;';
136a25f0a04SGreg Roach
137a25f0a04SGreg Roach        // Text alignment
138a25f0a04SGreg Roach        switch ($this->align) {
1397a6ee1acSGreg Roach            case 'C':
1407a6ee1acSGreg Roach                echo ' text-align:center;';
141a25f0a04SGreg Roach                break;
1427a6ee1acSGreg Roach            case 'L':
1437a6ee1acSGreg Roach                echo ' text-align:left;';
144a25f0a04SGreg Roach                break;
1457a6ee1acSGreg Roach            case 'R':
1467a6ee1acSGreg Roach                echo ' text-align:right;';
147a25f0a04SGreg Roach                break;
148a25f0a04SGreg Roach        }
149a25f0a04SGreg Roach
150a25f0a04SGreg Roach        // Print the collected HTML code
1517a6ee1acSGreg Roach        echo '">';
152a25f0a04SGreg Roach
153a25f0a04SGreg Roach        // Print URL
154a25f0a04SGreg Roach        if (!empty($this->url)) {
1557a6ee1acSGreg Roach            echo '<a href="', $this->url, '">';
156a25f0a04SGreg Roach        }
157a25f0a04SGreg Roach        // Print any text if exists
158a25f0a04SGreg Roach        if (!empty($temptext)) {
159a25f0a04SGreg Roach            $renderer->write($temptext, $this->tcolor, false);
160a25f0a04SGreg Roach        }
161a25f0a04SGreg Roach        if (!empty($this->url)) {
1627a6ee1acSGreg Roach            echo '</a>';
163a25f0a04SGreg Roach        }
164a25f0a04SGreg Roach        // Finish the cell printing and start to clean up
165a25f0a04SGreg Roach        echo "</div>\n";
166ce15a17aSGreg Roach
167a25f0a04SGreg Roach        // Where to place the next position
168d823340dSGreg Roach        if ($this->newline === 0) {
169ce15a17aSGreg Roach            // -> Next to this cell in the same line
170a25f0a04SGreg Roach            $renderer->setXy($this->left + $this->width, $this->top);
171a25f0a04SGreg Roach            $renderer->lastCellHeight = $this->height;
172d823340dSGreg Roach        } elseif ($this->newline === 1) {
173ce15a17aSGreg Roach            // -> On a new line at the margin - Default
17452135727SGreg Roach            $renderer->setXy(0, $renderer->getY() + $this->height + $cP * 2);
175a25f0a04SGreg Roach            // Reset the last cell height for the next line
176a25f0a04SGreg Roach            $renderer->lastCellHeight = 0;
177d823340dSGreg Roach        } elseif ($this->newline === 2) {
178ce15a17aSGreg Roach            // -> On a new line at the end of this cell
17952135727SGreg Roach            $renderer->setXy($renderer->getX() + $this->width, $renderer->getY() + $this->height + $cP * 2);
180a25f0a04SGreg Roach            // Reset the last cell height for the next line
181a25f0a04SGreg Roach            $renderer->lastCellHeight = 0;
182a25f0a04SGreg Roach        }
183a25f0a04SGreg Roach    }
184a25f0a04SGreg Roach}
185