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; 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 212de55a25SGreg Roachuse Fisharebest\Webtrees\FontAwesome; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 258c2e8227SGreg Roach 268c2e8227SGreg Roach/** 278c2e8227SGreg Roach * Class DescendancyModule 288c2e8227SGreg Roach */ 29e2a378d3SGreg Roachclass DescendancyModule extends AbstractModule implements ModuleSidebarInterface { 308c2e8227SGreg Roach /** {@inheritdoc} */ 318c2e8227SGreg Roach public function getTitle() { 328c2e8227SGreg Roach return /* I18N: Name of a module/sidebar */ 338c2e8227SGreg Roach I18N::translate('Descendants'); 348c2e8227SGreg Roach } 358c2e8227SGreg Roach 368c2e8227SGreg Roach /** {@inheritdoc} */ 378c2e8227SGreg Roach public function getDescription() { 388c2e8227SGreg Roach return /* I18N: Description of the “Descendants” module */ 398c2e8227SGreg Roach I18N::translate('A sidebar showing the descendants of an individual.'); 408c2e8227SGreg Roach } 418c2e8227SGreg Roach 4276692c8bSGreg Roach /** 4376692c8bSGreg Roach * This is a general purpose hook, allowing modules to respond to routes 4476692c8bSGreg Roach * of the form module.php?mod=FOO&mod_action=BAR 4576692c8bSGreg Roach * 4676692c8bSGreg Roach * @param string $mod_action 4776692c8bSGreg Roach */ 4876692c8bSGreg Roach public function modAction($mod_action) { 4924ec66ceSGreg Roach global $WT_TREE; 5024ec66ceSGreg Roach 518c2e8227SGreg Roach header('Content-Type: text/html; charset=UTF-8'); 528c2e8227SGreg Roach 5376692c8bSGreg Roach switch ($mod_action) { 548c2e8227SGreg Roach case 'search': 558c2e8227SGreg Roach $search = Filter::get('search'); 562622ba92SGreg Roach echo $this->search($search, $WT_TREE); 578c2e8227SGreg Roach break; 588c2e8227SGreg Roach case 'descendants': 5924ec66ceSGreg Roach $individual = Individual::getInstance(Filter::get('xref', WT_REGEX_XREF), $WT_TREE); 608c2e8227SGreg Roach if ($individual) { 618c2e8227SGreg Roach echo $this->loadSpouses($individual, 1); 628c2e8227SGreg Roach } 638c2e8227SGreg Roach break; 648c2e8227SGreg Roach default: 658c2e8227SGreg Roach http_response_code(404); 668c2e8227SGreg Roach break; 678c2e8227SGreg Roach } 688c2e8227SGreg Roach } 698c2e8227SGreg Roach 708c2e8227SGreg Roach /** {@inheritdoc} */ 718c2e8227SGreg Roach public function defaultSidebarOrder() { 728c2e8227SGreg Roach return 30; 738c2e8227SGreg Roach } 748c2e8227SGreg Roach 758c2e8227SGreg Roach /** {@inheritdoc} */ 76225e381fSGreg Roach public function hasSidebarContent(Individual $individual) { 7738a9583bSGreg Roach return true; 788c2e8227SGreg Roach } 798c2e8227SGreg Roach 808c2e8227SGreg Roach /** {@inheritdoc} */ 818c2e8227SGreg Roach public function getSidebarAjaxContent() { 828c2e8227SGreg Roach return ''; 838c2e8227SGreg Roach } 848c2e8227SGreg Roach 8576692c8bSGreg Roach /** 8676692c8bSGreg Roach * Load this sidebar synchronously. 8776692c8bSGreg Roach * 88225e381fSGreg Roach * @param Individual $individual 89225e381fSGreg Roach * 9076692c8bSGreg Roach * @return string 9176692c8bSGreg Roach */ 92225e381fSGreg Roach public function getSidebarContent(Individual $individual) { 93*1ef93f16SGreg Roach return view('modules/descendancy/sidebar', [ 94a817de92SGreg Roach 'individual_list' => $this->getPersonLi($individual, 1), 95a817de92SGreg Roach ]); 968c2e8227SGreg Roach } 978c2e8227SGreg Roach 988c2e8227SGreg Roach /** 9976692c8bSGreg Roach * Format an individual in a list. 10076692c8bSGreg Roach * 1018c2e8227SGreg Roach * @param Individual $person 102cbc1590aSGreg Roach * @param int $generations 1038c2e8227SGreg Roach * 1048c2e8227SGreg Roach * @return string 1058c2e8227SGreg Roach */ 1068c2e8227SGreg Roach public function getPersonLi(Individual $person, $generations = 0) { 1078c2e8227SGreg Roach $icon = $generations > 0 ? 'icon-minus' : 'icon-plus'; 1088c2e8227SGreg Roach $lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : ''; 1098c2e8227SGreg Roach $spouses = $generations > 0 ? $this->loadSpouses($person, 0) : ''; 1102622ba92SGreg Roach 1112622ba92SGreg Roach return 1122622ba92SGreg Roach '<li class="sb_desc_indi_li">' . 1132622ba92SGreg Roach '<a class="sb_desc_indi" href="module.php?mod=' . $this->getName() . '&mod_action=descendants&xref=' . $person->getXref() . '">' . 1142622ba92SGreg Roach '<i class="plusminus ' . $icon . '"></i>' . 1152622ba92SGreg Roach $person->getSexImage() . $person->getFullName() . $lifespan . 1162622ba92SGreg Roach '</a>' . 117b1f1e4efSGreg Roach FontAwesome::linkIcon('individual', $person->getFullName(), ['href' => $person->url()]) . 1182622ba92SGreg Roach '<div>' . $spouses . '</div>' . 1192622ba92SGreg Roach '</li>'; 1208c2e8227SGreg Roach } 1218c2e8227SGreg Roach 1228c2e8227SGreg Roach /** 12376692c8bSGreg Roach * Format a family in a list. 12476692c8bSGreg Roach * 1258c2e8227SGreg Roach * @param Family $family 1268c2e8227SGreg Roach * @param Individual $person 127cbc1590aSGreg Roach * @param int $generations 1288c2e8227SGreg Roach * 1298c2e8227SGreg Roach * @return string 1308c2e8227SGreg Roach */ 1318c2e8227SGreg Roach public function getFamilyLi(Family $family, Individual $person, $generations = 0) { 1322622ba92SGreg Roach $spouse = $family->getSpouse($person); 1332622ba92SGreg Roach if ($spouse) { 1342622ba92SGreg Roach $spouse_name = $spouse->getSexImage() . $spouse->getFullName(); 135b1f1e4efSGreg Roach $spouse_link = FontAwesome::linkIcon('individual', $spouse->getFullName(), ['href' => $person->url()]); 1362622ba92SGreg Roach } else { 1372622ba92SGreg Roach $spouse_name = ''; 1382622ba92SGreg Roach $spouse_link = ''; 1392622ba92SGreg Roach } 1402622ba92SGreg Roach 1418c2e8227SGreg Roach $marryear = $family->getMarriageYear(); 1428c2e8227SGreg Roach $marr = $marryear ? '<i class="icon-rings"></i>' . $marryear : ''; 1432622ba92SGreg Roach 1442622ba92SGreg Roach return 1452622ba92SGreg Roach '<li class="sb_desc_indi_li">' . 1462622ba92SGreg Roach '<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . $spouse_name . $marr . '</a>' . 1472622ba92SGreg Roach $spouse_link . 148b1f1e4efSGreg Roach FontAwesome::linkIcon('family', $family->getFullName(), ['href' => $family->url()]) . 1492622ba92SGreg Roach '<div>' . $this->loadChildren($family, $generations) . '</div>' . 1502622ba92SGreg Roach '</li>'; 1518c2e8227SGreg Roach } 1528c2e8227SGreg Roach 1538c2e8227SGreg Roach /** 15476692c8bSGreg Roach * Respond to an autocomplete search request. 15576692c8bSGreg Roach * 1562622ba92SGreg Roach * @param string $query Search for this term 1572622ba92SGreg Roach * @param Tree $tree Search in this tree 1588c2e8227SGreg Roach * 1598c2e8227SGreg Roach * @return string 1608c2e8227SGreg Roach */ 1612622ba92SGreg Roach public function search($query, Tree $tree) { 1628c2e8227SGreg Roach if (strlen($query) < 2) { 1638c2e8227SGreg Roach return ''; 1648c2e8227SGreg Roach } 1652622ba92SGreg Roach 1668c2e8227SGreg Roach $rows = Database::prepare( 1678c2e8227SGreg Roach "SELECT i_id AS xref" . 1682622ba92SGreg Roach " FROM `##individuals`" . 1692622ba92SGreg Roach " JOIN `##name` ON i_id = n_id AND i_file = n_file" . 1702622ba92SGreg Roach " WHERE n_sort LIKE CONCAT('%', :query, '%') AND i_file = :tree_id" . 1718c2e8227SGreg Roach " ORDER BY n_sort" 17213abd6f3SGreg Roach )->execute([ 1732622ba92SGreg Roach 'query' => $query, 1742622ba92SGreg Roach 'tree_id' => $tree->getTreeId(), 17513abd6f3SGreg Roach ])->fetchAll(); 1768c2e8227SGreg Roach 1778c2e8227SGreg Roach $out = ''; 1788c2e8227SGreg Roach foreach ($rows as $row) { 1792622ba92SGreg Roach $person = Individual::getInstance($row->xref, $tree); 180202f95afSGreg Roach if ($person && $person->canShowName()) { 1818c2e8227SGreg Roach $out .= $this->getPersonLi($person); 1828c2e8227SGreg Roach } 1838c2e8227SGreg Roach } 1848c2e8227SGreg Roach if ($out) { 1858c2e8227SGreg Roach return '<ul>' . $out . '</ul>'; 1868c2e8227SGreg Roach } else { 1878c2e8227SGreg Roach return ''; 1888c2e8227SGreg Roach } 1898c2e8227SGreg Roach } 1908c2e8227SGreg Roach 1918c2e8227SGreg Roach /** 19276692c8bSGreg Roach * Display spouses. 19376692c8bSGreg Roach * 1948c2e8227SGreg Roach * @param Individual $person 195cbc1590aSGreg Roach * @param int $generations 1968c2e8227SGreg Roach * 1978c2e8227SGreg Roach * @return string 1988c2e8227SGreg Roach */ 1998c2e8227SGreg Roach public function loadSpouses(Individual $person, $generations) { 2008c2e8227SGreg Roach $out = ''; 2018c2e8227SGreg Roach if ($person && $person->canShow()) { 2028c2e8227SGreg Roach foreach ($person->getSpouseFamilies() as $family) { 2032622ba92SGreg Roach $out .= $this->getFamilyLi($family, $person, $generations - 1); 2048c2e8227SGreg Roach } 2058c2e8227SGreg Roach } 2068c2e8227SGreg Roach if ($out) { 2078c2e8227SGreg Roach return '<ul>' . $out . '</ul>'; 2088c2e8227SGreg Roach } else { 2098c2e8227SGreg Roach return ''; 2108c2e8227SGreg Roach } 2118c2e8227SGreg Roach } 2128c2e8227SGreg Roach 2138c2e8227SGreg Roach /** 21476692c8bSGreg Roach * Display descendants. 21576692c8bSGreg Roach * 2168c2e8227SGreg Roach * @param Family $family 217cbc1590aSGreg Roach * @param int $generations 2188c2e8227SGreg Roach * 2198c2e8227SGreg Roach * @return string 2208c2e8227SGreg Roach */ 2218c2e8227SGreg Roach public function loadChildren(Family $family, $generations) { 2228c2e8227SGreg Roach $out = ''; 2238c2e8227SGreg Roach if ($family->canShow()) { 2248c2e8227SGreg Roach $children = $family->getChildren(); 2258c2e8227SGreg Roach if ($children) { 2268c2e8227SGreg Roach foreach ($children as $child) { 2278c2e8227SGreg Roach $out .= $this->getPersonLi($child, $generations - 1); 2288c2e8227SGreg Roach } 2298c2e8227SGreg Roach } else { 2308c2e8227SGreg Roach $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>'; 2318c2e8227SGreg Roach } 2328c2e8227SGreg Roach } 2338c2e8227SGreg Roach if ($out) { 2348c2e8227SGreg Roach return '<ul>' . $out . '</ul>'; 2358c2e8227SGreg Roach } else { 2368c2e8227SGreg Roach return ''; 2378c2e8227SGreg Roach } 2388c2e8227SGreg Roach } 2398c2e8227SGreg Roach} 240