. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\Http\ViewResponseTrait; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Services\SearchService; use Fisharebest\Webtrees\Services\TreeService; use Fisharebest\Webtrees\Site; use Fisharebest\Webtrees\Tree; use InvalidArgumentException; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use function array_filter; use function assert; use function in_array; /** * Search for (and optionally replace) genealogy data */ class SearchPhoneticPage implements RequestHandlerInterface { use ViewResponseTrait; /** @var SearchService */ private $search_service; /** @var TreeService */ private $tree_service; /** * SearchController constructor. * * @param SearchService $search_service * @param TreeService $tree_service */ public function __construct(SearchService $search_service, TreeService $tree_service) { $this->search_service = $search_service; $this->tree_service = $tree_service; } /** * The phonetic search. * * @param ServerRequestInterface $request * * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $tree = $request->getAttribute('tree'); assert($tree instanceof Tree, new InvalidArgumentException()); $params = $request->getQueryParams(); $firstname = $params['firstname'] ?? ''; $lastname = $params['lastname'] ?? ''; $place = $params['place'] ?? ''; $soundex = $params['soundex'] ?? 'Russell'; // What trees to seach? if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') { $all_trees = $this->tree_service->all()->all(); } else { $all_trees = [$tree]; } $search_tree_names = $params['search_trees'] ?? []; $search_trees = array_filter($all_trees, static function (Tree $tree) use ($search_tree_names): bool { return in_array($tree->name(), $search_tree_names, true); }); if ($search_trees === []) { $search_trees = [$tree]; } $individuals = []; if ($lastname !== '' || $firstname !== '' || $place !== '') { $individuals = $this->search_service->searchIndividualsPhonetic($soundex, $lastname, $firstname, $place, $search_trees); } $title = I18N::translate('Phonetic search'); return $this->viewResponse('search-phonetic-page', [ 'all_trees' => $all_trees, 'firstname' => $firstname, 'individuals' => $individuals, 'lastname' => $lastname, 'place' => $place, 'search_trees' => $search_trees, 'soundex' => $soundex, 'title' => $title, 'tree' => $tree, ]); } }