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