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