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 */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1976692c8bSGreg Roach 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 2102fe1c37SGreg Roachuse Fisharebest\Webtrees\Http\Controllers\PedigreeChartController; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 268d0ebef0SGreg Roachuse Fisharebest\Webtrees\Webtrees; 27a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 288c2e8227SGreg Roach 298c2e8227SGreg Roach/** 308c2e8227SGreg Roach * Class ChartsBlockModule 318c2e8227SGreg Roach */ 32c1010edaSGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleBlockInterface 33c1010edaSGreg Roach{ 348c2e8227SGreg Roach /** {@inheritdoc} */ 358f53f488SRico Sonntag public function getTitle(): string 36c1010edaSGreg Roach { 37bbb76c12SGreg Roach /* I18N: Name of a module/block */ 38bbb76c12SGreg Roach return I18N::translate('Charts'); 398c2e8227SGreg Roach } 408c2e8227SGreg Roach 418c2e8227SGreg Roach /** {@inheritdoc} */ 428f53f488SRico Sonntag public function getDescription(): string 43c1010edaSGreg Roach { 44bbb76c12SGreg Roach /* I18N: Description of the “Charts” module */ 45bbb76c12SGreg Roach return I18N::translate('An alternative way to display charts.'); 468c2e8227SGreg Roach } 478c2e8227SGreg Roach 4876692c8bSGreg Roach /** 4976692c8bSGreg Roach * Generate the HTML content of this block. 5076692c8bSGreg Roach * 51e490cd80SGreg Roach * @param Tree $tree 5276692c8bSGreg Roach * @param int $block_id 53*5f2ae573SGreg Roach * @param string $ctype 54727f238cSGreg Roach * @param string[] $cfg 5576692c8bSGreg Roach * 5676692c8bSGreg Roach * @return string 5776692c8bSGreg Roach */ 58*5f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 59c1010edaSGreg Roach { 60e490cd80SGreg Roach $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 61e490cd80SGreg Roach $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 628c2e8227SGreg Roach 63e2a378d3SGreg Roach $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 64bd44f43fSGreg Roach $pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID); 658c2e8227SGreg Roach 66c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 678c2e8227SGreg Roach 68e490cd80SGreg Roach $person = Individual::getInstance($pid, $tree); 698c2e8227SGreg Roach if (!$person) { 708c2e8227SGreg Roach $pid = $PEDIGREE_ROOT_ID; 71e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'pid', $pid); 72e490cd80SGreg Roach $person = Individual::getInstance($pid, $tree); 738c2e8227SGreg Roach } 748c2e8227SGreg Roach 759c6524dcSGreg Roach $title = $this->getTitle(); 768c2e8227SGreg Roach 778c2e8227SGreg Roach if ($person) { 78f679d3acSGreg Roach $content = ''; 798c2e8227SGreg Roach switch ($type) { 808c2e8227SGreg Roach case 'pedigree': 819c6524dcSGreg Roach $title = I18N::translate('Pedigree of %s', $person->getFullName()); 8202fe1c37SGreg Roach $chart_url = route('pedigree-chart', [ 83c0935879SGreg Roach 'xref' => $person->xref(), 84f4afa648SGreg Roach 'ged' => $person->tree()->name(), 8502fe1c37SGreg Roach 'generations' => 3, 8602fe1c37SGreg Roach 'layout' => PedigreeChartController::PORTRAIT, 8702fe1c37SGreg Roach ]); 88147e99aaSGreg Roach $content = view('modules/charts/chart', [ 8902fe1c37SGreg Roach 'block_id' => $block_id, 9002fe1c37SGreg Roach 'chart_url' => $chart_url, 9102fe1c37SGreg Roach ]); 928c2e8227SGreg Roach break; 938c2e8227SGreg Roach case 'descendants': 949c6524dcSGreg Roach $title = I18N::translate('Descendants of %s', $person->getFullName()); 9502fe1c37SGreg Roach $chart_url = route('descendants-chart', [ 96c0935879SGreg Roach 'xref' => $person->xref(), 97f4afa648SGreg Roach 'ged' => $person->tree()->name(), 9802fe1c37SGreg Roach 'generations' => 2, 9902fe1c37SGreg Roach 'chart_style' => 0, 10002fe1c37SGreg Roach ]); 101147e99aaSGreg Roach $content = view('modules/charts/chart', [ 10202fe1c37SGreg Roach 'block_id' => $block_id, 10302fe1c37SGreg Roach 'chart_url' => $chart_url, 10402fe1c37SGreg Roach ]); 1058c2e8227SGreg Roach break; 1068c2e8227SGreg Roach case 'hourglass': 1079c6524dcSGreg Roach $title = I18N::translate('Hourglass chart of %s', $person->getFullName()); 10802fe1c37SGreg Roach $chart_url = route('hourglass-chart', [ 109c0935879SGreg Roach 'xref' => $person->xref(), 110f4afa648SGreg Roach 'ged' => $person->tree()->name(), 11102fe1c37SGreg Roach 'generations' => 2, 11202fe1c37SGreg Roach 'layout' => PedigreeChartController::PORTRAIT, 11302fe1c37SGreg Roach ]); 114147e99aaSGreg Roach $content = view('modules/charts/chart', [ 11502fe1c37SGreg Roach 'block_id' => $block_id, 11602fe1c37SGreg Roach 'chart_url' => $chart_url, 11702fe1c37SGreg Roach ]); 1188c2e8227SGreg Roach break; 1198c2e8227SGreg Roach case 'treenav': 1209c6524dcSGreg Roach $title = I18N::translate('Interactive tree of %s', $person->getFullName()); 1218d0ebef0SGreg Roach $mod = new InteractiveTreeModule(Webtrees::MODULES_PATH . 'tree'); 12259f2f229SGreg Roach $tv = new TreeView(); 123564ae2d7SGreg Roach $content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>'; 1248c2e8227SGreg Roach $content .= '<script src="' . $mod->js() . '"></script>'; 12565e02381SGreg Roach [$html, $js] = $tv->drawViewport($person, 2); 1268c2e8227SGreg Roach $content .= $html . '<script>' . $js . '</script>'; 1278c2e8227SGreg Roach break; 1288c2e8227SGreg Roach } 1298c2e8227SGreg Roach } else { 1306e7bf391SGreg Roach $content = I18N::translate('You must select an individual and a chart type in the block preferences'); 1318c2e8227SGreg Roach } 1328c2e8227SGreg Roach 133*5f2ae573SGreg Roach if ($ctype) { 134e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 13502fe1c37SGreg Roach $config_url = route('tree-page-block-edit', [ 13602fe1c37SGreg Roach 'block_id' => $block_id, 137aa6f03bbSGreg Roach 'ged' => $tree->name(), 13802fe1c37SGreg Roach ]); 139397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 14002fe1c37SGreg Roach $config_url = route('user-page-block-edit', [ 14102fe1c37SGreg Roach 'block_id' => $block_id, 142aa6f03bbSGreg Roach 'ged' => $tree->name(), 14302fe1c37SGreg Roach ]); 1449c6524dcSGreg Roach } else { 1459c6524dcSGreg Roach $config_url = ''; 1469c6524dcSGreg Roach } 1479c6524dcSGreg Roach 148147e99aaSGreg Roach return view('modules/block-template', [ 1499c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1509c6524dcSGreg Roach 'id' => $block_id, 1519c6524dcSGreg Roach 'config_url' => $config_url, 152ea46934eSGreg Roach 'title' => strip_tags($title), 1539c6524dcSGreg Roach 'content' => $content, 1549c6524dcSGreg Roach ]); 1558c2e8227SGreg Roach } 156b2ce94c6SRico Sonntag 157b2ce94c6SRico Sonntag return $content; 1588c2e8227SGreg Roach } 1598c2e8227SGreg Roach 1608c2e8227SGreg Roach /** {@inheritdoc} */ 161c1010edaSGreg Roach public function loadAjax(): bool 162c1010edaSGreg Roach { 1638c2e8227SGreg Roach return true; 1648c2e8227SGreg Roach } 1658c2e8227SGreg Roach 1668c2e8227SGreg Roach /** {@inheritdoc} */ 167c1010edaSGreg Roach public function isUserBlock(): bool 168c1010edaSGreg Roach { 1698c2e8227SGreg Roach return true; 1708c2e8227SGreg Roach } 1718c2e8227SGreg Roach 1728c2e8227SGreg Roach /** {@inheritdoc} */ 173c1010edaSGreg Roach public function isGedcomBlock(): bool 174c1010edaSGreg Roach { 1758c2e8227SGreg Roach return true; 1768c2e8227SGreg Roach } 1778c2e8227SGreg Roach 17876692c8bSGreg Roach /** 179a45f9889SGreg Roach * Update the configuration for a block. 180a45f9889SGreg Roach * 181a45f9889SGreg Roach * @param Request $request 182a45f9889SGreg Roach * @param int $block_id 183a45f9889SGreg Roach * 184a45f9889SGreg Roach * @return void 185a45f9889SGreg Roach */ 186a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 187a45f9889SGreg Roach { 188a45f9889SGreg Roach $this->setBlockSetting($block_id, 'type', $request->get('type', 'pedigree')); 189a45f9889SGreg Roach $this->setBlockSetting($block_id, 'pid', $request->get('pid', '')); 190a45f9889SGreg Roach } 191a45f9889SGreg Roach 192a45f9889SGreg Roach /** 19376692c8bSGreg Roach * An HTML form to edit block settings 19476692c8bSGreg Roach * 195e490cd80SGreg Roach * @param Tree $tree 19676692c8bSGreg Roach * @param int $block_id 197a9430be8SGreg Roach * 198988d5e9cSGreg Roach * @return void 19976692c8bSGreg Roach */ 200a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 201c1010edaSGreg Roach { 202e490cd80SGreg Roach $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 203e490cd80SGreg Roach $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 2048c2e8227SGreg Roach 205e2a378d3SGreg Roach $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 206bd44f43fSGreg Roach $pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID); 2078c2e8227SGreg Roach 20813abd6f3SGreg Roach $charts = [ 209fddfbb14SGreg Roach 'pedigree' => I18N::translate('Pedigree'), 210fddfbb14SGreg Roach 'descendants' => I18N::translate('Descendants'), 211fddfbb14SGreg Roach 'hourglass' => I18N::translate('Hourglass chart'), 212fddfbb14SGreg Roach 'treenav' => I18N::translate('Interactive tree'), 21313abd6f3SGreg Roach ]; 214fddfbb14SGreg Roach uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp'); 215fddfbb14SGreg Roach 216e490cd80SGreg Roach $individual = Individual::getInstance($pid, $tree); 217c385536dSGreg Roach 218147e99aaSGreg Roach echo view('modules/charts/config', [ 219c385536dSGreg Roach 'charts' => $charts, 220c385536dSGreg Roach 'individual' => $individual, 221e490cd80SGreg Roach 'tree' => $tree, 222c385536dSGreg Roach 'type' => $type, 223c385536dSGreg Roach ]); 2248c2e8227SGreg Roach } 2258c2e8227SGreg Roach} 226