18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 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 1589f7189bSGreg Roach * along with this program. If not, see <https://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; 231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 27406008aaSGreg Roachuse Fisharebest\Webtrees\Registry; 284ca7e03cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 29e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 30748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator; 311e7a7a28SGreg Roachuse Illuminate\Support\Str; 326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 338c2e8227SGreg Roach 34406008aaSGreg Roachuse function extract; 35406008aaSGreg Roachuse function uasort; 36406008aaSGreg Roachuse function view; 37406008aaSGreg Roach 38406008aaSGreg Roachuse const EXTR_OVERWRITE; 39406008aaSGreg Roach 408c2e8227SGreg Roach/** 418c2e8227SGreg Roach * Class ChartsBlockModule 428c2e8227SGreg Roach */ 4337eb8894SGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleBlockInterface 44c1010edaSGreg Roach{ 4549a243cbSGreg Roach use ModuleBlockTrait; 4649a243cbSGreg Roach 4743f2f523SGreg Roach private ModuleService $module_service; 484ca7e03cSGreg Roach 494ca7e03cSGreg Roach /** 504ca7e03cSGreg Roach * @param ModuleService $module_service 514ca7e03cSGreg Roach */ 525bdbe281SGreg Roach public function __construct(ModuleService $module_service) 535bdbe281SGreg Roach { 544ca7e03cSGreg Roach $this->module_service = $module_service; 554ca7e03cSGreg Roach } 564ca7e03cSGreg Roach 574ca7e03cSGreg Roach /** 580cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 59961ec755SGreg Roach * 60961ec755SGreg Roach * @return string 61961ec755SGreg Roach */ 6249a243cbSGreg Roach public function title(): string 63c1010edaSGreg Roach { 64bbb76c12SGreg Roach /* I18N: Name of a module/block */ 65bbb76c12SGreg Roach return I18N::translate('Charts'); 668c2e8227SGreg Roach } 678c2e8227SGreg Roach 6849a243cbSGreg Roach public function description(): string 69c1010edaSGreg Roach { 70bbb76c12SGreg Roach /* I18N: Description of the “Charts” module */ 71bbb76c12SGreg Roach return I18N::translate('An alternative way to display charts.'); 728c2e8227SGreg Roach } 738c2e8227SGreg Roach 7476692c8bSGreg Roach /** 7576692c8bSGreg Roach * Generate the HTML content of this block. 7676692c8bSGreg Roach * 77e490cd80SGreg Roach * @param Tree $tree 7876692c8bSGreg Roach * @param int $block_id 793caaa4d2SGreg Roach * @param string $context 8076d39c55SGreg Roach * @param array<string,string> $config 8176692c8bSGreg Roach * 8276692c8bSGreg Roach * @return string 8376692c8bSGreg Roach */ 843caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 85c1010edaSGreg Roach { 86e490cd80SGreg Roach $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 871fe542e9SGreg Roach $gedcomid = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); 88575707d1SGreg Roach $default_xref = $gedcomid ?: $PEDIGREE_ROOT_ID; 898c2e8227SGreg Roach 90e2a378d3SGreg Roach $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 91575707d1SGreg Roach $xref = $this->getBlockSetting($block_id, 'pid', $default_xref); 928c2e8227SGreg Roach 933caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 948c2e8227SGreg Roach 956b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 968c2e8227SGreg Roach 9749a243cbSGreg Roach $title = $this->title(); 988c2e8227SGreg Roach 99575707d1SGreg Roach if ($individual instanceof Individual) { 1008c2e8227SGreg Roach switch ($type) { 101677aaceaSGreg Roach default: 1028c2e8227SGreg Roach case 'pedigree': 103ed9ba438SGreg Roach $module = $this->module_service->findByInterface(PedigreeChartModule::class)->first(); 104406008aaSGreg Roach if ($module instanceof PedigreeChartModule) { 105575707d1SGreg Roach $title = $module->chartTitle($individual); 106575707d1SGreg Roach $chart_url = $module->chartUrl($individual, [ 1079b5537c3SGreg Roach 'ajax' => true, 10802fe1c37SGreg Roach 'generations' => 3, 10971378461SGreg Roach 'layout' => PedigreeChartModule::STYLE_RIGHT, 11002fe1c37SGreg Roach ]); 111147e99aaSGreg Roach $content = view('modules/charts/chart', [ 11202fe1c37SGreg Roach 'block_id' => $block_id, 11302fe1c37SGreg Roach 'chart_url' => $chart_url, 11402fe1c37SGreg Roach ]); 115406008aaSGreg Roach } else { 116406008aaSGreg Roach $title = I18N::translate('Pedigree'); 117406008aaSGreg Roach $content = I18N::translate('The module “%s” has been disabled.', $title); 118406008aaSGreg Roach } 1198c2e8227SGreg Roach break; 120677aaceaSGreg Roach 1218c2e8227SGreg Roach case 'descendants': 122ed9ba438SGreg Roach $module = $this->module_service->findByInterface(DescendancyChartModule::class)->first(); 123406008aaSGreg Roach 124406008aaSGreg Roach if ($module instanceof DescendancyChartModule) { 125575707d1SGreg Roach $title = $module->chartTitle($individual); 126575707d1SGreg Roach $chart_url = $module->chartUrl($individual, [ 1279b5537c3SGreg Roach 'ajax' => true, 12802fe1c37SGreg Roach 'generations' => 2, 1290f1e0f10SGreg Roach 'chart_style' => DescendancyChartModule::CHART_STYLE_TREE, 13002fe1c37SGreg Roach ]); 131147e99aaSGreg Roach $content = view('modules/charts/chart', [ 13202fe1c37SGreg Roach 'block_id' => $block_id, 13302fe1c37SGreg Roach 'chart_url' => $chart_url, 13402fe1c37SGreg Roach ]); 135406008aaSGreg Roach } else { 136406008aaSGreg Roach $title = I18N::translate('Descendants'); 137406008aaSGreg Roach $content = I18N::translate('The module “%s” has been disabled.', $title); 138406008aaSGreg Roach } 139406008aaSGreg Roach 1408c2e8227SGreg Roach break; 141677aaceaSGreg Roach 1428c2e8227SGreg Roach case 'hourglass': 143ed9ba438SGreg Roach $module = $this->module_service->findByInterface(HourglassChartModule::class)->first(); 144406008aaSGreg Roach 145406008aaSGreg Roach if ($module instanceof HourglassChartModule) { 146575707d1SGreg Roach $title = $module->chartTitle($individual); 147575707d1SGreg Roach $chart_url = $module->chartUrl($individual, [ 1489b5537c3SGreg Roach 'ajax' => true, 14902fe1c37SGreg Roach 'generations' => 2, 15002fe1c37SGreg Roach ]); 151147e99aaSGreg Roach $content = view('modules/charts/chart', [ 15202fe1c37SGreg Roach 'block_id' => $block_id, 15302fe1c37SGreg Roach 'chart_url' => $chart_url, 15402fe1c37SGreg Roach ]); 155406008aaSGreg Roach } else { 156406008aaSGreg Roach $title = I18N::translate('Hourglass chart'); 157406008aaSGreg Roach $content = I18N::translate('The module “%s” has been disabled.', $title); 158406008aaSGreg Roach } 1598c2e8227SGreg Roach break; 160677aaceaSGreg Roach 1618c2e8227SGreg Roach case 'treenav': 16241f309deSGreg Roach $module = $this->module_service->findByInterface(InteractiveTreeModule::class)->first(); 163406008aaSGreg Roach 164406008aaSGreg Roach if ($module instanceof InteractiveTreeModule) { 165575707d1SGreg Roach $title = I18N::translate('Interactive tree of %s', $individual->fullName()); 16659f2f229SGreg Roach $tv = new TreeView(); 167575707d1SGreg Roach [$html, $js] = $tv->drawViewport($individual, 2); 168575707d1SGreg Roach $content = $html . '<script>' . $js . '</script>'; 169406008aaSGreg Roach } else { 170406008aaSGreg Roach $title = I18N::translate('Interactive tree'); 171406008aaSGreg Roach $content = I18N::translate('The module “%s” has been disabled.', $title); 172406008aaSGreg Roach } 173406008aaSGreg Roach 1748c2e8227SGreg Roach break; 1758c2e8227SGreg Roach } 1768c2e8227SGreg Roach } else { 1776e7bf391SGreg Roach $content = I18N::translate('You must select an individual and a chart type in the block preferences'); 1788c2e8227SGreg Roach } 1798c2e8227SGreg Roach 1803caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 181147e99aaSGreg Roach return view('modules/block-template', [ 1821e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1839c6524dcSGreg Roach 'id' => $block_id, 1843caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 1853f385873SGreg Roach 'title' => $title, 1869c6524dcSGreg Roach 'content' => $content, 1879c6524dcSGreg Roach ]); 1888c2e8227SGreg Roach } 189b2ce94c6SRico Sonntag 190b2ce94c6SRico Sonntag return $content; 1918c2e8227SGreg Roach } 1928c2e8227SGreg Roach 1933caaa4d2SGreg Roach /** 1943caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1953caaa4d2SGreg Roach * 1963caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1973caaa4d2SGreg Roach * 1983caaa4d2SGreg Roach * @return bool 1993caaa4d2SGreg Roach */ 200c1010edaSGreg Roach public function loadAjax(): bool 201c1010edaSGreg Roach { 2028c2e8227SGreg Roach return true; 2038c2e8227SGreg Roach } 2048c2e8227SGreg Roach 2053caaa4d2SGreg Roach /** 2063caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2073caaa4d2SGreg Roach * 2083caaa4d2SGreg Roach * @return bool 2093caaa4d2SGreg Roach */ 21063276d8fSGreg Roach public function isTreeBlock(): bool 211c1010edaSGreg Roach { 2128c2e8227SGreg Roach return true; 2138c2e8227SGreg Roach } 2148c2e8227SGreg Roach 21576692c8bSGreg Roach /** 216a45f9889SGreg Roach * Update the configuration for a block. 217a45f9889SGreg Roach * 2186ccdf4f0SGreg Roach * @param ServerRequestInterface $request 219a45f9889SGreg Roach * @param int $block_id 220a45f9889SGreg Roach * 221a45f9889SGreg Roach * @return void 222a45f9889SGreg Roach */ 2236ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 224a45f9889SGreg Roach { 225748dbe15SGreg Roach $type = Validator::parsedBody($request)->string('type'); 226748dbe15SGreg Roach $xref = Validator::parsedBody($request)->isXref()->string('xref'); 227b46c87bdSGreg Roach 228748dbe15SGreg Roach $this->setBlockSetting($block_id, 'type', $type); 229748dbe15SGreg Roach $this->setBlockSetting($block_id, 'pid', $xref); 230a45f9889SGreg Roach } 231a45f9889SGreg Roach 232a45f9889SGreg Roach /** 23376692c8bSGreg Roach * An HTML form to edit block settings 23476692c8bSGreg Roach * 235e490cd80SGreg Roach * @param Tree $tree 23676692c8bSGreg Roach * @param int $block_id 237a9430be8SGreg Roach * 2383caaa4d2SGreg Roach * @return string 23976692c8bSGreg Roach */ 2403caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 241c1010edaSGreg Roach { 242e490cd80SGreg Roach $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 2431fe542e9SGreg Roach $gedcomid = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); 244575707d1SGreg Roach $default_xref = $gedcomid ?: $PEDIGREE_ROOT_ID; 2458c2e8227SGreg Roach 246e2a378d3SGreg Roach $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 247575707d1SGreg Roach $xref = $this->getBlockSetting($block_id, 'pid', $default_xref); 2488c2e8227SGreg Roach 24913abd6f3SGreg Roach $charts = [ 250fddfbb14SGreg Roach 'pedigree' => I18N::translate('Pedigree'), 251fddfbb14SGreg Roach 'descendants' => I18N::translate('Descendants'), 252fddfbb14SGreg Roach 'hourglass' => I18N::translate('Hourglass chart'), 253fddfbb14SGreg Roach 'treenav' => I18N::translate('Interactive tree'), 25413abd6f3SGreg Roach ]; 25537646143SGreg Roach uasort($charts, I18N::comparator()); 256fddfbb14SGreg Roach 2576b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 258c385536dSGreg Roach 2593caaa4d2SGreg Roach return view('modules/charts/config', [ 260c385536dSGreg Roach 'charts' => $charts, 261c385536dSGreg Roach 'individual' => $individual, 262e490cd80SGreg Roach 'tree' => $tree, 263c385536dSGreg Roach 'type' => $type, 264c385536dSGreg Roach ]); 2658c2e8227SGreg Roach } 2668c2e8227SGreg Roach} 267