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