xref: /webtrees/app/Http/RequestHandlers/SearchAdvancedPage.php (revision b3679361640bf9d3bb5424fb15815c51cf0c1513)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\Tree;
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 assert;
37use function strtr;
38
39/**
40 * Search for genealogy data
41 */
42class SearchAdvancedPage implements RequestHandlerInterface
43{
44    use ViewResponseTrait;
45
46    private const DEFAULT_ADVANCED_FIELDS = [
47        'INDI:NAME:GIVN',
48        'INDI:NAME:SURN',
49        'INDI:BIRT:DATE',
50        'INDI:BIRT:PLAC',
51        'FAM:MARR:DATE',
52        'FAM:MARR:PLAC',
53        'INDI:DEAT:DATE',
54        'INDI:DEAT:PLAC',
55        'FATHER:NAME:GIVN',
56        'FATHER:NAME:SURN',
57        'MOTHER:NAME:GIVN',
58        'MOTHER:NAME:SURN',
59    ];
60
61    private const OTHER_ADVANCED_FIELDS = [
62        'INDI:ADOP:DATE',
63        'INDI:ADOP:PLAC',
64        'INDI:AFN',
65        'INDI:BAPL:DATE',
66        'INDI:BAPL:PLAC',
67        'INDI:BAPM:DATE',
68        'INDI:BAPM:PLAC',
69        'INDI:BARM:DATE',
70        'INDI:BARM:PLAC',
71        'INDI:BASM:DATE',
72        'INDI:BASM:PLAC',
73        'INDI:BLES:DATE',
74        'INDI:BLES:PLAC',
75        'INDI:BURI:DATE',
76        'INDI:BURI:PLAC',
77        'INDI:CENS:DATE',
78        'INDI:CENS:PLAC',
79        'INDI:CHAN:DATE',
80        'INDI:CHAN:_WT_USER',
81        'INDI:CHR:DATE',
82        'INDI:CHR:PLAC',
83        'INDI:CREM:DATE',
84        'INDI:CREM:PLAC',
85        'INDI:DSCR',
86        'INDI:EMIG:DATE',
87        'INDI:EMIG:PLAC',
88        'INDI:ENDL:DATE',
89        'INDI:ENDL:PLAC',
90        'INDI:EVEN',
91        'INDI:EVEN:TYPE',
92        'INDI:EVEN:DATE',
93        'INDI:EVEN:PLAC',
94        'INDI:FACT',
95        'INDI:FACT:TYPE',
96        'INDI:FCOM:DATE',
97        'INDI:FCOM:PLAC',
98        'INDI:IMMI:DATE',
99        'INDI:IMMI:PLAC',
100        'INDI:NAME:NICK',
101        'INDI:NAME:_MARNM',
102        'INDI:NAME:_HEB',
103        'INDI:NAME:ROMN',
104        'INDI:NATI',
105        'INDI:NATU:DATE',
106        'INDI:NATU:PLAC',
107        'INDI:NOTE',
108        'INDI:OCCU',
109        'INDI:ORDN:DATE',
110        'INDI:ORDN:PLAC',
111        'INDI:REFN',
112        'INDI:RELI',
113        'INDI:RESI:DATE',
114        'INDI:RESI:EMAIL',
115        'INDI:RESI:PLAC',
116        'INDI:SLGC:DATE',
117        'INDI:SLGC:PLAC',
118        'INDI:TITL',
119        'FAM:DIV:DATE',
120        'FAM:SLGS:DATE',
121        'FAM:SLGS:PLAC',
122    ];
123
124    private SearchService $search_service;
125
126    /**
127     * SearchController constructor.
128     *
129     * @param SearchService $search_service
130     */
131    public function __construct(SearchService $search_service)
132    {
133        $this->search_service = $search_service;
134    }
135
136    /**
137     * A structured search.
138     *
139     * @param ServerRequestInterface $request
140     *
141     * @return ResponseInterface
142     */
143    public function handle(ServerRequestInterface $request): ResponseInterface
144    {
145        $tree = $request->getAttribute('tree');
146        assert($tree instanceof Tree);
147
148        $default_fields = array_fill_keys(self::DEFAULT_ADVANCED_FIELDS, '');
149
150        $params = $request->getQueryParams();
151
152        $fields    = $params['fields'] ?? $default_fields;
153        $modifiers = $params['modifiers'] ?? [];
154
155        $other_fields = $this->otherFields($fields);
156        $date_options = $this->dateOptions();
157        $name_options = $this->nameOptions();
158
159        if (array_filter($fields) !== []) {
160            $individuals = $this->search_service->searchIndividualsAdvanced([$tree], $fields, $modifiers);
161        } else {
162            $individuals = new Collection();
163        }
164
165        $title = I18N::translate('Advanced search');
166
167        return $this->viewResponse('search-advanced-page', [
168            'date_options' => $date_options,
169            'fields'       => $fields,
170            'field_labels' => $this->fieldLabels(),
171            'individuals'  => $individuals,
172            'modifiers'    => $modifiers,
173            'name_options' => $name_options,
174            'other_fields' => $other_fields,
175            'title'        => $title,
176            'tree'         => $tree,
177        ]);
178    }
179
180    /**
181     * Extra search fields to add to the advanced search
182     *
183     * @param array<string> $fields
184     *
185     * @return array<string,string>
186     */
187    private function otherFields(array $fields): array
188    {
189        $default_facts = new Collection(self::OTHER_ADVANCED_FIELDS);
190
191        $comparator = static function (string $x, string $y): int {
192            $element_factory = Registry::elementFactory();
193
194            $label1 = $element_factory->make(strtr($x, [':DATE' => '', ':PLAC' => '', ':TYPE' => '']))->label();
195            $label2 = $element_factory->make(strtr($y, [':DATE' => '', ':PLAC' => '', ':TYPE' => '']))->label();
196
197            return I18N::comparator()($label1, $label2) ?: strcmp($x, $y);
198        };
199
200        return $default_facts
201            ->reject(fn (string $field): bool => array_key_exists($field, $fields))
202            ->sort($comparator)
203            ->mapWithKeys(fn (string $fact): array => [$fact => Registry::elementFactory()->make($fact)->label()])
204            ->all();
205    }
206
207
208    /**
209     * We use some pseudo-GEDCOM tags for some of our fields.
210     *
211     * @return array<string,string>
212     */
213    private function fieldLabels(): array
214    {
215        $return = [];
216
217        foreach (array_merge(self::OTHER_ADVANCED_FIELDS, self::DEFAULT_ADVANCED_FIELDS) as $field) {
218            $tmp = strtr($field, ['MOTHER:' => 'INDI:', 'FATHER:' => 'INDI:']);
219            $return[$field] = Registry::elementFactory()->make($tmp)->label();
220        }
221
222
223        return $return;
224    }
225
226    /**
227     * For the advanced search
228     *
229     * @return array<string>
230     */
231    private function dateOptions(): array
232    {
233        return [
234            0  => I18N::translate('Exact date'),
235            1  => I18N::plural('±%s year', '±%s years', 1, I18N::number(1)),
236            2  => I18N::plural('±%s year', '±%s years', 2, I18N::number(2)),
237            5  => I18N::plural('±%s year', '±%s years', 5, I18N::number(5)),
238            10 => I18N::plural('±%s year', '±%s years', 10, I18N::number(10)),
239            20 => I18N::plural('±%s year', '±%s years', 20, I18N::number(20)),
240        ];
241    }
242
243    /**
244     * For the advanced search
245     *
246     * @return array<string>
247     */
248    private function nameOptions(): array
249    {
250        return [
251            'EXACT'    => I18N::translate('Exact'),
252            'BEGINS'   => I18N::translate('Begins with'),
253            'CONTAINS' => I18N::translate('Contains'),
254            'SDX'      => I18N::translate('Sounds like'),
255        ];
256    }
257}
258