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