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