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 * ChartsBlockModule constructor. 514ca7e03cSGreg Roach * 524ca7e03cSGreg Roach * @param ModuleService $module_service 534ca7e03cSGreg Roach */ 545bdbe281SGreg Roach public function __construct(ModuleService $module_service) 555bdbe281SGreg Roach { 564ca7e03cSGreg Roach $this->module_service = $module_service; 574ca7e03cSGreg Roach } 584ca7e03cSGreg Roach 594ca7e03cSGreg Roach /** 600cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 61961ec755SGreg Roach * 62961ec755SGreg Roach * @return string 63961ec755SGreg Roach */ 6449a243cbSGreg Roach public function title(): string 65c1010edaSGreg Roach { 66bbb76c12SGreg Roach /* I18N: Name of a module/block */ 67bbb76c12SGreg Roach return I18N::translate('Charts'); 688c2e8227SGreg Roach } 698c2e8227SGreg Roach 70961ec755SGreg Roach /** 71961ec755SGreg Roach * A sentence describing what this module does. 72961ec755SGreg Roach * 73961ec755SGreg Roach * @return string 74961ec755SGreg Roach */ 7549a243cbSGreg Roach public function description(): string 76c1010edaSGreg Roach { 77bbb76c12SGreg Roach /* I18N: Description of the “Charts” module */ 78bbb76c12SGreg Roach return I18N::translate('An alternative way to display charts.'); 798c2e8227SGreg Roach } 808c2e8227SGreg Roach 8176692c8bSGreg Roach /** 8276692c8bSGreg Roach * Generate the HTML content of this block. 8376692c8bSGreg Roach * 84e490cd80SGreg Roach * @param Tree $tree 8576692c8bSGreg Roach * @param int $block_id 863caaa4d2SGreg Roach * @param string $context 8776d39c55SGreg Roach * @param array<string,string> $config 8876692c8bSGreg Roach * 8976692c8bSGreg Roach * @return string 9076692c8bSGreg Roach */ 913caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 92c1010edaSGreg Roach { 93e490cd80SGreg Roach $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 941fe542e9SGreg Roach $gedcomid = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); 95575707d1SGreg Roach $default_xref = $gedcomid ?: $PEDIGREE_ROOT_ID; 968c2e8227SGreg Roach 97e2a378d3SGreg Roach $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 98575707d1SGreg Roach $xref = $this->getBlockSetting($block_id, 'pid', $default_xref); 998c2e8227SGreg Roach 1003caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 1018c2e8227SGreg Roach 1026b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 1038c2e8227SGreg Roach 10449a243cbSGreg Roach $title = $this->title(); 1058c2e8227SGreg Roach 106575707d1SGreg Roach if ($individual instanceof Individual) { 1078c2e8227SGreg Roach switch ($type) { 108677aaceaSGreg Roach default: 1098c2e8227SGreg Roach case 'pedigree': 110ed9ba438SGreg Roach $module = $this->module_service->findByInterface(PedigreeChartModule::class)->first(); 111406008aaSGreg Roach if ($module instanceof PedigreeChartModule) { 112575707d1SGreg Roach $title = $module->chartTitle($individual); 113575707d1SGreg Roach $chart_url = $module->chartUrl($individual, [ 1149b5537c3SGreg Roach 'ajax' => true, 11502fe1c37SGreg Roach 'generations' => 3, 11671378461SGreg Roach 'layout' => PedigreeChartModule::STYLE_RIGHT, 11702fe1c37SGreg Roach ]); 118147e99aaSGreg Roach $content = view('modules/charts/chart', [ 11902fe1c37SGreg Roach 'block_id' => $block_id, 12002fe1c37SGreg Roach 'chart_url' => $chart_url, 12102fe1c37SGreg Roach ]); 122406008aaSGreg Roach } else { 123406008aaSGreg Roach $title = I18N::translate('Pedigree'); 124406008aaSGreg Roach $content = I18N::translate('The module “%s” has been disabled.', $title); 125406008aaSGreg Roach } 1268c2e8227SGreg Roach break; 127677aaceaSGreg Roach 1288c2e8227SGreg Roach case 'descendants': 129ed9ba438SGreg Roach $module = $this->module_service->findByInterface(DescendancyChartModule::class)->first(); 130406008aaSGreg Roach 131406008aaSGreg Roach if ($module instanceof DescendancyChartModule) { 132575707d1SGreg Roach $title = $module->chartTitle($individual); 133575707d1SGreg Roach $chart_url = $module->chartUrl($individual, [ 1349b5537c3SGreg Roach 'ajax' => true, 13502fe1c37SGreg Roach 'generations' => 2, 1360f1e0f10SGreg Roach 'chart_style' => DescendancyChartModule::CHART_STYLE_TREE, 13702fe1c37SGreg Roach ]); 138147e99aaSGreg Roach $content = view('modules/charts/chart', [ 13902fe1c37SGreg Roach 'block_id' => $block_id, 14002fe1c37SGreg Roach 'chart_url' => $chart_url, 14102fe1c37SGreg Roach ]); 142406008aaSGreg Roach } else { 143406008aaSGreg Roach $title = I18N::translate('Descendants'); 144406008aaSGreg Roach $content = I18N::translate('The module “%s” has been disabled.', $title); 145406008aaSGreg Roach } 146406008aaSGreg Roach 1478c2e8227SGreg Roach break; 148677aaceaSGreg Roach 1498c2e8227SGreg Roach case 'hourglass': 150ed9ba438SGreg Roach $module = $this->module_service->findByInterface(HourglassChartModule::class)->first(); 151406008aaSGreg Roach 152406008aaSGreg Roach if ($module instanceof HourglassChartModule) { 153575707d1SGreg Roach $title = $module->chartTitle($individual); 154575707d1SGreg Roach $chart_url = $module->chartUrl($individual, [ 1559b5537c3SGreg Roach 'ajax' => true, 15602fe1c37SGreg Roach 'generations' => 2, 15702fe1c37SGreg Roach ]); 158147e99aaSGreg Roach $content = view('modules/charts/chart', [ 15902fe1c37SGreg Roach 'block_id' => $block_id, 16002fe1c37SGreg Roach 'chart_url' => $chart_url, 16102fe1c37SGreg Roach ]); 162406008aaSGreg Roach } else { 163406008aaSGreg Roach $title = I18N::translate('Hourglass chart'); 164406008aaSGreg Roach $content = I18N::translate('The module “%s” has been disabled.', $title); 165406008aaSGreg Roach } 1668c2e8227SGreg Roach break; 167677aaceaSGreg Roach 1688c2e8227SGreg Roach case 'treenav': 16941f309deSGreg Roach $module = $this->module_service->findByInterface(InteractiveTreeModule::class)->first(); 170406008aaSGreg Roach 171406008aaSGreg Roach if ($module instanceof InteractiveTreeModule) { 172575707d1SGreg Roach $title = I18N::translate('Interactive tree of %s', $individual->fullName()); 17359f2f229SGreg Roach $tv = new TreeView(); 174575707d1SGreg Roach [$html, $js] = $tv->drawViewport($individual, 2); 175575707d1SGreg Roach $content = $html . '<script>' . $js . '</script>'; 176406008aaSGreg Roach } else { 177406008aaSGreg Roach $title = I18N::translate('Interactive tree'); 178406008aaSGreg Roach $content = I18N::translate('The module “%s” has been disabled.', $title); 179406008aaSGreg Roach } 180406008aaSGreg Roach 1818c2e8227SGreg Roach break; 1828c2e8227SGreg Roach } 1838c2e8227SGreg Roach } else { 1846e7bf391SGreg Roach $content = I18N::translate('You must select an individual and a chart type in the block preferences'); 1858c2e8227SGreg Roach } 1868c2e8227SGreg Roach 1873caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 188147e99aaSGreg Roach return view('modules/block-template', [ 1891e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1909c6524dcSGreg Roach 'id' => $block_id, 1913caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 1923f385873SGreg Roach 'title' => $title, 1939c6524dcSGreg Roach 'content' => $content, 1949c6524dcSGreg Roach ]); 1958c2e8227SGreg Roach } 196b2ce94c6SRico Sonntag 197b2ce94c6SRico Sonntag return $content; 1988c2e8227SGreg Roach } 1998c2e8227SGreg Roach 2003caaa4d2SGreg Roach /** 2013caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 2023caaa4d2SGreg Roach * 2033caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 2043caaa4d2SGreg Roach * 2053caaa4d2SGreg Roach * @return bool 2063caaa4d2SGreg Roach */ 207c1010edaSGreg Roach public function loadAjax(): bool 208c1010edaSGreg Roach { 2098c2e8227SGreg Roach return true; 2108c2e8227SGreg Roach } 2118c2e8227SGreg Roach 2123caaa4d2SGreg Roach /** 2133caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2143caaa4d2SGreg Roach * 2153caaa4d2SGreg Roach * @return bool 2163caaa4d2SGreg Roach */ 21763276d8fSGreg Roach public function isTreeBlock(): bool 218c1010edaSGreg Roach { 2198c2e8227SGreg Roach return true; 2208c2e8227SGreg Roach } 2218c2e8227SGreg Roach 22276692c8bSGreg Roach /** 223a45f9889SGreg Roach * Update the configuration for a block. 224a45f9889SGreg Roach * 2256ccdf4f0SGreg Roach * @param ServerRequestInterface $request 226a45f9889SGreg Roach * @param int $block_id 227a45f9889SGreg Roach * 228a45f9889SGreg Roach * @return void 229a45f9889SGreg Roach */ 2306ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 231a45f9889SGreg Roach { 232748dbe15SGreg Roach $type = Validator::parsedBody($request)->string('type'); 233748dbe15SGreg Roach $xref = Validator::parsedBody($request)->isXref()->string('xref'); 234b46c87bdSGreg Roach 235748dbe15SGreg Roach $this->setBlockSetting($block_id, 'type', $type); 236748dbe15SGreg Roach $this->setBlockSetting($block_id, 'pid', $xref); 237a45f9889SGreg Roach } 238a45f9889SGreg Roach 239a45f9889SGreg Roach /** 24076692c8bSGreg Roach * An HTML form to edit block settings 24176692c8bSGreg Roach * 242e490cd80SGreg Roach * @param Tree $tree 24376692c8bSGreg Roach * @param int $block_id 244a9430be8SGreg Roach * 2453caaa4d2SGreg Roach * @return string 24676692c8bSGreg Roach */ 2473caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 248c1010edaSGreg Roach { 249e490cd80SGreg Roach $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 2501fe542e9SGreg Roach $gedcomid = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); 251575707d1SGreg Roach $default_xref = $gedcomid ?: $PEDIGREE_ROOT_ID; 2528c2e8227SGreg Roach 253e2a378d3SGreg Roach $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 254575707d1SGreg Roach $xref = $this->getBlockSetting($block_id, 'pid', $default_xref); 2558c2e8227SGreg Roach 25613abd6f3SGreg Roach $charts = [ 257fddfbb14SGreg Roach 'pedigree' => I18N::translate('Pedigree'), 258fddfbb14SGreg Roach 'descendants' => I18N::translate('Descendants'), 259fddfbb14SGreg Roach 'hourglass' => I18N::translate('Hourglass chart'), 260fddfbb14SGreg Roach 'treenav' => I18N::translate('Interactive tree'), 26113abd6f3SGreg Roach ]; 26237646143SGreg Roach uasort($charts, I18N::comparator()); 263fddfbb14SGreg Roach 2646b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 265c385536dSGreg Roach 2663caaa4d2SGreg Roach return view('modules/charts/config', [ 267c385536dSGreg Roach 'charts' => $charts, 268c385536dSGreg Roach 'individual' => $individual, 269e490cd80SGreg Roach 'tree' => $tree, 270c385536dSGreg Roach 'type' => $type, 271c385536dSGreg Roach ]); 2728c2e8227SGreg Roach } 2738c2e8227SGreg Roach} 274