1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report; 21a25f0a04SGreg Roach 22b6f35a76SGreg Roachuse function abs; 23b6f35a76SGreg Roachuse function count; 24b6f35a76SGreg Roachuse function is_object; 25b6f35a76SGreg Roachuse function ksort; 26b6f35a76SGreg Roachuse function str_replace; 27b6f35a76SGreg Roachuse function trim; 28b6f35a76SGreg Roach 29a25f0a04SGreg Roach/** 30a25f0a04SGreg Roach * Class ReportHtmlTextbox 31a25f0a04SGreg Roach */ 32c1010edaSGreg Roachclass ReportHtmlTextbox extends ReportBaseTextbox 33c1010edaSGreg Roach{ 34a25f0a04SGreg Roach /** 3576692c8bSGreg Roach * Render the elements. 3676692c8bSGreg Roach * 37b6f35a76SGreg Roach * @param HtmlRenderer $renderer 38c7ff4153SGreg Roach * 39c7ff4153SGreg Roach * @return void 40a25f0a04SGreg Roach */ 41c1010edaSGreg Roach public function render($renderer) 42c1010edaSGreg Roach { 43a25f0a04SGreg Roach // checkFootnote 4413abd6f3SGreg Roach $newelements = []; 4513abd6f3SGreg Roach $lastelement = []; 4613abd6f3SGreg Roach $footnote_element = []; 47a25f0a04SGreg Roach // Element counter 48a25f0a04SGreg Roach $cE = count($this->elements); 49a25f0a04SGreg Roach //-- collapse duplicate elements 50a25f0a04SGreg Roach for ($i = 0; $i < $cE; $i++) { 51a25f0a04SGreg Roach $element = $this->elements[$i]; 52044416d2SGreg Roach if ($element instanceof ReportBaseElement) { 53a25f0a04SGreg Roach if ($element instanceof ReportBaseText) { 54a25f0a04SGreg Roach if (!empty($footnote_element)) { 55a25f0a04SGreg Roach ksort($footnote_element); 56a25f0a04SGreg Roach foreach ($footnote_element as $links) { 57a25f0a04SGreg Roach $newelements[] = $links; 58a25f0a04SGreg Roach } 5913abd6f3SGreg Roach $footnote_element = []; 60a25f0a04SGreg Roach } 61a25f0a04SGreg Roach if (empty($lastelement)) { 62a25f0a04SGreg Roach $lastelement = $element; 63a25f0a04SGreg Roach } else { 64a25f0a04SGreg Roach // Checking if the Text has the same style 6559597b37SGreg Roach if ($element->getStyleName() === $lastelement->getStyleName()) { 667a6ee1acSGreg Roach $lastelement->addText(str_replace("\n", '<br>', $element->getValue())); 67a25f0a04SGreg Roach } elseif (!empty($lastelement)) { 68a25f0a04SGreg Roach $newelements[] = $lastelement; 69a25f0a04SGreg Roach $lastelement = $element; 70a25f0a04SGreg Roach } 71a25f0a04SGreg Roach } 7259597b37SGreg Roach } elseif ($element instanceof ReportHtmlFootnote) { 73a25f0a04SGreg Roach // Check if the Footnote has been set with it’s link number 74a25f0a04SGreg Roach $renderer->checkFootnote($element); 75a25f0a04SGreg Roach // Save first the last element if any 76a25f0a04SGreg Roach if (!empty($lastelement)) { 77a25f0a04SGreg Roach $newelements[] = $lastelement; 7813abd6f3SGreg Roach $lastelement = []; 79a25f0a04SGreg Roach } 80a25f0a04SGreg Roach // Save the Footnote with it’s link number as key for sorting later 81a25f0a04SGreg Roach $footnote_element[$element->num] = $element; 8259597b37SGreg Roach } elseif (trim($element->getValue()) !== '') { 83ce15a17aSGreg Roach // Do not keep empty footnotes 84a25f0a04SGreg Roach if (!empty($footnote_element)) { 85a25f0a04SGreg Roach ksort($footnote_element); 86a25f0a04SGreg Roach foreach ($footnote_element as $links) { 87a25f0a04SGreg Roach $newelements[] = $links; 88a25f0a04SGreg Roach } 8913abd6f3SGreg Roach $footnote_element = []; 90a25f0a04SGreg Roach } 91a25f0a04SGreg Roach if (!empty($lastelement)) { 92a25f0a04SGreg Roach $newelements[] = $lastelement; 9313abd6f3SGreg Roach $lastelement = []; 94a25f0a04SGreg Roach } 95a25f0a04SGreg Roach $newelements[] = $element; 96a25f0a04SGreg Roach } 97a25f0a04SGreg Roach } else { 98a25f0a04SGreg Roach if (!empty($lastelement)) { 99a25f0a04SGreg Roach $newelements[] = $lastelement; 10013abd6f3SGreg Roach $lastelement = []; 101a25f0a04SGreg Roach } 102a25f0a04SGreg Roach if (!empty($footnote_element)) { 103a25f0a04SGreg Roach ksort($footnote_element); 104a25f0a04SGreg Roach foreach ($footnote_element as $links) { 105a25f0a04SGreg Roach $newelements[] = $links; 106a25f0a04SGreg Roach } 10713abd6f3SGreg Roach $footnote_element = []; 108a25f0a04SGreg Roach } 109a25f0a04SGreg Roach $newelements[] = $element; 110a25f0a04SGreg Roach } 111a25f0a04SGreg Roach } 112a25f0a04SGreg Roach if (!empty($lastelement)) { 113a25f0a04SGreg Roach $newelements[] = $lastelement; 114a25f0a04SGreg Roach } 115a25f0a04SGreg Roach if (!empty($footnote_element)) { 116a25f0a04SGreg Roach ksort($footnote_element); 117a25f0a04SGreg Roach foreach ($footnote_element as $links) { 118a25f0a04SGreg Roach $newelements[] = $links; 119a25f0a04SGreg Roach } 120a25f0a04SGreg Roach } 121a25f0a04SGreg Roach $this->elements = $newelements; 1228a4ee39cSGreg Roach unset($footnote_element, $lastelement, $newelements); 123a25f0a04SGreg Roach 124a25f0a04SGreg Roach $cP = 0; // Class Padding 125a25f0a04SGreg Roach 126a25f0a04SGreg Roach // Used with line breaks and cell height calculation within this box only 127a25f0a04SGreg Roach $renderer->largestFontHeight = 0; 128a25f0a04SGreg Roach 12967c69ce5SGreg Roach // If current position (left) 130c21bdddcSGreg Roach if ($this->left === ReportBaseElement::CURRENT_POSITION) { 1317820e4d7SGreg Roach $cX = $renderer->getX(); 132a25f0a04SGreg Roach } else { 133a25f0a04SGreg Roach $cX = $this->left; 1347820e4d7SGreg Roach $renderer->setX($cX); 135a25f0a04SGreg Roach } 13667c69ce5SGreg Roach // If current position (top) 137c21bdddcSGreg Roach if ($this->top === ReportBaseElement::CURRENT_POSITION) { 1383844edd4SGreg Roach $this->top = $renderer->getY(); 139a25f0a04SGreg Roach } else { 1407820e4d7SGreg Roach $renderer->setY($this->top); 141a25f0a04SGreg Roach } 142a25f0a04SGreg Roach 14367c69ce5SGreg Roach // Check the width if set to page wide OR set by xml to larger then page width (margin) 144a25f0a04SGreg Roach if ($this->width == 0 || $this->width > $renderer->getRemainingWidth()) { 145a25f0a04SGreg Roach $this->width = $renderer->getRemainingWidth(); 146a25f0a04SGreg Roach } 147a25f0a04SGreg Roach // Setup the CellPadding 148a25f0a04SGreg Roach if ($this->padding) { 149a25f0a04SGreg Roach $cP = $renderer->cPadding; 150a25f0a04SGreg Roach } 151a25f0a04SGreg Roach 152a25f0a04SGreg Roach // For padding, we have to use less wrap width 153a25f0a04SGreg Roach $cW = $this->width - ($cP * 2); 154a25f0a04SGreg Roach 155a25f0a04SGreg Roach //-- calculate the text box height 156a25f0a04SGreg Roach // Number of lines, will be converted to height 157a25f0a04SGreg Roach $cHT = 0; 158a25f0a04SGreg Roach // Element height (exept text) 159a25f0a04SGreg Roach $eH = 0; 160a25f0a04SGreg Roach // Footnote height (in points) 161a25f0a04SGreg Roach $fH = 0; 162a25f0a04SGreg Roach $w = 0; 163a25f0a04SGreg Roach //-- $lw is an array 164a25f0a04SGreg Roach // 0 => last line width 165a25f0a04SGreg Roach // 1 => 1 if text was wrapped, 0 if text did not wrap 166a25f0a04SGreg Roach // 2 => number of LF 16713abd6f3SGreg Roach $lw = []; 168a25f0a04SGreg Roach // Element counter 169a25f0a04SGreg Roach $cE = count($this->elements); 170a25f0a04SGreg Roach for ($i = 0; $i < $cE; $i++) { 171a25f0a04SGreg Roach if (is_object($this->elements[$i])) { 172a25f0a04SGreg Roach $ew = $this->elements[$i]->setWrapWidth($cW - $w - 2, $cW); 173a25f0a04SGreg Roach if ($ew == $cW) { 174a25f0a04SGreg Roach $w = 0; 175a25f0a04SGreg Roach } 176a25f0a04SGreg Roach $lw = $this->elements[$i]->getWidth($renderer); 177a25f0a04SGreg Roach // Text is already gets the # LF 178a25f0a04SGreg Roach $cHT += $lw[2]; 179a25f0a04SGreg Roach if ($lw[1] == 1) { 180a25f0a04SGreg Roach $w = $lw[0]; 181a25f0a04SGreg Roach } elseif ($lw[1] == 2) { 182a25f0a04SGreg Roach $w = 0; 183a25f0a04SGreg Roach } else { 184a25f0a04SGreg Roach $w += $lw[0]; 185a25f0a04SGreg Roach } 186a25f0a04SGreg Roach if ($w > $cW) { 187a25f0a04SGreg Roach $w = $lw[0]; 188a25f0a04SGreg Roach } 189a25f0a04SGreg Roach // For anything else but text (images), get the height 190a25f0a04SGreg Roach $eH += $this->elements[$i]->getHeight($renderer); 191a25f0a04SGreg Roach } else { 192a25f0a04SGreg Roach $fH += abs($renderer->getFootnotesHeight($cW)); 193a25f0a04SGreg Roach } 194a25f0a04SGreg Roach } 19567c69ce5SGreg Roach 196a25f0a04SGreg Roach // Add up what’s the final height 197a25f0a04SGreg Roach $cH = $this->height; 198a25f0a04SGreg Roach // If any element exist 199a25f0a04SGreg Roach if ($cE > 0) { 200a25f0a04SGreg Roach // Check if this is text or some other element, like images 201a25f0a04SGreg Roach if ($eH == 0) { 202a25f0a04SGreg Roach // Number of LF but at least one line 203a25f0a04SGreg Roach $cHT = ($cHT + 1) * $renderer->cellHeightRatio; 204a25f0a04SGreg Roach // Calculate the cell hight with the largest font size used 205e364afe4SGreg Roach $cHT *= $renderer->largestFontHeight; 206a25f0a04SGreg Roach if ($cH < $cHT) { 207a25f0a04SGreg Roach $cH = $cHT; 208a25f0a04SGreg Roach } 209ce15a17aSGreg Roach } else { 210ce15a17aSGreg Roach // This is any other element 211a25f0a04SGreg Roach if ($cH < $eH) { 212a25f0a04SGreg Roach $cH = $eH; 213a25f0a04SGreg Roach } 214a25f0a04SGreg Roach // Add Footnote height to the rest of the height 215a25f0a04SGreg Roach $cH += $fH; 216a25f0a04SGreg Roach } 217a25f0a04SGreg Roach } 218a25f0a04SGreg Roach 219a25f0a04SGreg Roach unset($lw, $cHT, $fH, $w); 220a25f0a04SGreg Roach 221a25f0a04SGreg Roach // Finaly, check the last cells height 222a25f0a04SGreg Roach if ($cH < $renderer->lastCellHeight) { 223a25f0a04SGreg Roach $cH = $renderer->lastCellHeight; 224a25f0a04SGreg Roach } 225a25f0a04SGreg Roach // Update max Y incase of a pagebreak 226a25f0a04SGreg Roach // We don't want to over write any images or other stuff 227a25f0a04SGreg Roach $renderer->addMaxY($this->top + $cH); 228a25f0a04SGreg Roach 229a25f0a04SGreg Roach // Start to print HTML 2307a6ee1acSGreg Roach echo '<div style="position:absolute;top:', $this->top, 'pt;'; 231a25f0a04SGreg Roach // LTR (left) or RTL (right) 2327a6ee1acSGreg Roach echo $renderer->alignRTL, ':', $cX, 'pt;'; 233a25f0a04SGreg Roach // Background color 234b6f35a76SGreg Roach if ($this->fill && $this->bgcolor !== '') { 2357a6ee1acSGreg Roach echo ' background-color:', $this->bgcolor, ';'; 236a25f0a04SGreg Roach } 237a25f0a04SGreg Roach // Print padding only when it’s set 238a25f0a04SGreg Roach if ($this->padding) { 239a25f0a04SGreg Roach // Use Cell around padding to support RTL also 2407a6ee1acSGreg Roach echo 'padding:', $cP, 'pt;'; 241a25f0a04SGreg Roach } 242a25f0a04SGreg Roach // Border setup 243a25f0a04SGreg Roach if ($this->border) { 2447a6ee1acSGreg Roach echo ' border:solid black 1pt;'; 2457a6ee1acSGreg Roach echo 'width:', ($this->width - 1 - ($cP * 2)), 'pt;height:', $cH - 1, 'pt;'; 246a25f0a04SGreg Roach } else { 2477a6ee1acSGreg Roach echo 'width:', ($this->width - ($cP * 2)), 'pt;height:', $cH, 'pt;'; 248a25f0a04SGreg Roach } 2497a6ee1acSGreg Roach echo '">'; 250a25f0a04SGreg Roach 251a25f0a04SGreg Roach // Do a little "margin" trick before print 252a25f0a04SGreg Roach // to get the correct current position => "." 2537820e4d7SGreg Roach $cXT = $renderer->getX(); 2547820e4d7SGreg Roach $cYT = $renderer->getY(); 2557820e4d7SGreg Roach $renderer->setXy(0, 0); 256a25f0a04SGreg Roach 257a25f0a04SGreg Roach // Print the text elements 258a25f0a04SGreg Roach foreach ($this->elements as $element) { 259*d9a53aa7SGreg Roach if ($element instanceof ReportHtmlText) { 260*d9a53aa7SGreg Roach $element->render($renderer, false); 261*d9a53aa7SGreg Roach } elseif ($element instanceof ReportBaseElement) { 262*d9a53aa7SGreg Roach $element->render($renderer); 263044416d2SGreg Roach } elseif ($element === 'footnotetexts') { 2647820e4d7SGreg Roach $renderer->footnotes(); 265044416d2SGreg Roach } elseif ($element === 'addpage') { 2667820e4d7SGreg Roach $renderer->addPage(); 267a25f0a04SGreg Roach } 268a25f0a04SGreg Roach } 269a25f0a04SGreg Roach echo "</div>\n"; 270a25f0a04SGreg Roach 271a25f0a04SGreg Roach // Reset "margins" 2727820e4d7SGreg Roach $renderer->setXy($cXT, $cYT); 273a25f0a04SGreg Roach // This will be mostly used to trick the multiple images last height 274a25f0a04SGreg Roach if ($this->reseth) { 275a25f0a04SGreg Roach $cH = 0; 276a25f0a04SGreg Roach } 277a25f0a04SGreg Roach // New line and some clean up 278a25f0a04SGreg Roach if (!$this->newline) { 2797820e4d7SGreg Roach $renderer->setXy($cX + $this->width, $this->top); 280a25f0a04SGreg Roach $renderer->lastCellHeight = $cH; 281a25f0a04SGreg Roach } else { 2827820e4d7SGreg Roach $renderer->setXy(0, $this->top + $cH + ($cP * 2)); 283a25f0a04SGreg Roach $renderer->lastCellHeight = 0; 284a25f0a04SGreg Roach } 285a25f0a04SGreg Roach } 286a25f0a04SGreg Roach} 287