xref: /webtrees/app/Module/DescendancyModule.php (revision 8f53f488f13e53e44dc48778e8f51ec9f91352dd)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
202de55a25SGreg Roachuse Fisharebest\Webtrees\FontAwesome;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
241c0676b2SGreg Roachuse Symfony\Component\HttpFoundation\Request;
251c0676b2SGreg Roachuse Symfony\Component\HttpFoundation\Response;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class DescendancyModule
298c2e8227SGreg Roach */
30c1010edaSGreg Roachclass DescendancyModule extends AbstractModule implements ModuleSidebarInterface
31c1010edaSGreg Roach{
328c2e8227SGreg Roach    /** {@inheritdoc} */
33*8f53f488SRico Sonntag    public function getTitle(): string
34c1010edaSGreg Roach    {
35bbb76c12SGreg Roach        /* I18N: Name of a module/sidebar */
36bbb76c12SGreg Roach        return I18N::translate('Descendants');
378c2e8227SGreg Roach    }
388c2e8227SGreg Roach
398c2e8227SGreg Roach    /** {@inheritdoc} */
40*8f53f488SRico Sonntag    public function getDescription(): string
41c1010edaSGreg Roach    {
42bbb76c12SGreg Roach        /* I18N: Description of the “Descendants” module */
43bbb76c12SGreg Roach        return I18N::translate('A sidebar showing the descendants of an individual.');
448c2e8227SGreg Roach    }
458c2e8227SGreg Roach
4676692c8bSGreg Roach    /**
471c0676b2SGreg Roach     * @param Request $request
48b6db7c1fSGreg Roach     * @param Tree    $tree
4976692c8bSGreg Roach     *
501c0676b2SGreg Roach     * @return Response
5176692c8bSGreg Roach     */
52b6db7c1fSGreg Roach    public function getSearchAction(Request $request, Tree $tree): Response
53c1010edaSGreg Roach    {
541c0676b2SGreg Roach        $search = $request->get('search', '');
558c2e8227SGreg Roach
561c0676b2SGreg Roach        $html = '';
571c0676b2SGreg Roach
581c0676b2SGreg Roach        if (strlen($search) >= 2) {
591c0676b2SGreg Roach            $rows = Database::prepare(
601c0676b2SGreg Roach                "SELECT i_id AS xref" .
611c0676b2SGreg Roach                " FROM `##individuals`" .
621c0676b2SGreg Roach                " JOIN `##name` ON i_id = n_id AND i_file = n_file" .
631c0676b2SGreg Roach                " WHERE n_sort LIKE CONCAT('%', :query, '%') AND i_file = :tree_id" .
641c0676b2SGreg Roach                " ORDER BY n_sort"
651c0676b2SGreg Roach            )->execute([
661c0676b2SGreg Roach                'query'   => $search,
671c0676b2SGreg Roach                'tree_id' => $tree->getTreeId(),
681c0676b2SGreg Roach            ])->fetchAll();
691c0676b2SGreg Roach
701c0676b2SGreg Roach            foreach ($rows as $row) {
711c0676b2SGreg Roach                $individual = Individual::getInstance($row->xref, $tree);
721c0676b2SGreg Roach                if ($individual !== null && $individual->canShow()) {
731c0676b2SGreg Roach                    $html .= $this->getPersonLi($individual);
748c2e8227SGreg Roach                }
758c2e8227SGreg Roach            }
768c2e8227SGreg Roach        }
778c2e8227SGreg Roach
781c0676b2SGreg Roach        if ($html !== '') {
791c0676b2SGreg Roach            $html = '<ul>' . $html . '</ul>';
801c0676b2SGreg Roach        }
811c0676b2SGreg Roach
821c0676b2SGreg Roach        return new Response($html);
831c0676b2SGreg Roach    }
841c0676b2SGreg Roach
851c0676b2SGreg Roach    /**
861c0676b2SGreg Roach     * @param Request $request
87b6db7c1fSGreg Roach     * @param Tree    $tree
881c0676b2SGreg Roach     *
891c0676b2SGreg Roach     * @return Response
901c0676b2SGreg Roach     */
91b6db7c1fSGreg Roach    public function getDescendantsAction(Request $request, Tree $tree): Response
92c1010edaSGreg Roach    {
931c0676b2SGreg Roach        $xref = $request->get('xref');
941c0676b2SGreg Roach
951c0676b2SGreg Roach        $individual = Individual::getInstance($xref, $tree);
961c0676b2SGreg Roach
971c0676b2SGreg Roach        if ($individual !== null && $individual->canShow()) {
981c0676b2SGreg Roach            $html = $this->loadSpouses($individual, 1);
991c0676b2SGreg Roach        } else {
1001c0676b2SGreg Roach            $html = '';
1011c0676b2SGreg Roach        }
1021c0676b2SGreg Roach
1031c0676b2SGreg Roach        return new Response($html);
1041c0676b2SGreg Roach    }
1051c0676b2SGreg Roach
1068c2e8227SGreg Roach    /** {@inheritdoc} */
107*8f53f488SRico Sonntag    public function defaultSidebarOrder(): int
108c1010edaSGreg Roach    {
1098c2e8227SGreg Roach        return 30;
1108c2e8227SGreg Roach    }
1118c2e8227SGreg Roach
1128c2e8227SGreg Roach    /** {@inheritdoc} */
113*8f53f488SRico Sonntag    public function hasSidebarContent(Individual $individual): bool
114c1010edaSGreg Roach    {
11538a9583bSGreg Roach        return true;
1168c2e8227SGreg Roach    }
1178c2e8227SGreg Roach
11876692c8bSGreg Roach    /**
11976692c8bSGreg Roach     * Load this sidebar synchronously.
12076692c8bSGreg Roach     *
121225e381fSGreg Roach     * @param Individual $individual
122225e381fSGreg Roach     *
12376692c8bSGreg Roach     * @return string
12476692c8bSGreg Roach     */
125*8f53f488SRico Sonntag    public function getSidebarContent(Individual $individual): string
126c1010edaSGreg Roach    {
1271ef93f16SGreg Roach        return view('modules/descendancy/sidebar', [
128a817de92SGreg Roach            'individual_list' => $this->getPersonLi($individual, 1),
129a817de92SGreg Roach        ]);
1308c2e8227SGreg Roach    }
1318c2e8227SGreg Roach
1328c2e8227SGreg Roach    /**
13376692c8bSGreg Roach     * Format an individual in a list.
13476692c8bSGreg Roach     *
1358c2e8227SGreg Roach     * @param Individual $person
136cbc1590aSGreg Roach     * @param int        $generations
1378c2e8227SGreg Roach     *
1388c2e8227SGreg Roach     * @return string
1398c2e8227SGreg Roach     */
140*8f53f488SRico Sonntag    public function getPersonLi(Individual $person, $generations = 0): string
141c1010edaSGreg Roach    {
1428c2e8227SGreg Roach        $icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
1438c2e8227SGreg Roach        $lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : '';
1448c2e8227SGreg Roach        $spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
1452622ba92SGreg Roach
1462622ba92SGreg Roach        return
1472622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
148c1010edaSGreg Roach            '<a class="sb_desc_indi" href="' . e(route('module', [
149c1010edaSGreg Roach                'module' => 'descendancy',
150c1010edaSGreg Roach                'action' => 'Descendants',
151c1010edaSGreg Roach                'ged'    => $person->getTree()->getName(),
152c1010edaSGreg Roach                'xref'   => $person->getXref(),
153c1010edaSGreg Roach            ])) . '">' .
1542622ba92SGreg Roach            '<i class="plusminus ' . $icon . '"></i>' .
1552622ba92SGreg Roach            $person->getSexImage() . $person->getFullName() . $lifespan .
1562622ba92SGreg Roach            '</a>' .
157b1f1e4efSGreg Roach            FontAwesome::linkIcon('individual', $person->getFullName(), ['href' => $person->url()]) .
1582622ba92SGreg Roach            '<div>' . $spouses . '</div>' .
1592622ba92SGreg Roach            '</li>';
1608c2e8227SGreg Roach    }
1618c2e8227SGreg Roach
1628c2e8227SGreg Roach    /**
16376692c8bSGreg Roach     * Format a family in a list.
16476692c8bSGreg Roach     *
1658c2e8227SGreg Roach     * @param Family     $family
1668c2e8227SGreg Roach     * @param Individual $person
167cbc1590aSGreg Roach     * @param int        $generations
1688c2e8227SGreg Roach     *
1698c2e8227SGreg Roach     * @return string
1708c2e8227SGreg Roach     */
171*8f53f488SRico Sonntag    public function getFamilyLi(Family $family, Individual $person, $generations = 0): string
172c1010edaSGreg Roach    {
1732622ba92SGreg Roach        $spouse = $family->getSpouse($person);
1742622ba92SGreg Roach        if ($spouse) {
1752622ba92SGreg Roach            $spouse_name = $spouse->getSexImage() . $spouse->getFullName();
176b1f1e4efSGreg Roach            $spouse_link = FontAwesome::linkIcon('individual', $spouse->getFullName(), ['href' => $person->url()]);
1772622ba92SGreg Roach        } else {
1782622ba92SGreg Roach            $spouse_name = '';
1792622ba92SGreg Roach            $spouse_link = '';
1802622ba92SGreg Roach        }
1812622ba92SGreg Roach
1828c2e8227SGreg Roach        $marryear = $family->getMarriageYear();
1838c2e8227SGreg Roach        $marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
1842622ba92SGreg Roach
1852622ba92SGreg Roach        return
1862622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
1872622ba92SGreg Roach            '<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . $spouse_name . $marr . '</a>' .
1882622ba92SGreg Roach            $spouse_link .
189b1f1e4efSGreg Roach            FontAwesome::linkIcon('family', $family->getFullName(), ['href' => $family->url()]) .
1902622ba92SGreg Roach            '<div>' . $this->loadChildren($family, $generations) . '</div>' .
1912622ba92SGreg Roach            '</li>';
1928c2e8227SGreg Roach    }
1938c2e8227SGreg Roach
1948c2e8227SGreg Roach    /**
19576692c8bSGreg Roach     * Display spouses.
19676692c8bSGreg Roach     *
1978c2e8227SGreg Roach     * @param Individual $person
198cbc1590aSGreg Roach     * @param int        $generations
1998c2e8227SGreg Roach     *
2008c2e8227SGreg Roach     * @return string
2018c2e8227SGreg Roach     */
202c1010edaSGreg Roach    public function loadSpouses(Individual $person, $generations)
203c1010edaSGreg Roach    {
2048c2e8227SGreg Roach        $out = '';
2058c2e8227SGreg Roach        if ($person && $person->canShow()) {
2068c2e8227SGreg Roach            foreach ($person->getSpouseFamilies() as $family) {
2072622ba92SGreg Roach                $out .= $this->getFamilyLi($family, $person, $generations - 1);
2088c2e8227SGreg Roach            }
2098c2e8227SGreg Roach        }
2108c2e8227SGreg Roach        if ($out) {
2118c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2128c2e8227SGreg Roach        } else {
2138c2e8227SGreg Roach            return '';
2148c2e8227SGreg Roach        }
2158c2e8227SGreg Roach    }
2168c2e8227SGreg Roach
2178c2e8227SGreg Roach    /**
21876692c8bSGreg Roach     * Display descendants.
21976692c8bSGreg Roach     *
2208c2e8227SGreg Roach     * @param Family $family
221cbc1590aSGreg Roach     * @param int    $generations
2228c2e8227SGreg Roach     *
2238c2e8227SGreg Roach     * @return string
2248c2e8227SGreg Roach     */
225c1010edaSGreg Roach    public function loadChildren(Family $family, $generations)
226c1010edaSGreg Roach    {
2278c2e8227SGreg Roach        $out = '';
2288c2e8227SGreg Roach        if ($family->canShow()) {
2298c2e8227SGreg Roach            $children = $family->getChildren();
2308c2e8227SGreg Roach            if ($children) {
2318c2e8227SGreg Roach                foreach ($children as $child) {
2328c2e8227SGreg Roach                    $out .= $this->getPersonLi($child, $generations - 1);
2338c2e8227SGreg Roach                }
2348c2e8227SGreg Roach            } else {
2358c2e8227SGreg Roach                $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
2368c2e8227SGreg Roach            }
2378c2e8227SGreg Roach        }
2388c2e8227SGreg Roach        if ($out) {
2398c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2408c2e8227SGreg Roach        } else {
2418c2e8227SGreg Roach            return '';
2428c2e8227SGreg Roach        }
2438c2e8227SGreg Roach    }
2448c2e8227SGreg Roach}
245