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\Controller\PageController; 20use Fisharebest\Webtrees\Filter; 21use Fisharebest\Webtrees\FlashMessages; 22use Fisharebest\Webtrees\Functions\FunctionsEdit; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Individual; 25use Fisharebest\Webtrees\Menu; 26use Fisharebest\Webtrees\Tree; 27 28/** 29 * Class RelationshipsChartModule 30 */ 31class RelationshipsChartModule extends AbstractModule implements ModuleConfigInterface, ModuleChartInterface { 32 /** It would be more correct to use PHP_INT_MAX, but this isn't friendly in URLs */ 33 const UNLIMITED_RECURSION = 99; 34 35 /** By default new trees allow unlimited recursion */ 36 const DEFAULT_RECURSION = self::UNLIMITED_RECURSION; 37 38 /** By default new trees search for all relationships (not via ancestors) */ 39 const DEFAULT_ANCESTORS = 0; 40 41 /** 42 * How should this module be labelled on tabs, menus, etc.? 43 * 44 * @return string 45 */ 46 public function getTitle() { 47 return /* I18N: Name of a module/chart */ I18N::translate('Relationships'); 48 } 49 50 /** 51 * A sentence describing what this module does. 52 * 53 * @return string 54 */ 55 public function getDescription() { 56 return /* I18N: Description of the “RelationshipsChart” module */ I18N::translate('A chart displaying relationships between two individuals.'); 57 } 58 59 /** 60 * What is the default access level for this module? 61 * 62 * Some modules are aimed at admins or managers, and are not generally shown to users. 63 * 64 * @return int 65 */ 66 public function defaultAccessLevel() { 67 return Auth::PRIV_PRIVATE; 68 } 69 70 /** 71 * Return a menu item for this chart. 72 * 73 * @param Individual $individual 74 * 75 * @return Menu|null 76 */ 77 public function getChartMenu(Individual $individual) { 78 $tree = $individual->getTree(); 79 $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 80 81 if ($gedcomid) { 82 return new Menu( 83 I18N::translate('Relationship to me'), 84 'relationship.php?pid1=' . $gedcomid . '&pid2=' . $individual->getXref() . '&ged=' . $tree->getNameUrl(), 85 'menu-chart-relationship', 86 ['rel' => 'nofollow'] 87 ); 88 } else { 89 return new Menu( 90 I18N::translate('Relationships'), 91 'relationship.php?pid1=' . $individual->getXref() . '&ged=' . $tree->getNameUrl(), 92 'menu-chart-relationship', 93 ['rel' => 'nofollow'] 94 ); 95 } 96 } 97 98 /** 99 * Return a menu item for this chart - for use in individual boxes. 100 * 101 * @param Individual $individual 102 * 103 * @return Menu|null 104 */ 105 public function getBoxChartMenu(Individual $individual) { 106 return $this->getChartMenu($individual); 107 } 108 109 /** 110 * This is a general purpose hook, allowing modules to respond to routes 111 * of the form module.php?mod=FOO&mod_action=BAR 112 * 113 * @param string $mod_action 114 */ 115 public function modAction($mod_action) { 116 switch ($mod_action) { 117 case 'admin': 118 if ($_SERVER['REQUEST_METHOD'] === 'POST') { 119 $this->saveConfig(); 120 } else { 121 $this->editConfig(); 122 } 123 break; 124 default: 125 http_response_code(404); 126 } 127 } 128 129 /** 130 * Possible options for the ancestors option 131 */ 132 private function ancestorsOptions() { 133 return [ 134 0 => I18N::translate('Find any relationship'), 135 1 => I18N::translate('Find relationships via ancestors'), 136 ]; 137 } 138 139 /** 140 * Possible options for the recursion option 141 */ 142 private function recursionOptions() { 143 return [ 144 0 => I18N::translate('none'), 145 1 => I18N::number(1), 146 2 => I18N::number(2), 147 3 => I18N::number(3), 148 self::UNLIMITED_RECURSION => I18N::translate('unlimited'), 149 ]; 150 } 151 152 /** 153 * Display a form to edit configuration settings. 154 */ 155 private function editConfig() { 156 $controller = new PageController; 157 $controller 158 ->restrictAccess(Auth::isAdmin()) 159 ->setPageTitle(I18N::translate('Chart preferences') . ' — ' . $this->getTitle()) 160 ->pageHeader(); 161 162 ?> 163 <ol class="breadcrumb small"> 164 <li><a href="admin.php"><?php echo I18N::translate('Control panel'); ?></a></li> 165 <li><a href="admin_modules.php"><?php echo I18N::translate('Module administration'); ?></a></li> 166 <li class="active"><?php echo $controller->getPageTitle(); ?></li> 167 </ol> 168 <h1><?php echo $controller->getPageTitle(); ?></h1> 169 170 <p> 171 <?php echo I18N::translate('Searching for all possible relationships can take a lot of time in complex trees.') ?> 172 </p> 173 174 <form method="post"> 175 <?php foreach (Tree::getAll() as $tree): ?> 176 <h2><?php echo $tree->getTitleHtml() ?></h2> 177 <div class="form-group"> 178 <label class="control-label col-sm-3" for="relationship-ancestors-<?php echo $tree->getTreeId() ?>"> 179 <?php echo /* I18N: Configuration option */I18N::translate('Relationships'); ?> 180 </label> 181 <div class="col-sm-9"> 182 <?php echo FunctionsEdit::selectEditControl('relationship-ancestors-' . $tree->getTreeId(), $this->ancestorsOptions(), null, $tree->getPreference('RELATIONSHIP_ANCESTORS', self::DEFAULT_ANCESTORS), 'class="form-control"'); ?> 183 </div> 184 </div> 185 186 <fieldset class="form-group"> 187 <legend class="control-label col-sm-3"> 188 <?php echo /* I18N: Configuration option */I18N::translate('How much recursion to use when searching for relationships'); ?> 189 </legend> 190 <div class="col-sm-9"> 191 <?php echo FunctionsEdit::radioButtons('relationship-recursion-' . $tree->getTreeId(), $this->recursionOptions(), $tree->getPreference('RELATIONSHIP_RECURSION', self::DEFAULT_RECURSION), 'class="radio-inline"'); ?> 192 </div> 193 </fieldset> 194 <?php endforeach; ?> 195 196 <div class="form-group"> 197 <div class="col-sm-offset-3 col-sm-9"> 198 <button type="submit" class="btn btn-primary"> 199 <i class="fa fa-check"></i> 200 <?php echo I18N::translate('save'); ?> 201 </button> 202 </div> 203 </div> 204 </form> 205 <?php 206 } 207 208 /** 209 * Save updated configuration settings. 210 */ 211 private function saveConfig() { 212 if (Auth::isAdmin()) { 213 foreach (Tree::getAll() as $tree) { 214 $tree->setPreference('RELATIONSHIP_RECURSION', Filter::post('relationship-recursion-' . $tree->getTreeId())); 215 $tree->setPreference('RELATIONSHIP_ANCESTORS', Filter::post('relationship-ancestors-' . $tree->getTreeId())); 216 } 217 218 FlashMessages::addMessage(I18N::translate('The preferences for the chart “%s” have been updated.', $this->getTitle()), 'success'); 219 } 220 221 header('Location: ' . WT_BASE_URL . 'module.php?mod=' . $this->getName() . '&mod_action=admin'); 222 } 223 224 /** 225 * The URL to a page where the user can modify the configuration of this module. 226 * 227 * @return string 228 */ 229 public function getConfigLink() { 230 return 'module.php?mod=' . $this->getName() . '&mod_action=admin'; 231 } 232} 233