xref: /webtrees/app/Module/DescendancyModule.php (revision 353b36ab5fd4f6ba09f89e2865edee7108bdf7c0)
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;
2432cd2800SGreg 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 */
3249a243cbSGreg Roachclass DescendancyModule extends AbstractModule implements ModuleInterface, ModuleSidebarInterface
33c1010edaSGreg Roach{
3449a243cbSGreg Roach    use ModuleSidebarTrait;
3549a243cbSGreg Roach
36961ec755SGreg Roach    /**
37961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
38961ec755SGreg Roach     *
39961ec755SGreg Roach     * @return string
40961ec755SGreg Roach     */
4149a243cbSGreg Roach    public function title(): string
42c1010edaSGreg Roach    {
43bbb76c12SGreg Roach        /* I18N: Name of a module/sidebar */
44bbb76c12SGreg Roach        return I18N::translate('Descendants');
458c2e8227SGreg Roach    }
468c2e8227SGreg Roach
47961ec755SGreg Roach    /**
48961ec755SGreg Roach     * A sentence describing what this module does.
49961ec755SGreg Roach     *
50961ec755SGreg Roach     * @return string
51961ec755SGreg Roach     */
5249a243cbSGreg Roach    public function description(): string
53c1010edaSGreg Roach    {
54bbb76c12SGreg Roach        /* I18N: Description of the “Descendants” module */
55bbb76c12SGreg Roach        return I18N::translate('A sidebar showing the descendants of an individual.');
568c2e8227SGreg Roach    }
578c2e8227SGreg Roach
5876692c8bSGreg Roach    /**
5949a243cbSGreg Roach     * The default position for this sidebar.  It can be changed in the control panel.
6049a243cbSGreg Roach     *
6149a243cbSGreg Roach     * @return int
6249a243cbSGreg Roach     */
6349a243cbSGreg Roach    public function defaultSidebarOrder(): int
6449a243cbSGreg Roach    {
65*353b36abSGreg Roach        return 3;
6649a243cbSGreg Roach    }
6749a243cbSGreg Roach
6849a243cbSGreg Roach    /**
691c0676b2SGreg Roach     * @param Request       $request
70b6db7c1fSGreg Roach     * @param Tree          $tree
7132cd2800SGreg Roach     * @param SearchService $search_service
7276692c8bSGreg Roach     *
731c0676b2SGreg Roach     * @return Response
7476692c8bSGreg Roach     */
7532cd2800SGreg Roach    public function getSearchAction(Request $request, Tree $tree, SearchService $search_service): Response
76c1010edaSGreg Roach    {
771c0676b2SGreg Roach        $search = $request->get('search', '');
788c2e8227SGreg Roach
791c0676b2SGreg Roach        $html = '';
801c0676b2SGreg Roach
811c0676b2SGreg Roach        if (strlen($search) >= 2) {
8232cd2800SGreg Roach            $html = $search_service
83a7a24840SGreg Roach                ->searchIndividualNames([$tree], [$search])
8432cd2800SGreg Roach                ->map(function (Individual $individual): string {
8532cd2800SGreg Roach                    return $this->getPersonLi($individual);
8632cd2800SGreg Roach                })
8732cd2800SGreg Roach                ->implode('');
888c2e8227SGreg Roach        }
898c2e8227SGreg Roach
901c0676b2SGreg Roach        if ($html !== '') {
911c0676b2SGreg Roach            $html = '<ul>' . $html . '</ul>';
921c0676b2SGreg Roach        }
931c0676b2SGreg Roach
941c0676b2SGreg Roach        return new Response($html);
951c0676b2SGreg Roach    }
961c0676b2SGreg Roach
971c0676b2SGreg Roach    /**
981c0676b2SGreg Roach     * @param Request $request
99b6db7c1fSGreg Roach     * @param Tree    $tree
1001c0676b2SGreg Roach     *
1011c0676b2SGreg Roach     * @return Response
1021c0676b2SGreg Roach     */
103b6db7c1fSGreg Roach    public function getDescendantsAction(Request $request, Tree $tree): Response
104c1010edaSGreg Roach    {
1059e648e55SGreg Roach        $xref = $request->get('xref', '');
1061c0676b2SGreg Roach
1071c0676b2SGreg Roach        $individual = Individual::getInstance($xref, $tree);
1081c0676b2SGreg Roach
1091c0676b2SGreg Roach        if ($individual !== null && $individual->canShow()) {
1101c0676b2SGreg Roach            $html = $this->loadSpouses($individual, 1);
1111c0676b2SGreg Roach        } else {
1121c0676b2SGreg Roach            $html = '';
1131c0676b2SGreg Roach        }
1141c0676b2SGreg Roach
1151c0676b2SGreg Roach        return new Response($html);
1161c0676b2SGreg Roach    }
1171c0676b2SGreg Roach
1188c2e8227SGreg Roach    /** {@inheritdoc} */
1198f53f488SRico Sonntag    public function hasSidebarContent(Individual $individual): bool
120c1010edaSGreg Roach    {
12138a9583bSGreg Roach        return true;
1228c2e8227SGreg Roach    }
1238c2e8227SGreg Roach
12476692c8bSGreg Roach    /**
12576692c8bSGreg Roach     * Load this sidebar synchronously.
12676692c8bSGreg Roach     *
127225e381fSGreg Roach     * @param Individual $individual
128225e381fSGreg Roach     *
12976692c8bSGreg Roach     * @return string
13076692c8bSGreg Roach     */
1318f53f488SRico Sonntag    public function getSidebarContent(Individual $individual): string
132c1010edaSGreg Roach    {
1331ef93f16SGreg Roach        return view('modules/descendancy/sidebar', [
134a817de92SGreg Roach            'individual_list' => $this->getPersonLi($individual, 1),
135a817de92SGreg Roach        ]);
1368c2e8227SGreg Roach    }
1378c2e8227SGreg Roach
1388c2e8227SGreg Roach    /**
13976692c8bSGreg Roach     * Format an individual in a list.
14076692c8bSGreg Roach     *
1418c2e8227SGreg Roach     * @param Individual $person
142cbc1590aSGreg Roach     * @param int        $generations
1438c2e8227SGreg Roach     *
1448c2e8227SGreg Roach     * @return string
1458c2e8227SGreg Roach     */
1468f53f488SRico Sonntag    public function getPersonLi(Individual $person, $generations = 0): string
147c1010edaSGreg Roach    {
1488c2e8227SGreg Roach        $icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
1498c2e8227SGreg Roach        $lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : '';
1508c2e8227SGreg Roach        $spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
1512622ba92SGreg Roach
1522622ba92SGreg Roach        return
1532622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
154c1010edaSGreg Roach            '<a class="sb_desc_indi" href="' . e(route('module', [
15549a243cbSGreg Roach                'module' => $this->getName(),
156c1010edaSGreg Roach                'action' => 'Descendants',
157f4afa648SGreg Roach                'ged'    => $person->tree()->name(),
158c0935879SGreg Roach                'xref'   => $person->xref(),
159c1010edaSGreg Roach            ])) . '">' .
1602622ba92SGreg Roach            '<i class="plusminus ' . $icon . '"></i>' .
1612622ba92SGreg Roach            $person->getSexImage() . $person->getFullName() . $lifespan .
1622622ba92SGreg Roach            '</a>' .
163b1f1e4efSGreg Roach            FontAwesome::linkIcon('individual', $person->getFullName(), ['href' => $person->url()]) .
1642622ba92SGreg Roach            '<div>' . $spouses . '</div>' .
1652622ba92SGreg Roach            '</li>';
1668c2e8227SGreg Roach    }
1678c2e8227SGreg Roach
1688c2e8227SGreg Roach    /**
16976692c8bSGreg Roach     * Format a family in a list.
17076692c8bSGreg Roach     *
1718c2e8227SGreg Roach     * @param Family     $family
1728c2e8227SGreg Roach     * @param Individual $person
173cbc1590aSGreg Roach     * @param int        $generations
1748c2e8227SGreg Roach     *
1758c2e8227SGreg Roach     * @return string
1768c2e8227SGreg Roach     */
1778f53f488SRico Sonntag    public function getFamilyLi(Family $family, Individual $person, $generations = 0): string
178c1010edaSGreg Roach    {
1792622ba92SGreg Roach        $spouse = $family->getSpouse($person);
1802622ba92SGreg Roach        if ($spouse) {
1812622ba92SGreg Roach            $spouse_name = $spouse->getSexImage() . $spouse->getFullName();
182b1f1e4efSGreg Roach            $spouse_link = FontAwesome::linkIcon('individual', $spouse->getFullName(), ['href' => $person->url()]);
1832622ba92SGreg Roach        } else {
1842622ba92SGreg Roach            $spouse_name = '';
1852622ba92SGreg Roach            $spouse_link = '';
1862622ba92SGreg Roach        }
1872622ba92SGreg Roach
1888c2e8227SGreg Roach        $marryear = $family->getMarriageYear();
1898c2e8227SGreg Roach        $marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
1902622ba92SGreg Roach
1912622ba92SGreg Roach        return
1922622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
1932622ba92SGreg Roach            '<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . $spouse_name . $marr . '</a>' .
1942622ba92SGreg Roach            $spouse_link .
195b1f1e4efSGreg Roach            FontAwesome::linkIcon('family', $family->getFullName(), ['href' => $family->url()]) .
1962622ba92SGreg Roach            '<div>' . $this->loadChildren($family, $generations) . '</div>' .
1972622ba92SGreg Roach            '</li>';
1988c2e8227SGreg Roach    }
1998c2e8227SGreg Roach
2008c2e8227SGreg Roach    /**
20176692c8bSGreg Roach     * Display spouses.
20276692c8bSGreg Roach     *
20349a243cbSGreg Roach     * @param Individual $individual
204cbc1590aSGreg Roach     * @param int        $generations
2058c2e8227SGreg Roach     *
2068c2e8227SGreg Roach     * @return string
2078c2e8227SGreg Roach     */
20849a243cbSGreg Roach    public function loadSpouses(Individual $individual, $generations)
209c1010edaSGreg Roach    {
2108c2e8227SGreg Roach        $out = '';
21149a243cbSGreg Roach        if ($individual->canShow()) {
21249a243cbSGreg Roach            foreach ($individual->getSpouseFamilies() as $family) {
21349a243cbSGreg Roach                $out .= $this->getFamilyLi($family, $individual, $generations - 1);
2148c2e8227SGreg Roach            }
2158c2e8227SGreg Roach        }
2168c2e8227SGreg Roach        if ($out) {
2178c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2188c2e8227SGreg Roach        }
219b2ce94c6SRico Sonntag
220b2ce94c6SRico Sonntag        return '';
2218c2e8227SGreg Roach    }
2228c2e8227SGreg Roach
2238c2e8227SGreg Roach    /**
22476692c8bSGreg Roach     * Display descendants.
22576692c8bSGreg Roach     *
2268c2e8227SGreg Roach     * @param Family $family
227cbc1590aSGreg Roach     * @param int    $generations
2288c2e8227SGreg Roach     *
2298c2e8227SGreg Roach     * @return string
2308c2e8227SGreg Roach     */
231c1010edaSGreg Roach    public function loadChildren(Family $family, $generations)
232c1010edaSGreg Roach    {
2338c2e8227SGreg Roach        $out = '';
2348c2e8227SGreg Roach        if ($family->canShow()) {
2358c2e8227SGreg Roach            $children = $family->getChildren();
2368c2e8227SGreg Roach            if ($children) {
2378c2e8227SGreg Roach                foreach ($children as $child) {
2388c2e8227SGreg Roach                    $out .= $this->getPersonLi($child, $generations - 1);
2398c2e8227SGreg Roach                }
2408c2e8227SGreg Roach            } else {
2418c2e8227SGreg Roach                $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
2428c2e8227SGreg Roach            }
2438c2e8227SGreg Roach        }
2448c2e8227SGreg Roach        if ($out) {
2458c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2468c2e8227SGreg Roach        }
247b2ce94c6SRico Sonntag
248b2ce94c6SRico Sonntag        return '';
2498c2e8227SGreg Roach    }
2508c2e8227SGreg Roach}
251