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