. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Module; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\FontAwesome; use Fisharebest\Webtrees\Functions\FunctionsCharts; use Fisharebest\Webtrees\Functions\FunctionsPrint; use Fisharebest\Webtrees\Gedcom; 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 Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; /** * Class AncestorsChartModule */ class AncestorsChartModule extends AbstractModule implements ModuleInterface, ModuleChartInterface { use ModuleChartTrait; // Chart styles protected const CHART_STYLE_LIST = 'list'; protected const CHART_STYLE_BOOKLET = 'booklet'; protected const CHART_STYLE_INDIVIDUALS = 'individuals'; protected const CHART_STYLE_FAMILIES = 'families'; // Defaults protected const DEFAULT_COUSINS = false; protected const DEFAULT_STYLE = self::CHART_STYLE_LIST; protected const DEFAULT_GENERATIONS = '3'; protected const DEFAULT_MAXIMUM_GENERATIONS = '9'; /** * 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('Ancestors'); } /** * A sentence describing what this module does. * * @return string */ public function description(): string { /* I18N: Description of the “AncestorsChart” module */ return I18N::translate('A chart of an individual’s ancestors.'); } /** * CSS class for the URL. * * @return string */ public function chartMenuClass(): string { return 'menu-chart-ancestry'; } /** * 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('Ancestors 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 = (bool) $request->get('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); $show_cousins = (bool) $request->get('show_cousins', self::DEFAULT_COUSINS); $chart_style = $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) { $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations); switch ($chart_style) { default: case self::CHART_STYLE_LIST: return $this->ancestorsList($individual, $generations); case self::CHART_STYLE_BOOKLET: return $this->ancestorsBooklet($ancestors, $show_cousins); case self::CHART_STYLE_INDIVIDUALS: return $this->ancestorsIndividuals($tree, $ancestors); case self::CHART_STYLE_FAMILIES: return $this->ancestorsFamilies($tree, $ancestors); } } $ajax_url = $this->chartUrl($individual, [ 'generations' => $generations, 'chart_style' => $chart_style, 'show_cousins' => $show_cousins, 'ajax' => true, ]); return $this->viewResponse('modules/ancestors-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_name' => $this->name(), 'show_cousins' => $show_cousins, 'title' => $this->chartTitle($individual), ]); } /** * Show a hierarchical list of ancestors * * @TODO replace ob_start() with views. * * @param Individual $individual * @param int $generations * * @return Response */ protected function ancestorsList(Individual $individual, int $generations): Response { ob_start(); $this->printChildAscendancy($individual, 1, $generations - 1); $html = ob_get_clean(); $html = ''; return new Response($html); } /** * print a child ascendancy * * @param Individual $individual * @param int $sosa * @param int $generations * * @return void */ protected function printChildAscendancy(Individual $individual, $sosa, $generations) { echo '
  • '; echo ''; echo '
    '; if ($sosa === 1) { echo ''; } else { echo ''; echo ''; } echo FunctionsPrint::printPedigreePerson($individual); echo ''; if ($sosa > 1) { echo FontAwesome::linkIcon('arrow-down', $this->chartTitle($individual), [ 'href' => $this->chartUrl($individual, [ 'generations' => $generations, 'chart_style' => self::CHART_STYLE_LIST, ]) ]); } echo ' ', I18N::number($sosa), ' '; echo ' ', FunctionsCharts::getSosaName($sosa), '
    '; // Parents $family = $individual->getPrimaryChildFamily(); if ($family && $generations > 0) { // Marriage details echo ''; echo ''; echo ' ', I18N::number($sosa * 2), ' ', I18N::translate('and'); echo ' ', I18N::number($sosa * 2 + 1), ''; if ($family->canShow()) { foreach ($family->facts(Gedcom::MARRIAGE_EVENTS) as $fact) { echo ' ', $fact->summary(), ''; } } echo ''; echo ''; } echo '
  • '; } /** * Show a tabular list of individual ancestors. * * @param Tree $tree * @param Collection $ancestors * * @return Response */ protected function ancestorsIndividuals(Tree $tree, Collection $ancestors): Response { $this->layout = 'layouts/ajax'; return $this->viewResponse('lists/individuals-table', [ 'individuals' => $ancestors, 'sosa' => true, 'tree' => $tree, ]); } /** * Show a tabular list of individual ancestors. * * @param Tree $tree * @param Collection $ancestors * * @return Response */ protected function ancestorsFamilies(Tree $tree, Collection $ancestors): Response { $this->layout = 'layouts/ajax'; $families = []; foreach ($ancestors as $individual) { foreach ($individual->getChildFamilies() as $family) { $families[$family->xref()] = $family; } } return $this->viewResponse('lists/families-table', [ 'families' => $families, 'tree' => $tree, ]); } /** * Show a booklet view of ancestors * * @TODO replace ob_start() with views. * * @param Collection $ancestors * @param bool $show_cousins * * @return Response */ protected function ancestorsBooklet(Collection $ancestors, bool $show_cousins): Response { ob_start(); echo FunctionsPrint::printPedigreePerson($ancestors[1]); foreach ($ancestors as $sosa => $individual) { foreach ($individual->getChildFamilies() as $family) { FunctionsCharts::printSosaFamily($family, $individual->xref(), $sosa, '', '', '', $show_cousins); } } $html = ob_get_clean(); return new Response($html); } /** * This chart can display its output in a number of styles * * @return array */ protected function chartStyles(): array { return [ self::CHART_STYLE_LIST => I18N::translate('List'), self::CHART_STYLE_BOOKLET => I18N::translate('Booklet'), self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), self::CHART_STYLE_FAMILIES => I18N::translate('Families'), ]; } }