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