. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Module; use Aura\Router\RouterContainer; use Fig\Http\Message\RequestMethodInterface; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\Contracts\UserInterface; use Fisharebest\Webtrees\Registry; use Fisharebest\Webtrees\Family; use Fisharebest\Webtrees\GedcomCode\GedcomCodePedi; use Fisharebest\Webtrees\GedcomRecord; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Individual; use Fisharebest\Webtrees\Services\ModuleService; use Fisharebest\Webtrees\Soundex; use Fisharebest\Webtrees\Tree; use Fisharebest\Webtrees\User; use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\JoinClause; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use function app; use function array_search; use function assert; use function e; use function explode; use function in_array; use function is_int; use function key; use function log; use function next; use function redirect; use function route; use function strip_tags; use function stripos; use function strtolower; use function usort; use function view; /** * Class BranchesListModule */ class BranchesListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface { use ModuleListTrait; protected const ROUTE_URL = '/tree/{tree}/branches{/surname}'; /** @var ModuleService */ protected $module_service; /** * BranchesListModule constructor. * * @param ModuleService $module_service */ public function __construct(ModuleService $module_service) { $this->module_service = $module_service; } /** * Initialization. * * @return void */ public function boot(): void { $router_container = app(RouterContainer::class); assert($router_container instanceof RouterContainer); $router_container->getMap() ->get(static::class, static::ROUTE_URL, $this) ->allows(RequestMethodInterface::METHOD_POST); } /** * How should this module be identified in the control panel, etc.? * * @return string */ public function title(): string { /* I18N: Name of a module/list */ return I18N::translate('Branches'); } /** * A sentence describing what this module does. * * @return string */ public function description(): string { /* I18N: Description of the “Branches” module */ return I18N::translate('A list of branches of a family.'); } /** * CSS class for the URL. * * @return string */ public function listMenuClass(): string { return 'menu-branches'; } /** * @param Tree $tree * @param mixed[] $parameters * * @return string */ public function listUrl(Tree $tree, array $parameters = []): string { $xref = app(ServerRequestInterface::class)->getAttribute('xref', ''); if ($xref !== '') { $individual = Registry::individualFactory()->make($xref, $tree); if ($individual instanceof Individual && $individual->canShow()) { $parameters['surname'] = $parameters['surname'] ?? $individual->getAllNames()[0]['surn'] ?? null; } } $parameters['tree'] = $tree->name(); return route(static::class, $parameters); } /** * @return string[] */ public function listUrlAttributes(): array { return []; } /** * Handle URLs generated by older versions of webtrees * * @param ServerRequestInterface $request * * @return ResponseInterface */ public function getPageAction(ServerRequestInterface $request): ResponseInterface { return redirect($this->listUrl($request->getAttribute('tree'), $request->getQueryParams())); } /** * @param ServerRequestInterface $request * * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $tree = $request->getAttribute('tree'); assert($tree instanceof Tree); $user = $request->getAttribute('user'); assert($user instanceof UserInterface); Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user); // Convert POST requests into GET requests for pretty URLs. if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { return redirect($this->listUrl($tree, (array) $request->getParsedBody())); } $surname = (string) $request->getAttribute('surname'); $params = $request->getQueryParams(); $ajax = $params['ajax'] ?? ''; $soundex_std = (bool) ($params['soundex_std'] ?? false); $soundex_dm = (bool) ($params['soundex_dm'] ?? false); if ($ajax === '1') { $this->layout = 'layouts/ajax'; // Highlight direct-line ancestors of this individual. $xref = $tree->getUserPreference($user, User::PREF_TREE_ACCOUNT_XREF); $self = Registry::individualFactory()->make($xref, $tree); if ($surname !== '') { $individuals = $this->loadIndividuals($tree, $surname, $soundex_dm, $soundex_std); } else { $individuals = []; } if ($self instanceof Individual) { $ancestors = $this->allAncestors($self); } else { $ancestors = []; } return $this->viewResponse('modules/branches/list', [ 'branches' => $this->getPatriarchsHtml($tree, $individuals, $ancestors, $surname, $soundex_dm, $soundex_std), ]); } if ($surname !== '') { /* I18N: %s is a surname */ $title = I18N::translate('Branches of the %s family', e($surname)); $ajax_url = $this->listUrl($tree, $params + ['ajax' => true, 'surname' => $surname]); } else { /* I18N: Branches of a family tree */ $title = I18N::translate('Branches'); $ajax_url = ''; } return $this->viewResponse('branches-page', [ 'ajax_url' => $ajax_url, 'soundex_dm' => $soundex_dm, 'soundex_std' => $soundex_std, 'surname' => $surname, 'title' => $title, 'tree' => $tree, ]); } /** * Find all ancestors of an individual, indexed by the Sosa-Stradonitz number. * * @param Individual $individual * * @return Individual[] */ private function allAncestors(Individual $individual): array { $ancestors = [ 1 => $individual, ]; do { $sosa = key($ancestors); $family = $ancestors[$sosa]->childFamilies()->first(); if ($family !== null) { if ($family->husband() !== null) { $ancestors[$sosa * 2] = $family->husband(); } if ($family->wife() !== null) { $ancestors[$sosa * 2 + 1] = $family->wife(); } } } while (next($ancestors)); return $ancestors; } /** * Fetch all individuals with a matching surname * * @param Tree $tree * @param string $surname * @param bool $soundex_dm * @param bool $soundex_std * * @return Individual[] */ private function loadIndividuals(Tree $tree, string $surname, bool $soundex_dm, bool $soundex_std): array { $individuals = DB::table('individuals') ->join('name', static function (JoinClause $join): void { $join ->on('name.n_file', '=', 'individuals.i_file') ->on('name.n_id', '=', 'individuals.i_id'); }) ->where('i_file', '=', $tree->id()) ->where('n_type', '<>', '_MARNM') ->where(static function (Builder $query) use ($surname, $soundex_dm, $soundex_std): void { $query ->where('n_surn', '=', $surname) ->orWhere('n_surname', '=', $surname); if ($soundex_std) { $sdx = Soundex::russell($surname); if ($sdx !== '') { foreach (explode(':', $sdx) as $value) { $query->orWhere('n_soundex_surn_std', 'LIKE', '%' . $value . '%'); } } } if ($soundex_dm) { $sdx = Soundex::daitchMokotoff($surname); if ($sdx !== '') { foreach (explode(':', $sdx) as $value) { $query->orWhere('n_soundex_surn_dm', 'LIKE', '%' . $value . '%'); } } } }) ->select(['individuals.*']) ->distinct() ->get() ->map(Registry::individualFactory()->mapper($tree)) ->filter(GedcomRecord::accessFilter()) ->all(); usort($individuals, Individual::birthDateComparator()); return $individuals; } /** * For each individual with no ancestors, list their descendants. * * @param Tree $tree * @param Individual[] $individuals * @param Individual[] $ancestors * @param string $surname * @param bool $soundex_dm * @param bool $soundex_std * * @return string */ private function getPatriarchsHtml(Tree $tree, array $individuals, array $ancestors, string $surname, bool $soundex_dm, bool $soundex_std): string { $html = ''; foreach ($individuals as $individual) { foreach ($individual->childFamilies() as $family) { foreach ($family->spouses() as $parent) { if (in_array($parent, $individuals, true)) { continue 3; } } } $html .= $this->getDescendantsHtml($tree, $individuals, $ancestors, $surname, $soundex_dm, $soundex_std, $individual, null); } return $html; } /** * Generate a recursive list of descendants of an individual. * If parents are specified, we can also show the pedigree (adopted, etc.). * * @param Tree $tree * @param Individual[] $individuals * @param Individual[] $ancestors * @param string $surname * @param bool $soundex_dm * @param bool $soundex_std * @param Individual $individual * @param Family|null $parents * * @return string */ private function getDescendantsHtml(Tree $tree, array $individuals, array $ancestors, string $surname, bool $soundex_dm, bool $soundex_std, Individual $individual, Family $parents = null): string { $module = $this->module_service->findByComponent(ModuleChartInterface::class, $tree, Auth::user())->first(static function (ModuleInterface $module) { return $module instanceof RelationshipsChartModule; }); // A person has many names. Select the one that matches the searched surname $person_name = ''; foreach ($individual->getAllNames() as $name) { [$surn1] = explode(',', $name['sort']); if ($this->surnamesMatch($surn1, $surname, $soundex_std, $soundex_dm)) { $person_name = $name['full']; break; } } // No matching name? Typically children with a different surname. The branch stops here. if ($person_name === '') { return '