18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1976692c8bSGreg Roach 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 23677aaceaSGreg Roachuse Fisharebest\Webtrees\Module; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 26a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 278c2e8227SGreg Roach 288c2e8227SGreg Roach/** 298c2e8227SGreg Roach * Class ChartsBlockModule 308c2e8227SGreg Roach */ 3149a243cbSGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface 32c1010edaSGreg Roach{ 3349a243cbSGreg Roach use ModuleBlockTrait; 3449a243cbSGreg Roach 35961ec755SGreg Roach /** 36961ec755SGreg Roach * How should this module be labelled on tabs, menus, etc.? 37961ec755SGreg Roach * 38961ec755SGreg Roach * @return string 39961ec755SGreg Roach */ 4049a243cbSGreg Roach public function title(): string 41c1010edaSGreg Roach { 42bbb76c12SGreg Roach /* I18N: Name of a module/block */ 43bbb76c12SGreg Roach return I18N::translate('Charts'); 448c2e8227SGreg Roach } 458c2e8227SGreg Roach 46961ec755SGreg Roach /** 47961ec755SGreg Roach * A sentence describing what this module does. 48961ec755SGreg Roach * 49961ec755SGreg Roach * @return string 50961ec755SGreg Roach */ 5149a243cbSGreg Roach public function description(): string 52c1010edaSGreg Roach { 53bbb76c12SGreg Roach /* I18N: Description of the “Charts” module */ 54bbb76c12SGreg Roach return I18N::translate('An alternative way to display charts.'); 558c2e8227SGreg Roach } 568c2e8227SGreg Roach 5776692c8bSGreg Roach /** 5876692c8bSGreg Roach * Generate the HTML content of this block. 5976692c8bSGreg Roach * 60e490cd80SGreg Roach * @param Tree $tree 6176692c8bSGreg Roach * @param int $block_id 625f2ae573SGreg Roach * @param string $ctype 63727f238cSGreg Roach * @param string[] $cfg 6476692c8bSGreg Roach * 6576692c8bSGreg Roach * @return string 6676692c8bSGreg Roach */ 675f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 68c1010edaSGreg Roach { 69e490cd80SGreg Roach $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 70e490cd80SGreg Roach $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 718c2e8227SGreg Roach 72e2a378d3SGreg Roach $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 73bd44f43fSGreg Roach $pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID); 748c2e8227SGreg Roach 75c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 768c2e8227SGreg Roach 77e490cd80SGreg Roach $person = Individual::getInstance($pid, $tree); 788c2e8227SGreg Roach if (!$person) { 798c2e8227SGreg Roach $pid = $PEDIGREE_ROOT_ID; 80e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'pid', $pid); 81e490cd80SGreg Roach $person = Individual::getInstance($pid, $tree); 828c2e8227SGreg Roach } 838c2e8227SGreg Roach 8449a243cbSGreg Roach $title = $this->title(); 858c2e8227SGreg Roach 868c2e8227SGreg Roach if ($person) { 878c2e8227SGreg Roach switch ($type) { 88677aaceaSGreg Roach default: 898c2e8227SGreg Roach case 'pedigree': 90d7f3769dSGreg Roach /** @var PedigreeChartModule $module */ 91677aaceaSGreg Roach $module = Module::getModuleByClassName(PedigreeChartModule::class); 92677aaceaSGreg Roach $title = $module->chartTitle($person); 93677aaceaSGreg Roach $chart_url = $module->chartUrl($person, [ 94*9b5537c3SGreg Roach 'ajax' => true, 9502fe1c37SGreg Roach 'generations' => 3, 96677aaceaSGreg Roach 'layout' => PedigreeChartModule::PORTRAIT, 9702fe1c37SGreg Roach ]); 98147e99aaSGreg Roach $content = view('modules/charts/chart', [ 9902fe1c37SGreg Roach 'block_id' => $block_id, 10002fe1c37SGreg Roach 'chart_url' => $chart_url, 10102fe1c37SGreg Roach ]); 1028c2e8227SGreg Roach break; 103677aaceaSGreg Roach 1048c2e8227SGreg Roach case 'descendants': 105d7f3769dSGreg Roach /** @var DescendancyChartModule $module */ 106677aaceaSGreg Roach $module = Module::getModuleByClassName(DescendancyChartModule::class); 107677aaceaSGreg Roach $title = $module->chartTitle($person); 108677aaceaSGreg Roach $chart_url = $module->chartUrl($person, [ 109*9b5537c3SGreg Roach 'ajax' => true, 11002fe1c37SGreg Roach 'generations' => 2, 111677aaceaSGreg Roach 'chart_style' => DescendancyChartModule::CHART_STYLE_LIST, 11202fe1c37SGreg Roach ]); 113147e99aaSGreg Roach $content = view('modules/charts/chart', [ 11402fe1c37SGreg Roach 'block_id' => $block_id, 11502fe1c37SGreg Roach 'chart_url' => $chart_url, 11602fe1c37SGreg Roach ]); 1178c2e8227SGreg Roach break; 118677aaceaSGreg Roach 1198c2e8227SGreg Roach case 'hourglass': 120d7f3769dSGreg Roach /** @var HourglassChartModule $module */ 121677aaceaSGreg Roach $module = Module::getModuleByClassName(HourglassChartModule::class); 122677aaceaSGreg Roach $title = $module->chartTitle($person); 123677aaceaSGreg Roach $chart_url = $module->chartUrl($person, [ 124*9b5537c3SGreg Roach 'ajax' => true, 12502fe1c37SGreg Roach 'generations' => 2, 12602fe1c37SGreg Roach ]); 127147e99aaSGreg Roach $content = view('modules/charts/chart', [ 12802fe1c37SGreg Roach 'block_id' => $block_id, 12902fe1c37SGreg Roach 'chart_url' => $chart_url, 13002fe1c37SGreg Roach ]); 1318c2e8227SGreg Roach break; 132677aaceaSGreg Roach 1338c2e8227SGreg Roach case 'treenav': 134d7f3769dSGreg Roach /** @var InteractiveTreeModule $module */ 135677aaceaSGreg Roach $module = Module::getModuleByClassName(InteractiveTreeModule::class); 1369c6524dcSGreg Roach $title = I18N::translate('Interactive tree of %s', $person->getFullName()); 13759f2f229SGreg Roach $tv = new TreeView(); 138677aaceaSGreg Roach $content = '<script>$("head").append(\'<link rel="stylesheet" href="' . $module->css() . '" type="text/css" />\');</script>'; 139677aaceaSGreg Roach $content .= '<script src="' . $module->js() . '"></script>'; 14065e02381SGreg Roach [$html, $js] = $tv->drawViewport($person, 2); 1418c2e8227SGreg Roach $content .= $html . '<script>' . $js . '</script>'; 1428c2e8227SGreg Roach break; 1438c2e8227SGreg Roach } 1448c2e8227SGreg Roach } else { 1456e7bf391SGreg Roach $content = I18N::translate('You must select an individual and a chart type in the block preferences'); 1468c2e8227SGreg Roach } 1478c2e8227SGreg Roach 1486a8879feSGreg Roach if ($ctype !== '') { 149e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 15002fe1c37SGreg Roach $config_url = route('tree-page-block-edit', [ 15102fe1c37SGreg Roach 'block_id' => $block_id, 152aa6f03bbSGreg Roach 'ged' => $tree->name(), 15302fe1c37SGreg Roach ]); 154397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 15502fe1c37SGreg Roach $config_url = route('user-page-block-edit', [ 15602fe1c37SGreg Roach 'block_id' => $block_id, 157aa6f03bbSGreg Roach 'ged' => $tree->name(), 15802fe1c37SGreg Roach ]); 1599c6524dcSGreg Roach } else { 1609c6524dcSGreg Roach $config_url = ''; 1619c6524dcSGreg Roach } 1629c6524dcSGreg Roach 163147e99aaSGreg Roach return view('modules/block-template', [ 16426684e68SGreg Roach 'block' => str_replace('_', '-', $this->name()), 1659c6524dcSGreg Roach 'id' => $block_id, 1669c6524dcSGreg Roach 'config_url' => $config_url, 167ea46934eSGreg Roach 'title' => strip_tags($title), 1689c6524dcSGreg Roach 'content' => $content, 1699c6524dcSGreg Roach ]); 1708c2e8227SGreg Roach } 171b2ce94c6SRico Sonntag 172b2ce94c6SRico Sonntag return $content; 1738c2e8227SGreg Roach } 1748c2e8227SGreg Roach 1758c2e8227SGreg Roach /** {@inheritdoc} */ 176c1010edaSGreg Roach public function loadAjax(): bool 177c1010edaSGreg Roach { 1788c2e8227SGreg Roach return true; 1798c2e8227SGreg Roach } 1808c2e8227SGreg Roach 1818c2e8227SGreg Roach /** {@inheritdoc} */ 182c1010edaSGreg Roach public function isUserBlock(): bool 183c1010edaSGreg Roach { 1848c2e8227SGreg Roach return true; 1858c2e8227SGreg Roach } 1868c2e8227SGreg Roach 1878c2e8227SGreg Roach /** {@inheritdoc} */ 188c1010edaSGreg Roach public function isGedcomBlock(): bool 189c1010edaSGreg Roach { 1908c2e8227SGreg Roach return true; 1918c2e8227SGreg Roach } 1928c2e8227SGreg Roach 19376692c8bSGreg Roach /** 194a45f9889SGreg Roach * Update the configuration for a block. 195a45f9889SGreg Roach * 196a45f9889SGreg Roach * @param Request $request 197a45f9889SGreg Roach * @param int $block_id 198a45f9889SGreg Roach * 199a45f9889SGreg Roach * @return void 200a45f9889SGreg Roach */ 201a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 202a45f9889SGreg Roach { 203a45f9889SGreg Roach $this->setBlockSetting($block_id, 'type', $request->get('type', 'pedigree')); 204a45f9889SGreg Roach $this->setBlockSetting($block_id, 'pid', $request->get('pid', '')); 205a45f9889SGreg Roach } 206a45f9889SGreg Roach 207a45f9889SGreg Roach /** 20876692c8bSGreg Roach * An HTML form to edit block settings 20976692c8bSGreg Roach * 210e490cd80SGreg Roach * @param Tree $tree 21176692c8bSGreg Roach * @param int $block_id 212a9430be8SGreg Roach * 213988d5e9cSGreg Roach * @return void 21476692c8bSGreg Roach */ 215a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 216c1010edaSGreg Roach { 217e490cd80SGreg Roach $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 218e490cd80SGreg Roach $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 2198c2e8227SGreg Roach 220e2a378d3SGreg Roach $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 221bd44f43fSGreg Roach $pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID); 2228c2e8227SGreg Roach 22313abd6f3SGreg Roach $charts = [ 224fddfbb14SGreg Roach 'pedigree' => I18N::translate('Pedigree'), 225fddfbb14SGreg Roach 'descendants' => I18N::translate('Descendants'), 226fddfbb14SGreg Roach 'hourglass' => I18N::translate('Hourglass chart'), 227fddfbb14SGreg Roach 'treenav' => I18N::translate('Interactive tree'), 22813abd6f3SGreg Roach ]; 229fddfbb14SGreg Roach uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp'); 230fddfbb14SGreg Roach 231e490cd80SGreg Roach $individual = Individual::getInstance($pid, $tree); 232c385536dSGreg Roach 233147e99aaSGreg Roach echo view('modules/charts/config', [ 234c385536dSGreg Roach 'charts' => $charts, 235c385536dSGreg Roach 'individual' => $individual, 236e490cd80SGreg Roach 'tree' => $tree, 237c385536dSGreg Roach 'type' => $type, 238c385536dSGreg Roach ]); 2398c2e8227SGreg Roach } 2408c2e8227SGreg Roach} 241