1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fisharebest\Webtrees\GedcomTag; 23use Fisharebest\Webtrees\Http\ViewResponseTrait; 24use Fisharebest\Webtrees\I18N; 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 assert; 36use function explode; 37 38/** 39 * Search for genealogy data 40 */ 41class SearchAdvancedPage implements RequestHandlerInterface 42{ 43 use ViewResponseTrait; 44 45 private const DEFAULT_ADVANCED_FIELDS = [ 46 'NAME:GIVN', 47 'NAME:SURN', 48 'BIRT:DATE', 49 'BIRT:PLAC', 50 'FAMS:MARR:DATE', 51 'FAMS:MARR:PLAC', 52 'DEAT:DATE', 53 'DEAT:PLAC', 54 'FAMC:HUSB:NAME:GIVN', 55 'FAMC:HUSB:NAME:SURN', 56 'FAMC:WIFE:NAME:GIVN', 57 'FAMC:WIFE:NAME:SURN', 58 ]; 59 60 private const OTHER_ADVANCED_FIELDS = [ 61 'ADOP:DATE', 62 'ADOP:PLAC', 63 'AFN', 64 'BAPL:DATE', 65 'BAPL:PLAC', 66 'BAPM:DATE', 67 'BAPM:PLAC', 68 'BARM:DATE', 69 'BARM:PLAC', 70 'BASM:DATE', 71 'BASM:PLAC', 72 'BLES:DATE', 73 'BLES:PLAC', 74 'BURI:DATE', 75 'BURI:PLAC', 76 'CAST', 77 'CENS:DATE', 78 'CENS:PLAC', 79 'CHAN:DATE', 80 'CHAN:_WT_USER', 81 'CHR:DATE', 82 'CHR:PLAC', 83 'CREM:DATE', 84 'CREM:PLAC', 85 'DSCR', 86 'EMAIL', 87 'EMIG:DATE', 88 'EMIG:PLAC', 89 'ENDL:DATE', 90 'ENDL:PLAC', 91 'EVEN', 92 'EVEN:TYPE', 93 'EVEN:DATE', 94 'EVEN:PLAC', 95 'FACT', 96 'FACT:TYPE', 97 'FAMS:CENS:DATE', 98 'FAMS:CENS:PLAC', 99 'FAMS:DIV:DATE', 100 'FAMS:NOTE', 101 'FAMS:SLGS:DATE', 102 'FAMS:SLGS:PLAC', 103 'FAX', 104 'FCOM:DATE', 105 'FCOM:PLAC', 106 'IMMI:DATE', 107 'IMMI:PLAC', 108 'NAME:NICK', 109 'NAME:_MARNM', 110 'NAME:_HEB', 111 'NAME:ROMN', 112 'NATI', 113 'NATU:DATE', 114 'NATU:PLAC', 115 'NOTE', 116 'OCCU', 117 'ORDN:DATE', 118 'ORDN:PLAC', 119 'REFN', 120 'RELI', 121 'RESI', 122 'RESI:DATE', 123 'RESI:PLAC', 124 'SLGC:DATE', 125 'SLGC:PLAC', 126 'TITL', 127 ]; 128 129 /** @var SearchService */ 130 private $search_service; 131 132 /** 133 * SearchController constructor. 134 * 135 * @param SearchService $search_service 136 */ 137 public function __construct(SearchService $search_service) 138 { 139 $this->search_service = $search_service; 140 } 141 142 /** 143 * A structured search. 144 * 145 * @param ServerRequestInterface $request 146 * 147 * @return ResponseInterface 148 */ 149 public function handle(ServerRequestInterface $request): ResponseInterface 150 { 151 $tree = $request->getAttribute('tree'); 152 assert($tree instanceof Tree); 153 154 $default_fields = array_fill_keys(self::DEFAULT_ADVANCED_FIELDS, ''); 155 156 $params = $request->getQueryParams(); 157 158 $fields = $params['fields'] ?? $default_fields; 159 $modifiers = $params['modifiers'] ?? []; 160 161 $other_fields = $this->otherFields($tree, $fields); 162 $date_options = $this->dateOptions(); 163 $name_options = $this->nameOptions(); 164 165 if (array_filter($fields) !== []) { 166 $individuals = $this->search_service->searchIndividualsAdvanced([$tree], $fields, $modifiers); 167 } else { 168 $individuals = new Collection(); 169 } 170 171 $title = I18N::translate('Advanced search'); 172 173 return $this->viewResponse('search-advanced-page', [ 174 'date_options' => $date_options, 175 'fields' => $fields, 176 'individuals' => $individuals, 177 'modifiers' => $modifiers, 178 'name_options' => $name_options, 179 'other_fields' => $other_fields, 180 'title' => $title, 181 'tree' => $tree, 182 ]); 183 } 184 185 /** 186 * Extra search fields to add to the advanced search 187 * 188 * @param Tree $tree 189 * @param string[] $fields 190 * 191 * @return array<string,string> 192 */ 193 private function otherFields(Tree $tree, array $fields): array 194 { 195 $default_facts = new Collection(self::OTHER_ADVANCED_FIELDS); 196 $indi_facts_add = new Collection(explode(',', $tree->getPreference('INDI_FACTS_ADD'))); 197 $indi_facts_unique = new Collection(explode(',', $tree->getPreference('INDI_FACTS_UNIQUE'))); 198 199 return $default_facts 200 ->merge($indi_facts_add) 201 ->merge($indi_facts_unique) 202 ->unique() 203 ->reject(static function (string $field) use ($fields): bool { 204 return 205 array_key_exists($field, $fields) || 206 array_key_exists($field . ':DATE', $fields) || 207 array_key_exists($field . ':PLAC', $fields); 208 }) 209 ->mapWithKeys(static function (string $fact): array { 210 return [$fact => GedcomTag::getLabel($fact)]; 211 }) 212 ->all(); 213 } 214 215 /** 216 * For the advanced search 217 * 218 * @return 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 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