CalendarPage::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Date;
23
use Fisharebest\Webtrees\Http\ViewResponseTrait;
24
use Fisharebest\Webtrees\I18N;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Fisharebest\Webtrees\Services\CalendarService;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Services\CalendarService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Fisharebest\Webtrees\Validator;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
final class CalendarPage implements RequestHandlerInterface
32
{
33
    use ViewResponseTrait;
34
35
    public function __construct(
36
        private readonly CalendarService $calendar_service,
37
    ) {
38
    }
39
40
    public function handle(ServerRequestInterface $request): ResponseInterface
41
    {
42
        $tree     = Validator::attributes($request)->tree();
43
        $view     = Validator::attributes($request)->isInArray(['day', 'month', 'year'])->string('view');
44
        $cal      = Validator::queryParams($request)->string('cal', '');
45
        $day      = Validator::queryParams($request)->string('day', '');
46
        $month    = Validator::queryParams($request)->string('month', '');
47
        $year     = Validator::queryParams($request)->string('year', '');
48
        $filterev = Validator::queryParams($request)->string('filterev', 'BIRT-MARR-DEAT');
49
        $filterof = Validator::queryParams($request)->string('filterof', 'all');
50
        $filtersx = Validator::queryParams($request)->string('filtersx', '');
51
52
        if ($cal . $day . $month . $year === '') {
53
            // No date specified? Use the most likely calendar
54
            $cal = I18N::language()->calendar()->gedcomCalendarEscape();
55
        }
56
57
        // need BC to parse date
58
        if (str_starts_with($year, '-')) {
59
            $year = substr($year, 1) . ' B.C.';
60
        }
61
        $ged_date = new Date($cal . ' ' . $day . ' ' . $month . ' ' . $year);
62
        // need negative year for year entry field.
63
        $year     = $ged_date->minimumDate()->year;
64
        $cal_date = $ged_date->minimumDate();
65
66
        // Fill in any missing bits with todays date
67
        $today = $cal_date->today();
68
        if ($cal_date->day === 0) {
69
            $cal_date->day = $today->day;
70
        }
71
        if ($cal_date->month === 0) {
72
            $cal_date->month = $today->month;
73
        }
74
        if ($cal_date->year === 0) {
75
            $cal_date->year = $today->year;
76
        }
77
78
        $cal_date->setJdFromYmd();
79
80
        if ($year === 0) {
81
            $year = $cal_date->year;
82
        }
83
84
        // Extract values from date
85
        $days_in_month = $cal_date->daysInMonth();
86
        $cal_month     = $cal_date->format('%O');
87
        $today_month   = $today->format('%O');
88
89
        // Invalid dates? Go to monthly view, where they'll be found.
90
        if ($cal_date->day > $days_in_month && $view === 'day') {
91
            $view = 'month';
92
        }
93
94
        $title = I18N::translate('Anniversary calendar');
95
96
        switch ($view) {
97
            case 'day':
98
                $title = I18N::translate('On this day…') . ' ' . $ged_date->display($tree);
99
                break;
100
            case 'month':
101
                $title = I18N::translate('In this month…') . ' ' . $ged_date->display($tree, '%F %Y');
102
                break;
103
            case 'year':
104
                $title = I18N::translate('In this year…') . ' ' . $ged_date->display($tree, '%Y');
105
                break;
106
        }
107
108
        return $this->viewResponse('calendar-page', [
109
            'cal'           => $cal,
110
            'cal_date'      => $cal_date,
111
            'cal_month'     => $cal_month,
112
            'day'           => $day,
113
            'days_in_month' => $days_in_month,
114
            'filterev'      => $filterev,
115
            'filterof'      => $filterof,
116
            'filtersx'      => $filtersx,
117
            'month'         => $month,
118
            'months'        => $this->calendar_service->calendarMonthsInYear($cal, $year),
119
            'title'         => $title,
120
            'today'         => $today,
121
            'today_month'   => $today_month,
122
            'tree'          => $tree,
123
            'view'          => $view,
124
            'year'          => $year,
125
        ]);
126
    }
127
}
128