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; 190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 2002fe1c37SGreg Roachuse Fisharebest\Webtrees\Http\Controllers\PedigreeChartController; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 24e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 258c2e8227SGreg Roach 268c2e8227SGreg Roach/** 278c2e8227SGreg Roach * Class ChartsBlockModule 288c2e8227SGreg Roach */ 29c1010edaSGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleBlockInterface 30c1010edaSGreg Roach{ 318c2e8227SGreg Roach /** {@inheritdoc} */ 32c1010edaSGreg Roach public function getTitle() 33c1010edaSGreg Roach { 3402fe1c37SGreg Roach return /* I18N: Name of a module/block */ 3502fe1c37SGreg Roach I18N::translate('Charts'); 368c2e8227SGreg Roach } 378c2e8227SGreg Roach 388c2e8227SGreg Roach /** {@inheritdoc} */ 39c1010edaSGreg Roach public function getDescription() 40c1010edaSGreg Roach { 4102fe1c37SGreg Roach return /* I18N: Description of the “Charts” module */ 4202fe1c37SGreg Roach 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'); 63*bd44f43fSGreg 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'); 1218c2e8227SGreg 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 /** 17876692c8bSGreg Roach * An HTML form to edit block settings 17976692c8bSGreg Roach * 180e490cd80SGreg Roach * @param Tree $tree 18176692c8bSGreg Roach * @param int $block_id 182a9430be8SGreg Roach * 183988d5e9cSGreg Roach * @return void 18476692c8bSGreg Roach */ 185c1010edaSGreg Roach public function configureBlock(Tree $tree, int $block_id) 186c1010edaSGreg Roach { 187e490cd80SGreg Roach $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 188e490cd80SGreg Roach $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 1898c2e8227SGreg Roach 190c385536dSGreg Roach if ($_SERVER['REQUEST_METHOD'] === 'POST') { 191e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'type', Filter::post('type', 'pedigree|descendants|hourglass|treenav', 'pedigree')); 192e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'pid', Filter::post('pid', WT_REGEX_XREF)); 193c385536dSGreg Roach 194c385536dSGreg Roach return; 1958c2e8227SGreg Roach } 1968c2e8227SGreg Roach 197e2a378d3SGreg Roach $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 198*bd44f43fSGreg Roach $pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID); 1998c2e8227SGreg Roach 20013abd6f3SGreg Roach $charts = [ 201fddfbb14SGreg Roach 'pedigree' => I18N::translate('Pedigree'), 202fddfbb14SGreg Roach 'descendants' => I18N::translate('Descendants'), 203fddfbb14SGreg Roach 'hourglass' => I18N::translate('Hourglass chart'), 204fddfbb14SGreg Roach 'treenav' => I18N::translate('Interactive tree'), 20513abd6f3SGreg Roach ]; 206fddfbb14SGreg Roach uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp'); 207fddfbb14SGreg Roach 208e490cd80SGreg Roach $individual = Individual::getInstance($pid, $tree); 209c385536dSGreg Roach 210147e99aaSGreg Roach echo view('modules/charts/config', [ 211c385536dSGreg Roach 'charts' => $charts, 212c385536dSGreg Roach 'individual' => $individual, 213e490cd80SGreg Roach 'tree' => $tree, 214c385536dSGreg Roach 'type' => $type, 215c385536dSGreg Roach ]); 2168c2e8227SGreg Roach } 2178c2e8227SGreg Roach} 218