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