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