Issues (2500)

app/Http/RequestHandlers/HelpText.php (1 issue)

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