xref: /webtrees/app/Module/OnThisDayModule.php (revision b0f03dfe90252b729e1ddaafe0b37dc485b3c221)
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    public function description(): string
106    {
107        /* I18N: Description of the “On this day” module */
108        return I18N::translate('A list of the anniversaries that occur today.');
109    }
110
111    /**
112     * Generate the HTML content of this block.
113     *
114     * @param Tree                 $tree
115     * @param int                  $block_id
116     * @param string               $context
117     * @param array<string,string> $config
118     *
119     * @return string
120     */
121    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
122    {
123        $calendar_service = new CalendarService();
124
125        $default_events = implode(',', self::DEFAULT_EVENTS);
126
127        $filter    = (bool) $this->getBlockSetting($block_id, 'filter', '1');
128        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
129        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT);
130        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
131
132        extract($config, EXTR_OVERWRITE);
133
134        $event_array = explode(',', $events);
135
136        // If we are only showing living individuals, then we don't need to search for DEAT events.
137        if ($filter) {
138            $event_array = array_diff($event_array, Gedcom::DEATH_EVENTS);
139        }
140
141        $events_filter = implode('|', $event_array);
142
143        $startjd = Registry::timestampFactory()->now()->julianDay();
144        $endjd   = $startjd;
145
146        $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
147
148        if ($facts->isEmpty()) {
149            if ($filter && Auth::check()) {
150                $message = I18N::translate('No events for living individuals exist for today.');
151            } else {
152                $message = I18N::translate('No events exist for today.');
153            }
154            $content = view('modules/todays_events/empty', ['message' => $message]);
155        } elseif ($infoStyle === 'list') {
156            $content = view('lists/anniversaries-list', [
157                'id'         => $block_id,
158                'facts'      => $facts,
159                'limit_low'  => self::LIMIT_LOW,
160                'limit_high' => self::LIMIT_HIGH,
161            ]);
162        } else {
163            $content = view('lists/anniversaries-table', [
164                'facts'      => $facts,
165                'limit_low'  => self::LIMIT_LOW,
166                'limit_high' => self::LIMIT_HIGH,
167                'order'      => self::DATATABLES_ORDER[$sortStyle] ?? self::DATATABLES_ORDER[self::DEFAULT_SORT],
168            ]);
169        }
170
171        if ($context !== self::CONTEXT_EMBED) {
172            return view('modules/block-template', [
173                'block'      => Str::kebab($this->name()),
174                'id'         => $block_id,
175                'config_url' => $this->configUrl($tree, $context, $block_id),
176                'title'      => $this->title(),
177                'content'    => $content,
178            ]);
179        }
180
181        return $content;
182    }
183
184    /**
185     * Should this block load asynchronously using AJAX?
186     *
187     * Simple blocks are faster in-line, more complex ones can be loaded later.
188     *
189     * @return bool
190     */
191    public function loadAjax(): bool
192    {
193        return true;
194    }
195
196    /**
197     * Can this block be shown on the user’s home page?
198     *
199     * @return bool
200     */
201    public function isUserBlock(): bool
202    {
203        return true;
204    }
205
206    /**
207     * Can this block be shown on the tree’s home page?
208     *
209     * @return bool
210     */
211    public function isTreeBlock(): bool
212    {
213        return true;
214    }
215
216    /**
217     * Update the configuration for a block.
218     *
219     * @param ServerRequestInterface $request
220     * @param int                    $block_id
221     *
222     * @return void
223     */
224    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
225    {
226        $filter     = Validator::parsedBody($request)->string('filter');
227        $info_style = Validator::parsedBody($request)->string('infoStyle');
228        $sort_style = Validator::parsedBody($request)->string('sortStyle');
229        $events     = Validator::parsedBody($request)->array('events');
230
231        $this->setBlockSetting($block_id, 'filter', $filter);
232        $this->setBlockSetting($block_id, 'infoStyle', $info_style);
233        $this->setBlockSetting($block_id, 'sortStyle', $sort_style);
234        $this->setBlockSetting($block_id, 'events', implode(',', $events));
235    }
236
237    /**
238     * An HTML form to edit block settings
239     *
240     * @param Tree $tree
241     * @param int  $block_id
242     *
243     * @return string
244     */
245    public function editBlockConfiguration(Tree $tree, int $block_id): string
246    {
247        $default_events = implode(',', self::DEFAULT_EVENTS);
248
249        $filter     = $this->getBlockSetting($block_id, 'filter', '1');
250        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
251        $sort_style = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT);
252        $events     = $this->getBlockSetting($block_id, 'events', $default_events);
253
254        $event_array = explode(',', $events);
255
256        $all_events = [];
257        foreach (self::ALL_EVENTS as $event => $tag) {
258            $all_events[$event] = Registry::elementFactory()->make($tag)->label();
259        }
260
261        $info_styles = [
262            /* I18N: An option in a list-box */
263            'list'  => I18N::translate('list'),
264            /* I18N: An option in a list-box */
265            'table' => I18N::translate('table'),
266        ];
267
268        $sort_styles = [
269            /* I18N: An option in a list-box */
270            'alpha' => I18N::translate('sort by name'),
271            /* I18N: An option in a list-box */
272            'anniv_asc'  => I18N::translate('sort by date, oldest first'),
273            /* I18N: An option in a list-box */
274            'anniv_desc' => I18N::translate('sort by date, newest first'),
275        ];
276
277        return view('modules/todays_events/config', [
278            'all_events'  => $all_events,
279            'event_array' => $event_array,
280            'filter'      => $filter,
281            'info_style'  => $info_style,
282            'info_styles' => $info_styles,
283            'sort_style'  => $sort_style,
284            'sort_styles' => $sort_styles,
285        ]);
286    }
287}
288