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