xref: /webtrees/app/Http/RequestHandlers/HelpText.php (revision d4ec5ec0fe611c85b1d77527db3e9becde7e473d)
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\Auth;
23use Fisharebest\Webtrees\Date;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Services\LocalizationService;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Server\RequestHandlerInterface;
29
30use function array_keys;
31use function response;
32use function strip_tags;
33use function view;
34
35/**
36 * Show help text.
37 */
38class HelpText implements RequestHandlerInterface
39{
40    private const FRENCH_DATES = [
41        '@#DFRENCH R@ 12',
42        '@#DFRENCH R@ VEND 12',
43        'ABT @#DFRENCH R@ BRUM 12',
44        'BET @#DFRENCH R@ FRIM 12 AND @#DFRENCH R@ NIVO 12',
45        'FROM @#DFRENCH R@ PLUV 12 TO @#DFRENCH R@ VENT 12',
46        'AFT @#DFRENCH R@ GERM 12',
47        'BEF @#DFRENCH R@ FLOR 12',
48        'ABT @#DFRENCH R@ PRAI 12',
49        'FROM @#DFRENCH R@ MESS 12',
50        'TO @#DFRENCH R@ THER 12',
51        'EST @#DFRENCH R@ FRUC 12',
52        '@#DFRENCH R@ 03 COMP 12',
53    ];
54
55    private const HIJRI_DATES = [
56        '@#DHIJRI@ 1497',
57        '@#DHIJRI@ MUHAR 1497',
58        'ABT @#DHIJRI@ SAFAR 1497',
59        'BET @#DHIJRI@ RABIA 1497 AND @#DHIJRI@ RABIT 1497',
60        'FROM @#DHIJRI@ JUMAA 1497 TO @#DHIJRI@ JUMAT 1497',
61        'AFT @#DHIJRI@ RAJAB 1497',
62        'BEF @#DHIJRI@ SHAAB 1497',
63        'ABT @#DHIJRI@ RAMAD 1497',
64        'FROM @#DHIJRI@ SHAWW 1497',
65        'TO @#DHIJRI@ DHUAQ 1497',
66        '@#DHIJRI@ 03 DHUAH 1497',
67    ];
68
69    private const JALALI_DATES = [
70        '@#DJALALI@ 1497',
71        '@#DJALALI@ FARVA 1497',
72        'ABT @#DJALALI@ ORDIB 1497',
73        'BET @#DJALALI@ KHORD 1497 AND @#DHIJRI@ TIR 1497',
74        'FROM @#DJALALI@ MORDA 1497 TO @#DHIJRI@ SHAHR 1497',
75        'AFT @#DJALALI@ MEHR 1497',
76        'BEF @#DJALALI@ ABAN 1497',
77        'ABT @#DJALALI@ AZAR 1497',
78        'FROM @#DJALALI@ DEY 1497',
79        'TO @#DJALALI@ BAHMA 1497',
80        '@#DJALALI@ 03 XXXXX 1497',
81    ];
82
83    private const JEWISH_DATES = [
84        '@#DHEBREW@ 5481',
85        '@#DHEBREW@ TSH 5481',
86        'ABT @#DHEBREW@ CSH 5481',
87        'BET @#DHEBREW@ KSL 5481 AND @#DHEBREW@ TVT 5481',
88        'FROM @#DHEBREW@ SHV 5481 TO @#DHEBREW@ ADR 5481',
89        'AFT @#DHEBREW@ ADR 5481',
90        'AFT @#DHEBREW@ ADS 5480',
91        'BEF @#DHEBREW@ NSN 5481',
92        'ABT @#DHEBREW@ IYR 5481',
93        'FROM @#DHEBREW@ SVN 5481',
94        'TO @#DHEBREW@ TMZ 5481',
95        'EST @#DHEBREW@ AAV 5481',
96        '@#DHEBREW@ 03 ELL 5481',
97    ];
98
99    private const JULIAN_DATES = [
100        '@#DJULIAN@ 14 JAN 1700',
101        '@#DJULIAN@ 44 B.C.',
102        '@#DJULIAN@ 20 FEB 1742/43',
103        'BET @#DJULIAN@ 01 SEP 1752 AND @#DGREGORIAN@ 30 SEP 1752',
104    ];
105
106    private const DATE_SHORTCUTS = [
107        '1900'           => [],
108        'JAN 1900'       => [],
109        'FEB 1900'       => [],
110        'MAR 1900'       => [],
111        'APR 1900'       => [],
112        'MAY 1900'       => [],
113        'JUN 1900'       => [],
114        'JUL 1900'       => [],
115        'AUG 1900'       => [],
116        'SEP 1900'       => [],
117        'OCT 1900'       => [],
118        'NOV 1900'       => [],
119        'DEC 1900'       => [],
120        'ABT 1900'       => ['~1900'],
121        'EST 1900'       => ['*1900'],
122        'CAL 1900'       => ['#1900'],
123        'INT 1900 (...)' => [],
124    ];
125
126    private const DATE_RANGE_SHORTCUTS = [
127        'BET 1900 AND 1910'         => ['1900-1910'],
128        'AFT 1900'                  => ['>1900'],
129        'BEF 1910'                  => ['<1910'],
130        'BET JAN 1900 AND MAR 1900' => ['Q1 1900'],
131        'BET APR 1900 AND JUN 1900' => ['Q2 1900'],
132        'BET JUL 1900 AND SEP 1900' => ['Q3 1900'],
133        'BET OCT 1900 AND DEC 1900' => ['Q4 1900'],
134    ];
135
136    private const DATE_PERIOD_SHORTCUTS = [
137        'FROM 1900 TO 1910' => ['1900~1910'],
138        'FROM 1900'         => ['1900-'],
139        'TO 1910'           => ['-1900'],
140    ];
141
142    private const DMY_SHORTCUTS = [
143        '11 DEC 1913' => [
144            '11/12/1913',
145            '11-12-1913',
146            '11.12.1913',
147        ],
148        '01 FEB 2003' => [
149            '01/02/03',
150            '01-02-03',
151            '01.02.03',
152        ],
153    ];
154
155    private const MDY_SHORTCUTS = [
156        '11 DEC 1913' => [
157            '12/11/1913',
158            '12-11-1913',
159            '12.11.1913',
160        ],
161        '01 FEB 2003' => [
162            '02/01/03',
163            '02-01-03',
164            '02.01.03',
165        ],
166    ];
167
168    private const YMD_SHORTCUTS = [
169        '11 DEC 1913' => [
170            '11/12/1913',
171            '11-12-1913',
172            '11.12.1913',
173        ],
174        '01 FEB 2003' => [
175            '03/02/01',
176            '03-02-01',
177            '03.02.01',
178        ],
179    ];
180
181    private LocalizationService $localisation_service;
182
183    /**
184     * HelpText constructor.
185     *
186     * @param LocalizationService $localization_service
187     */
188    public function __construct(LocalizationService $localization_service)
189    {
190        $this->localisation_service = $localization_service;
191    }
192
193    /**
194     * @param ServerRequestInterface $request
195     *
196     * @return ResponseInterface
197     */
198    public function handle(ServerRequestInterface $request): ResponseInterface
199    {
200        $topic = $request->getAttribute('topic');
201
202        $dmy = $this->localisation_service->dateFormatToOrder(I18N::dateFormat());
203
204        switch ($topic) {
205            case 'DATE':
206                switch ($dmy) {
207                    case 'YMD':
208                        $date_shortcuts = self::DATE_SHORTCUTS + self::YMD_SHORTCUTS;
209                        break;
210                    case 'MDY':
211                        $date_shortcuts = self::DATE_SHORTCUTS + self::MDY_SHORTCUTS;
212                        break;
213                    case 'DMY':
214                    default:
215                        $date_shortcuts = self::DATE_SHORTCUTS + self::DMY_SHORTCUTS;
216                        break;
217                }
218
219                $title = I18N::translate('Date');
220                $text  = view('help/date', [
221                    'date_dates'            => $this->formatDates(array_keys($date_shortcuts)),
222                    'date_shortcuts'        => $date_shortcuts,
223                    'date_period_dates'     => $this->formatDates(array_keys(self::DATE_PERIOD_SHORTCUTS)),
224                    'date_period_shortcuts' => self::DATE_PERIOD_SHORTCUTS,
225                    'date_range_dates'      => $this->formatDates(array_keys(self::DATE_RANGE_SHORTCUTS)),
226                    'date_range_shortcuts'  => self::DATE_RANGE_SHORTCUTS,
227                    'french_dates'          => $this->formatDates(self::FRENCH_DATES),
228                    'hijri_dates'           => $this->formatDates(self::HIJRI_DATES),
229                    'jalali_dates'          => $this->formatDates(self::JALALI_DATES),
230                    'jewish_dates'          => $this->formatDates(self::JEWISH_DATES),
231                    'julian_dates'          => $this->formatDates(self::JULIAN_DATES),
232                ]);
233                break;
234
235            case 'NAME':
236                $title = I18N::translate('Name');
237                $text  = view('help/name');
238                break;
239
240            case 'SURN':
241                $title = I18N::translate('Surname');
242                $text  = view('help/surname');
243                break;
244
245            case 'OBJE':
246                $title = I18N::translate('Media object');
247                $text  = view('help/media-object');
248                break;
249
250            case 'PLAC':
251                $title = I18N::translate('Place');
252                $text  = view('help/place');
253                break;
254
255            case 'RESN':
256                $title = I18N::translate('Restriction');
257                $text  = view('help/restriction');
258                break;
259
260            case 'ROMN':
261                $title = I18N::translate('Romanized');
262                $text  = view('help/romanized');
263                break;
264
265            case '_HEB':
266                $title = I18N::translate('Hebrew');
267                $text  = view('help/hebrew');
268                break;
269
270            case 'data-fixes':
271                $title = I18N::translate('Data fixes');
272                $text  = view('help/data-fixes');
273                break;
274
275            case 'edit_SOUR_EVEN':
276                $title = I18N::translate('Associate events with this source');
277                $text  = view('help/source-events');
278                break;
279
280            case 'pending_changes':
281                $title = I18N::translate('Pending changes');
282                $text  = view('help/pending-changes', [
283                    'is_admin' => Auth::isAdmin(),
284                ]);
285                break;
286
287            case 'relationship-privacy':
288                $title = I18N::translate('Restrict to immediate family');
289                $text  = view('help/relationship-privacy');
290                break;
291
292            case 'iso-8859-1':
293                $title = I18N::translate('Convert from UTF-8 to ISO-8859-1');
294                $text  = view('help/iso-8859-1');
295                break;
296
297            case 'zip-gedcom':
298                $title = I18N::translate('Compress the GEDCOM file');
299                $text  = view('help/zip-gedcom');
300                break;
301
302            default:
303                $title = I18N::translate('Help');
304                $text  = I18N::translate('The help text has not been written for this item.');
305                break;
306        }
307
308        $html = view('modals/help', [
309            'title' => $title,
310            'text'  => $text,
311        ]);
312
313        return response($html);
314    }
315
316    /**
317     * Format GEDCOM dates in the local language.
318     *
319     * @param array<string>|array<int> $gedcom_dates
320     *
321     * @return array<string>
322     */
323    private function formatDates(array $gedcom_dates): array
324    {
325        $dates = [];
326
327        foreach ($gedcom_dates as $gedcom_date) {
328            // PHP converts numeric array keys ('1900') to integers (1900), so reverse this.
329            $gedcom_date = (string) $gedcom_date;
330
331            $date                = new Date($gedcom_date);
332            $dates[$gedcom_date] = strip_tags($date->display());
333        }
334
335        return $dates;
336    }
337}
338