xref: /webtrees/app/Module/RelationshipsChartModule.php (revision 8f53f488f13e53e44dc48778e8f51ec9f91352dd)
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;
24291c1b19SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
25291c1b19SGreg Roachuse Symfony\Component\HttpFoundation\Request;
26291c1b19SGreg Roachuse Symfony\Component\HttpFoundation\Response;
27168ff6f3Sric2016
28168ff6f3Sric2016/**
29168ff6f3Sric2016 * Class RelationshipsChartModule
30168ff6f3Sric2016 */
31c1010edaSGreg Roachclass RelationshipsChartModule extends AbstractModule implements ModuleConfigInterface, ModuleChartInterface
32c1010edaSGreg Roach{
331e3273c9SGreg Roach    /** It would be more correct to use PHP_INT_MAX, but this isn't friendly in URLs */
341e3273c9SGreg Roach    const UNLIMITED_RECURSION = 99;
351e3273c9SGreg Roach
361e3273c9SGreg Roach    /** By default new trees allow unlimited recursion */
371e3273c9SGreg Roach    const DEFAULT_RECURSION = self::UNLIMITED_RECURSION;
3845ac604bSGreg Roach
39e0bd7dc9SGreg Roach    /** By default new trees search for all relationships (not via ancestors) */
40e0bd7dc9SGreg Roach    const DEFAULT_ANCESTORS = 0;
41e0bd7dc9SGreg Roach
42168ff6f3Sric2016    /**
43168ff6f3Sric2016     * How should this module be labelled on tabs, menus, etc.?
44168ff6f3Sric2016     *
45168ff6f3Sric2016     * @return string
46168ff6f3Sric2016     */
47*8f53f488SRico Sonntag    public function getTitle(): string
48c1010edaSGreg Roach    {
49bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
50bbb76c12SGreg Roach        return I18N::translate('Relationships');
51168ff6f3Sric2016    }
52168ff6f3Sric2016
53168ff6f3Sric2016    /**
54168ff6f3Sric2016     * A sentence describing what this module does.
55168ff6f3Sric2016     *
56168ff6f3Sric2016     * @return string
57168ff6f3Sric2016     */
58*8f53f488SRico Sonntag    public function getDescription(): string
59c1010edaSGreg Roach    {
60bbb76c12SGreg Roach        /* I18N: Description of the “RelationshipsChart” module */
61bbb76c12SGreg Roach        return I18N::translate('A chart displaying relationships between two individuals.');
62168ff6f3Sric2016    }
63168ff6f3Sric2016
64168ff6f3Sric2016    /**
65168ff6f3Sric2016     * What is the default access level for this module?
66168ff6f3Sric2016     *
67168ff6f3Sric2016     * Some modules are aimed at admins or managers, and are not generally shown to users.
68168ff6f3Sric2016     *
69168ff6f3Sric2016     * @return int
70168ff6f3Sric2016     */
71*8f53f488SRico Sonntag    public function defaultAccessLevel(): int
72c1010edaSGreg Roach    {
73168ff6f3Sric2016        return Auth::PRIV_PRIVATE;
74168ff6f3Sric2016    }
75168ff6f3Sric2016
76168ff6f3Sric2016    /**
77168ff6f3Sric2016     * Return a menu item for this chart.
78168ff6f3Sric2016     *
798e69695bSGreg Roach     * @param Individual $individual
808e69695bSGreg Roach     *
814eb71cfaSGreg Roach     * @return Menu|null
82168ff6f3Sric2016     */
83c1010edaSGreg Roach    public function getChartMenu(Individual $individual)
84c1010edaSGreg Roach    {
854eb71cfaSGreg Roach        $tree     = $individual->getTree();
867015ba1fSGreg Roach        $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid');
87168ff6f3Sric2016
882c689cc8SGreg Roach        if ($gedcomid !== '') {
89168ff6f3Sric2016            return new Menu(
90168ff6f3Sric2016                I18N::translate('Relationship to me'),
91c1010edaSGreg Roach                route('relationships', [
92c1010edaSGreg Roach                    'xref1' => $gedcomid,
93c1010edaSGreg Roach                    'xref2' => $individual->getXref(),
94c1010edaSGreg Roach                    'ged'   => $individual->getTree()->getName(),
95c1010edaSGreg Roach                ]),
96168ff6f3Sric2016                'menu-chart-relationship',
9713abd6f3SGreg Roach                ['rel' => 'nofollow']
98168ff6f3Sric2016            );
99168ff6f3Sric2016        } else {
100168ff6f3Sric2016            return new Menu(
101168ff6f3Sric2016                I18N::translate('Relationships'),
102c1010edaSGreg Roach                route('relationships', [
103c1010edaSGreg Roach                    'xref1' => $individual->getXref(),
104c1010edaSGreg Roach                    'ged'   => $individual->getTree()->getName(),
105c1010edaSGreg Roach                ]),
106168ff6f3Sric2016                'menu-chart-relationship',
10713abd6f3SGreg Roach                ['rel' => 'nofollow']
108168ff6f3Sric2016            );
109168ff6f3Sric2016        }
110168ff6f3Sric2016    }
111168ff6f3Sric2016
1124eb71cfaSGreg Roach    /**
1134eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1144eb71cfaSGreg Roach     *
1158e69695bSGreg Roach     * @param Individual $individual
1168e69695bSGreg Roach     *
1174eb71cfaSGreg Roach     * @return Menu|null
1184eb71cfaSGreg Roach     */
119c1010edaSGreg Roach    public function getBoxChartMenu(Individual $individual)
120c1010edaSGreg Roach    {
121168ff6f3Sric2016        return $this->getChartMenu($individual);
122168ff6f3Sric2016    }
12345ac604bSGreg Roach
124291c1b19SGreg Roach
125291c1b19SGreg Roach    /**
126291c1b19SGreg Roach     * The URL to a page where the user can modify the configuration of this module.
127291c1b19SGreg Roach     *
128291c1b19SGreg Roach     * @return string
129291c1b19SGreg Roach     */
130*8f53f488SRico Sonntag    public function getConfigLink(): string
131c1010edaSGreg Roach    {
132c1010edaSGreg Roach        return route('module', [
133c1010edaSGreg Roach            'module' => $this->getName(),
134c1010edaSGreg Roach            'action' => 'Admin',
135c1010edaSGreg Roach        ]);
136291c1b19SGreg Roach    }
137291c1b19SGreg Roach
138291c1b19SGreg Roach    /**
139291c1b19SGreg Roach     * @param Request $request
140291c1b19SGreg Roach     *
141291c1b19SGreg Roach     * @return Response
142291c1b19SGreg Roach     */
143c1010edaSGreg Roach    public function getAdminAction(Request $request): Response
144c1010edaSGreg Roach    {
145291c1b19SGreg Roach        $this->layout = 'layouts/administration';
146291c1b19SGreg Roach
147291c1b19SGreg Roach        return $this->viewResponse('modules/relationships_chart/config', [
148291c1b19SGreg Roach            'all_trees'         => Tree::getAll(),
149291c1b19SGreg Roach            'ancestors_options' => $this->ancestorsOptions(),
150291c1b19SGreg Roach            'default_ancestors' => self::DEFAULT_ANCESTORS,
151291c1b19SGreg Roach            'default_recursion' => self::DEFAULT_RECURSION,
152291c1b19SGreg Roach            'recursion_options' => $this->recursionOptions(),
153291c1b19SGreg Roach            'title'             => I18N::translate('Chart preferences') . ' — ' . $this->getTitle(),
154291c1b19SGreg Roach        ]);
155291c1b19SGreg Roach    }
156291c1b19SGreg Roach
157291c1b19SGreg Roach    /**
158291c1b19SGreg Roach     * @param Request $request
159291c1b19SGreg Roach     *
160291c1b19SGreg Roach     * @return RedirectResponse
161291c1b19SGreg Roach     */
162c1010edaSGreg Roach    public function postAdminAction(Request $request): RedirectResponse
163c1010edaSGreg Roach    {
164291c1b19SGreg Roach        foreach (Tree::getAll() as $tree) {
165291c1b19SGreg Roach            $tree->setPreference('RELATIONSHIP_RECURSION', $request->get('relationship-recursion-' . $tree->getTreeId()));
166291c1b19SGreg Roach            $tree->setPreference('RELATIONSHIP_ANCESTORS', $request->get('relationship-ancestors-' . $tree->getTreeId()));
167291c1b19SGreg Roach        }
168291c1b19SGreg Roach
169291c1b19SGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->getTitle()), 'success');
170291c1b19SGreg Roach
171291c1b19SGreg Roach        return new RedirectResponse($this->getConfigLink());
172291c1b19SGreg Roach    }
173291c1b19SGreg Roach
17445ac604bSGreg Roach    /**
175e0bd7dc9SGreg Roach     * Possible options for the ancestors option
176e0bd7dc9SGreg Roach     */
177c1010edaSGreg Roach    private function ancestorsOptions()
178c1010edaSGreg Roach    {
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     */
188c1010edaSGreg Roach    private function recursionOptions()
189c1010edaSGreg Roach    {
19013abd6f3SGreg Roach        return [
1911e3273c9SGreg Roach            0                         => I18N::translate('none'),
1921e3273c9SGreg Roach            1                         => I18N::number(1),
1931e3273c9SGreg Roach            2                         => I18N::number(2),
1941e3273c9SGreg Roach            3                         => I18N::number(3),
195e0bd7dc9SGreg Roach            self::UNLIMITED_RECURSION => I18N::translate('unlimited'),
19613abd6f3SGreg Roach        ];
1971e3273c9SGreg Roach    }
198168ff6f3Sric2016}
199