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\I18N; 22use Fisharebest\Webtrees\Individual; 23use Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 24use Fisharebest\Webtrees\Services\ModuleService; 25use Fisharebest\Webtrees\Tree; 26use Illuminate\Support\Str; 27use Psr\Http\Message\ServerRequestInterface; 28 29/** 30 * Class ChartsBlockModule 31 */ 32class ChartsBlockModule extends AbstractModule implements ModuleBlockInterface 33{ 34 use ModuleBlockTrait; 35 36 /** 37 * @var ModuleService 38 */ 39 private $module_service; 40 41 /** 42 * ChartsBlockModule constructor. 43 * 44 * @param ModuleService $module_service 45 */ 46 public function __construct(ModuleService $module_service) 47 { 48 $this->module_service = $module_service; 49 } 50 51 /** 52 * How should this module be identified in the control panel, etc.? 53 * 54 * @return string 55 */ 56 public function title(): string 57 { 58 /* I18N: Name of a module/block */ 59 return I18N::translate('Charts'); 60 } 61 62 /** 63 * A sentence describing what this module does. 64 * 65 * @return string 66 */ 67 public function description(): string 68 { 69 /* I18N: Description of the “Charts” module */ 70 return I18N::translate('An alternative way to display charts.'); 71 } 72 73 /** 74 * Generate the HTML content of this block. 75 * 76 * @param Tree $tree 77 * @param int $block_id 78 * @param string $context 79 * @param string[] $config 80 * 81 * @return string 82 */ 83 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 84 { 85 $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 86 $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 87 $default_xref = $gedcomid ?: $PEDIGREE_ROOT_ID; 88 89 $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 90 $xref = $this->getBlockSetting($block_id, 'pid', $default_xref); 91 92 extract($config, EXTR_OVERWRITE); 93 94 $individual = Individual::getInstance($xref, $tree); 95 96 $title = $this->title(); 97 98 if ($individual instanceof Individual) { 99 switch ($type) { 100 default: 101 case 'pedigree': 102 /** @var PedigreeChartModule $module */ 103 $module = $this->module_service->findByInterface(PedigreeChartModule::class)->first(); 104 $title = $module->chartTitle($individual); 105 $chart_url = $module->chartUrl($individual, [ 106 'ajax' => true, 107 'generations' => 3, 108 'layout' => PedigreeChartModule::ORIENTATION_RIGHT, 109 ]); 110 $content = view('modules/charts/chart', [ 111 'block_id' => $block_id, 112 'chart_url' => $chart_url, 113 'class' => 'wt-chart-pedigree', 114 ]); 115 break; 116 117 case 'descendants': 118 /** @var DescendancyChartModule $module */ 119 $module = $this->module_service->findByInterface(DescendancyChartModule::class)->first(); 120 $title = $module->chartTitle($individual); 121 $chart_url = $module->chartUrl($individual, [ 122 'ajax' => true, 123 'generations' => 2, 124 'chart_style' => DescendancyChartModule::CHART_STYLE_TREE, 125 ]); 126 $content = view('modules/charts/chart', [ 127 'block_id' => $block_id, 128 'chart_url' => $chart_url, 129 'class' => 'wt-chart-descendants', 130 ]); 131 break; 132 133 case 'hourglass': 134 /** @var HourglassChartModule $module */ 135 $module = $this->module_service->findByInterface(HourglassChartModule::class)->first(); 136 $title = $module->chartTitle($individual); 137 $chart_url = $module->chartUrl($individual, [ 138 'ajax' => true, 139 'generations' => 2, 140 ]); 141 $content = view('modules/charts/chart', [ 142 'block_id' => $block_id, 143 'chart_url' => $chart_url, 144 'class' => 'wt-chart-hourglass', 145 ]); 146 break; 147 148 case 'treenav': 149 /** @var InteractiveTreeModule $module */ 150 $module = $this->module_service->findByInterface(InteractiveTreeModule::class)->first(); 151 $title = I18N::translate('Interactive tree of %s', $individual->fullName()); 152 $tv = new TreeView(); 153 [$html, $js] = $tv->drawViewport($individual, 2); 154 $content = $html . '<script>' . $js . '</script>'; 155 break; 156 } 157 } else { 158 $content = I18N::translate('You must select an individual and a chart type in the block preferences'); 159 } 160 161 if ($context !== self::CONTEXT_EMBED) { 162 return view('modules/block-template', [ 163 'block' => Str::kebab($this->name()), 164 'id' => $block_id, 165 'config_url' => $this->configUrl($tree, $context, $block_id), 166 'title' => strip_tags($title), 167 'content' => $content, 168 ]); 169 } 170 171 return $content; 172 } 173 174 /** 175 * Should this block load asynchronously using AJAX? 176 * 177 * Simple blocks are faster in-line, more complex ones can be loaded later. 178 * 179 * @return bool 180 */ 181 public function loadAjax(): bool 182 { 183 return true; 184 } 185 186 /** 187 * Can this block be shown on the user’s home page? 188 * 189 * @return bool 190 */ 191 public function isUserBlock(): bool 192 { 193 return true; 194 } 195 196 /** 197 * Can this block be shown on the tree’s home page? 198 * 199 * @return bool 200 */ 201 public function isTreeBlock(): bool 202 { 203 return true; 204 } 205 206 /** 207 * Update the configuration for a block. 208 * 209 * @param ServerRequestInterface $request 210 * @param int $block_id 211 * 212 * @return void 213 */ 214 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 215 { 216 $this->setBlockSetting($block_id, 'type', $request->getParsedBody()['type'] ?? 'pedigree'); 217 $this->setBlockSetting($block_id, 'pid', $request->getParsedBody()['xref'] ?? ''); 218 } 219 220 /** 221 * An HTML form to edit block settings 222 * 223 * @param Tree $tree 224 * @param int $block_id 225 * 226 * @return string 227 */ 228 public function editBlockConfiguration(Tree $tree, int $block_id): string 229 { 230 $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 231 $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 232 $default_xref = $gedcomid ?: $PEDIGREE_ROOT_ID; 233 234 $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 235 $xref = $this->getBlockSetting($block_id, 'pid', $default_xref); 236 237 $charts = [ 238 'pedigree' => I18N::translate('Pedigree'), 239 'descendants' => I18N::translate('Descendants'), 240 'hourglass' => I18N::translate('Hourglass chart'), 241 'treenav' => I18N::translate('Interactive tree'), 242 ]; 243 uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp'); 244 245 $individual = Individual::getInstance($xref, $tree); 246 247 return view('modules/charts/config', [ 248 'charts' => $charts, 249 'individual' => $individual, 250 'tree' => $tree, 251 'type' => $type, 252 ]); 253 } 254} 255