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 */ 16namespace Fisharebest\Webtrees\Module; 17 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Filter; 20use Fisharebest\Webtrees\Functions\FunctionsDate; 21use Fisharebest\Webtrees\I18N; 22use Fisharebest\Webtrees\Site; 23use Fisharebest\Webtrees\Stats; 24use Fisharebest\Webtrees\Tree; 25 26/** 27 * Class HtmlBlockModule 28 */ 29class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface { 30 /** {@inheritdoc} */ 31 public function getTitle() { 32 return /* I18N: Name of a module */ 33 I18N::translate('HTML'); 34 } 35 36 /** {@inheritdoc} */ 37 public function getDescription() { 38 return /* I18N: Description of the “HTML” module */ 39 I18N::translate('Add your own text and graphics.'); 40 } 41 42 /** 43 * Generate the HTML content of this block. 44 * 45 * @param Tree $tree 46 * @param int $block_id 47 * @param bool $template 48 * @param string[] $cfg 49 * 50 * @return string 51 */ 52 public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string { 53 global $ctype; 54 55 $title = $this->getBlockSetting($block_id, 'title', ''); 56 $content = $this->getBlockSetting($block_id, 'html', ''); 57 $gedcom = $this->getBlockSetting($block_id, 'gedcom'); 58 $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0'); 59 $languages = $this->getBlockSetting($block_id, 'languages'); 60 61 // Only show this block for certain languages 62 if ($languages && !in_array(WT_LOCALE, explode(',', $languages))) { 63 return ''; 64 } 65 66 /* 67 * Select GEDCOM 68 */ 69 switch ($gedcom) { 70 case '__current__': 71 $stats = new Stats($tree); 72 break; 73 case '__default__': 74 $tree = Tree::findByName(Site::getPreference('DEFAULT_GEDCOM')) ?? $tree; 75 $stats = new Stats($tree); 76 break; 77 default: 78 $tree = Tree::findByName($gedcom); 79 if ($tree) { 80 $stats = new Stats($tree); 81 } else { 82 $stats = new Stats($tree); 83 } 84 break; 85 } 86 87 /* 88 * Retrieve text, process embedded variables 89 */ 90 $title = $stats->embedTags($title); 91 $content = $stats->embedTags($content); 92 93 if ($show_timestamp === '1') { 94 $content .= '<br>' . FunctionsDate::formatTimestamp((int) $this->getBlockSetting($block_id, 'timestamp', WT_TIMESTAMP) + WT_TIMESTAMP_OFFSET); 95 } 96 97 if ($template) { 98 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 99 $config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]); 100 } elseif ($ctype === 'user' && Auth::check()) { 101 $config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]); 102 } else { 103 $config_url = ''; 104 } 105 106 return view('blocks/template', [ 107 'block' => str_replace('_', '-', $this->getName()), 108 'id' => $block_id, 109 'config_url' => $config_url, 110 'title' => $title, 111 'content' => $content, 112 ]); 113 } else { 114 return $content; 115 } 116 } 117 118 /** {@inheritdoc} */ 119 public function loadAjax(): bool { 120 return false; 121 } 122 123 /** {@inheritdoc} */ 124 public function isUserBlock(): bool { 125 return true; 126 } 127 128 /** {@inheritdoc} */ 129 public function isGedcomBlock(): bool { 130 return true; 131 } 132 133 /** 134 * An HTML form to edit block settings 135 * 136 * @param Tree $tree 137 * @param int $block_id 138 * 139 * @return void 140 */ 141 public function configureBlock(Tree $tree, int $block_id) { 142 if ($_SERVER['REQUEST_METHOD'] === 'POST') { 143 $languages = Filter::postArray('lang'); 144 $this->setBlockSetting($block_id, 'gedcom', Filter::post('gedcom')); 145 $this->setBlockSetting($block_id, 'title', Filter::post('title')); 146 $this->setBlockSetting($block_id, 'html', Filter::post('html')); 147 $this->setBlockSetting($block_id, 'show_timestamp', Filter::postBool('show_timestamp')); 148 $this->setBlockSetting($block_id, 'timestamp', Filter::post('timestamp')); 149 $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 150 151 return; 152 } 153 154 $templates = [ 155 I18N::translate('Keyword examples') => view('blocks/html-template-keywords', []), 156 I18N::translate('Narrative description') => view('blocks/html-template-narrative', []), 157 I18N::translate('Statistics') => view('blocks/html-template-statistics', []), 158 ]; 159 160 $title = $this->getBlockSetting($block_id, 'title', ''); 161 $html = $this->getBlockSetting($block_id, 'html', ''); 162 $gedcom = $this->getBlockSetting($block_id, 'gedcom', '__current__'); 163 $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0'); 164 $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 165 $all_trees = Tree::getNameList(); 166 167 echo view('blocks/html-config', [ 168 'all_trees' => $all_trees, 169 'gedcom' => $gedcom, 170 'html' => $html, 171 'languages' => $languages, 172 'show_timestamp' => $show_timestamp, 173 'templates' => $templates, 174 'title' => $title, 175 ]); 176 } 177} 178