xref: /webtrees/app/Report/ReportHtmlCell.php (revision 0115bc1678cafd957052cd2d256563ad3e242cc1)
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        // Set up the text style
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 === ReportBaseElement::CURRENT_POSITION) {
57            $this->left = $renderer->getX();
58        } else {
59            $renderer->setX($this->left);
60        }
61
62        if ($this->top === ReportBaseElement::CURRENT_POSITION) {
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
75        // Background color
76        if (!empty($this->bgcolor)) {
77            echo 'background-color:', $this->bgcolor, ';';
78        }
79
80        // Borders
81        $bpixX = 0;
82        $bpixY = 0;
83        if (!empty($this->border)) {
84            // Border all around
85            if ($this->border == 1) {
86                echo ' border:solid ';
87                if (!empty($this->bocolor)) {
88                    echo $this->bocolor;
89                } else {
90                    echo 'black';
91                }
92                echo ' 1pt;';
93                $bpixX = 1;
94                $bpixY = 1;
95            } else {
96                if (stripos($this->border, 'T') !== false) {
97                    echo ' border-top:solid ';
98                    if (!empty($this->bocolor)) {
99                        echo $this->bocolor;
100                    } else {
101                        echo 'black';
102                    }
103                    echo ' 1pt;';
104                    $bpixY = 1;
105                }
106                if (stripos($this->border, 'B') !== false) {
107                    echo ' border-bottom:solid ';
108                    if (!empty($this->bocolor)) {
109                        echo $this->bocolor;
110                    } else {
111                        echo 'black';
112                    }
113                    echo ' 1pt;';
114                    $bpixY = 1;
115                }
116                if (stripos($this->border, 'R') !== false) {
117                    echo ' border-right:solid ';
118                    if (!empty($this->bocolor)) {
119                        echo $this->bocolor;
120                    } else {
121                        echo 'black';
122                    }
123                    echo ' 1pt;';
124                    $bpixX = 1;
125                }
126                if (stripos($this->border, 'L') !== false) {
127                    echo ' border-left:solid ';
128                    if (!empty($this->bocolor)) {
129                        echo $this->bocolor;
130                    } else {
131                        echo 'black';
132                    }
133                    echo ' 1pt;';
134                    $bpixX = 1;
135                }
136            }
137        }
138        // Check the width if set to page wide OR set by xml to larger then page wide
139        if ($this->width == 0 || $this->width > $renderer->getRemainingWidth()) {
140            $this->width = $renderer->getRemainingWidth();
141        }
142        // We have to calculate a different width for the padding, counting on both side
143        $cW = $this->width - ($cP * 2);
144
145        // If there is any text
146        if (!empty($temptext)) {
147            // Wrap the text
148            $temptext = $renderer->textWrap($temptext, $cW);
149            $tmph     = $renderer->getTextCellHeight($temptext);
150            // Add some cell padding
151            $this->height += $cP;
152            if ($tmph > $this->height) {
153                $this->height = $tmph;
154            }
155        }
156        // Check the last cell height and adjust the current cell height if needed
157        if ($renderer->lastCellHeight > $this->height) {
158            $this->height = $renderer->lastCellHeight;
159        }
160        echo ' width:', $cW - $bpixX, 'pt;height:', $this->height - $bpixY, 'pt;';
161
162        // Text alignment
163        switch ($this->align) {
164            case 'C':
165                echo ' text-align:center;';
166                break;
167            case 'L':
168                echo ' text-align:left;';
169                break;
170            case 'R':
171                echo ' text-align:right;';
172                break;
173        }
174
175        // Print the collected HTML code
176        echo '">';
177
178        // Print URL
179        if (!empty($this->url)) {
180            echo '<a href="', $this->url, '">';
181        }
182        // Print any text if exists
183        if (!empty($temptext)) {
184            $renderer->write($temptext, $this->tcolor, false);
185        }
186        if (!empty($this->url)) {
187            echo '</a>';
188        }
189        // Finish the cell printing and start to clean up
190        echo "</div>\n";
191
192        // Where to place the next position
193        if ($this->newline == 0) {
194            // -> Next to this cell in the same line
195            $renderer->setXy($this->left + $this->width, $this->top);
196            $renderer->lastCellHeight = $this->height;
197        } elseif ($this->newline == 1) {
198            // -> On a new line at the margin - Default
199            $renderer->setXy(0, $renderer->getY() + $this->height + ($cP * 2));
200            // Reset the last cell height for the next line
201            $renderer->lastCellHeight = 0;
202        } elseif ($this->newline == 2) {
203            // -> On a new line at the end of this cell
204            $renderer->setXy($renderer->getX() + $this->width, $renderer->getY() + $this->height + ($cP * 2));
205            // Reset the last cell height for the next line
206            $renderer->lastCellHeight = 0;
207        }
208    }
209}
210