xref: /webtrees/app/Module/RelationshipsChartModule.php (revision 76a5e7c78a07f5736a3cd5b7437c8f18dc196a5e)
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\PageController;
21use Fisharebest\Webtrees\Filter;
22use Fisharebest\Webtrees\FlashMessages;
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 . '&amp;pid2=' . $individual->getXref() . '&amp;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() . '&amp;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		echo Bootstrap4::breadcrumbs([
163			'admin.php'         => I18N::translate('Control panel'),
164			'admin_modules.php' => I18N::translate('Module administration'),
165		], $controller->getPageTitle());
166		?>
167
168		<h1><?= $controller->getPageTitle() ?></h1>
169
170		<p>
171			<?= 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><?= $tree->getTitleHtml() ?></h2>
177				<div class="row form-group">
178					<label class="col-sm-3 col-form-label" for="relationship-ancestors-<?= $tree->getTreeId() ?>">
179						<?= /* I18N: Configuration option */I18N::translate('Relationships') ?>
180					</label>
181					<div class="col-sm-9">
182						<?= Bootstrap4::select($this->ancestorsOptions(), $tree->getPreference('RELATIONSHIP_ANCESTORS', self::DEFAULT_ANCESTORS), ['id' => 'relationship-ancestors-' . $tree->getTreeId(), 'name' => 'relationship-ancestors-' . $tree->getTreeId()]) ?>
183					</div>
184				</div>
185
186				<fieldset class="form-group">
187					<div class="row">
188						<legend class="col-form-legend col-sm-3">
189							<?= /* I18N: Configuration option */I18N::translate('How much recursion to use when searching for relationships') ?>
190						</legend>
191						<div class="col-sm-9">
192							<?= Bootstrap4::radioButtons('relationship-recursion-' . $tree->getTreeId(), $this->recursionOptions(), $tree->getPreference('RELATIONSHIP_RECURSION', self::DEFAULT_RECURSION), true) ?>
193						</div>
194					</div>
195				</fieldset>
196			<?php endforeach ?>
197
198			<div class="row form-group">
199				<div class="offset-sm-3 col-sm-9">
200					<button type="submit" class="btn btn-primary">
201						<i class="fa fa-check"></i>
202						<?= I18N::translate('save') ?>
203					</button>
204				</div>
205			</div>
206		</form>
207		<?php
208	}
209
210	/**
211	 * Save updated configuration settings.
212	 */
213	private function saveConfig() {
214		if (Auth::isAdmin()) {
215			foreach (Tree::getAll() as $tree) {
216				$tree->setPreference('RELATIONSHIP_RECURSION', Filter::post('relationship-recursion-' . $tree->getTreeId()));
217				$tree->setPreference('RELATIONSHIP_ANCESTORS', Filter::post('relationship-ancestors-' . $tree->getTreeId()));
218			}
219
220			FlashMessages::addMessage(I18N::translate('The preferences for the chart “%s” have been updated.', $this->getTitle()), 'success');
221		}
222
223		header('Location: module.php?mod=' . $this->getName() . '&mod_action=admin');
224	}
225
226	/**
227	 * The URL to a page where the user can modify the configuration of this module.
228	 *
229	 * @return string
230	 */
231	public function getConfigLink() {
232		return 'module.php?mod=' . $this->getName() . '&amp;mod_action=admin';
233	}
234}
235