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