xref: /webtrees/app/Module/RelationshipsChartModule.php (revision 291c1b192cb6e1eb15ae13834707fd06a4b995ad)
1168ff6f3Sric2016<?php
2168ff6f3Sric2016/**
3168ff6f3Sric2016 * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
5168ff6f3Sric2016 * This program is free software: you can redistribute it and/or modify
6168ff6f3Sric2016 * it under the terms of the GNU General Public License as published by
7168ff6f3Sric2016 * the Free Software Foundation, either version 3 of the License, or
8168ff6f3Sric2016 * (at your option) any later version.
9168ff6f3Sric2016 * This program is distributed in the hope that it will be useful,
10168ff6f3Sric2016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11168ff6f3Sric2016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12168ff6f3Sric2016 * GNU General Public License for more details.
13168ff6f3Sric2016 * You should have received a copy of the GNU General Public License
14168ff6f3Sric2016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15168ff6f3Sric2016 */
16168ff6f3Sric2016namespace Fisharebest\Webtrees\Module;
17168ff6f3Sric2016
18168ff6f3Sric2016use Fisharebest\Webtrees\Auth;
1945ac604bSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
20168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
21168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
221e3273c9SGreg Roachuse Fisharebest\Webtrees\Menu;
2345ac604bSGreg Roachuse Fisharebest\Webtrees\Tree;
24*291c1b19SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
25*291c1b19SGreg Roachuse Symfony\Component\HttpFoundation\Request;
26*291c1b19SGreg Roachuse Symfony\Component\HttpFoundation\Response;
27168ff6f3Sric2016
28168ff6f3Sric2016/**
29168ff6f3Sric2016 * Class RelationshipsChartModule
30168ff6f3Sric2016 */
3145ac604bSGreg Roachclass RelationshipsChartModule extends AbstractModule implements ModuleConfigInterface, ModuleChartInterface {
321e3273c9SGreg Roach	/** It would be more correct to use PHP_INT_MAX, but this isn't friendly in URLs */
331e3273c9SGreg Roach	const UNLIMITED_RECURSION = 99;
341e3273c9SGreg Roach
351e3273c9SGreg Roach	/** By default new trees allow unlimited recursion */
361e3273c9SGreg Roach	const DEFAULT_RECURSION = self::UNLIMITED_RECURSION;
3745ac604bSGreg Roach
38e0bd7dc9SGreg Roach	/** By default new trees search for all relationships (not via ancestors) */
39e0bd7dc9SGreg Roach	const DEFAULT_ANCESTORS = 0;
40e0bd7dc9SGreg Roach
41168ff6f3Sric2016	/**
42168ff6f3Sric2016	 * How should this module be labelled on tabs, menus, etc.?
43168ff6f3Sric2016	 *
44168ff6f3Sric2016	 * @return string
45168ff6f3Sric2016	 */
46168ff6f3Sric2016	public function getTitle() {
47*291c1b19SGreg Roach		return /* I18N: Name of a module/chart */
48*291c1b19SGreg Roach			I18N::translate('Relationships');
49168ff6f3Sric2016	}
50168ff6f3Sric2016
51168ff6f3Sric2016	/**
52168ff6f3Sric2016	 * A sentence describing what this module does.
53168ff6f3Sric2016	 *
54168ff6f3Sric2016	 * @return string
55168ff6f3Sric2016	 */
56168ff6f3Sric2016	public function getDescription() {
57*291c1b19SGreg Roach		return /* I18N: Description of the “RelationshipsChart” module */
58*291c1b19SGreg Roach			I18N::translate('A chart displaying relationships between two individuals.');
59168ff6f3Sric2016	}
60168ff6f3Sric2016
61168ff6f3Sric2016	/**
62168ff6f3Sric2016	 * What is the default access level for this module?
63168ff6f3Sric2016	 *
64168ff6f3Sric2016	 * Some modules are aimed at admins or managers, and are not generally shown to users.
65168ff6f3Sric2016	 *
66168ff6f3Sric2016	 * @return int
67168ff6f3Sric2016	 */
68168ff6f3Sric2016	public function defaultAccessLevel() {
69168ff6f3Sric2016		return Auth::PRIV_PRIVATE;
70168ff6f3Sric2016	}
71168ff6f3Sric2016
72168ff6f3Sric2016	/**
73168ff6f3Sric2016	 * Return a menu item for this chart.
74168ff6f3Sric2016	 *
758e69695bSGreg Roach	 * @param Individual $individual
768e69695bSGreg Roach	 *
774eb71cfaSGreg Roach	 * @return Menu|null
78168ff6f3Sric2016	 */
79168ff6f3Sric2016	public function getChartMenu(Individual $individual) {
804eb71cfaSGreg Roach		$tree     = $individual->getTree();
812c689cc8SGreg Roach		$gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid', '');
82168ff6f3Sric2016
832c689cc8SGreg Roach		if ($gedcomid !== '') {
84168ff6f3Sric2016			return new Menu(
85168ff6f3Sric2016				I18N::translate('Relationship to me'),
862c689cc8SGreg Roach				e(route('relationships', ['xref1' => $gedcomid, 'xref2' => $individual->getXref(), 'ged' => $individual->getTree()->getName()])),
87168ff6f3Sric2016				'menu-chart-relationship',
8813abd6f3SGreg Roach				['rel' => 'nofollow']
89168ff6f3Sric2016			);
90168ff6f3Sric2016		} else {
91168ff6f3Sric2016			return new Menu(
92168ff6f3Sric2016				I18N::translate('Relationships'),
9307895620SGreg Roach				e(route('relationships', ['xref1' => $individual->getXref(), 'ged' => $individual->getTree()->getName()])),
94168ff6f3Sric2016				'menu-chart-relationship',
9513abd6f3SGreg Roach				['rel' => 'nofollow']
96168ff6f3Sric2016			);
97168ff6f3Sric2016		}
98168ff6f3Sric2016	}
99168ff6f3Sric2016
1004eb71cfaSGreg Roach	/**
1014eb71cfaSGreg Roach	 * Return a menu item for this chart - for use in individual boxes.
1024eb71cfaSGreg Roach	 *
1038e69695bSGreg Roach	 * @param Individual $individual
1048e69695bSGreg Roach	 *
1054eb71cfaSGreg Roach	 * @return Menu|null
1064eb71cfaSGreg Roach	 */
107168ff6f3Sric2016	public function getBoxChartMenu(Individual $individual) {
108168ff6f3Sric2016		return $this->getChartMenu($individual);
109168ff6f3Sric2016	}
11045ac604bSGreg Roach
111*291c1b19SGreg Roach
112*291c1b19SGreg Roach	/**
113*291c1b19SGreg Roach	 * The URL to a page where the user can modify the configuration of this module.
114*291c1b19SGreg Roach	 *
115*291c1b19SGreg Roach	 * @return string
116*291c1b19SGreg Roach	 */
117*291c1b19SGreg Roach	public function getConfigLink() {
118*291c1b19SGreg Roach		return route('module', ['module' => $this->getName(), 'action' => 'Admin']);
119*291c1b19SGreg Roach	}
120*291c1b19SGreg Roach
121*291c1b19SGreg Roach	/**
122*291c1b19SGreg Roach	 * @param Request $request
123*291c1b19SGreg Roach	 *
124*291c1b19SGreg Roach	 * @return Response
125*291c1b19SGreg Roach	 */
126*291c1b19SGreg Roach	public function getAdminAction(Request $request): Response {
127*291c1b19SGreg Roach		$this->layout = 'layouts/administration';
128*291c1b19SGreg Roach
129*291c1b19SGreg Roach		return $this->viewResponse('modules/relationships_chart/config', [
130*291c1b19SGreg Roach			'all_trees'         => Tree::getAll(),
131*291c1b19SGreg Roach			'ancestors_options' => $this->ancestorsOptions(),
132*291c1b19SGreg Roach			'default_ancestors' => self::DEFAULT_ANCESTORS,
133*291c1b19SGreg Roach			'default_recursion' => self::DEFAULT_RECURSION,
134*291c1b19SGreg Roach			'recursion_options' => $this->recursionOptions(),
135*291c1b19SGreg Roach			'title'             => I18N::translate('Chart preferences') . ' — ' . $this->getTitle(),
136*291c1b19SGreg Roach		]);
137*291c1b19SGreg Roach	}
138*291c1b19SGreg Roach
139*291c1b19SGreg Roach	/**
140*291c1b19SGreg Roach	 * @param Request $request
141*291c1b19SGreg Roach	 *
142*291c1b19SGreg Roach	 * @return RedirectResponse
143*291c1b19SGreg Roach	 */
144*291c1b19SGreg Roach	public function postAdminAction(Request $request): RedirectResponse {
145*291c1b19SGreg Roach		foreach (Tree::getAll() as $tree) {
146*291c1b19SGreg Roach			$tree->setPreference('RELATIONSHIP_RECURSION', $request->get('relationship-recursion-' . $tree->getTreeId()));
147*291c1b19SGreg Roach			$tree->setPreference('RELATIONSHIP_ANCESTORS', $request->get('relationship-ancestors-' . $tree->getTreeId()));
148*291c1b19SGreg Roach		}
149*291c1b19SGreg Roach
150*291c1b19SGreg Roach		FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->getTitle()), 'success');
151*291c1b19SGreg Roach
152*291c1b19SGreg Roach		return new RedirectResponse($this->getConfigLink());
153*291c1b19SGreg Roach	}
154*291c1b19SGreg Roach
15545ac604bSGreg Roach	/**
15645ac604bSGreg Roach	 * This is a general purpose hook, allowing modules to respond to routes
15745ac604bSGreg Roach	 * of the form module.php?mod=FOO&mod_action=BAR
15845ac604bSGreg Roach	 *
15945ac604bSGreg Roach	 * @param string $mod_action
16045ac604bSGreg Roach	 */
16145ac604bSGreg Roach	public function modAction($mod_action) {
16245ac604bSGreg Roach		switch ($mod_action) {
16345ac604bSGreg Roach			case 'admin':
16445ac604bSGreg Roach				if ($_SERVER['REQUEST_METHOD'] === 'POST') {
16545ac604bSGreg Roach					$this->saveConfig();
16645ac604bSGreg Roach				} else {
16745ac604bSGreg Roach					$this->editConfig();
16845ac604bSGreg Roach				}
16945ac604bSGreg Roach				break;
17045ac604bSGreg Roach			default:
17145ac604bSGreg Roach				http_response_code(404);
17245ac604bSGreg Roach		}
17345ac604bSGreg Roach	}
17445ac604bSGreg Roach
17545ac604bSGreg Roach	/**
176e0bd7dc9SGreg Roach	 * Possible options for the ancestors option
177e0bd7dc9SGreg Roach	 */
178e0bd7dc9SGreg Roach	private function ancestorsOptions() {
17913abd6f3SGreg Roach		return [
180e0bd7dc9SGreg Roach			0 => I18N::translate('Find any relationship'),
181e0bd7dc9SGreg Roach			1 => I18N::translate('Find relationships via ancestors'),
18213abd6f3SGreg Roach		];
183e0bd7dc9SGreg Roach	}
184e0bd7dc9SGreg Roach
185e0bd7dc9SGreg Roach	/**
1861e3273c9SGreg Roach	 * Possible options for the recursion option
1871e3273c9SGreg Roach	 */
1881e3273c9SGreg Roach	private function recursionOptions() {
18913abd6f3SGreg Roach		return [
1901e3273c9SGreg Roach			0                         => I18N::translate('none'),
1911e3273c9SGreg Roach			1                         => I18N::number(1),
1921e3273c9SGreg Roach			2                         => I18N::number(2),
1931e3273c9SGreg Roach			3                         => I18N::number(3),
194e0bd7dc9SGreg Roach			self::UNLIMITED_RECURSION => I18N::translate('unlimited'),
19513abd6f3SGreg Roach		];
1961e3273c9SGreg Roach	}
197168ff6f3Sric2016}
198