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 Symfony\Component\HttpFoundation\Request; 27 28/** 29 * Class ChartsBlockModule 30 */ 31class ChartsBlockModule extends AbstractModule implements ModuleBlockInterface 32{ 33 use ModuleBlockTrait; 34 35 /** 36 * @var ModuleService 37 */ 38 private $module_service; 39 40 /** 41 * ChartsBlockModule constructor. 42 * 43 * @param ModuleService $module_service 44 */ 45 public function __construct(ModuleService $module_service) 46 { 47 $this->module_service = $module_service; 48 } 49 50 /** 51 * How should this module be identified in the control panel, 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->findByInterface(PedigreeChartModule::class)->first(); 107 $title = $module->chartTitle($person); 108 $chart_url = $module->chartUrl($person, [ 109 'ajax' => true, 110 'generations' => 3, 111 'layout' => PedigreeChartModule::ORIENTATION_RIGHT, 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->findByInterface(DescendancyChartModule::class)->first(); 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->findByInterface(HourglassChartModule::class)->first(); 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->findByInterface(InteractiveTreeModule::class); 151 $title = I18N::translate('Interactive tree of %s', $person->fullName()); 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): void 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): void 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