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