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