xref: /webtrees/app/Report/ReportHtmlCell.php (revision c0d396ead092b2de2bd331757c79810fd256aad8)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Report;
17
18/**
19 * Class ReportHtmlCell
20 */
21class ReportHtmlCell extends ReportBaseCell
22{
23    /**
24     * HTML Cell renderer
25     *
26     * @param ReportHtml $renderer
27     */
28    public function render($renderer)
29    {
30        if (strpos($this->text, '{{:ptp:}}') !== false) {
31            return;
32        }
33        $temptext = str_replace('#PAGENUM#', $renderer->pageNo(), $this->text);
34        // underline «title» part of Source item
35        $temptext = str_replace([
36            '«',
37            '»',
38        ], [
39            '<u>',
40            '</u>',
41        ], $temptext);
42
43        // Setup the style name
44        if ($renderer->getCurrentStyle() != $this->styleName) {
45            $renderer->setCurrentStyle($this->styleName);
46        }
47
48        // If (Future-feature-enable/disable cell padding)
49        $cP = $renderer->cPadding;
50
51        // Adjust the positions
52        if ($this->left == '.') {
53            $this->left = $renderer->getX();
54        } else {
55            $renderer->setX($this->left);
56        }
57
58        if ($this->top == '.') {
59            $this->top = $renderer->getY();
60        } else {
61            $renderer->setY($this->top);
62        }
63
64        // Start collecting the HTML code
65        echo '<div class="', $this->styleName, '" style="position:absolute;top:', $this->top, 'pt;';
66        // Use Cell around padding to support RTL also
67        echo 'padding:', $cP, 'pt;';
68        // LTR (left) or RTL (right)
69        echo $renderer->alignRTL, ':', $this->left, 'pt;';
70        // Background color
71        if (!empty($this->bgcolor)) {
72            echo 'background-color:', $this->bgcolor, ';';
73        }
74        // Border setup
75        $bpixX = 0;
76        $bpixY = 0;
77        if (!empty($this->border)) {
78            // Border all around
79            if ($this->border == 1) {
80                echo ' border:solid ';
81                if (!empty($this->bocolor)) {
82                    echo $this->bocolor;
83                } else {
84                    echo 'black';
85                }
86                echo ' 1pt;';
87                $bpixX = 1;
88                $bpixY = 1;
89            } else {
90                if (stripos($this->border, 'T') !== false) {
91                    echo ' border-top:solid ';
92                    if (!empty($this->bocolor)) {
93                        echo $this->bocolor;
94                    } else {
95                        echo 'black';
96                    }
97                    echo ' 1pt;';
98                    $bpixY = 1;
99                }
100                if (stripos($this->border, 'B') !== false) {
101                    echo ' border-bottom:solid ';
102                    if (!empty($this->bocolor)) {
103                        echo $this->bocolor;
104                    } else {
105                        echo 'black';
106                    }
107                    echo ' 1pt;';
108                    $bpixY = 1;
109                }
110                if (stripos($this->border, 'R') !== false) {
111                    echo ' border-right:solid ';
112                    if (!empty($this->bocolor)) {
113                        echo $this->bocolor;
114                    } else {
115                        echo 'black';
116                    }
117                    echo ' 1pt;';
118                    $bpixX = 1;
119                }
120                if (stripos($this->border, 'L') !== false) {
121                    echo ' border-left:solid ';
122                    if (!empty($this->bocolor)) {
123                        echo $this->bocolor;
124                    } else {
125                        echo 'black';
126                    }
127                    echo ' 1pt;';
128                    $bpixX = 1;
129                }
130            }
131        }
132        // Check the width if set to page wide OR set by xml to larger then page wide
133        if ($this->width == 0 || $this->width > $renderer->getRemainingWidth()) {
134            $this->width = $renderer->getRemainingWidth();
135        }
136        // We have to calculate a different width for the padding, counting on both side
137        $cW = $this->width - ($cP * 2);
138
139        // If there is any text
140        if (!empty($temptext)) {
141            // Wrap the text
142            $temptext = $renderer->textWrap($temptext, $cW);
143            $tmph     = $renderer->getTextCellHeight($temptext);
144            // Add some cell padding
145            $this->height += $cP;
146            if ($tmph > $this->height) {
147                $this->height = $tmph;
148            }
149        }
150        // Check the last cell height and ajust with the current cell height
151        if ($renderer->lastCellHeight > $this->height) {
152            $this->height = $renderer->lastCellHeight;
153        }
154        echo ' width:', $cW - $bpixX, 'pt;height:', $this->height - $bpixY, 'pt;';
155
156        // Text alignment
157        switch ($this->align) {
158            case 'C':
159                echo ' text-align:center;';
160                break;
161            case 'L':
162                echo ' text-align:left;';
163                break;
164            case 'R':
165                echo ' text-align:right;';
166                break;
167        }
168
169        // Print the collected HTML code
170        echo '">';
171
172        // Print URL
173        if (!empty($this->url)) {
174            echo '<a href="', $this->url, '">';
175        }
176        // Print any text if exists
177        if (!empty($temptext)) {
178            $renderer->write($temptext, $this->tcolor, false);
179        }
180        if (!empty($this->url)) {
181            echo '</a>';
182        }
183        // Finish the cell printing and start to clean up
184        echo "</div>\n";
185
186        // Where to place the next position
187        if ($this->newline == 0) {
188            // -> Next to this cell in the same line
189            $renderer->setXy($this->left + $this->width, $this->top);
190            $renderer->lastCellHeight = $this->height;
191        } elseif ($this->newline == 1) {
192            // -> On a new line at the margin - Default
193            $renderer->setXy(0, $renderer->getY() + $this->height + ($cP * 2));
194            // Reset the last cell height for the next line
195            $renderer->lastCellHeight = 0;
196        } elseif ($this->newline == 2) {
197            // -> On a new line at the end of this cell
198            $renderer->setXy($renderer->getX() + $this->width, $renderer->getY() + $this->height + ($cP * 2));
199            // Reset the last cell height for the next line
200            $renderer->lastCellHeight = 0;
201        }
202    }
203}
204