18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5a091ac74SGreg Roach * Copyright (C) 2020 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 23*6b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 274ca7e03cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 28e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 297c4add84SGreg Roachuse Fisharebest\Webtrees\User; 301e7a7a28SGreg Roachuse Illuminate\Support\Str; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 328c2e8227SGreg Roach 338c2e8227SGreg Roach/** 348c2e8227SGreg Roach * Class ChartsBlockModule 358c2e8227SGreg Roach */ 3637eb8894SGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleBlockInterface 37c1010edaSGreg Roach{ 3849a243cbSGreg Roach use ModuleBlockTrait; 3949a243cbSGreg Roach 40961ec755SGreg Roach /** 414ca7e03cSGreg Roach * @var ModuleService 424ca7e03cSGreg Roach */ 434ca7e03cSGreg Roach private $module_service; 444ca7e03cSGreg Roach 454ca7e03cSGreg Roach /** 464ca7e03cSGreg Roach * ChartsBlockModule constructor. 474ca7e03cSGreg Roach * 484ca7e03cSGreg Roach * @param ModuleService $module_service 494ca7e03cSGreg Roach */ 505bdbe281SGreg Roach public function __construct(ModuleService $module_service) 515bdbe281SGreg Roach { 524ca7e03cSGreg Roach $this->module_service = $module_service; 534ca7e03cSGreg Roach } 544ca7e03cSGreg Roach 554ca7e03cSGreg Roach /** 560cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 57961ec755SGreg Roach * 58961ec755SGreg Roach * @return string 59961ec755SGreg Roach */ 6049a243cbSGreg Roach public function title(): string 61c1010edaSGreg Roach { 62bbb76c12SGreg Roach /* I18N: Name of a module/block */ 63bbb76c12SGreg Roach return I18N::translate('Charts'); 648c2e8227SGreg Roach } 658c2e8227SGreg Roach 66961ec755SGreg Roach /** 67961ec755SGreg Roach * A sentence describing what this module does. 68961ec755SGreg Roach * 69961ec755SGreg Roach * @return string 70961ec755SGreg Roach */ 7149a243cbSGreg Roach public function description(): string 72c1010edaSGreg Roach { 73bbb76c12SGreg Roach /* I18N: Description of the “Charts” module */ 74bbb76c12SGreg Roach return I18N::translate('An alternative way to display charts.'); 758c2e8227SGreg Roach } 768c2e8227SGreg Roach 7776692c8bSGreg Roach /** 7876692c8bSGreg Roach * Generate the HTML content of this block. 7976692c8bSGreg Roach * 80e490cd80SGreg Roach * @param Tree $tree 8176692c8bSGreg Roach * @param int $block_id 823caaa4d2SGreg Roach * @param string $context 833caaa4d2SGreg Roach * @param string[] $config 8476692c8bSGreg Roach * 8576692c8bSGreg Roach * @return string 8676692c8bSGreg Roach */ 873caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 88c1010edaSGreg Roach { 89e490cd80SGreg Roach $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 907c4add84SGreg Roach $gedcomid = $tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF); 91575707d1SGreg Roach $default_xref = $gedcomid ?: $PEDIGREE_ROOT_ID; 928c2e8227SGreg Roach 93e2a378d3SGreg Roach $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 94575707d1SGreg Roach $xref = $this->getBlockSetting($block_id, 'pid', $default_xref); 958c2e8227SGreg Roach 963caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 978c2e8227SGreg Roach 98*6b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 998c2e8227SGreg Roach 10049a243cbSGreg Roach $title = $this->title(); 1018c2e8227SGreg Roach 102575707d1SGreg Roach if ($individual instanceof Individual) { 1038c2e8227SGreg Roach switch ($type) { 104677aaceaSGreg Roach default: 1058c2e8227SGreg Roach case 'pedigree': 106d7f3769dSGreg Roach /** @var PedigreeChartModule $module */ 107ed9ba438SGreg Roach $module = $this->module_service->findByInterface(PedigreeChartModule::class)->first(); 108575707d1SGreg Roach $title = $module->chartTitle($individual); 109575707d1SGreg Roach $chart_url = $module->chartUrl($individual, [ 1109b5537c3SGreg Roach 'ajax' => true, 11102fe1c37SGreg Roach 'generations' => 3, 11271378461SGreg Roach 'layout' => PedigreeChartModule::STYLE_RIGHT, 11302fe1c37SGreg Roach ]); 114147e99aaSGreg Roach $content = view('modules/charts/chart', [ 11502fe1c37SGreg Roach 'block_id' => $block_id, 11602fe1c37SGreg Roach 'chart_url' => $chart_url, 11741f309deSGreg Roach 'class' => 'wt-chart-pedigree', 11802fe1c37SGreg Roach ]); 1198c2e8227SGreg Roach break; 120677aaceaSGreg Roach 1218c2e8227SGreg Roach case 'descendants': 122d7f3769dSGreg Roach /** @var DescendancyChartModule $module */ 123ed9ba438SGreg Roach $module = $this->module_service->findByInterface(DescendancyChartModule::class)->first(); 124575707d1SGreg Roach $title = $module->chartTitle($individual); 125575707d1SGreg Roach $chart_url = $module->chartUrl($individual, [ 1269b5537c3SGreg Roach 'ajax' => true, 12702fe1c37SGreg Roach 'generations' => 2, 1280f1e0f10SGreg Roach 'chart_style' => DescendancyChartModule::CHART_STYLE_TREE, 12902fe1c37SGreg Roach ]); 130147e99aaSGreg Roach $content = view('modules/charts/chart', [ 13102fe1c37SGreg Roach 'block_id' => $block_id, 13202fe1c37SGreg Roach 'chart_url' => $chart_url, 13341f309deSGreg Roach 'class' => 'wt-chart-descendants', 13402fe1c37SGreg Roach ]); 1358c2e8227SGreg Roach break; 136677aaceaSGreg Roach 1378c2e8227SGreg Roach case 'hourglass': 138d7f3769dSGreg Roach /** @var HourglassChartModule $module */ 139ed9ba438SGreg Roach $module = $this->module_service->findByInterface(HourglassChartModule::class)->first(); 140575707d1SGreg Roach $title = $module->chartTitle($individual); 141575707d1SGreg Roach $chart_url = $module->chartUrl($individual, [ 1429b5537c3SGreg Roach 'ajax' => true, 14302fe1c37SGreg Roach 'generations' => 2, 14402fe1c37SGreg Roach ]); 145147e99aaSGreg Roach $content = view('modules/charts/chart', [ 14602fe1c37SGreg Roach 'block_id' => $block_id, 14702fe1c37SGreg Roach 'chart_url' => $chart_url, 14841f309deSGreg Roach 'class' => 'wt-chart-hourglass', 14902fe1c37SGreg Roach ]); 1508c2e8227SGreg Roach break; 151677aaceaSGreg Roach 1528c2e8227SGreg Roach case 'treenav': 153d7f3769dSGreg Roach /** @var InteractiveTreeModule $module */ 15441f309deSGreg Roach $module = $this->module_service->findByInterface(InteractiveTreeModule::class)->first(); 155575707d1SGreg Roach $title = I18N::translate('Interactive tree of %s', $individual->fullName()); 15659f2f229SGreg Roach $tv = new TreeView(); 157575707d1SGreg Roach [$html, $js] = $tv->drawViewport($individual, 2); 158575707d1SGreg Roach $content = $html . '<script>' . $js . '</script>'; 1598c2e8227SGreg Roach break; 1608c2e8227SGreg Roach } 1618c2e8227SGreg Roach } else { 1626e7bf391SGreg Roach $content = I18N::translate('You must select an individual and a chart type in the block preferences'); 1638c2e8227SGreg Roach } 1648c2e8227SGreg Roach 1653caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 166147e99aaSGreg Roach return view('modules/block-template', [ 1671e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1689c6524dcSGreg Roach 'id' => $block_id, 1693caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 170ea46934eSGreg Roach 'title' => strip_tags($title), 1719c6524dcSGreg Roach 'content' => $content, 1729c6524dcSGreg Roach ]); 1738c2e8227SGreg Roach } 174b2ce94c6SRico Sonntag 175b2ce94c6SRico Sonntag return $content; 1768c2e8227SGreg Roach } 1778c2e8227SGreg Roach 1783caaa4d2SGreg Roach /** 1793caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1803caaa4d2SGreg Roach * 1813caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1823caaa4d2SGreg Roach * 1833caaa4d2SGreg Roach * @return bool 1843caaa4d2SGreg Roach */ 185c1010edaSGreg Roach public function loadAjax(): bool 186c1010edaSGreg Roach { 1878c2e8227SGreg Roach return true; 1888c2e8227SGreg Roach } 1898c2e8227SGreg Roach 1903caaa4d2SGreg Roach /** 1913caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1923caaa4d2SGreg Roach * 1933caaa4d2SGreg Roach * @return bool 1943caaa4d2SGreg Roach */ 195c1010edaSGreg Roach public function isUserBlock(): bool 196c1010edaSGreg Roach { 1978c2e8227SGreg Roach return true; 1988c2e8227SGreg Roach } 1998c2e8227SGreg Roach 2003caaa4d2SGreg Roach /** 2013caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2023caaa4d2SGreg Roach * 2033caaa4d2SGreg Roach * @return bool 2043caaa4d2SGreg Roach */ 20563276d8fSGreg Roach public function isTreeBlock(): bool 206c1010edaSGreg Roach { 2078c2e8227SGreg Roach return true; 2088c2e8227SGreg Roach } 2098c2e8227SGreg Roach 21076692c8bSGreg Roach /** 211a45f9889SGreg Roach * Update the configuration for a block. 212a45f9889SGreg Roach * 2136ccdf4f0SGreg Roach * @param ServerRequestInterface $request 214a45f9889SGreg Roach * @param int $block_id 215a45f9889SGreg Roach * 216a45f9889SGreg Roach * @return void 217a45f9889SGreg Roach */ 2186ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 219a45f9889SGreg Roach { 220b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 221b46c87bdSGreg Roach 222b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'type', $params['type'] ?? 'pedigree'); 223b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'pid', $params['xref'] ?? ''); 224a45f9889SGreg Roach } 225a45f9889SGreg Roach 226a45f9889SGreg Roach /** 22776692c8bSGreg Roach * An HTML form to edit block settings 22876692c8bSGreg Roach * 229e490cd80SGreg Roach * @param Tree $tree 23076692c8bSGreg Roach * @param int $block_id 231a9430be8SGreg Roach * 2323caaa4d2SGreg Roach * @return string 23376692c8bSGreg Roach */ 2343caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 235c1010edaSGreg Roach { 236e490cd80SGreg Roach $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 2377c4add84SGreg Roach $gedcomid = $tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF); 238575707d1SGreg Roach $default_xref = $gedcomid ?: $PEDIGREE_ROOT_ID; 2398c2e8227SGreg Roach 240e2a378d3SGreg Roach $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 241575707d1SGreg Roach $xref = $this->getBlockSetting($block_id, 'pid', $default_xref); 2428c2e8227SGreg Roach 24313abd6f3SGreg Roach $charts = [ 244fddfbb14SGreg Roach 'pedigree' => I18N::translate('Pedigree'), 245fddfbb14SGreg Roach 'descendants' => I18N::translate('Descendants'), 246fddfbb14SGreg Roach 'hourglass' => I18N::translate('Hourglass chart'), 247fddfbb14SGreg Roach 'treenav' => I18N::translate('Interactive tree'), 24813abd6f3SGreg Roach ]; 249fddfbb14SGreg Roach uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp'); 250fddfbb14SGreg Roach 251*6b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 252c385536dSGreg Roach 2533caaa4d2SGreg Roach return view('modules/charts/config', [ 254c385536dSGreg Roach 'charts' => $charts, 255c385536dSGreg Roach 'individual' => $individual, 256e490cd80SGreg Roach 'tree' => $tree, 257c385536dSGreg Roach 'type' => $type, 258c385536dSGreg Roach ]); 2598c2e8227SGreg Roach } 2608c2e8227SGreg Roach} 261