xref: /webtrees/app/Module/UpcomingAnniversariesModule.php (revision 8136679edbff8170fb92bd6e8dd8df8eb645bb1a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\Gedcom;
22use Fisharebest\Webtrees\GedcomTag;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\CalendarService;
25use Fisharebest\Webtrees\Tree;
26use Symfony\Component\HttpFoundation\Request;
27
28/**
29 * Class UpcomingAnniversariesModule
30 */
31class UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface
32{
33    use ModuleBlockTrait;
34
35    // Default values for new blocks.
36    private const DEFAULT_DAYS   = '7';
37    private const DEFAULT_FILTER = '1';
38    private const DEFAULT_SORT   = 'alpha';
39    private const DEFAULT_STYLE  = 'table';
40
41    // Can show this number of days into the future.
42    private const MIN_DAYS = 1;
43    private const MAX_DAYS = 30;
44
45    // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
46    private const ALL_EVENTS = [
47        'ADOP',
48        'ANUL',
49        'BAPM',
50        'BARM',
51        'BASM',
52        'BIRT',
53        'BLES',
54        'BURI',
55        'CHR',
56        'CHRA',
57        'CONF',
58        'CREM',
59        'DEAT',
60        'DIV',
61        'DIVF',
62        'EMIG',
63        'ENGA',
64        'FCOM',
65        'GRAD',
66        'IMMI',
67        'MARB',
68        'MARC',
69        'MARL',
70        'MARR',
71        'MARS',
72        'NATU',
73        'ORDN',
74        'PROB',
75        'RETI',
76        'WILL',
77    ];
78
79    private const DEFAULT_EVENTS = [
80        'BIRT',
81        'MARR',
82        'DEAT',
83    ];
84
85    /**
86     * How should this module be labelled on tabs, menus, etc.?
87     *
88     * @return string
89     */
90    public function title(): string
91    {
92        /* I18N: Name of a module */
93        return I18N::translate('Upcoming events');
94    }
95
96    /**
97     * A sentence describing what this module does.
98     *
99     * @return string
100     */
101    public function description(): string
102    {
103        /* I18N: Description of the “Upcoming events” module */
104        return I18N::translate('A list of the anniversaries that will occur in the near future.');
105    }
106
107    /**
108     * Generate the HTML content of this block.
109     *
110     * @param Tree     $tree
111     * @param int      $block_id
112     * @param string   $ctype
113     * @param string[] $cfg
114     *
115     * @return string
116     */
117    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
118    {
119        $calendar_service = new CalendarService();
120
121        $default_events = implode(',', self::DEFAULT_EVENTS);
122
123        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
124        $filter    = (bool) $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER);
125        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
126        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT);
127        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
128
129        extract($cfg, EXTR_OVERWRITE);
130
131        $event_array = explode(',', $events);
132
133        // If we are only showing living individuals, then we don't need to search for DEAT events.
134        if ($filter) {
135            $event_array  = array_diff($event_array, Gedcom::DEATH_EVENTS);
136        }
137
138        $events_filter = implode('|', $event_array);
139
140        $startjd = WT_CLIENT_JD + 1;
141        $endjd   = WT_CLIENT_JD + $days;
142
143        $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
144
145        if (empty($facts)) {
146            if ($endjd == $startjd) {
147                $content = view('modules/upcoming_events/empty', [
148                    'message' => I18N::translate('No events exist for tomorrow.'),
149                ]);
150            } else {
151                /* I18N: translation for %s==1 is unused; it is translated separately as “tomorrow” */                $content = view('modules/upcoming_events/empty', [
152                    '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)),
153                ]);
154            }
155        } elseif ($infoStyle === 'list') {
156            $content = view('modules/upcoming_events/list', [
157                'facts' => $facts,
158            ]);
159        } else {
160            $content = view('modules/upcoming_events/table', [
161                'facts' => $facts,
162            ]);
163        }
164
165        if ($ctype !== '') {
166            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
167                $config_url = route('tree-page-block-edit', [
168                    'block_id' => $block_id,
169                    'ged'      => $tree->name(),
170                ]);
171            } elseif ($ctype === 'user' && Auth::check()) {
172                $config_url = route('user-page-block-edit', [
173                    'block_id' => $block_id,
174                    'ged'      => $tree->name(),
175                ]);
176            } else {
177                $config_url = '';
178            }
179
180            return view('modules/block-template', [
181                'block'      => str_replace('_', '-', $this->name()),
182                'id'         => $block_id,
183                'config_url' => $config_url,
184                'title'      => $this->title(),
185                'content'    => $content,
186            ]);
187        }
188
189        return $content;
190    }
191
192    /** {@inheritdoc} */
193    public function loadAjax(): bool
194    {
195        return true;
196    }
197
198    /** {@inheritdoc} */
199    public function isUserBlock(): bool
200    {
201        return true;
202    }
203
204    /** {@inheritdoc} */
205    public function isTreeBlock(): bool
206    {
207        return true;
208    }
209
210    /**
211     * Update the configuration for a block.
212     *
213     * @param Request $request
214     * @param int     $block_id
215     *
216     * @return void
217     */
218    public function saveBlockConfiguration(Request $request, int $block_id)
219    {
220        $this->setBlockSetting($block_id, 'days', $request->get('days', self::DEFAULT_DAYS));
221        $this->setBlockSetting($block_id, 'filter', $request->get('filter', ''));
222        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
223        $this->setBlockSetting($block_id, 'sortStyle', $request->get('sortStyle', self::DEFAULT_SORT));
224        $this->setBlockSetting($block_id, 'events', implode(',', (array) $request->get('events')));
225    }
226
227    /**
228     * An HTML form to edit block settings
229     *
230     * @param Tree $tree
231     * @param int  $block_id
232     *
233     * @return void
234     */
235    public function editBlockConfiguration(Tree $tree, int $block_id)
236    {
237        $default_events = implode(',', self::DEFAULT_EVENTS);
238
239        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
240        $filter    = $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER);
241        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
242        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT);
243        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
244
245        $event_array = explode(',', $events);
246
247        $all_events = [];
248        foreach (self::ALL_EVENTS as $event) {
249            $all_events[$event] = GedcomTag::getLabel($event);
250        }
251
252        $info_styles = [
253            /* I18N: An option in a list-box */
254            'list'  => I18N::translate('list'),
255            /* I18N: An option in a list-box */
256            'table' => I18N::translate('table'),
257        ];
258
259        $sort_styles = [
260            /* I18N: An option in a list-box */
261            'alpha' => I18N::translate('sort by name'),
262            /* I18N: An option in a list-box */
263            'anniv' => I18N::translate('sort by date'),
264        ];
265
266        echo view('modules/upcoming_events/config', [
267            'all_events'  => $all_events,
268            'days'        => $days,
269            'event_array' => $event_array,
270            'filter'      => $filter,
271            'infoStyle'   => $infoStyle,
272            'info_styles' => $info_styles,
273            'max_days'    => self::MAX_DAYS,
274            'sortStyle'   => $sortStyle,
275            'sort_styles' => $sort_styles,
276        ]);
277    }
278}
279