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