1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\I18N; 22use Fisharebest\Webtrees\Individual; 23use Fisharebest\Webtrees\Module; 24use Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 25use Fisharebest\Webtrees\Services\ModuleService; 26use Fisharebest\Webtrees\Tree; 27use Symfony\Component\HttpFoundation\Request; 28 29/** 30 * Class ChartsBlockModule 31 */ 32class ChartsBlockModule extends AbstractModule implements ModuleBlockInterface 33{ 34 use ModuleBlockTrait; 35 36 /** 37 * @var ModuleService 38 */ 39 private $module_service; 40 41 /** 42 * ChartsBlockModule constructor. 43 * 44 * @param ModuleService $module_service 45 */ 46 public function __construct(ModuleService $module_service) { 47 $this->module_service = $module_service; 48 } 49 50 /** 51 * How should this module be labelled on tabs, menus, etc.? 52 * 53 * @return string 54 */ 55 public function title(): string 56 { 57 /* I18N: Name of a module/block */ 58 return I18N::translate('Charts'); 59 } 60 61 /** 62 * A sentence describing what this module does. 63 * 64 * @return string 65 */ 66 public function description(): string 67 { 68 /* I18N: Description of the “Charts” module */ 69 return I18N::translate('An alternative way to display charts.'); 70 } 71 72 /** 73 * Generate the HTML content of this block. 74 * 75 * @param Tree $tree 76 * @param int $block_id 77 * @param string $ctype 78 * @param string[] $cfg 79 * 80 * @return string 81 */ 82 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 83 { 84 $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 85 $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 86 87 $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 88 $pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID); 89 90 extract($cfg, EXTR_OVERWRITE); 91 92 $person = Individual::getInstance($pid, $tree); 93 if (!$person) { 94 $pid = $PEDIGREE_ROOT_ID; 95 $this->setBlockSetting($block_id, 'pid', $pid); 96 $person = Individual::getInstance($pid, $tree); 97 } 98 99 $title = $this->title(); 100 101 if ($person) { 102 switch ($type) { 103 default: 104 case 'pedigree': 105 /** @var PedigreeChartModule $module */ 106 $module = $this->module_service->findByClass(PedigreeChartModule::class); 107 $title = $module->chartTitle($person); 108 $chart_url = $module->chartUrl($person, [ 109 'ajax' => true, 110 'generations' => 3, 111 'layout' => PedigreeChartModule::PORTRAIT, 112 ]); 113 $content = view('modules/charts/chart', [ 114 'block_id' => $block_id, 115 'chart_url' => $chart_url, 116 ]); 117 break; 118 119 case 'descendants': 120 /** @var DescendancyChartModule $module */ 121 $module = $this->module_service->findByClass(DescendancyChartModule::class); 122 $title = $module->chartTitle($person); 123 $chart_url = $module->chartUrl($person, [ 124 'ajax' => true, 125 'generations' => 2, 126 'chart_style' => DescendancyChartModule::CHART_STYLE_LIST, 127 ]); 128 $content = view('modules/charts/chart', [ 129 'block_id' => $block_id, 130 'chart_url' => $chart_url, 131 ]); 132 break; 133 134 case 'hourglass': 135 /** @var HourglassChartModule $module */ 136 $module = $this->module_service->findByClass(HourglassChartModule::class); 137 $title = $module->chartTitle($person); 138 $chart_url = $module->chartUrl($person, [ 139 'ajax' => true, 140 'generations' => 2, 141 ]); 142 $content = view('modules/charts/chart', [ 143 'block_id' => $block_id, 144 'chart_url' => $chart_url, 145 ]); 146 break; 147 148 case 'treenav': 149 /** @var InteractiveTreeModule $module */ 150 $module = $this->module_service->findByClass(InteractiveTreeModule::class); 151 $title = I18N::translate('Interactive tree of %s', $person->getFullName()); 152 $tv = new TreeView(); 153 $content = '<script>$("head").append(\'<link rel="stylesheet" href="' . $module->css() . '" type="text/css" />\');</script>'; 154 $content .= '<script src="' . $module->js() . '"></script>'; 155 [$html, $js] = $tv->drawViewport($person, 2); 156 $content .= $html . '<script>' . $js . '</script>'; 157 break; 158 } 159 } else { 160 $content = I18N::translate('You must select an individual and a chart type in the block preferences'); 161 } 162 163 if ($ctype !== '') { 164 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 165 $config_url = route('tree-page-block-edit', [ 166 'block_id' => $block_id, 167 'ged' => $tree->name(), 168 ]); 169 } elseif ($ctype === 'user' && Auth::check()) { 170 $config_url = route('user-page-block-edit', [ 171 'block_id' => $block_id, 172 'ged' => $tree->name(), 173 ]); 174 } else { 175 $config_url = ''; 176 } 177 178 return view('modules/block-template', [ 179 'block' => str_replace('_', '-', $this->name()), 180 'id' => $block_id, 181 'config_url' => $config_url, 182 'title' => strip_tags($title), 183 'content' => $content, 184 ]); 185 } 186 187 return $content; 188 } 189 190 /** {@inheritdoc} */ 191 public function loadAjax(): bool 192 { 193 return true; 194 } 195 196 /** {@inheritdoc} */ 197 public function isUserBlock(): bool 198 { 199 return true; 200 } 201 202 /** {@inheritdoc} */ 203 public function isTreeBlock(): bool 204 { 205 return true; 206 } 207 208 /** 209 * Update the configuration for a block. 210 * 211 * @param Request $request 212 * @param int $block_id 213 * 214 * @return void 215 */ 216 public function saveBlockConfiguration(Request $request, int $block_id) 217 { 218 $this->setBlockSetting($block_id, 'type', $request->get('type', 'pedigree')); 219 $this->setBlockSetting($block_id, 'pid', $request->get('pid', '')); 220 } 221 222 /** 223 * An HTML form to edit block settings 224 * 225 * @param Tree $tree 226 * @param int $block_id 227 * 228 * @return void 229 */ 230 public function editBlockConfiguration(Tree $tree, int $block_id) 231 { 232 $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 233 $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 234 235 $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 236 $pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID); 237 238 $charts = [ 239 'pedigree' => I18N::translate('Pedigree'), 240 'descendants' => I18N::translate('Descendants'), 241 'hourglass' => I18N::translate('Hourglass chart'), 242 'treenav' => I18N::translate('Interactive tree'), 243 ]; 244 uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp'); 245 246 $individual = Individual::getInstance($pid, $tree); 247 248 echo view('modules/charts/config', [ 249 'charts' => $charts, 250 'individual' => $individual, 251 'tree' => $tree, 252 'type' => $type, 253 ]); 254 } 255} 256