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