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; 24*1c0676b2SGreg Roachuse Symfony\Component\HttpFoundation\Request; 25*1c0676b2SGreg Roachuse Symfony\Component\HttpFoundation\Response; 268c2e8227SGreg Roach 278c2e8227SGreg Roach/** 288c2e8227SGreg Roach * Class DescendancyModule 298c2e8227SGreg Roach */ 30e2a378d3SGreg Roachclass DescendancyModule extends AbstractModule implements ModuleSidebarInterface { 318c2e8227SGreg Roach /** {@inheritdoc} */ 328c2e8227SGreg Roach public function getTitle() { 338c2e8227SGreg Roach return /* I18N: Name of a module/sidebar */ 348c2e8227SGreg Roach I18N::translate('Descendants'); 358c2e8227SGreg Roach } 368c2e8227SGreg Roach 378c2e8227SGreg Roach /** {@inheritdoc} */ 388c2e8227SGreg Roach public function getDescription() { 398c2e8227SGreg Roach return /* I18N: Description of the “Descendants” module */ 408c2e8227SGreg Roach I18N::translate('A sidebar showing the descendants of an individual.'); 418c2e8227SGreg Roach } 428c2e8227SGreg Roach 4376692c8bSGreg Roach /** 44*1c0676b2SGreg Roach * @param Request $request 4576692c8bSGreg Roach * 46*1c0676b2SGreg Roach * @return Response 4776692c8bSGreg Roach */ 48*1c0676b2SGreg Roach public function getSearchAction(Request $request): Response { 49*1c0676b2SGreg Roach /** @var Tree $tree */ 50*1c0676b2SGreg Roach $tree = $request->attributes->get('tree'); 5124ec66ceSGreg Roach 52*1c0676b2SGreg Roach $search = $request->get('search', ''); 538c2e8227SGreg Roach 54*1c0676b2SGreg Roach $html = ''; 55*1c0676b2SGreg Roach 56*1c0676b2SGreg Roach if (strlen($search) >= 2) { 57*1c0676b2SGreg Roach $rows = Database::prepare( 58*1c0676b2SGreg Roach "SELECT i_id AS xref" . 59*1c0676b2SGreg Roach " FROM `##individuals`" . 60*1c0676b2SGreg Roach " JOIN `##name` ON i_id = n_id AND i_file = n_file" . 61*1c0676b2SGreg Roach " WHERE n_sort LIKE CONCAT('%', :query, '%') AND i_file = :tree_id" . 62*1c0676b2SGreg Roach " ORDER BY n_sort" 63*1c0676b2SGreg Roach )->execute([ 64*1c0676b2SGreg Roach 'query' => $search, 65*1c0676b2SGreg Roach 'tree_id' => $tree->getTreeId(), 66*1c0676b2SGreg Roach ])->fetchAll(); 67*1c0676b2SGreg Roach 68*1c0676b2SGreg Roach foreach ($rows as $row) { 69*1c0676b2SGreg Roach $individual = Individual::getInstance($row->xref, $tree); 70*1c0676b2SGreg Roach if ($individual !== null && $individual->canShow()) { 71*1c0676b2SGreg Roach $html .= $this->getPersonLi($individual); 728c2e8227SGreg Roach } 738c2e8227SGreg Roach } 748c2e8227SGreg Roach } 758c2e8227SGreg Roach 76*1c0676b2SGreg Roach if ($html !== '') { 77*1c0676b2SGreg Roach $html = '<ul>' . $html . '</ul>'; 78*1c0676b2SGreg Roach } 79*1c0676b2SGreg Roach 80*1c0676b2SGreg Roach return new Response($html); 81*1c0676b2SGreg Roach } 82*1c0676b2SGreg Roach 83*1c0676b2SGreg Roach /** 84*1c0676b2SGreg Roach * @param Request $request 85*1c0676b2SGreg Roach * 86*1c0676b2SGreg Roach * @return Response 87*1c0676b2SGreg Roach */ 88*1c0676b2SGreg Roach public function getDescendantsAction(Request $request): Response { 89*1c0676b2SGreg Roach /** @var Tree $tree */ 90*1c0676b2SGreg Roach $tree = $request->attributes->get('tree'); 91*1c0676b2SGreg Roach 92*1c0676b2SGreg Roach $xref = $request->get('xref'); 93*1c0676b2SGreg Roach 94*1c0676b2SGreg Roach $individual = Individual::getInstance($xref, $tree); 95*1c0676b2SGreg Roach 96*1c0676b2SGreg Roach if ($individual !== null && $individual->canShow()) { 97*1c0676b2SGreg Roach $html = $this->loadSpouses($individual, 1); 98*1c0676b2SGreg Roach } else { 99*1c0676b2SGreg Roach $html = ''; 100*1c0676b2SGreg Roach } 101*1c0676b2SGreg Roach 102*1c0676b2SGreg Roach return new Response($html); 103*1c0676b2SGreg Roach } 104*1c0676b2SGreg Roach 1058c2e8227SGreg Roach /** {@inheritdoc} */ 1068c2e8227SGreg Roach public function defaultSidebarOrder() { 1078c2e8227SGreg Roach return 30; 1088c2e8227SGreg Roach } 1098c2e8227SGreg Roach 1108c2e8227SGreg Roach /** {@inheritdoc} */ 111225e381fSGreg Roach public function hasSidebarContent(Individual $individual) { 11238a9583bSGreg Roach return true; 1138c2e8227SGreg Roach } 1148c2e8227SGreg Roach 1158c2e8227SGreg Roach /** {@inheritdoc} */ 1168c2e8227SGreg Roach public function getSidebarAjaxContent() { 1178c2e8227SGreg Roach return ''; 1188c2e8227SGreg Roach } 1198c2e8227SGreg Roach 12076692c8bSGreg Roach /** 12176692c8bSGreg Roach * Load this sidebar synchronously. 12276692c8bSGreg Roach * 123225e381fSGreg Roach * @param Individual $individual 124225e381fSGreg Roach * 12576692c8bSGreg Roach * @return string 12676692c8bSGreg Roach */ 127225e381fSGreg Roach public function getSidebarContent(Individual $individual) { 1281ef93f16SGreg Roach return view('modules/descendancy/sidebar', [ 129a817de92SGreg Roach 'individual_list' => $this->getPersonLi($individual, 1), 130a817de92SGreg Roach ]); 1318c2e8227SGreg Roach } 1328c2e8227SGreg Roach 1338c2e8227SGreg Roach /** 13476692c8bSGreg Roach * Format an individual in a list. 13576692c8bSGreg Roach * 1368c2e8227SGreg Roach * @param Individual $person 137cbc1590aSGreg Roach * @param int $generations 1388c2e8227SGreg Roach * 1398c2e8227SGreg Roach * @return string 1408c2e8227SGreg Roach */ 1418c2e8227SGreg Roach public function getPersonLi(Individual $person, $generations = 0) { 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">' . 148*1c0676b2SGreg Roach '<a class="sb_desc_indi" href="' . e(route('module', ['module' => 'descendancy', 'action' => 'Descendants', 'ged' => $person->getTree()->getName(), 'xref' => $person->getXref()])) . '">' . 1492622ba92SGreg Roach '<i class="plusminus ' . $icon . '"></i>' . 1502622ba92SGreg Roach $person->getSexImage() . $person->getFullName() . $lifespan . 1512622ba92SGreg Roach '</a>' . 152b1f1e4efSGreg Roach FontAwesome::linkIcon('individual', $person->getFullName(), ['href' => $person->url()]) . 1532622ba92SGreg Roach '<div>' . $spouses . '</div>' . 1542622ba92SGreg Roach '</li>'; 1558c2e8227SGreg Roach } 1568c2e8227SGreg Roach 1578c2e8227SGreg Roach /** 15876692c8bSGreg Roach * Format a family in a list. 15976692c8bSGreg Roach * 1608c2e8227SGreg Roach * @param Family $family 1618c2e8227SGreg Roach * @param Individual $person 162cbc1590aSGreg Roach * @param int $generations 1638c2e8227SGreg Roach * 1648c2e8227SGreg Roach * @return string 1658c2e8227SGreg Roach */ 1668c2e8227SGreg Roach public function getFamilyLi(Family $family, Individual $person, $generations = 0) { 1672622ba92SGreg Roach $spouse = $family->getSpouse($person); 1682622ba92SGreg Roach if ($spouse) { 1692622ba92SGreg Roach $spouse_name = $spouse->getSexImage() . $spouse->getFullName(); 170b1f1e4efSGreg Roach $spouse_link = FontAwesome::linkIcon('individual', $spouse->getFullName(), ['href' => $person->url()]); 1712622ba92SGreg Roach } else { 1722622ba92SGreg Roach $spouse_name = ''; 1732622ba92SGreg Roach $spouse_link = ''; 1742622ba92SGreg Roach } 1752622ba92SGreg Roach 1768c2e8227SGreg Roach $marryear = $family->getMarriageYear(); 1778c2e8227SGreg Roach $marr = $marryear ? '<i class="icon-rings"></i>' . $marryear : ''; 1782622ba92SGreg Roach 1792622ba92SGreg Roach return 1802622ba92SGreg Roach '<li class="sb_desc_indi_li">' . 1812622ba92SGreg Roach '<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . $spouse_name . $marr . '</a>' . 1822622ba92SGreg Roach $spouse_link . 183b1f1e4efSGreg Roach FontAwesome::linkIcon('family', $family->getFullName(), ['href' => $family->url()]) . 1842622ba92SGreg Roach '<div>' . $this->loadChildren($family, $generations) . '</div>' . 1852622ba92SGreg Roach '</li>'; 1868c2e8227SGreg Roach } 1878c2e8227SGreg Roach 1888c2e8227SGreg Roach /** 18976692c8bSGreg Roach * Display spouses. 19076692c8bSGreg Roach * 1918c2e8227SGreg Roach * @param Individual $person 192cbc1590aSGreg Roach * @param int $generations 1938c2e8227SGreg Roach * 1948c2e8227SGreg Roach * @return string 1958c2e8227SGreg Roach */ 1968c2e8227SGreg Roach public function loadSpouses(Individual $person, $generations) { 1978c2e8227SGreg Roach $out = ''; 1988c2e8227SGreg Roach if ($person && $person->canShow()) { 1998c2e8227SGreg Roach foreach ($person->getSpouseFamilies() as $family) { 2002622ba92SGreg Roach $out .= $this->getFamilyLi($family, $person, $generations - 1); 2018c2e8227SGreg Roach } 2028c2e8227SGreg Roach } 2038c2e8227SGreg Roach if ($out) { 2048c2e8227SGreg Roach return '<ul>' . $out . '</ul>'; 2058c2e8227SGreg Roach } else { 2068c2e8227SGreg Roach return ''; 2078c2e8227SGreg Roach } 2088c2e8227SGreg Roach } 2098c2e8227SGreg Roach 2108c2e8227SGreg Roach /** 21176692c8bSGreg Roach * Display descendants. 21276692c8bSGreg Roach * 2138c2e8227SGreg Roach * @param Family $family 214cbc1590aSGreg Roach * @param int $generations 2158c2e8227SGreg Roach * 2168c2e8227SGreg Roach * @return string 2178c2e8227SGreg Roach */ 2188c2e8227SGreg Roach public function loadChildren(Family $family, $generations) { 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 } else { 2338c2e8227SGreg Roach return ''; 2348c2e8227SGreg Roach } 2358c2e8227SGreg Roach } 2368c2e8227SGreg Roach} 237