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