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