xref: /webtrees/app/Module/UpcomingAnniversariesModule.php (revision e24053e5c68a36a62ca2714caf7fca093e7dc791)
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 UpcomingAnniversariesModule
33 */
34class UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface
35{
36    use ModuleBlockTrait;
37
38    // Default values for new blocks.
39    private const DEFAULT_DAYS   = '7';
40    private const DEFAULT_FILTER = '1';
41    private const DEFAULT_SORT   = 'alpha';
42    private const DEFAULT_STYLE  = 'table';
43
44    // Can show this number of days into the future.
45    private const MIN_DAYS = 1;
46    private const MAX_DAYS = 30;
47
48    // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
49    private const ALL_EVENTS = [
50        'ADOP',
51        'ANUL',
52        'BAPM',
53        'BARM',
54        'BASM',
55        'BIRT',
56        'BLES',
57        'BURI',
58        'CHR',
59        'CHRA',
60        'CONF',
61        'CREM',
62        'DEAT',
63        'DIV',
64        'DIVF',
65        'EMIG',
66        'ENGA',
67        'FCOM',
68        'GRAD',
69        'IMMI',
70        'MARB',
71        'MARC',
72        'MARL',
73        'MARR',
74        'MARS',
75        'NATU',
76        'ORDN',
77        'PROB',
78        'RETI',
79        'WILL',
80    ];
81
82    private const DEFAULT_EVENTS = [
83        'BIRT',
84        'MARR',
85        'DEAT',
86    ];
87
88    /**
89     * How should this module be identified in the control panel, etc.?
90     *
91     * @return string
92     */
93    public function title(): string
94    {
95        /* I18N: Name of a module */
96        return I18N::translate('Upcoming events');
97    }
98
99    /**
100     * A sentence describing what this module does.
101     *
102     * @return string
103     */
104    public function description(): string
105    {
106        /* I18N: Description of the “Upcoming events” module */
107        return I18N::translate('A list of the anniversaries that will occur in the near future.');
108    }
109
110    /**
111     * Generate the HTML content of this block.
112     *
113     * @param Tree     $tree
114     * @param int      $block_id
115     * @param string   $context
116     * @param string[] $config
117     *
118     * @return string
119     */
120    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
121    {
122        $calendar_service = new CalendarService();
123
124        $default_events = implode(',', self::DEFAULT_EVENTS);
125
126        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
127        $filter    = (bool) $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER);
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 = Carbon::now()->julianDay() + 1;
144        $endjd   = Carbon::now()->julianDay() + $days;
145
146        $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
147
148        if ($facts->isEmpty()) {
149            if ($endjd == $startjd) {
150                $content = view('modules/upcoming_events/empty', [
151                    'message' => I18N::translate('No events exist for tomorrow.'),
152                ]);
153            } else {
154                /* I18N: translation for %s==1 is unused; it is translated separately as “tomorrow” */                $content = view('modules/upcoming_events/empty', [
155                    'message' => I18N::plural('No events exist for the next %s day.', 'No events exist for the next %s days.', $endjd - $startjd + 1, I18N::number($endjd - $startjd + 1)),
156                ]);
157            }
158        } elseif ($infoStyle === 'list') {
159            $content = view('lists/anniversaries-list', [
160                'id'    => $block_id,
161                'facts' => $facts,
162                'limit' => 10,
163            ]);
164        } else {
165            $content = view('lists/anniversaries-table', [
166                'facts' => $facts,
167                'limit' => 10,
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        $params = (array) $request->getParsedBody();
227
228        $this->setBlockSetting($block_id, 'days', $params['days']);
229        $this->setBlockSetting($block_id, 'filter', $params['filter']);
230        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
231        $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']);
232        $this->setBlockSetting($block_id, 'events', implode(',', $params['events'] ?? []));
233    }
234
235    /**
236     * An HTML form to edit block settings
237     *
238     * @param Tree $tree
239     * @param int  $block_id
240     *
241     * @return string
242     */
243    public function editBlockConfiguration(Tree $tree, int $block_id): string
244    {
245        $default_events = implode(',', self::DEFAULT_EVENTS);
246
247        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
248        $filter    = $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER);
249        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
250        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT);
251        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
252
253        $event_array = explode(',', $events);
254
255        $all_events = [];
256        foreach (self::ALL_EVENTS as $event) {
257            $all_events[$event] = GedcomTag::getLabel($event);
258        }
259
260        $info_styles = [
261            /* I18N: An option in a list-box */
262            'list'  => I18N::translate('list'),
263            /* I18N: An option in a list-box */
264            'table' => I18N::translate('table'),
265        ];
266
267        $sort_styles = [
268            /* I18N: An option in a list-box */
269            'alpha' => I18N::translate('sort by name'),
270            /* I18N: An option in a list-box */
271            'anniv' => I18N::translate('sort by date'),
272        ];
273
274        return view('modules/upcoming_events/config', [
275            'all_events'  => $all_events,
276            'days'        => $days,
277            'event_array' => $event_array,
278            'filter'      => $filter,
279            'infoStyle'   => $infoStyle,
280            'info_styles' => $info_styles,
281            'max_days'    => self::MAX_DAYS,
282            'sortStyle'   => $sortStyle,
283            'sort_styles' => $sort_styles,
284        ]);
285    }
286}
287