1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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 */ 16namespace Fisharebest\Webtrees\Module; 17 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Filter; 20use Fisharebest\Webtrees\Http\Controllers\PedigreeChartController; 21use Fisharebest\Webtrees\I18N; 22use Fisharebest\Webtrees\Individual; 23use Fisharebest\Webtrees\Module\InteractiveTree\TreeView; 24use Fisharebest\Webtrees\Tree; 25 26/** 27 * Class ChartsBlockModule 28 */ 29class ChartsBlockModule extends AbstractModule implements ModuleBlockInterface 30{ 31 /** {@inheritdoc} */ 32 public function getTitle() 33 { 34 /* I18N: Name of a module/block */ 35 return I18N::translate('Charts'); 36 } 37 38 /** {@inheritdoc} */ 39 public function getDescription() 40 { 41 /* I18N: Description of the “Charts” module */ 42 return I18N::translate('An alternative way to display charts.'); 43 } 44 45 /** 46 * Generate the HTML content of this block. 47 * 48 * @param Tree $tree 49 * @param int $block_id 50 * @param bool $template 51 * @param string[] $cfg 52 * 53 * @return string 54 */ 55 public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 56 { 57 global $ctype; 58 59 $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 60 $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 61 62 $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 63 $pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID); 64 65 extract($cfg, EXTR_OVERWRITE); 66 67 $person = Individual::getInstance($pid, $tree); 68 if (!$person) { 69 $pid = $PEDIGREE_ROOT_ID; 70 $this->setBlockSetting($block_id, 'pid', $pid); 71 $person = Individual::getInstance($pid, $tree); 72 } 73 74 $title = $this->getTitle(); 75 76 if ($person) { 77 $content = ''; 78 switch ($type) { 79 case 'pedigree': 80 $title = I18N::translate('Pedigree of %s', $person->getFullName()); 81 $chart_url = route('pedigree-chart', [ 82 'xref' => $person->getXref(), 83 'ged' => $person->getTree()->getName(), 84 'generations' => 3, 85 'layout' => PedigreeChartController::PORTRAIT, 86 ]); 87 $content = view('modules/charts/chart', [ 88 'block_id' => $block_id, 89 'chart_url' => $chart_url, 90 ]); 91 break; 92 case 'descendants': 93 $title = I18N::translate('Descendants of %s', $person->getFullName()); 94 $chart_url = route('descendants-chart', [ 95 'xref' => $person->getXref(), 96 'ged' => $person->getTree()->getName(), 97 'generations' => 2, 98 'chart_style' => 0, 99 ]); 100 $content = view('modules/charts/chart', [ 101 'block_id' => $block_id, 102 'chart_url' => $chart_url, 103 ]); 104 break; 105 case 'hourglass': 106 $title = I18N::translate('Hourglass chart of %s', $person->getFullName()); 107 $chart_url = route('hourglass-chart', [ 108 'xref' => $person->getXref(), 109 'ged' => $person->getTree()->getName(), 110 'generations' => 2, 111 'layout' => PedigreeChartController::PORTRAIT, 112 ]); 113 $content = view('modules/charts/chart', [ 114 'block_id' => $block_id, 115 'chart_url' => $chart_url, 116 ]); 117 break; 118 case 'treenav': 119 $title = I18N::translate('Interactive tree of %s', $person->getFullName()); 120 $mod = new InteractiveTreeModule(WT_MODULES_DIR . 'tree'); 121 $tv = new TreeView(); 122 $content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>'; 123 $content .= '<script src="' . $mod->js() . '"></script>'; 124 list($html, $js) = $tv->drawViewport($person, 2); 125 $content .= $html . '<script>' . $js . '</script>'; 126 break; 127 } 128 } else { 129 $content = I18N::translate('You must select an individual and a chart type in the block preferences'); 130 } 131 132 if ($template) { 133 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 134 $config_url = route('tree-page-block-edit', [ 135 'block_id' => $block_id, 136 'ged' => $tree->getName(), 137 ]); 138 } elseif ($ctype === 'user' && Auth::check()) { 139 $config_url = route('user-page-block-edit', [ 140 'block_id' => $block_id, 141 'ged' => $tree->getName(), 142 ]); 143 } else { 144 $config_url = ''; 145 } 146 147 return view('modules/block-template', [ 148 'block' => str_replace('_', '-', $this->getName()), 149 'id' => $block_id, 150 'config_url' => $config_url, 151 'title' => strip_tags($title), 152 'content' => $content, 153 ]); 154 } else { 155 return $content; 156 } 157 } 158 159 /** {@inheritdoc} */ 160 public function loadAjax(): bool 161 { 162 return true; 163 } 164 165 /** {@inheritdoc} */ 166 public function isUserBlock(): bool 167 { 168 return true; 169 } 170 171 /** {@inheritdoc} */ 172 public function isGedcomBlock(): bool 173 { 174 return true; 175 } 176 177 /** 178 * An HTML form to edit block settings 179 * 180 * @param Tree $tree 181 * @param int $block_id 182 * 183 * @return void 184 */ 185 public function configureBlock(Tree $tree, int $block_id) 186 { 187 $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); 188 $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 189 190 if ($_SERVER['REQUEST_METHOD'] === 'POST') { 191 $this->setBlockSetting($block_id, 'type', Filter::post('type', 'pedigree|descendants|hourglass|treenav', 'pedigree')); 192 $this->setBlockSetting($block_id, 'pid', Filter::post('pid', WT_REGEX_XREF)); 193 194 return; 195 } 196 197 $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); 198 $pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID); 199 200 $charts = [ 201 'pedigree' => I18N::translate('Pedigree'), 202 'descendants' => I18N::translate('Descendants'), 203 'hourglass' => I18N::translate('Hourglass chart'), 204 'treenav' => I18N::translate('Interactive tree'), 205 ]; 206 uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp'); 207 208 $individual = Individual::getInstance($pid, $tree); 209 210 echo view('modules/charts/config', [ 211 'charts' => $charts, 212 'individual' => $individual, 213 'tree' => $tree, 214 'type' => $type, 215 ]); 216 } 217} 218