.
*/
namespace Fisharebest\Webtrees\Module;
use Fisharebest\Webtrees\Database;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\Filter;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Tree;
/**
* Class DescendancyModule
*/
class DescendancyModule extends AbstractModule implements ModuleSidebarInterface {
/** {@inheritdoc} */
public function getTitle() {
return /* I18N: Name of a module/sidebar */
I18N::translate('Descendants');
}
/** {@inheritdoc} */
public function getDescription() {
return /* I18N: Description of the “Descendants” module */
I18N::translate('A sidebar showing the descendants of an individual.');
}
/**
* This is a general purpose hook, allowing modules to respond to routes
* of the form module.php?mod=FOO&mod_action=BAR
*
* @param string $mod_action
*/
public function modAction($mod_action) {
global $WT_TREE;
header('Content-Type: text/html; charset=UTF-8');
switch ($mod_action) {
case 'search':
$search = Filter::get('search');
echo $this->search($search, $WT_TREE);
break;
case 'descendants':
$individual = Individual::getInstance(Filter::get('xref', WT_REGEX_XREF), $WT_TREE);
if ($individual) {
echo $this->loadSpouses($individual, 1);
}
break;
default:
http_response_code(404);
break;
}
}
/** {@inheritdoc} */
public function defaultSidebarOrder() {
return 30;
}
/** {@inheritdoc} */
public function hasSidebarContent() {
return true;
}
/** {@inheritdoc} */
public function getSidebarAjaxContent() {
return '';
}
/**
* Load this sidebar synchronously.
*
* @return string
*/
public function getSidebarContent() {
global $controller;
$controller->addInlineJavascript('
function dsearchQ() {
var query = jQuery("#sb_desc_name").val();
if (query.length>1) {
jQuery("#sb_desc_content").load("module.php?mod=' . $this->getName() . '&mod_action=search&search="+query);
}
}
jQuery("#sb_desc_name").focus(function(){this.select();});
jQuery("#sb_desc_name").blur(function(){if (this.value=="") this.value="' . I18N::translate('Search') . '";});
var dtimerid = null;
jQuery("#sb_desc_name").keyup(function(e) {
if (dtimerid) window.clearTimeout(dtimerid);
dtimerid = window.setTimeout("dsearchQ()", 500);
});
jQuery("#sb_desc_content").on("click", ".sb_desc_indi", function() {
var self = jQuery(this),
state = self.children(".plusminus"),
target = self.siblings("div");
if(state.hasClass("icon-plus")) {
if (jQuery.trim(target.html())) {
target.show("fast"); // already got content so just show it
} else {
target
.hide()
.load(self.attr("href"), function(response, status, xhr) {
if(status == "success" && response !== "") {
target.show("fast");
}
})
}
} else {
target.hide("fast");
}
state.toggleClass("icon-minus icon-plus");
return false;
});
');
return
'
' .
'' .
'
' . $this->getPersonLi($controller->record, 1) . '
' .
'
';
}
/**
* Format an individual in a list.
*
* @param Individual $person
* @param int $generations
*
* @return string
*/
public function getPersonLi(Individual $person, $generations = 0) {
$icon = $generations > 0 ? 'icon-minus' : 'icon-plus';
$lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : '';
$spouses = $generations > 0 ? $this->loadSpouses($person, 0) : '';
return
'' .
'' .
'' .
$person->getSexImage() . $person->getFullName() . $lifespan .
'' .
'' .
'' . $spouses . '
' .
'';
}
/**
* Format a family in a list.
*
* @param Family $family
* @param Individual $person
* @param int $generations
*
* @return string
*/
public function getFamilyLi(Family $family, Individual $person, $generations = 0) {
$spouse = $family->getSpouse($person);
if ($spouse) {
$spouse_name = $spouse->getSexImage() . $spouse->getFullName();
$spouse_link = '';
} else {
$spouse_name = '';
$spouse_link = '';
}
$marryear = $family->getMarriageYear();
$marr = $marryear ? '' . $marryear : '';
return
'' .
'' . $spouse_name . $marr . '' .
$spouse_link .
'' .
'' . $this->loadChildren($family, $generations) . '
' .
'';
}
/**
* Respond to an autocomplete search request.
*
* @param string $query Search for this term
* @param Tree $tree Search in this tree
*
* @return string
*/
public function search($query, Tree $tree) {
if (strlen($query) < 2) {
return '';
}
$rows = Database::prepare(
"SELECT i_id AS xref" .
" FROM `##individuals`" .
" JOIN `##name` ON i_id = n_id AND i_file = n_file" .
" WHERE n_sort LIKE CONCAT('%', :query, '%') AND i_file = :tree_id" .
" ORDER BY n_sort"
)->execute([
'query' => $query,
'tree_id' => $tree->getTreeId(),
])->fetchAll();
$out = '';
foreach ($rows as $row) {
$person = Individual::getInstance($row->xref, $tree);
if ($person && $person->canShowName()) {
$out .= $this->getPersonLi($person);
}
}
if ($out) {
return '';
} else {
return '';
}
}
/**
* Display spouses.
*
* @param Individual $person
* @param int $generations
*
* @return string
*/
public function loadSpouses(Individual $person, $generations) {
$out = '';
if ($person && $person->canShow()) {
foreach ($person->getSpouseFamilies() as $family) {
$out .= $this->getFamilyLi($family, $person, $generations - 1);
}
}
if ($out) {
return '';
} else {
return '';
}
}
/**
* Display descendants.
*
* @param Family $family
* @param int $generations
*
* @return string
*/
public function loadChildren(Family $family, $generations) {
$out = '';
if ($family->canShow()) {
$children = $family->getChildren();
if ($children) {
foreach ($children as $child) {
$out .= $this->getPersonLi($child, $generations - 1);
}
} else {
$out .= '' . I18N::translate('No children') . '';
}
}
if ($out) {
return '';
} else {
return '';
}
}
}