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