xref: /webtrees/app/Module/UpcomingAnniversariesModule.php (revision 59597b37d69e8147c3f4a27643e9c8edaa2a0592)
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 (empty($facts)) {
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                'facts' => $facts,
161            ]);
162        } else {
163            $content = view('lists/anniversaries-table', [
164                'facts' => $facts,
165            ]);
166        }
167
168        if ($context !== self::CONTEXT_EMBED) {
169            return view('modules/block-template', [
170                'block'      => Str::kebab($this->name()),
171                'id'         => $block_id,
172                'config_url' => $this->configUrl($tree, $context, $block_id),
173                'title'      => $this->title(),
174                'content'    => $content,
175            ]);
176        }
177
178        return $content;
179    }
180
181    /**
182     * Should this block load asynchronously using AJAX?
183     *
184     * Simple blocks are faster in-line, more complex ones can be loaded later.
185     *
186     * @return bool
187     */
188    public function loadAjax(): bool
189    {
190        return true;
191    }
192
193    /**
194     * Can this block be shown on the user’s home page?
195     *
196     * @return bool
197     */
198    public function isUserBlock(): bool
199    {
200        return true;
201    }
202
203    /**
204     * Can this block be shown on the tree’s home page?
205     *
206     * @return bool
207     */
208    public function isTreeBlock(): bool
209    {
210        return true;
211    }
212
213    /**
214     * Update the configuration for a block.
215     *
216     * @param ServerRequestInterface $request
217     * @param int     $block_id
218     *
219     * @return void
220     */
221    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
222    {
223        $params = $request->getParsedBody();
224
225        $this->setBlockSetting($block_id, 'days', $params['days']);
226        $this->setBlockSetting($block_id, 'filter', $params['filter']);
227        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
228        $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']);
229        $this->setBlockSetting($block_id, 'events', implode(',', $params['events'] ?? []));
230    }
231
232    /**
233     * An HTML form to edit block settings
234     *
235     * @param Tree $tree
236     * @param int  $block_id
237     *
238     * @return string
239     */
240    public function editBlockConfiguration(Tree $tree, int $block_id): string
241    {
242        $default_events = implode(',', self::DEFAULT_EVENTS);
243
244        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
245        $filter    = $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER);
246        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
247        $sortStyle = $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) {
254            $all_events[$event] = GedcomTag::getLabel($event);
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' => I18N::translate('sort by date'),
269        ];
270
271        return view('modules/upcoming_events/config', [
272            'all_events'  => $all_events,
273            'days'        => $days,
274            'event_array' => $event_array,
275            'filter'      => $filter,
276            'infoStyle'   => $infoStyle,
277            'info_styles' => $info_styles,
278            'max_days'    => self::MAX_DAYS,
279            'sortStyle'   => $sortStyle,
280            'sort_styles' => $sort_styles,
281        ]);
282    }
283}
284