xref: /webtrees/app/Module/RelationshipsChartModule.php (revision 11a0010435a453eda73cbccf8d1b203c75c28b87)
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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
18168ff6f3Sric2016namespace Fisharebest\Webtrees\Module;
19168ff6f3Sric2016
20168ff6f3Sric2016use Fisharebest\Webtrees\Auth;
2145ac604bSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
22168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
23168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
241e3273c9SGreg Roachuse Fisharebest\Webtrees\Menu;
2545ac604bSGreg Roachuse Fisharebest\Webtrees\Tree;
26291c1b19SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
27291c1b19SGreg Roachuse Symfony\Component\HttpFoundation\Request;
28291c1b19SGreg Roachuse Symfony\Component\HttpFoundation\Response;
29168ff6f3Sric2016
30168ff6f3Sric2016/**
31168ff6f3Sric2016 * Class RelationshipsChartModule
32168ff6f3Sric2016 */
33c1010edaSGreg Roachclass RelationshipsChartModule extends AbstractModule implements ModuleConfigInterface, ModuleChartInterface
34c1010edaSGreg Roach{
351e3273c9SGreg Roach    /** It would be more correct to use PHP_INT_MAX, but this isn't friendly in URLs */
36*11a00104SGreg Roach    const UNLIMITED_RECURSION = 99;
371e3273c9SGreg Roach
381e3273c9SGreg Roach    /** By default new trees allow unlimited recursion */
39*11a00104SGreg Roach    const DEFAULT_RECURSION = '99';
4045ac604bSGreg Roach
41e0bd7dc9SGreg Roach    /** By default new trees search for all relationships (not via ancestors) */
42abe291acSGreg Roach    const DEFAULT_ANCESTORS = '0';
43e0bd7dc9SGreg Roach
44168ff6f3Sric2016    /**
45168ff6f3Sric2016     * How should this module be labelled on tabs, menus, etc.?
46168ff6f3Sric2016     *
47168ff6f3Sric2016     * @return string
48168ff6f3Sric2016     */
498f53f488SRico Sonntag    public function getTitle(): string
50c1010edaSGreg Roach    {
51bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
52bbb76c12SGreg Roach        return I18N::translate('Relationships');
53168ff6f3Sric2016    }
54168ff6f3Sric2016
55168ff6f3Sric2016    /**
56168ff6f3Sric2016     * A sentence describing what this module does.
57168ff6f3Sric2016     *
58168ff6f3Sric2016     * @return string
59168ff6f3Sric2016     */
608f53f488SRico Sonntag    public function getDescription(): string
61c1010edaSGreg Roach    {
62bbb76c12SGreg Roach        /* I18N: Description of the “RelationshipsChart” module */
63bbb76c12SGreg Roach        return I18N::translate('A chart displaying relationships between two individuals.');
64168ff6f3Sric2016    }
65168ff6f3Sric2016
66168ff6f3Sric2016    /**
67168ff6f3Sric2016     * What is the default access level for this module?
68168ff6f3Sric2016     *
69168ff6f3Sric2016     * Some modules are aimed at admins or managers, and are not generally shown to users.
70168ff6f3Sric2016     *
71168ff6f3Sric2016     * @return int
72168ff6f3Sric2016     */
738f53f488SRico Sonntag    public function defaultAccessLevel(): int
74c1010edaSGreg Roach    {
75168ff6f3Sric2016        return Auth::PRIV_PRIVATE;
76168ff6f3Sric2016    }
77168ff6f3Sric2016
78168ff6f3Sric2016    /**
79168ff6f3Sric2016     * Return a menu item for this chart.
80168ff6f3Sric2016     *
818e69695bSGreg Roach     * @param Individual $individual
828e69695bSGreg Roach     *
834eb71cfaSGreg Roach     * @return Menu|null
84168ff6f3Sric2016     */
85c1010edaSGreg Roach    public function getChartMenu(Individual $individual)
86c1010edaSGreg Roach    {
874eb71cfaSGreg Roach        $tree     = $individual->getTree();
887015ba1fSGreg Roach        $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid');
89168ff6f3Sric2016
902c689cc8SGreg Roach        if ($gedcomid !== '') {
91168ff6f3Sric2016            return new Menu(
92168ff6f3Sric2016                I18N::translate('Relationship to me'),
93c1010edaSGreg Roach                route('relationships', [
94c1010edaSGreg Roach                    'xref1' => $gedcomid,
95c1010edaSGreg Roach                    'xref2' => $individual->getXref(),
96c1010edaSGreg Roach                    'ged'   => $individual->getTree()->getName(),
97c1010edaSGreg Roach                ]),
98168ff6f3Sric2016                'menu-chart-relationship',
9913abd6f3SGreg Roach                ['rel' => 'nofollow']
100168ff6f3Sric2016            );
101b2ce94c6SRico Sonntag        }
102b2ce94c6SRico Sonntag
103168ff6f3Sric2016        return new Menu(
104168ff6f3Sric2016            I18N::translate('Relationships'),
105c1010edaSGreg Roach            route('relationships', [
106c1010edaSGreg Roach                'xref1' => $individual->getXref(),
107c1010edaSGreg Roach                'ged'   => $individual->getTree()->getName(),
108c1010edaSGreg Roach            ]),
109168ff6f3Sric2016            'menu-chart-relationship',
11013abd6f3SGreg Roach            ['rel' => 'nofollow']
111168ff6f3Sric2016        );
112168ff6f3Sric2016    }
113168ff6f3Sric2016
1144eb71cfaSGreg Roach    /**
1154eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1164eb71cfaSGreg Roach     *
1178e69695bSGreg Roach     * @param Individual $individual
1188e69695bSGreg Roach     *
1194eb71cfaSGreg Roach     * @return Menu|null
1204eb71cfaSGreg Roach     */
121c1010edaSGreg Roach    public function getBoxChartMenu(Individual $individual)
122c1010edaSGreg Roach    {
123168ff6f3Sric2016        return $this->getChartMenu($individual);
124168ff6f3Sric2016    }
12545ac604bSGreg Roach
126291c1b19SGreg Roach
127291c1b19SGreg Roach    /**
128291c1b19SGreg Roach     * The URL to a page where the user can modify the configuration of this module.
129291c1b19SGreg Roach     *
130291c1b19SGreg Roach     * @return string
131291c1b19SGreg Roach     */
1328f53f488SRico Sonntag    public function getConfigLink(): string
133c1010edaSGreg Roach    {
134c1010edaSGreg Roach        return route('module', [
135c1010edaSGreg Roach            'module' => $this->getName(),
136c1010edaSGreg Roach            'action' => 'Admin',
137c1010edaSGreg Roach        ]);
138291c1b19SGreg Roach    }
139291c1b19SGreg Roach
140291c1b19SGreg Roach    /**
141291c1b19SGreg Roach     * @param Request $request
142291c1b19SGreg Roach     *
143291c1b19SGreg Roach     * @return Response
144291c1b19SGreg Roach     */
145c1010edaSGreg Roach    public function getAdminAction(Request $request): Response
146c1010edaSGreg Roach    {
147291c1b19SGreg Roach        $this->layout = 'layouts/administration';
148291c1b19SGreg Roach
149291c1b19SGreg Roach        return $this->viewResponse('modules/relationships_chart/config', [
150291c1b19SGreg Roach            'all_trees'         => Tree::getAll(),
151291c1b19SGreg Roach            'ancestors_options' => $this->ancestorsOptions(),
152291c1b19SGreg Roach            'default_ancestors' => self::DEFAULT_ANCESTORS,
153291c1b19SGreg Roach            'default_recursion' => self::DEFAULT_RECURSION,
154291c1b19SGreg Roach            'recursion_options' => $this->recursionOptions(),
155291c1b19SGreg Roach            'title'             => I18N::translate('Chart preferences') . ' — ' . $this->getTitle(),
156291c1b19SGreg Roach        ]);
157291c1b19SGreg Roach    }
158291c1b19SGreg Roach
159291c1b19SGreg Roach    /**
160291c1b19SGreg Roach     * @param Request $request
161291c1b19SGreg Roach     *
162291c1b19SGreg Roach     * @return RedirectResponse
163291c1b19SGreg Roach     */
164c1010edaSGreg Roach    public function postAdminAction(Request $request): RedirectResponse
165c1010edaSGreg Roach    {
166291c1b19SGreg Roach        foreach (Tree::getAll() as $tree) {
167291c1b19SGreg Roach            $tree->setPreference('RELATIONSHIP_RECURSION', $request->get('relationship-recursion-' . $tree->getTreeId()));
168291c1b19SGreg Roach            $tree->setPreference('RELATIONSHIP_ANCESTORS', $request->get('relationship-ancestors-' . $tree->getTreeId()));
169291c1b19SGreg Roach        }
170291c1b19SGreg Roach
171291c1b19SGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->getTitle()), 'success');
172291c1b19SGreg Roach
173291c1b19SGreg Roach        return new RedirectResponse($this->getConfigLink());
174291c1b19SGreg Roach    }
175291c1b19SGreg Roach
17645ac604bSGreg Roach    /**
177e0bd7dc9SGreg Roach     * Possible options for the ancestors option
17818d7a90dSGreg Roach     *
17918d7a90dSGreg Roach     * @return string[]
180e0bd7dc9SGreg Roach     */
18118d7a90dSGreg Roach    private function ancestorsOptions(): array
182c1010edaSGreg Roach    {
18313abd6f3SGreg Roach        return [
184e0bd7dc9SGreg Roach            0 => I18N::translate('Find any relationship'),
185e0bd7dc9SGreg Roach            1 => I18N::translate('Find relationships via ancestors'),
18613abd6f3SGreg Roach        ];
187e0bd7dc9SGreg Roach    }
188e0bd7dc9SGreg Roach
189e0bd7dc9SGreg Roach    /**
1901e3273c9SGreg Roach     * Possible options for the recursion option
19118d7a90dSGreg Roach     *
19218d7a90dSGreg Roach     * @return string[]
1931e3273c9SGreg Roach     */
19418d7a90dSGreg Roach    private function recursionOptions(): array
195c1010edaSGreg Roach    {
19613abd6f3SGreg Roach        return [
1971e3273c9SGreg Roach            0                         => I18N::translate('none'),
1981e3273c9SGreg Roach            1                         => I18N::number(1),
1991e3273c9SGreg Roach            2                         => I18N::number(2),
2001e3273c9SGreg Roach            3                         => I18N::number(3),
201e0bd7dc9SGreg Roach            self::UNLIMITED_RECURSION => I18N::translate('unlimited'),
20213abd6f3SGreg Roach        ];
2031e3273c9SGreg Roach    }
204168ff6f3Sric2016}
205