Passed
Push — develop ( b53f60...9961ae )
by Greg
14:15
created

HelpText::handle()   D

Complexity

Conditions 16
Paths 16

Size

Total Lines 106
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 82
c 0
b 0
f 0
nc 16
nop 1
dl 0
loc 106
rs 4.846

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2022 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 Fisharebest\Webtrees\Registry;
26
use Fisharebest\Webtrees\Services\LocalizationService;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
use function array_keys;
32
use function strip_tags;
33
use function view;
34
35
/**
36
 * Show help text.
37
 */
38
class 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
    /**
182
     * @param ServerRequestInterface $request
183
     *
184
     * @return ResponseInterface
185
     */
186
    public function handle(ServerRequestInterface $request): ResponseInterface
187
    {
188
        $topic = $request->getAttribute('topic');
189
190
        $dmy = I18N::language()->dateOrder();
191
192
        switch ($topic) {
193
            case 'DATE':
194
                switch ($dmy) {
195
                    case 'YMD':
196
                        $date_shortcuts = self::DATE_SHORTCUTS + self::YMD_SHORTCUTS;
197
                        break;
198
                    case 'MDY':
199
                        $date_shortcuts = self::DATE_SHORTCUTS + self::MDY_SHORTCUTS;
200
                        break;
201
                    case 'DMY':
202
                    default:
203
                        $date_shortcuts = self::DATE_SHORTCUTS + self::DMY_SHORTCUTS;
204
                        break;
205
                }
206
207
                $title = I18N::translate('Date');
208
                $text  = view('help/date', [
209
                    'date_dates'            => $this->formatDates(array_keys($date_shortcuts)),
210
                    'date_shortcuts'        => $date_shortcuts,
211
                    'date_period_dates'     => $this->formatDates(array_keys(self::DATE_PERIOD_SHORTCUTS)),
212
                    'date_period_shortcuts' => self::DATE_PERIOD_SHORTCUTS,
213
                    'date_range_dates'      => $this->formatDates(array_keys(self::DATE_RANGE_SHORTCUTS)),
214
                    'date_range_shortcuts'  => self::DATE_RANGE_SHORTCUTS,
215
                    'french_dates'          => $this->formatDates(self::FRENCH_DATES),
216
                    'hijri_dates'           => $this->formatDates(self::HIJRI_DATES),
217
                    'jalali_dates'          => $this->formatDates(self::JALALI_DATES),
218
                    'jewish_dates'          => $this->formatDates(self::JEWISH_DATES),
219
                    'julian_dates'          => $this->formatDates(self::JULIAN_DATES),
220
                ]);
221
                break;
222
223
            case 'NAME':
224
                $title = I18N::translate('Name');
225
                $text  = view('help/name');
226
                break;
227
228
            case 'SURN':
229
                $title = I18N::translate('Surname');
230
                $text  = view('help/surname');
231
                break;
232
233
            case 'OBJE':
234
                $title = I18N::translate('Media object');
235
                $text  = view('help/media-object');
236
                break;
237
238
            case 'PLAC':
239
                $title = I18N::translate('Place');
240
                $text  = view('help/place');
241
                break;
242
243
            case 'RESN':
244
                $title = I18N::translate('Restriction');
245
                $text  = view('help/restriction');
246
                break;
247
248
            case 'ROMN':
249
                $title = I18N::translate('Romanized');
250
                $text  = view('help/romanized');
251
                break;
252
253
            case '_HEB':
254
                $title = I18N::translate('Hebrew');
255
                $text  = view('help/hebrew');
256
                break;
257
258
            case 'data-fixes':
259
                $title = I18N::translate('Data fixes');
260
                $text  = view('help/data-fixes');
261
                break;
262
263
            case 'edit_SOUR_EVEN':
264
                $title = I18N::translate('Associate events with this source');
265
                $text  = view('help/source-events');
266
                break;
267
268
            case 'pending_changes':
269
                $title = I18N::translate('Pending changes');
270
                $text  = view('help/pending-changes', [
271
                    'is_admin' => Auth::isAdmin(),
272
                ]);
273
                break;
274
275
            case 'relationship-privacy':
276
                $title = I18N::translate('Restrict to immediate family');
277
                $text  = view('help/relationship-privacy');
278
                break;
279
280
            default:
281
                $title = I18N::translate('Help');
282
                $text  = I18N::translate('The help text has not been written for this item.');
283
                break;
284
        }
285
286
        $html = view('modals/help', [
287
            'title' => $title,
288
            'text'  => $text,
289
        ]);
290
291
        return Registry::responseFactory()->response($html);
292
    }
293
294
    /**
295
     * Format GEDCOM dates in the local language.
296
     *
297
     * @param array<string>|array<int> $gedcom_dates
298
     *
299
     * @return array<string>
300
     */
301
    private function formatDates(array $gedcom_dates): array
302
    {
303
        $dates = [];
304
305
        foreach ($gedcom_dates as $gedcom_date) {
306
            // PHP converts numeric array keys ('1900') to integers (1900), so reverse this.
307
            $gedcom_date = (string) $gedcom_date;
308
309
            $date                = new Date($gedcom_date);
310
            $dates[$gedcom_date] = strip_tags($date->display());
311
        }
312
313
        return $dates;
314
    }
315
}
316