xref: /webtrees/app/Module/RelationshipsChartModule.php (revision 0120d29d7e1471af71218964540c1319d6060e5a)
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 */
3611a00104SGreg Roach    const UNLIMITED_RECURSION = 99;
371e3273c9SGreg Roach
381e3273c9SGreg Roach    /** By default new trees allow unlimited recursion */
3911a00104SGreg 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    {
87f4afa648SGreg Roach        $tree     = $individual->tree();
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,
95c0935879SGreg Roach                    'xref2' => $individual->xref(),
96f4afa648SGreg Roach                    'ged'   => $individual->tree()->name(),
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', [
106c0935879SGreg Roach                'xref1' => $individual->xref(),
107f4afa648SGreg Roach                'ged'   => $individual->tree()->name(),
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     * @return Response
142291c1b19SGreg Roach     */
143*0120d29dSGreg Roach    public function getAdminAction(): 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) {
16572cf66d4SGreg Roach            $recursion = $request->get('relationship-recursion-' . $tree->id(), '');
16672cf66d4SGreg Roach            $ancestors = $request->get('relationship-ancestors-' . $tree->id(), '');
16775ee5198SGreg Roach
16875ee5198SGreg Roach            $tree->setPreference('RELATIONSHIP_RECURSION', $recursion);
16975ee5198SGreg Roach            $tree->setPreference('RELATIONSHIP_ANCESTORS', $ancestors);
170291c1b19SGreg Roach        }
171291c1b19SGreg Roach
172291c1b19SGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->getTitle()), 'success');
173291c1b19SGreg Roach
174291c1b19SGreg Roach        return new RedirectResponse($this->getConfigLink());
175291c1b19SGreg Roach    }
176291c1b19SGreg Roach
17745ac604bSGreg Roach    /**
178e0bd7dc9SGreg Roach     * Possible options for the ancestors option
17918d7a90dSGreg Roach     *
18018d7a90dSGreg Roach     * @return string[]
181e0bd7dc9SGreg Roach     */
18218d7a90dSGreg Roach    private function ancestorsOptions(): array
183c1010edaSGreg Roach    {
18413abd6f3SGreg Roach        return [
185e0bd7dc9SGreg Roach            0 => I18N::translate('Find any relationship'),
186e0bd7dc9SGreg Roach            1 => I18N::translate('Find relationships via ancestors'),
18713abd6f3SGreg Roach        ];
188e0bd7dc9SGreg Roach    }
189e0bd7dc9SGreg Roach
190e0bd7dc9SGreg Roach    /**
1911e3273c9SGreg Roach     * Possible options for the recursion option
19218d7a90dSGreg Roach     *
19318d7a90dSGreg Roach     * @return string[]
1941e3273c9SGreg Roach     */
19518d7a90dSGreg Roach    private function recursionOptions(): array
196c1010edaSGreg Roach    {
19713abd6f3SGreg Roach        return [
1981e3273c9SGreg Roach            0                         => I18N::translate('none'),
1991e3273c9SGreg Roach            1                         => I18N::number(1),
2001e3273c9SGreg Roach            2                         => I18N::number(2),
2011e3273c9SGreg Roach            3                         => I18N::number(3),
202e0bd7dc9SGreg Roach            self::UNLIMITED_RECURSION => I18N::translate('unlimited'),
20313abd6f3SGreg Roach        ];
2041e3273c9SGreg Roach    }
205168ff6f3Sric2016}
206