18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1776692c8bSGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 1902fe1c37SGreg Roachuse Fisharebest\Webtrees\Http\Controllers\PedigreeChartController; 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 24a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 258c2e8227SGreg Roach 268c2e8227SGreg Roach/** 278c2e8227SGreg Roach * Class ChartsBlockModule 288c2e8227SGreg Roach */ 29c1010edaSGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleBlockInterface 30c1010edaSGreg Roach{ 318c2e8227SGreg Roach /** {@inheritdoc} */ 32*8f53f488SRico Sonntag public function getTitle(): string 33c1010edaSGreg Roach { 34bbb76c12SGreg Roach /* I18N: Name of a module/block */ 35bbb76c12SGreg Roach return I18N::translate('Charts'); 368c2e8227SGreg Roach } 378c2e8227SGreg Roach 388c2e8227SGreg Roach /** {@inheritdoc} */ 39*8f53f488SRico Sonntag public function getDescription(): string 40c1010edaSGreg Roach { 41bbb76c12SGreg Roach /* I18N: Description of the “Charts” module */ 42bbb76c12SGreg Roach return I18N::translate('An alternative way to display charts.'); 438c2e8227SGreg Roach } 448c2e8227SGreg Roach 4576692c8bSGreg Roach /** 4676692c8bSGreg Roach * Generate the HTML content of this block. 4776692c8bSGreg Roach * 48e490cd80SGreg Roach * @param Tree $tree 4976692c8bSGreg Roach * @param int $block_id 5076692c8bSGreg Roach * @param bool $template 51727f238cSGreg Roach * @param string[] $cfg 5276692c8bSGreg Roach * 5376692c8bSGreg Roach * @return string 5476692c8bSGreg Roach */ 55c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 56c1010edaSGreg Roach { 57e490cd80SGreg Roach global $ctype; 588c2e8227SGreg Roach 59e490cd80SGreg Roach $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 60e490cd80SGreg Roach $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 618c2e8227SGreg Roach 62e2a378d3SGreg Roach $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 63bd44f43fSGreg Roach $pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID); 648c2e8227SGreg Roach 65c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 668c2e8227SGreg Roach 67e490cd80SGreg Roach $person = Individual::getInstance($pid, $tree); 688c2e8227SGreg Roach if (!$person) { 698c2e8227SGreg Roach $pid = $PEDIGREE_ROOT_ID; 70e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'pid', $pid); 71e490cd80SGreg Roach $person = Individual::getInstance($pid, $tree); 728c2e8227SGreg Roach } 738c2e8227SGreg Roach 749c6524dcSGreg Roach $title = $this->getTitle(); 758c2e8227SGreg Roach 768c2e8227SGreg Roach if ($person) { 77f679d3acSGreg Roach $content = ''; 788c2e8227SGreg Roach switch ($type) { 798c2e8227SGreg Roach case 'pedigree': 809c6524dcSGreg Roach $title = I18N::translate('Pedigree of %s', $person->getFullName()); 8102fe1c37SGreg Roach $chart_url = route('pedigree-chart', [ 8202fe1c37SGreg Roach 'xref' => $person->getXref(), 8302fe1c37SGreg Roach 'ged' => $person->getTree()->getName(), 8402fe1c37SGreg Roach 'generations' => 3, 8502fe1c37SGreg Roach 'layout' => PedigreeChartController::PORTRAIT, 8602fe1c37SGreg Roach ]); 87147e99aaSGreg Roach $content = view('modules/charts/chart', [ 8802fe1c37SGreg Roach 'block_id' => $block_id, 8902fe1c37SGreg Roach 'chart_url' => $chart_url, 9002fe1c37SGreg Roach ]); 918c2e8227SGreg Roach break; 928c2e8227SGreg Roach case 'descendants': 939c6524dcSGreg Roach $title = I18N::translate('Descendants of %s', $person->getFullName()); 9402fe1c37SGreg Roach $chart_url = route('descendants-chart', [ 9502fe1c37SGreg Roach 'xref' => $person->getXref(), 9602fe1c37SGreg Roach 'ged' => $person->getTree()->getName(), 9702fe1c37SGreg Roach 'generations' => 2, 9802fe1c37SGreg Roach 'chart_style' => 0, 9902fe1c37SGreg Roach ]); 100147e99aaSGreg Roach $content = view('modules/charts/chart', [ 10102fe1c37SGreg Roach 'block_id' => $block_id, 10202fe1c37SGreg Roach 'chart_url' => $chart_url, 10302fe1c37SGreg Roach ]); 1048c2e8227SGreg Roach break; 1058c2e8227SGreg Roach case 'hourglass': 1069c6524dcSGreg Roach $title = I18N::translate('Hourglass chart of %s', $person->getFullName()); 10702fe1c37SGreg Roach $chart_url = route('hourglass-chart', [ 10802fe1c37SGreg Roach 'xref' => $person->getXref(), 10902fe1c37SGreg Roach 'ged' => $person->getTree()->getName(), 11002fe1c37SGreg Roach 'generations' => 2, 11102fe1c37SGreg Roach 'layout' => PedigreeChartController::PORTRAIT, 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; 1188c2e8227SGreg Roach case 'treenav': 1199c6524dcSGreg Roach $title = I18N::translate('Interactive tree of %s', $person->getFullName()); 1208c2e8227SGreg Roach $mod = new InteractiveTreeModule(WT_MODULES_DIR . 'tree'); 12159f2f229SGreg Roach $tv = new TreeView(); 122564ae2d7SGreg Roach $content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>'; 1238c2e8227SGreg Roach $content .= '<script src="' . $mod->js() . '"></script>'; 1248c2e8227SGreg Roach list($html, $js) = $tv->drawViewport($person, 2); 1258c2e8227SGreg Roach $content .= $html . '<script>' . $js . '</script>'; 1268c2e8227SGreg Roach break; 1278c2e8227SGreg Roach } 1288c2e8227SGreg Roach } else { 1296e7bf391SGreg Roach $content = I18N::translate('You must select an individual and a chart type in the block preferences'); 1308c2e8227SGreg Roach } 1318c2e8227SGreg Roach 1328c2e8227SGreg Roach if ($template) { 133e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 13402fe1c37SGreg Roach $config_url = route('tree-page-block-edit', [ 13502fe1c37SGreg Roach 'block_id' => $block_id, 136e490cd80SGreg Roach 'ged' => $tree->getName(), 13702fe1c37SGreg Roach ]); 138397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 13902fe1c37SGreg Roach $config_url = route('user-page-block-edit', [ 14002fe1c37SGreg Roach 'block_id' => $block_id, 141e490cd80SGreg Roach 'ged' => $tree->getName(), 14202fe1c37SGreg Roach ]); 1439c6524dcSGreg Roach } else { 1449c6524dcSGreg Roach $config_url = ''; 1459c6524dcSGreg Roach } 1469c6524dcSGreg Roach 147147e99aaSGreg Roach return view('modules/block-template', [ 1489c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1499c6524dcSGreg Roach 'id' => $block_id, 1509c6524dcSGreg Roach 'config_url' => $config_url, 151ea46934eSGreg Roach 'title' => strip_tags($title), 1529c6524dcSGreg Roach 'content' => $content, 1539c6524dcSGreg Roach ]); 1548c2e8227SGreg Roach } else { 1558c2e8227SGreg Roach return $content; 1568c2e8227SGreg Roach } 1578c2e8227SGreg Roach } 1588c2e8227SGreg Roach 1598c2e8227SGreg Roach /** {@inheritdoc} */ 160c1010edaSGreg Roach public function loadAjax(): bool 161c1010edaSGreg Roach { 1628c2e8227SGreg Roach return true; 1638c2e8227SGreg Roach } 1648c2e8227SGreg Roach 1658c2e8227SGreg Roach /** {@inheritdoc} */ 166c1010edaSGreg Roach public function isUserBlock(): bool 167c1010edaSGreg Roach { 1688c2e8227SGreg Roach return true; 1698c2e8227SGreg Roach } 1708c2e8227SGreg Roach 1718c2e8227SGreg Roach /** {@inheritdoc} */ 172c1010edaSGreg Roach public function isGedcomBlock(): bool 173c1010edaSGreg Roach { 1748c2e8227SGreg Roach return true; 1758c2e8227SGreg Roach } 1768c2e8227SGreg Roach 17776692c8bSGreg Roach /** 178a45f9889SGreg Roach * Update the configuration for a block. 179a45f9889SGreg Roach * 180a45f9889SGreg Roach * @param Request $request 181a45f9889SGreg Roach * @param int $block_id 182a45f9889SGreg Roach * 183a45f9889SGreg Roach * @return void 184a45f9889SGreg Roach */ 185a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 186a45f9889SGreg Roach { 187a45f9889SGreg Roach $this->setBlockSetting($block_id, 'type', $request->get('type', 'pedigree')); 188a45f9889SGreg Roach $this->setBlockSetting($block_id, 'pid', $request->get('pid', '')); 189a45f9889SGreg Roach } 190a45f9889SGreg Roach 191a45f9889SGreg Roach /** 19276692c8bSGreg Roach * An HTML form to edit block settings 19376692c8bSGreg Roach * 194e490cd80SGreg Roach * @param Tree $tree 19576692c8bSGreg Roach * @param int $block_id 196a9430be8SGreg Roach * 197988d5e9cSGreg Roach * @return void 19876692c8bSGreg Roach */ 199a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 200c1010edaSGreg Roach { 201e490cd80SGreg Roach $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 202e490cd80SGreg Roach $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 2038c2e8227SGreg Roach 204e2a378d3SGreg Roach $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 205bd44f43fSGreg Roach $pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID); 2068c2e8227SGreg Roach 20713abd6f3SGreg Roach $charts = [ 208fddfbb14SGreg Roach 'pedigree' => I18N::translate('Pedigree'), 209fddfbb14SGreg Roach 'descendants' => I18N::translate('Descendants'), 210fddfbb14SGreg Roach 'hourglass' => I18N::translate('Hourglass chart'), 211fddfbb14SGreg Roach 'treenav' => I18N::translate('Interactive tree'), 21213abd6f3SGreg Roach ]; 213fddfbb14SGreg Roach uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp'); 214fddfbb14SGreg Roach 215e490cd80SGreg Roach $individual = Individual::getInstance($pid, $tree); 216c385536dSGreg Roach 217147e99aaSGreg Roach echo view('modules/charts/config', [ 218c385536dSGreg Roach 'charts' => $charts, 219c385536dSGreg Roach 'individual' => $individual, 220e490cd80SGreg Roach 'tree' => $tree, 221c385536dSGreg Roach 'type' => $type, 222c385536dSGreg Roach ]); 2238c2e8227SGreg Roach } 2248c2e8227SGreg Roach} 225