xref: /webtrees/app/Http/RequestHandlers/SearchAdvancedPage.php (revision bd51cb5d7e4bcd204833cb85b18909a70c2bff49)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Http\ViewResponseTrait;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Services\SearchService;
26use Fisharebest\Webtrees\Validator;
27use Illuminate\Support\Collection;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function array_fill_keys;
33use function array_filter;
34use function array_key_exists;
35use function array_merge;
36use function strtr;
37
38/**
39 * Search for genealogy data
40 */
41class SearchAdvancedPage implements RequestHandlerInterface
42{
43    use ViewResponseTrait;
44
45    private const DEFAULT_ADVANCED_FIELDS = [
46        'INDI:NAME:GIVN',
47        'INDI:NAME:SURN',
48        'INDI:BIRT:DATE',
49        'INDI:BIRT:PLAC',
50        'FAM:MARR:DATE',
51        'FAM:MARR:PLAC',
52        'INDI:DEAT:DATE',
53        'INDI:DEAT:PLAC',
54        'FATHER:NAME:GIVN',
55        'FATHER:NAME:SURN',
56        'MOTHER:NAME:GIVN',
57        'MOTHER:NAME:SURN',
58    ];
59
60    private const OTHER_ADVANCED_FIELDS = [
61        'INDI:ADOP:DATE',
62        'INDI:ADOP:PLAC',
63        'INDI:AFN',
64        'INDI:BAPL:DATE',
65        'INDI:BAPL:PLAC',
66        'INDI:BAPM:DATE',
67        'INDI:BAPM:PLAC',
68        'INDI:BARM:DATE',
69        'INDI:BARM:PLAC',
70        'INDI:BASM:DATE',
71        'INDI:BASM:PLAC',
72        'INDI:BLES:DATE',
73        'INDI:BLES:PLAC',
74        'INDI:BURI:DATE',
75        'INDI:BURI:PLAC',
76        'INDI:CENS:DATE',
77        'INDI:CENS:PLAC',
78        'INDI:CHAN:DATE',
79        'INDI:CHAN:_WT_USER',
80        'INDI:CHR:DATE',
81        'INDI:CHR:PLAC',
82        'INDI:CREM:DATE',
83        'INDI:CREM:PLAC',
84        'INDI:DSCR',
85        'INDI:EMIG:DATE',
86        'INDI:EMIG:PLAC',
87        'INDI:ENDL:DATE',
88        'INDI:ENDL:PLAC',
89        'INDI:EVEN',
90        'INDI:EVEN:TYPE',
91        'INDI:EVEN:DATE',
92        'INDI:EVEN:PLAC',
93        'INDI:FACT',
94        'INDI:FACT:TYPE',
95        'INDI:FCOM:DATE',
96        'INDI:FCOM:PLAC',
97        'INDI:IMMI:DATE',
98        'INDI:IMMI:PLAC',
99        'INDI:NAME:NICK',
100        'INDI:NAME:_MARNM',
101        'INDI:NAME:_HEB',
102        'INDI:NAME:ROMN',
103        'INDI:NATI',
104        'INDI:NATU:DATE',
105        'INDI:NATU:PLAC',
106        'INDI:NOTE',
107        'INDI:OCCU',
108        'INDI:ORDN:DATE',
109        'INDI:ORDN:PLAC',
110        'INDI:REFN',
111        'INDI:RELI',
112        'INDI:RESI:DATE',
113        'INDI:RESI:EMAIL',
114        'INDI:RESI:PLAC',
115        'INDI:SLGC:DATE',
116        'INDI:SLGC:PLAC',
117        'INDI:TITL',
118        'FAM:DIV:DATE',
119        'FAM:SLGS:DATE',
120        'FAM:SLGS:PLAC',
121    ];
122
123    private SearchService $search_service;
124
125    /**
126     * @param SearchService $search_service
127     */
128    public function __construct(SearchService $search_service)
129    {
130        $this->search_service = $search_service;
131    }
132
133    /**
134     * @param ServerRequestInterface $request
135     *
136     * @return ResponseInterface
137     */
138    public function handle(ServerRequestInterface $request): ResponseInterface
139    {
140        $tree           = Validator::attributes($request)->tree();
141        $default_fields = array_fill_keys(self::DEFAULT_ADVANCED_FIELDS, '');
142        $fields         = Validator::queryParams($request)->array('fields') ?: $default_fields;
143        $modifiers      = Validator::queryParams($request)->array('modifiers');
144        $other_fields   = $this->otherFields($fields);
145        $date_options   = $this->dateOptions();
146        $name_options   = $this->nameOptions();
147
148        if (array_filter($fields) !== []) {
149            $individuals = $this->search_service->searchIndividualsAdvanced([$tree], $fields, $modifiers);
150        } else {
151            $individuals = new Collection();
152        }
153
154        $title = I18N::translate('Advanced search');
155
156        return $this->viewResponse('search-advanced-page', [
157            'date_options' => $date_options,
158            'fields'       => $fields,
159            'field_labels' => $this->fieldLabels(),
160            'individuals'  => $individuals,
161            'modifiers'    => $modifiers,
162            'name_options' => $name_options,
163            'other_fields' => $other_fields,
164            'title'        => $title,
165            'tree'         => $tree,
166        ]);
167    }
168
169    /**
170     * Extra search fields to add to the advanced search
171     *
172     * @param array<string> $fields
173     *
174     * @return array<string,string>
175     */
176    private function otherFields(array $fields): array
177    {
178        $default_facts = new Collection(self::OTHER_ADVANCED_FIELDS);
179
180        $comparator = static function (string $x, string $y): int {
181            $element_factory = Registry::elementFactory();
182
183            $label1 = $element_factory->make(strtr($x, [':DATE' => '', ':PLAC' => '', ':TYPE' => '']))->label();
184            $label2 = $element_factory->make(strtr($y, [':DATE' => '', ':PLAC' => '', ':TYPE' => '']))->label();
185
186            return I18N::comparator()($label1, $label2) ?: strcmp($x, $y);
187        };
188
189        return $default_facts
190            ->reject(fn (string $field): bool => array_key_exists($field, $fields))
191            ->sort($comparator)
192            ->mapWithKeys(fn (string $fact): array => [$fact => Registry::elementFactory()->make($fact)->label()])
193            ->all();
194    }
195
196
197    /**
198     * We use some pseudo-GEDCOM tags for some of our fields.
199     *
200     * @return array<string,string>
201     */
202    private function fieldLabels(): array
203    {
204        $return = [];
205
206        foreach (array_merge(self::OTHER_ADVANCED_FIELDS, self::DEFAULT_ADVANCED_FIELDS) as $field) {
207            $tmp = strtr($field, ['MOTHER:' => 'INDI:', 'FATHER:' => 'INDI:']);
208            $return[$field] = Registry::elementFactory()->make($tmp)->label();
209        }
210
211
212        return $return;
213    }
214
215    /**
216     * For the advanced search
217     *
218     * @return array<string>
219     */
220    private function dateOptions(): array
221    {
222        return [
223            0  => I18N::translate('Exact date'),
224            1  => I18N::plural('±%s year', '±%s years', 1, I18N::number(1)),
225            2  => I18N::plural('±%s year', '±%s years', 2, I18N::number(2)),
226            5  => I18N::plural('±%s year', '±%s years', 5, I18N::number(5)),
227            10 => I18N::plural('±%s year', '±%s years', 10, I18N::number(10)),
228            20 => I18N::plural('±%s year', '±%s years', 20, I18N::number(20)),
229        ];
230    }
231
232    /**
233     * For the advanced search
234     *
235     * @return array<string>
236     */
237    private function nameOptions(): array
238    {
239        return [
240            'EXACT'    => I18N::translate('Exact'),
241            'BEGINS'   => I18N::translate('Begins with'),
242            'CONTAINS' => I18N::translate('Contains'),
243            'SDX'      => I18N::translate('Sounds like'),
244        ];
245    }
246}
247