xref: /webtrees/app/Module/DescendancyModule.php (revision c1010eda29c0909ed4d5d463f32d32bfefdd4dfe)
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 */
30*c1010edaSGreg Roachclass DescendancyModule extends AbstractModule implements ModuleSidebarInterface
31*c1010edaSGreg Roach{
328c2e8227SGreg Roach    /** {@inheritdoc} */
33*c1010edaSGreg Roach    public function getTitle()
34*c1010edaSGreg Roach    {
358c2e8227SGreg Roach        return /* I18N: Name of a module/sidebar */
368c2e8227SGreg Roach            I18N::translate('Descendants');
378c2e8227SGreg Roach    }
388c2e8227SGreg Roach
398c2e8227SGreg Roach    /** {@inheritdoc} */
40*c1010edaSGreg Roach    public function getDescription()
41*c1010edaSGreg Roach    {
428c2e8227SGreg Roach        return /* I18N: Description of the “Descendants” module */
438c2e8227SGreg Roach            I18N::translate('A sidebar showing the descendants of an individual.');
448c2e8227SGreg Roach    }
458c2e8227SGreg Roach
4676692c8bSGreg Roach    /**
471c0676b2SGreg Roach     * @param Request $request
4876692c8bSGreg Roach     *
491c0676b2SGreg Roach     * @return Response
5076692c8bSGreg Roach     */
51*c1010edaSGreg Roach    public function getSearchAction(Request $request): Response
52*c1010edaSGreg Roach    {
531c0676b2SGreg Roach        /** @var Tree $tree */
541c0676b2SGreg Roach        $tree = $request->attributes->get('tree');
5524ec66ceSGreg Roach
561c0676b2SGreg Roach        $search = $request->get('search', '');
578c2e8227SGreg Roach
581c0676b2SGreg Roach        $html = '';
591c0676b2SGreg Roach
601c0676b2SGreg Roach        if (strlen($search) >= 2) {
611c0676b2SGreg Roach            $rows = Database::prepare(
621c0676b2SGreg Roach                "SELECT i_id AS xref" .
631c0676b2SGreg Roach                " FROM `##individuals`" .
641c0676b2SGreg Roach                " JOIN `##name` ON i_id = n_id AND i_file = n_file" .
651c0676b2SGreg Roach                " WHERE n_sort LIKE CONCAT('%', :query, '%') AND i_file = :tree_id" .
661c0676b2SGreg Roach                " ORDER BY n_sort"
671c0676b2SGreg Roach            )->execute([
681c0676b2SGreg Roach                'query'   => $search,
691c0676b2SGreg Roach                'tree_id' => $tree->getTreeId(),
701c0676b2SGreg Roach            ])->fetchAll();
711c0676b2SGreg Roach
721c0676b2SGreg Roach            foreach ($rows as $row) {
731c0676b2SGreg Roach                $individual = Individual::getInstance($row->xref, $tree);
741c0676b2SGreg Roach                if ($individual !== null && $individual->canShow()) {
751c0676b2SGreg Roach                    $html .= $this->getPersonLi($individual);
768c2e8227SGreg Roach                }
778c2e8227SGreg Roach            }
788c2e8227SGreg Roach        }
798c2e8227SGreg Roach
801c0676b2SGreg Roach        if ($html !== '') {
811c0676b2SGreg Roach            $html = '<ul>' . $html . '</ul>';
821c0676b2SGreg Roach        }
831c0676b2SGreg Roach
841c0676b2SGreg Roach        return new Response($html);
851c0676b2SGreg Roach    }
861c0676b2SGreg Roach
871c0676b2SGreg Roach    /**
881c0676b2SGreg Roach     * @param Request $request
891c0676b2SGreg Roach     *
901c0676b2SGreg Roach     * @return Response
911c0676b2SGreg Roach     */
92*c1010edaSGreg Roach    public function getDescendantsAction(Request $request): Response
93*c1010edaSGreg Roach    {
941c0676b2SGreg Roach        /** @var Tree $tree */
951c0676b2SGreg Roach        $tree = $request->attributes->get('tree');
961c0676b2SGreg Roach
971c0676b2SGreg Roach        $xref = $request->get('xref');
981c0676b2SGreg Roach
991c0676b2SGreg Roach        $individual = Individual::getInstance($xref, $tree);
1001c0676b2SGreg Roach
1011c0676b2SGreg Roach        if ($individual !== null && $individual->canShow()) {
1021c0676b2SGreg Roach            $html = $this->loadSpouses($individual, 1);
1031c0676b2SGreg Roach        } else {
1041c0676b2SGreg Roach            $html = '';
1051c0676b2SGreg Roach        }
1061c0676b2SGreg Roach
1071c0676b2SGreg Roach        return new Response($html);
1081c0676b2SGreg Roach    }
1091c0676b2SGreg Roach
1108c2e8227SGreg Roach    /** {@inheritdoc} */
111*c1010edaSGreg Roach    public function defaultSidebarOrder()
112*c1010edaSGreg Roach    {
1138c2e8227SGreg Roach        return 30;
1148c2e8227SGreg Roach    }
1158c2e8227SGreg Roach
1168c2e8227SGreg Roach    /** {@inheritdoc} */
117*c1010edaSGreg Roach    public function hasSidebarContent(Individual $individual)
118*c1010edaSGreg Roach    {
11938a9583bSGreg Roach        return true;
1208c2e8227SGreg Roach    }
1218c2e8227SGreg Roach
12276692c8bSGreg Roach    /**
12376692c8bSGreg Roach     * Load this sidebar synchronously.
12476692c8bSGreg Roach     *
125225e381fSGreg Roach     * @param Individual $individual
126225e381fSGreg Roach     *
12776692c8bSGreg Roach     * @return string
12876692c8bSGreg Roach     */
129*c1010edaSGreg Roach    public function getSidebarContent(Individual $individual)
130*c1010edaSGreg Roach    {
1311ef93f16SGreg Roach        return view('modules/descendancy/sidebar', [
132a817de92SGreg Roach            'individual_list' => $this->getPersonLi($individual, 1),
133a817de92SGreg Roach        ]);
1348c2e8227SGreg Roach    }
1358c2e8227SGreg Roach
1368c2e8227SGreg Roach    /**
13776692c8bSGreg Roach     * Format an individual in a list.
13876692c8bSGreg Roach     *
1398c2e8227SGreg Roach     * @param Individual $person
140cbc1590aSGreg Roach     * @param int        $generations
1418c2e8227SGreg Roach     *
1428c2e8227SGreg Roach     * @return string
1438c2e8227SGreg Roach     */
144*c1010edaSGreg Roach    public function getPersonLi(Individual $person, $generations = 0)
145*c1010edaSGreg Roach    {
1468c2e8227SGreg Roach        $icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
1478c2e8227SGreg Roach        $lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : '';
1488c2e8227SGreg Roach        $spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
1492622ba92SGreg Roach
1502622ba92SGreg Roach        return
1512622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
152*c1010edaSGreg Roach            '<a class="sb_desc_indi" href="' . e(route('module', [
153*c1010edaSGreg Roach                'module' => 'descendancy',
154*c1010edaSGreg Roach                'action' => 'Descendants',
155*c1010edaSGreg Roach                'ged'    => $person->getTree()->getName(),
156*c1010edaSGreg Roach                'xref'   => $person->getXref(),
157*c1010edaSGreg Roach            ])) . '">' .
1582622ba92SGreg Roach            '<i class="plusminus ' . $icon . '"></i>' .
1592622ba92SGreg Roach            $person->getSexImage() . $person->getFullName() . $lifespan .
1602622ba92SGreg Roach            '</a>' .
161b1f1e4efSGreg Roach            FontAwesome::linkIcon('individual', $person->getFullName(), ['href' => $person->url()]) .
1622622ba92SGreg Roach            '<div>' . $spouses . '</div>' .
1632622ba92SGreg Roach            '</li>';
1648c2e8227SGreg Roach    }
1658c2e8227SGreg Roach
1668c2e8227SGreg Roach    /**
16776692c8bSGreg Roach     * Format a family in a list.
16876692c8bSGreg Roach     *
1698c2e8227SGreg Roach     * @param Family     $family
1708c2e8227SGreg Roach     * @param Individual $person
171cbc1590aSGreg Roach     * @param int        $generations
1728c2e8227SGreg Roach     *
1738c2e8227SGreg Roach     * @return string
1748c2e8227SGreg Roach     */
175*c1010edaSGreg Roach    public function getFamilyLi(Family $family, Individual $person, $generations = 0)
176*c1010edaSGreg Roach    {
1772622ba92SGreg Roach        $spouse = $family->getSpouse($person);
1782622ba92SGreg Roach        if ($spouse) {
1792622ba92SGreg Roach            $spouse_name = $spouse->getSexImage() . $spouse->getFullName();
180b1f1e4efSGreg Roach            $spouse_link = FontAwesome::linkIcon('individual', $spouse->getFullName(), ['href' => $person->url()]);
1812622ba92SGreg Roach        } else {
1822622ba92SGreg Roach            $spouse_name = '';
1832622ba92SGreg Roach            $spouse_link = '';
1842622ba92SGreg Roach        }
1852622ba92SGreg Roach
1868c2e8227SGreg Roach        $marryear = $family->getMarriageYear();
1878c2e8227SGreg Roach        $marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
1882622ba92SGreg Roach
1892622ba92SGreg Roach        return
1902622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
1912622ba92SGreg Roach            '<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . $spouse_name . $marr . '</a>' .
1922622ba92SGreg Roach            $spouse_link .
193b1f1e4efSGreg Roach            FontAwesome::linkIcon('family', $family->getFullName(), ['href' => $family->url()]) .
1942622ba92SGreg Roach            '<div>' . $this->loadChildren($family, $generations) . '</div>' .
1952622ba92SGreg Roach            '</li>';
1968c2e8227SGreg Roach    }
1978c2e8227SGreg Roach
1988c2e8227SGreg Roach    /**
19976692c8bSGreg Roach     * Display spouses.
20076692c8bSGreg Roach     *
2018c2e8227SGreg Roach     * @param Individual $person
202cbc1590aSGreg Roach     * @param int        $generations
2038c2e8227SGreg Roach     *
2048c2e8227SGreg Roach     * @return string
2058c2e8227SGreg Roach     */
206*c1010edaSGreg Roach    public function loadSpouses(Individual $person, $generations)
207*c1010edaSGreg Roach    {
2088c2e8227SGreg Roach        $out = '';
2098c2e8227SGreg Roach        if ($person && $person->canShow()) {
2108c2e8227SGreg Roach            foreach ($person->getSpouseFamilies() as $family) {
2112622ba92SGreg Roach                $out .= $this->getFamilyLi($family, $person, $generations - 1);
2128c2e8227SGreg Roach            }
2138c2e8227SGreg Roach        }
2148c2e8227SGreg Roach        if ($out) {
2158c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2168c2e8227SGreg Roach        } else {
2178c2e8227SGreg Roach            return '';
2188c2e8227SGreg Roach        }
2198c2e8227SGreg Roach    }
2208c2e8227SGreg Roach
2218c2e8227SGreg Roach    /**
22276692c8bSGreg Roach     * Display descendants.
22376692c8bSGreg Roach     *
2248c2e8227SGreg Roach     * @param Family $family
225cbc1590aSGreg Roach     * @param int    $generations
2268c2e8227SGreg Roach     *
2278c2e8227SGreg Roach     * @return string
2288c2e8227SGreg Roach     */
229*c1010edaSGreg Roach    public function loadChildren(Family $family, $generations)
230*c1010edaSGreg Roach    {
2318c2e8227SGreg Roach        $out = '';
2328c2e8227SGreg Roach        if ($family->canShow()) {
2338c2e8227SGreg Roach            $children = $family->getChildren();
2348c2e8227SGreg Roach            if ($children) {
2358c2e8227SGreg Roach                foreach ($children as $child) {
2368c2e8227SGreg Roach                    $out .= $this->getPersonLi($child, $generations - 1);
2378c2e8227SGreg Roach                }
2388c2e8227SGreg Roach            } else {
2398c2e8227SGreg Roach                $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
2408c2e8227SGreg Roach            }
2418c2e8227SGreg Roach        }
2428c2e8227SGreg Roach        if ($out) {
2438c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2448c2e8227SGreg Roach        } else {
2458c2e8227SGreg Roach            return '';
2468c2e8227SGreg Roach        }
2478c2e8227SGreg Roach    }
2488c2e8227SGreg Roach}
249