. */ 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 = '
'; 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), ' | '; echo '