xref: /webtrees/app/Module/OnThisDayModule.php (revision 80ea34ecc70d4262c6d367d07e1df0ef880f5367)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Gedcom;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Services\CalendarService;
27use Fisharebest\Webtrees\Tree;
28use Fisharebest\Webtrees\Validator;
29use Illuminate\Support\Str;
30use Psr\Http\Message\ServerRequestInterface;
31
32/**
33 * Class OnThisDayModule
34 */
35class OnThisDayModule extends AbstractModule implements ModuleBlockInterface
36{
37    use ModuleBlockTrait;
38
39    // Pagination
40    private const LIMIT_LOW  = 10;
41    private const LIMIT_HIGH = 20;
42
43    // Default values for new blocks.
44    private const DEFAULT_SORT = 'date_desc';
45    private const DEFAULT_STYLE = 'date_desc';
46
47    // Initial sorting for datatables
48    private const DATATABLES_ORDER = [
49        'alpha'     => [[0, 'asc']],
50        'date_asc'  => [[2, 'asc']],
51        'date_desc' => [[2, 'desc']],
52    ];
53
54    // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
55    private const ALL_EVENTS = [
56        'ADOP' => 'INDI:ADOP',
57        'ANUL' => 'FAM:ANUL',
58        'BAPM' => 'INDI:BAPM',
59        'BARM' => 'INDI:BARM',
60        'BASM' => 'INDI:BASM',
61        'BIRT' => 'INDI:BIRT',
62        'BLES' => 'INDI:BLES',
63        'BURI' => 'INDI:BURI',
64        'CHR'  => 'INDI:CHR',
65        'CHRA' => 'INDI:CHRA',
66        'CONF' => 'INDI:CONF',
67        'CREM' => 'INDI:CREM',
68        'DEAT' => 'INDI:DEAT',
69        'DIV'  => 'FAM:DIV',
70        'DIVF' => 'FAM:DIVF',
71        'EMIG' => 'INDI:EMIG',
72        'ENGA' => 'FAM:ENGA',
73        'FCOM' => 'INDI:FCOM',
74        'GRAD' => 'INDI:GRAD',
75        'IMMI' => 'INDI:IMMI',
76        'MARB' => 'FAM:MARB',
77        'MARC' => 'FAM:MARC',
78        'MARL' => 'FAM:MARL',
79        'MARR' => 'FAM:MARR',
80        'MARS' => 'FAM:MARS',
81        'NATU' => 'INDI:NATU',
82        'ORDN' => 'INDI:ORDN',
83        'PROB' => 'INDI:PROB',
84        'RETI' => 'INDI:RETI',
85        'WILL' => 'INDI:WILL',
86    ];
87
88    private const DEFAULT_EVENTS = [
89        'BIRT',
90        'MARR',
91        'DEAT',
92    ];
93
94    /**
95     * How should this module be identified in the control panel, etc.?
96     *
97     * @return string
98     */
99    public function title(): string
100    {
101        /* I18N: Name of a module */
102        return I18N::translate('On this day');
103    }
104
105    /**
106     * A sentence describing what this module does.
107     *
108     * @return string
109     */
110    public function description(): string
111    {
112        /* I18N: Description of the “On this day” module */
113        return I18N::translate('A list of the anniversaries that occur today.');
114    }
115
116    /**
117     * Generate the HTML content of this block.
118     *
119     * @param Tree                 $tree
120     * @param int                  $block_id
121     * @param string               $context
122     * @param array<string,string> $config
123     *
124     * @return string
125     */
126    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
127    {
128        $calendar_service = new CalendarService();
129
130        $default_events = implode(',', self::DEFAULT_EVENTS);
131
132        $filter    = (bool) $this->getBlockSetting($block_id, 'filter', '1');
133        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
134        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT);
135        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
136
137        extract($config, EXTR_OVERWRITE);
138
139        $event_array = explode(',', $events);
140
141        // If we are only showing living individuals, then we don't need to search for DEAT events.
142        if ($filter) {
143            $event_array = array_diff($event_array, Gedcom::DEATH_EVENTS);
144        }
145
146        $events_filter = implode('|', $event_array);
147
148        $startjd = Registry::timestampFactory()->now()->julianDay();
149        $endjd   = $startjd;
150
151        $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
152
153        if ($facts->isEmpty()) {
154            if ($filter && Auth::check()) {
155                $message = I18N::translate('No events for living individuals exist for today.');
156            } else {
157                $message = I18N::translate('No events exist for today.');
158            }
159            $content = view('modules/todays_events/empty', ['message' => $message]);
160        } elseif ($infoStyle === 'list') {
161            $content = view('lists/anniversaries-list', [
162                'id'         => $block_id,
163                'facts'      => $facts,
164                'limit_low'  => self::LIMIT_LOW,
165                'limit_high' => self::LIMIT_HIGH,
166            ]);
167        } else {
168            $content = view('lists/anniversaries-table', [
169                'facts'      => $facts,
170                'limit_low'  => self::LIMIT_LOW,
171                'limit_high' => self::LIMIT_HIGH,
172                'order'      => self::DATATABLES_ORDER[$sortStyle] ?? self::DATATABLES_ORDER[self::DEFAULT_SORT],
173            ]);
174        }
175
176        if ($context !== self::CONTEXT_EMBED) {
177            return view('modules/block-template', [
178                'block'      => Str::kebab($this->name()),
179                'id'         => $block_id,
180                'config_url' => $this->configUrl($tree, $context, $block_id),
181                'title'      => $this->title(),
182                'content'    => $content,
183            ]);
184        }
185
186        return $content;
187    }
188
189    /**
190     * Should this block load asynchronously using AJAX?
191     *
192     * Simple blocks are faster in-line, more complex ones can be loaded later.
193     *
194     * @return bool
195     */
196    public function loadAjax(): bool
197    {
198        return true;
199    }
200
201    /**
202     * Can this block be shown on the user’s home page?
203     *
204     * @return bool
205     */
206    public function isUserBlock(): bool
207    {
208        return true;
209    }
210
211    /**
212     * Can this block be shown on the tree’s home page?
213     *
214     * @return bool
215     */
216    public function isTreeBlock(): bool
217    {
218        return true;
219    }
220
221    /**
222     * Update the configuration for a block.
223     *
224     * @param ServerRequestInterface $request
225     * @param int                    $block_id
226     *
227     * @return void
228     */
229    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
230    {
231        $filter     = Validator::parsedBody($request)->string('filter');
232        $info_style = Validator::parsedBody($request)->string('infoStyle');
233        $sort_style = Validator::parsedBody($request)->string('sortStyle');
234        $events     = Validator::parsedBody($request)->array('events');
235
236        $this->setBlockSetting($block_id, 'filter', $filter);
237        $this->setBlockSetting($block_id, 'infoStyle', $info_style);
238        $this->setBlockSetting($block_id, 'sortStyle', $sort_style);
239        $this->setBlockSetting($block_id, 'events', implode(',', $events));
240    }
241
242    /**
243     * An HTML form to edit block settings
244     *
245     * @param Tree $tree
246     * @param int  $block_id
247     *
248     * @return string
249     */
250    public function editBlockConfiguration(Tree $tree, int $block_id): string
251    {
252        $default_events = implode(',', self::DEFAULT_EVENTS);
253
254        $filter     = $this->getBlockSetting($block_id, 'filter', '1');
255        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
256        $sort_style = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT);
257        $events     = $this->getBlockSetting($block_id, 'events', $default_events);
258
259        $event_array = explode(',', $events);
260
261        $all_events = [];
262        foreach (self::ALL_EVENTS as $event => $tag) {
263            $all_events[$event] = Registry::elementFactory()->make($tag)->label();
264        }
265
266        $info_styles = [
267            /* I18N: An option in a list-box */
268            'list'  => I18N::translate('list'),
269            /* I18N: An option in a list-box */
270            'table' => I18N::translate('table'),
271        ];
272
273        $sort_styles = [
274            /* I18N: An option in a list-box */
275            'alpha' => I18N::translate('sort by name'),
276            /* I18N: An option in a list-box */
277            'anniv_asc'  => I18N::translate('sort by date, oldest first'),
278            /* I18N: An option in a list-box */
279            'anniv_desc' => I18N::translate('sort by date, newest first'),
280        ];
281
282        return view('modules/todays_events/config', [
283            'all_events'  => $all_events,
284            'event_array' => $event_array,
285            'filter'      => $filter,
286            'info_style'  => $info_style,
287            'info_styles' => $info_styles,
288            'sort_style'  => $sort_style,
289            'sort_styles' => $sort_styles,
290        ]);
291    }
292}
293