xref: /webtrees/app/Module/RelationshipsChartModule.php (revision e65629820189baa4ae743eb866cb88cc852d5956)
1168ff6f3Sric2016<?php
2168ff6f3Sric2016/**
3168ff6f3Sric2016 * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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 */
3349a243cbSGreg Roachclass RelationshipsChartModule extends AbstractModule implements ModuleInterface, ModuleChartInterface, ModuleConfigInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleChartTrait;
3649a243cbSGreg Roach    use ModuleConfigTrait;
3749a243cbSGreg Roach
381e3273c9SGreg Roach    /** It would be more correct to use PHP_INT_MAX, but this isn't friendly in URLs */
3916d6367aSGreg Roach    public const UNLIMITED_RECURSION = 99;
401e3273c9SGreg Roach
411e3273c9SGreg Roach    /** By default new trees allow unlimited recursion */
4216d6367aSGreg Roach    public const DEFAULT_RECURSION = '99';
4345ac604bSGreg Roach
44e0bd7dc9SGreg Roach    /** By default new trees search for all relationships (not via ancestors) */
4516d6367aSGreg Roach    public const DEFAULT_ANCESTORS = '0';
46e0bd7dc9SGreg Roach
47168ff6f3Sric2016    /**
48168ff6f3Sric2016     * How should this module be labelled on tabs, menus, etc.?
49168ff6f3Sric2016     *
50168ff6f3Sric2016     * @return string
51168ff6f3Sric2016     */
5249a243cbSGreg Roach    public function title(): string
53c1010edaSGreg Roach    {
54bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
55bbb76c12SGreg Roach        return I18N::translate('Relationships');
56168ff6f3Sric2016    }
57168ff6f3Sric2016
58168ff6f3Sric2016    /**
59168ff6f3Sric2016     * A sentence describing what this module does.
60168ff6f3Sric2016     *
61168ff6f3Sric2016     * @return string
62168ff6f3Sric2016     */
6349a243cbSGreg Roach    public function description(): string
64c1010edaSGreg Roach    {
65bbb76c12SGreg Roach        /* I18N: Description of the “RelationshipsChart” module */
66bbb76c12SGreg Roach        return I18N::translate('A chart displaying relationships between two individuals.');
67168ff6f3Sric2016    }
68168ff6f3Sric2016
69168ff6f3Sric2016    /**
70*e6562982SGreg Roach     * A main menu item for this chart.
71168ff6f3Sric2016     *
728e69695bSGreg Roach     * @param Individual $individual
738e69695bSGreg Roach     *
74*e6562982SGreg Roach     * @return Menu
75168ff6f3Sric2016     */
76*e6562982SGreg Roach    public function chartMenu(Individual $individual): Menu
77c1010edaSGreg Roach    {
78*e6562982SGreg Roach        $gedcomid = $individual->tree()->getUserPreference(Auth::user(), 'gedcomid');
79168ff6f3Sric2016
802c689cc8SGreg Roach        if ($gedcomid !== '') {
81168ff6f3Sric2016            return new Menu(
82168ff6f3Sric2016                I18N::translate('Relationship to me'),
83*e6562982SGreg Roach                $this->chartUrl($individual, ['xref2' => $gedcomid]),
84*e6562982SGreg Roach                $this->chartUrlClasss(),
85*e6562982SGreg Roach                $this->chartUrlAttributes()
86168ff6f3Sric2016            );
87b2ce94c6SRico Sonntag        }
88b2ce94c6SRico Sonntag
89168ff6f3Sric2016        return new Menu(
90*e6562982SGreg Roach            $this->title(),
91*e6562982SGreg Roach            $this->chartUrl($individual),
92*e6562982SGreg Roach            $this->chartUrlClasss(),
93*e6562982SGreg Roach            $this->chartUrlAttributes()
94168ff6f3Sric2016        );
95168ff6f3Sric2016    }
96168ff6f3Sric2016
974eb71cfaSGreg Roach    /**
984eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
994eb71cfaSGreg Roach     *
1008e69695bSGreg Roach     * @param Individual $individual
1018e69695bSGreg Roach     *
1024eb71cfaSGreg Roach     * @return Menu|null
1034eb71cfaSGreg Roach     */
104*e6562982SGreg Roach    public function chartMenuIndividual(Individual $individual): ?Menu
105c1010edaSGreg Roach    {
106*e6562982SGreg Roach        return $this->chartMenu($individual);
107*e6562982SGreg Roach    }
108*e6562982SGreg Roach
109*e6562982SGreg Roach    /**
110*e6562982SGreg Roach     * The URL for this chart.
111*e6562982SGreg Roach     *
112*e6562982SGreg Roach     * @param Individual $individual
113*e6562982SGreg Roach     * @param string[]   $parameters
114*e6562982SGreg Roach     *
115*e6562982SGreg Roach     * @return string
116*e6562982SGreg Roach     */
117*e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
118*e6562982SGreg Roach    {
119*e6562982SGreg Roach        return route('relationships', [
120*e6562982SGreg Roach            'xref1' => $individual->xref(),
121*e6562982SGreg Roach            'ged'   => $individual->tree()->name(),
122*e6562982SGreg Roach        ] + $parameters);
123*e6562982SGreg Roach    }
124*e6562982SGreg Roach
125*e6562982SGreg Roach    /**
126*e6562982SGreg Roach     * CSS class for the URL.
127*e6562982SGreg Roach     *
128*e6562982SGreg Roach     * @return string
129*e6562982SGreg Roach     */
130*e6562982SGreg Roach    public function chartUrlClasss(): string
131*e6562982SGreg Roach    {
132*e6562982SGreg Roach        return 'menu-chart-relationship';
133168ff6f3Sric2016    }
13445ac604bSGreg Roach
135291c1b19SGreg Roach    /**
136291c1b19SGreg Roach     * @return Response
137291c1b19SGreg Roach     */
1380120d29dSGreg Roach    public function getAdminAction(): Response
139c1010edaSGreg Roach    {
140291c1b19SGreg Roach        $this->layout = 'layouts/administration';
141291c1b19SGreg Roach
142291c1b19SGreg Roach        return $this->viewResponse('modules/relationships_chart/config', [
143291c1b19SGreg Roach            'all_trees'         => Tree::getAll(),
144291c1b19SGreg Roach            'ancestors_options' => $this->ancestorsOptions(),
145291c1b19SGreg Roach            'default_ancestors' => self::DEFAULT_ANCESTORS,
146291c1b19SGreg Roach            'default_recursion' => self::DEFAULT_RECURSION,
147291c1b19SGreg Roach            'recursion_options' => $this->recursionOptions(),
14849a243cbSGreg Roach            'title'             => I18N::translate('Chart preferences') . ' — ' . $this->title(),
149291c1b19SGreg Roach        ]);
150291c1b19SGreg Roach    }
151291c1b19SGreg Roach
152291c1b19SGreg Roach    /**
153291c1b19SGreg Roach     * @param Request $request
154291c1b19SGreg Roach     *
155291c1b19SGreg Roach     * @return RedirectResponse
156291c1b19SGreg Roach     */
157c1010edaSGreg Roach    public function postAdminAction(Request $request): RedirectResponse
158c1010edaSGreg Roach    {
159291c1b19SGreg Roach        foreach (Tree::getAll() as $tree) {
16072cf66d4SGreg Roach            $recursion = $request->get('relationship-recursion-' . $tree->id(), '');
16172cf66d4SGreg Roach            $ancestors = $request->get('relationship-ancestors-' . $tree->id(), '');
16275ee5198SGreg Roach
16375ee5198SGreg Roach            $tree->setPreference('RELATIONSHIP_RECURSION', $recursion);
16475ee5198SGreg Roach            $tree->setPreference('RELATIONSHIP_ANCESTORS', $ancestors);
165291c1b19SGreg Roach        }
166291c1b19SGreg Roach
16749a243cbSGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
168291c1b19SGreg Roach
169291c1b19SGreg Roach        return new RedirectResponse($this->getConfigLink());
170291c1b19SGreg Roach    }
171291c1b19SGreg Roach
17245ac604bSGreg Roach    /**
173e0bd7dc9SGreg Roach     * Possible options for the ancestors option
17418d7a90dSGreg Roach     *
17518d7a90dSGreg Roach     * @return string[]
176e0bd7dc9SGreg Roach     */
17718d7a90dSGreg Roach    private function ancestorsOptions(): array
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
18718d7a90dSGreg Roach     *
18818d7a90dSGreg Roach     * @return string[]
1891e3273c9SGreg Roach     */
19018d7a90dSGreg Roach    private function recursionOptions(): array
191c1010edaSGreg Roach    {
19213abd6f3SGreg Roach        return [
1931e3273c9SGreg Roach            0                         => I18N::translate('none'),
1941e3273c9SGreg Roach            1                         => I18N::number(1),
1951e3273c9SGreg Roach            2                         => I18N::number(2),
1961e3273c9SGreg Roach            3                         => I18N::number(3),
197e0bd7dc9SGreg Roach            self::UNLIMITED_RECURSION => I18N::translate('unlimited'),
19813abd6f3SGreg Roach        ];
1991e3273c9SGreg Roach    }
200168ff6f3Sric2016}
201