xref: /webtrees/app/Module/DescendancyModule.php (revision 32cd280058cdb3b5f1807bfaaeffb8d79d5ebf3f)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
212de55a25SGreg Roachuse Fisharebest\Webtrees\FontAwesome;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
24*32cd2800SGreg Roachuse Fisharebest\Webtrees\Services\SearchService;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
261c0676b2SGreg Roachuse Symfony\Component\HttpFoundation\Request;
271c0676b2SGreg Roachuse Symfony\Component\HttpFoundation\Response;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class DescendancyModule
318c2e8227SGreg Roach */
32c1010edaSGreg Roachclass DescendancyModule extends AbstractModule implements ModuleSidebarInterface
33c1010edaSGreg Roach{
348c2e8227SGreg Roach    /** {@inheritdoc} */
358f53f488SRico Sonntag    public function getTitle(): string
36c1010edaSGreg Roach    {
37bbb76c12SGreg Roach        /* I18N: Name of a module/sidebar */
38bbb76c12SGreg Roach        return I18N::translate('Descendants');
398c2e8227SGreg Roach    }
408c2e8227SGreg Roach
418c2e8227SGreg Roach    /** {@inheritdoc} */
428f53f488SRico Sonntag    public function getDescription(): string
43c1010edaSGreg Roach    {
44bbb76c12SGreg Roach        /* I18N: Description of the “Descendants” module */
45bbb76c12SGreg Roach        return I18N::translate('A sidebar showing the descendants of an individual.');
468c2e8227SGreg Roach    }
478c2e8227SGreg Roach
4876692c8bSGreg Roach    /**
491c0676b2SGreg Roach     * @param Request       $request
50b6db7c1fSGreg Roach     * @param Tree          $tree
51*32cd2800SGreg Roach     * @param SearchService $search_service
5276692c8bSGreg Roach     *
531c0676b2SGreg Roach     * @return Response
5476692c8bSGreg Roach     */
55*32cd2800SGreg Roach    public function getSearchAction(Request $request, Tree $tree, SearchService $search_service): Response
56c1010edaSGreg Roach    {
571c0676b2SGreg Roach        $search = $request->get('search', '');
588c2e8227SGreg Roach
591c0676b2SGreg Roach        $html = '';
601c0676b2SGreg Roach
611c0676b2SGreg Roach        if (strlen($search) >= 2) {
62*32cd2800SGreg Roach            $html = $search_service
63*32cd2800SGreg Roach                ->searchIndividualsByName($tree, $search)
64*32cd2800SGreg Roach                ->map(function (Individual $individual): string {
65*32cd2800SGreg Roach                    return $this->getPersonLi($individual);
66*32cd2800SGreg Roach                })
67*32cd2800SGreg Roach                ->implode('');
688c2e8227SGreg Roach        }
698c2e8227SGreg Roach
701c0676b2SGreg Roach        if ($html !== '') {
711c0676b2SGreg Roach            $html = '<ul>' . $html . '</ul>';
721c0676b2SGreg Roach        }
731c0676b2SGreg Roach
741c0676b2SGreg Roach        return new Response($html);
751c0676b2SGreg Roach    }
761c0676b2SGreg Roach
771c0676b2SGreg Roach    /**
781c0676b2SGreg Roach     * @param Request $request
79b6db7c1fSGreg Roach     * @param Tree    $tree
801c0676b2SGreg Roach     *
811c0676b2SGreg Roach     * @return Response
821c0676b2SGreg Roach     */
83b6db7c1fSGreg Roach    public function getDescendantsAction(Request $request, Tree $tree): Response
84c1010edaSGreg Roach    {
859e648e55SGreg Roach        $xref = $request->get('xref', '');
861c0676b2SGreg Roach
871c0676b2SGreg Roach        $individual = Individual::getInstance($xref, $tree);
881c0676b2SGreg Roach
891c0676b2SGreg Roach        if ($individual !== null && $individual->canShow()) {
901c0676b2SGreg Roach            $html = $this->loadSpouses($individual, 1);
911c0676b2SGreg Roach        } else {
921c0676b2SGreg Roach            $html = '';
931c0676b2SGreg Roach        }
941c0676b2SGreg Roach
951c0676b2SGreg Roach        return new Response($html);
961c0676b2SGreg Roach    }
971c0676b2SGreg Roach
988c2e8227SGreg Roach    /** {@inheritdoc} */
998f53f488SRico Sonntag    public function defaultSidebarOrder(): int
100c1010edaSGreg Roach    {
1018c2e8227SGreg Roach        return 30;
1028c2e8227SGreg Roach    }
1038c2e8227SGreg Roach
1048c2e8227SGreg Roach    /** {@inheritdoc} */
1058f53f488SRico Sonntag    public function hasSidebarContent(Individual $individual): bool
106c1010edaSGreg Roach    {
10738a9583bSGreg Roach        return true;
1088c2e8227SGreg Roach    }
1098c2e8227SGreg Roach
11076692c8bSGreg Roach    /**
11176692c8bSGreg Roach     * Load this sidebar synchronously.
11276692c8bSGreg Roach     *
113225e381fSGreg Roach     * @param Individual $individual
114225e381fSGreg Roach     *
11576692c8bSGreg Roach     * @return string
11676692c8bSGreg Roach     */
1178f53f488SRico Sonntag    public function getSidebarContent(Individual $individual): string
118c1010edaSGreg Roach    {
1191ef93f16SGreg Roach        return view('modules/descendancy/sidebar', [
120a817de92SGreg Roach            'individual_list' => $this->getPersonLi($individual, 1),
121a817de92SGreg Roach        ]);
1228c2e8227SGreg Roach    }
1238c2e8227SGreg Roach
1248c2e8227SGreg Roach    /**
12576692c8bSGreg Roach     * Format an individual in a list.
12676692c8bSGreg Roach     *
1278c2e8227SGreg Roach     * @param Individual $person
128cbc1590aSGreg Roach     * @param int        $generations
1298c2e8227SGreg Roach     *
1308c2e8227SGreg Roach     * @return string
1318c2e8227SGreg Roach     */
1328f53f488SRico Sonntag    public function getPersonLi(Individual $person, $generations = 0): string
133c1010edaSGreg Roach    {
1348c2e8227SGreg Roach        $icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
1358c2e8227SGreg Roach        $lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : '';
1368c2e8227SGreg Roach        $spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
1372622ba92SGreg Roach
1382622ba92SGreg Roach        return
1392622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
140c1010edaSGreg Roach            '<a class="sb_desc_indi" href="' . e(route('module', [
141c1010edaSGreg Roach                'module' => 'descendancy',
142c1010edaSGreg Roach                'action' => 'Descendants',
143f4afa648SGreg Roach                'ged'    => $person->tree()->name(),
144c0935879SGreg Roach                'xref'   => $person->xref(),
145c1010edaSGreg Roach            ])) . '">' .
1462622ba92SGreg Roach            '<i class="plusminus ' . $icon . '"></i>' .
1472622ba92SGreg Roach            $person->getSexImage() . $person->getFullName() . $lifespan .
1482622ba92SGreg Roach            '</a>' .
149b1f1e4efSGreg Roach            FontAwesome::linkIcon('individual', $person->getFullName(), ['href' => $person->url()]) .
1502622ba92SGreg Roach            '<div>' . $spouses . '</div>' .
1512622ba92SGreg Roach            '</li>';
1528c2e8227SGreg Roach    }
1538c2e8227SGreg Roach
1548c2e8227SGreg Roach    /**
15576692c8bSGreg Roach     * Format a family in a list.
15676692c8bSGreg Roach     *
1578c2e8227SGreg Roach     * @param Family     $family
1588c2e8227SGreg Roach     * @param Individual $person
159cbc1590aSGreg Roach     * @param int        $generations
1608c2e8227SGreg Roach     *
1618c2e8227SGreg Roach     * @return string
1628c2e8227SGreg Roach     */
1638f53f488SRico Sonntag    public function getFamilyLi(Family $family, Individual $person, $generations = 0): string
164c1010edaSGreg Roach    {
1652622ba92SGreg Roach        $spouse = $family->getSpouse($person);
1662622ba92SGreg Roach        if ($spouse) {
1672622ba92SGreg Roach            $spouse_name = $spouse->getSexImage() . $spouse->getFullName();
168b1f1e4efSGreg Roach            $spouse_link = FontAwesome::linkIcon('individual', $spouse->getFullName(), ['href' => $person->url()]);
1692622ba92SGreg Roach        } else {
1702622ba92SGreg Roach            $spouse_name = '';
1712622ba92SGreg Roach            $spouse_link = '';
1722622ba92SGreg Roach        }
1732622ba92SGreg Roach
1748c2e8227SGreg Roach        $marryear = $family->getMarriageYear();
1758c2e8227SGreg Roach        $marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
1762622ba92SGreg Roach
1772622ba92SGreg Roach        return
1782622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
1792622ba92SGreg Roach            '<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . $spouse_name . $marr . '</a>' .
1802622ba92SGreg Roach            $spouse_link .
181b1f1e4efSGreg Roach            FontAwesome::linkIcon('family', $family->getFullName(), ['href' => $family->url()]) .
1822622ba92SGreg Roach            '<div>' . $this->loadChildren($family, $generations) . '</div>' .
1832622ba92SGreg Roach            '</li>';
1848c2e8227SGreg Roach    }
1858c2e8227SGreg Roach
1868c2e8227SGreg Roach    /**
18776692c8bSGreg Roach     * Display spouses.
18876692c8bSGreg Roach     *
1898c2e8227SGreg Roach     * @param Individual $person
190cbc1590aSGreg Roach     * @param int        $generations
1918c2e8227SGreg Roach     *
1928c2e8227SGreg Roach     * @return string
1938c2e8227SGreg Roach     */
194c1010edaSGreg Roach    public function loadSpouses(Individual $person, $generations)
195c1010edaSGreg Roach    {
1968c2e8227SGreg Roach        $out = '';
1978c2e8227SGreg Roach        if ($person && $person->canShow()) {
1988c2e8227SGreg Roach            foreach ($person->getSpouseFamilies() as $family) {
1992622ba92SGreg Roach                $out .= $this->getFamilyLi($family, $person, $generations - 1);
2008c2e8227SGreg Roach            }
2018c2e8227SGreg Roach        }
2028c2e8227SGreg Roach        if ($out) {
2038c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2048c2e8227SGreg Roach        }
205b2ce94c6SRico Sonntag
206b2ce94c6SRico Sonntag        return '';
2078c2e8227SGreg Roach    }
2088c2e8227SGreg Roach
2098c2e8227SGreg Roach    /**
21076692c8bSGreg Roach     * Display descendants.
21176692c8bSGreg Roach     *
2128c2e8227SGreg Roach     * @param Family $family
213cbc1590aSGreg Roach     * @param int    $generations
2148c2e8227SGreg Roach     *
2158c2e8227SGreg Roach     * @return string
2168c2e8227SGreg Roach     */
217c1010edaSGreg Roach    public function loadChildren(Family $family, $generations)
218c1010edaSGreg Roach    {
2198c2e8227SGreg Roach        $out = '';
2208c2e8227SGreg Roach        if ($family->canShow()) {
2218c2e8227SGreg Roach            $children = $family->getChildren();
2228c2e8227SGreg Roach            if ($children) {
2238c2e8227SGreg Roach                foreach ($children as $child) {
2248c2e8227SGreg Roach                    $out .= $this->getPersonLi($child, $generations - 1);
2258c2e8227SGreg Roach                }
2268c2e8227SGreg Roach            } else {
2278c2e8227SGreg Roach                $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
2288c2e8227SGreg Roach            }
2298c2e8227SGreg Roach        }
2308c2e8227SGreg Roach        if ($out) {
2318c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2328c2e8227SGreg Roach        }
233b2ce94c6SRico Sonntag
234b2ce94c6SRico Sonntag        return '';
2358c2e8227SGreg Roach    }
2368c2e8227SGreg Roach}
237