. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Module; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\Family; use Fisharebest\Webtrees\FontAwesome; use Fisharebest\Webtrees\Functions\FunctionsCharts; use Fisharebest\Webtrees\Functions\FunctionsPrint; use Fisharebest\Webtrees\Gedcom; use Fisharebest\Webtrees\GedcomTag; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Individual; use Fisharebest\Webtrees\Menu; use Fisharebest\Webtrees\Services\ChartService; use Fisharebest\Webtrees\Theme; use Fisharebest\Webtrees\Tree; use Illuminate\Support\Collection; use Ramsey\Uuid\Uuid; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; /** * Class DescendancyChartModule */ class DescendancyChartModule extends AbstractModule implements ModuleInterface, ModuleChartInterface { use ModuleChartTrait; // Chart styles private const CHART_STYLE_LIST = 0; private const CHART_STYLE_BOOKLET = 1; private const CHART_STYLE_INDIVIDUALS = 2; private const CHART_STYLE_FAMILIES = 3; // Defaults private const DEFAULT_STYLE = self::CHART_STYLE_LIST; private const DEFAULT_GENERATIONS = '3'; private const DEFAULT_MAXIMUM_GENERATIONS = '9'; /** @var int[] */ protected $dabo_num = []; /** @var string[] */ protected $dabo_sex = []; /** * How should this module be labelled on tabs, menus, etc.? * * @return string */ public function title(): string { /* I18N: Name of a module/chart */ return I18N::translate('Descendants'); } /** * A sentence describing what this module does. * * @return string */ public function description(): string { /* I18N: Description of the “DescendancyChart” module */ return I18N::translate('A chart of an individual’s descendants.'); } /** * CSS class for the URL. * * @return string */ public function chartMenuClass(): string { return 'menu-chart-descendants'; } /** * Return a menu item for this chart - for use in individual boxes. * * @param Individual $individual * * @return Menu|null */ public function chartBoxMenu(Individual $individual): ?Menu { return $this->chartMenu($individual); } /** * The title for a specific instance of this chart. * * @param Individual $individual * * @return string */ public function chartTitle(Individual $individual): string { /* I18N: %s is an individual’s name */ return I18N::translate('Descendants of %s', $individual->getFullName()); } /** * A form to request the chart parameters. * * @param Request $request * @param Tree $tree * @param ChartService $chart_service * * @return Response */ public function getChartAction(Request $request, Tree $tree, ChartService $chart_service): Response { $ajax = $request->get('ajax', ''); $xref = $request->get('xref', ''); $individual = Individual::getInstance($xref, $tree); Auth::checkIndividualAccess($individual); $minimum_generations = 2; $maximum_generations = (int) $tree->getPreference('MAX_DESCENDANCY_GENERATIONS', self::DEFAULT_MAXIMUM_GENERATIONS); $default_generations = (int) $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS', self::DEFAULT_GENERATIONS); $chart_style = (int) $request->get('chart_style', self::DEFAULT_STYLE); $generations = (int) $request->get('generations', $default_generations); $generations = min($generations, $maximum_generations); $generations = max($generations, $minimum_generations); if ($ajax === '1') { return $this->chart($request, $tree, $chart_service); } $ajax_url = $this->chartUrl($individual, [ 'chart_style' => $chart_style, 'generations' => $generations, 'ajax' => '1', ]); return $this->viewResponse('modules/descendancy_chart/chart-page', [ 'ajax_url' => $ajax_url, 'chart_style' => $chart_style, 'chart_styles' => $this->chartStyles(), 'default_generations' => $default_generations, 'generations' => $generations, 'individual' => $individual, 'maximum_generations' => $maximum_generations, 'minimum_generations' => $minimum_generations, 'module' => $this, 'title' => $this->chartTitle($individual), ]); } /** * @param Request $request * @param Tree $tree * @param ChartService $chart_service * * @return Response */ public function chart(Request $request, Tree $tree, ChartService $chart_service): Response { $this->layout = 'layouts/ajax'; $xref = $request->get('xref', ''); $individual = Individual::getInstance($xref, $tree); Auth::checkIndividualAccess($individual); $minimum_generations = 2; $maximum_generations = (int) $tree->getPreference('MAX_PEDIGREE_GENERATIONS', self::DEFAULT_MAXIMUM_GENERATIONS); $default_generations = (int) $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS', self::DEFAULT_GENERATIONS); $chart_style = (int) $request->get('chart_style', self::DEFAULT_STYLE); $generations = (int) $request->get('generations', $default_generations); $generations = min($generations, $maximum_generations); $generations = max($generations, $minimum_generations); switch ($chart_style) { case self::CHART_STYLE_LIST: default: return $this->descendantsList($individual, $generations); case self::CHART_STYLE_BOOKLET: return $this->descendantsBooklet($individual, $generations); case self::CHART_STYLE_INDIVIDUALS: $individuals = $chart_service->descendants($individual, $generations - 1); return $this->descendantsIndividuals($tree, $individuals); case self::CHART_STYLE_FAMILIES: $families = $chart_service->descendantFamilies($individual, $generations - 1); return $this->descendantsFamilies($tree, $families); } } /** * Show a hierarchical list of descendants * * @TODO replace ob_start() with views. * * @param Individual $individual * @param int $generations * * @return Response */ private function descendantsList(Individual $individual, int $generations): Response { ob_start(); echo '
'; if ($depth == $generations) { echo ' | '; } else { echo ''; echo ' | '; } echo FunctionsPrint::printPedigreePerson($person); echo ' | '; // check if child has parents and add an arrow echo ''; echo ' | ';
foreach ($person->getChildFamilies() as $cfamily) {
foreach ($cfamily->getSpouses() as $parent) {
echo FontAwesome::linkIcon('arrow-up', I18N::translate('Start at parents'), ['href' => route('descendants', ['ged' => $parent->tree()->name(),
'xref' => $parent->xref(),
'generations' => $generations,
]),
]);
// only show the arrow for one of the parents
break;
}
}
// d'Aboville child number
$level = $generations - $depth;
echo ' '; echo ''; //needed so that RTL languages will display this properly if (!isset($this->dabo_num[$level])) { $this->dabo_num[$level] = 0; } $this->dabo_num[$level]++; $this->dabo_num[$level + 1] = 0; $this->dabo_sex[$level] = $person->getSex(); for ($i = 0; $i <= $level; $i++) { $isf = $this->dabo_sex[$i]; if ($isf === 'M') { $isf = ''; } if ($isf === 'U') { $isf = 'NN'; } echo ' ' . $this->dabo_num[$i] . ' '; if ($i < $level) { echo '.'; } } echo ''; echo ' |
'; echo FunctionsPrint::printPedigreePerson($spouse); echo ' | '; // check if spouse has parents and add an arrow echo ''; echo ' | ';
if ($spouse) {
foreach ($spouse->getChildFamilies() as $cfamily) {
foreach ($cfamily->getSpouses() as $parent) {
echo FontAwesome::linkIcon('arrow-up', I18N::translate('Start at parents'), ['href' => route('descendants', ['ged' => $parent->tree()->name(),
'xref' => $parent->xref(),
'generations' => $generations,
]),
]);
// only show the arrow for one of the parents
break;
}
}
}
echo ' '; echo ' |
'; if (!empty($children)) { echo GedcomTag::getLabel('NCHI') . ': ' . count($children); } else { // Distinguish between no children (NCHI 0) and no recorded // children (no CHIL records) if (strpos($family->gedcom(), '\n1 NCHI 0') !== false) { echo GedcomTag::getLabel('NCHI') . ': ' . count($children); } else { echo I18N::translate('No children'); } } echo ' |