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\InteractiveTree\TreeView; 24use Fisharebest\Webtrees\Services\ModuleService; 25use Fisharebest\Webtrees\Tree; 26use Illuminate\Support\Str; 27use Psr\Http\Message\ServerRequestInterface; 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 { 48 $this->module_service = $module_service; 49 } 50 51 /** 52 * How should this module be identified in the control panel, etc.? 53 * 54 * @return string 55 */ 56 public function title(): string 57 { 58 /* I18N: Name of a module/block */ 59 return I18N::translate('Charts'); 60 } 61 62 /** 63 * A sentence describing what this module does. 64 * 65 * @return string 66 */ 67 public function description(): string 68 { 69 /* I18N: Description of the “Charts” module */ 70 return I18N::translate('An alternative way to display charts.'); 71 } 72 73 /** 74 * Generate the HTML content of this block. 75 * 76 * @param Tree $tree 77 * @param int $block_id 78 * @param string $ctype 79 * @param string[] $cfg 80 * 81 * @return string 82 */ 83 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 84 { 85 $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 86 $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 87 88 $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 89 $pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID); 90 91 extract($cfg, EXTR_OVERWRITE); 92 93 $person = Individual::getInstance($pid, $tree); 94 if (!$person) { 95 $pid = $PEDIGREE_ROOT_ID; 96 $this->setBlockSetting($block_id, 'pid', $pid); 97 $person = Individual::getInstance($pid, $tree); 98 } 99 100 $title = $this->title(); 101 102 if ($person) { 103 switch ($type) { 104 default: 105 case 'pedigree': 106 /** @var PedigreeChartModule $module */ 107 $module = $this->module_service->findByInterface(PedigreeChartModule::class)->first(); 108 $title = $module->chartTitle($person); 109 $chart_url = $module->chartUrl($person, [ 110 'ajax' => true, 111 'generations' => 3, 112 'layout' => PedigreeChartModule::ORIENTATION_RIGHT, 113 ]); 114 $content = view('modules/charts/chart', [ 115 'block_id' => $block_id, 116 'chart_url' => $chart_url, 117 'class' => 'wt-chart-pedigree', 118 ]); 119 break; 120 121 case 'descendants': 122 /** @var DescendancyChartModule $module */ 123 $module = $this->module_service->findByInterface(DescendancyChartModule::class)->first(); 124 $title = $module->chartTitle($person); 125 $chart_url = $module->chartUrl($person, [ 126 'ajax' => true, 127 'generations' => 2, 128 'chart_style' => DescendancyChartModule::CHART_STYLE_TREE, 129 ]); 130 $content = view('modules/charts/chart', [ 131 'block_id' => $block_id, 132 'chart_url' => $chart_url, 133 'class' => 'wt-chart-descendants', 134 ]); 135 break; 136 137 case 'hourglass': 138 /** @var HourglassChartModule $module */ 139 $module = $this->module_service->findByInterface(HourglassChartModule::class)->first(); 140 $title = $module->chartTitle($person); 141 $chart_url = $module->chartUrl($person, [ 142 'ajax' => true, 143 'generations' => 2, 144 ]); 145 $content = view('modules/charts/chart', [ 146 'block_id' => $block_id, 147 'chart_url' => $chart_url, 148 'class' => 'wt-chart-hourglass', 149 ]); 150 break; 151 152 case 'treenav': 153 /** @var InteractiveTreeModule $module */ 154 $module = $this->module_service->findByInterface(InteractiveTreeModule::class)->first(); 155 $title = I18N::translate('Interactive tree of %s', $person->fullName()); 156 $tv = new TreeView(); 157 $content = '<script>$("head").append(\'<link rel="stylesheet" href="' . $module->css() . '" type="text/css" />\');</script>'; 158 $content .= '<script src="' . $module->js() . '"></script>'; 159 [$html, $js] = $tv->drawViewport($person, 2); 160 $content .= $html . '<script>' . $js . '</script>'; 161 break; 162 } 163 } else { 164 $content = I18N::translate('You must select an individual and a chart type in the block preferences'); 165 } 166 167 if ($ctype !== '') { 168 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 169 $config_url = route('tree-page-block-edit', [ 170 'block_id' => $block_id, 171 'ged' => $tree->name(), 172 ]); 173 } elseif ($ctype === 'user' && Auth::check()) { 174 $config_url = route('user-page-block-edit', [ 175 'block_id' => $block_id, 176 'ged' => $tree->name(), 177 ]); 178 } else { 179 $config_url = ''; 180 } 181 182 return view('modules/block-template', [ 183 'block' => Str::kebab($this->name()), 184 'id' => $block_id, 185 'config_url' => $config_url, 186 'title' => strip_tags($title), 187 'content' => $content, 188 ]); 189 } 190 191 return $content; 192 } 193 194 /** {@inheritdoc} */ 195 public function loadAjax(): bool 196 { 197 return true; 198 } 199 200 /** {@inheritdoc} */ 201 public function isUserBlock(): bool 202 { 203 return true; 204 } 205 206 /** {@inheritdoc} */ 207 public function isTreeBlock(): bool 208 { 209 return true; 210 } 211 212 /** 213 * Update the configuration for a block. 214 * 215 * @param ServerRequestInterface $request 216 * @param int $block_id 217 * 218 * @return void 219 */ 220 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 221 { 222 $this->setBlockSetting($block_id, 'type', $request->getQueryParams()['type'] ?? 'pedigree'); 223 $this->setBlockSetting($block_id, 'pid', $request->getQueryParams()['pid'] ?? ''); 224 } 225 226 /** 227 * An HTML form to edit block settings 228 * 229 * @param Tree $tree 230 * @param int $block_id 231 * 232 * @return void 233 */ 234 public function editBlockConfiguration(Tree $tree, int $block_id): void 235 { 236 $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 237 $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 238 239 $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 240 $pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID); 241 242 $charts = [ 243 'pedigree' => I18N::translate('Pedigree'), 244 'descendants' => I18N::translate('Descendants'), 245 'hourglass' => I18N::translate('Hourglass chart'), 246 'treenav' => I18N::translate('Interactive tree'), 247 ]; 248 uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp'); 249 250 $individual = Individual::getInstance($pid, $tree); 251 252 echo view('modules/charts/config', [ 253 'charts' => $charts, 254 'individual' => $individual, 255 'tree' => $tree, 256 'type' => $type, 257 ]); 258 } 259} 260