xref: /webtrees/app/Module/OnThisDayModule.php (revision 126654d77c042f1254afcd89fbf831cc610b808e)
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 OnThisDayModule
30 */
31class OnThisDayModule extends AbstractModule implements ModuleBlockInterface
32{
33    // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
34    const ALL_EVENTS = [
35        'ADOP',
36        'ANUL',
37        'BAPM',
38        'BARM',
39        'BASM',
40        'BIRT',
41        'BLES',
42        'BURI',
43        'CHR',
44        'CHRA',
45        'CONF',
46        'CREM',
47        'DEAT',
48        'DIV',
49        'DIVF',
50        'EMIG',
51        'ENGA',
52        'FCOM',
53        'GRAD',
54        'IMMI',
55        'MARB',
56        'MARC',
57        'MARL',
58        'MARR',
59        'MARS',
60        'NATU',
61        'ORDN',
62        'PROB',
63        'RETI',
64        'WILL',
65    ];
66
67    const DEFAULT_EVENTS = [
68        'BIRT',
69        'MARR',
70        'DEAT',
71    ];
72
73    /**
74     * How should this module be labelled on tabs, menus, etc.?
75     *
76     * @return string
77     */
78    public function getTitle(): string
79    {
80        /* I18N: Name of a module */
81        return I18N::translate('On this day');
82    }
83
84    /**
85     * A sentence describing what this module does.
86     *
87     * @return string
88     */
89    public function getDescription(): string
90    {
91        /* I18N: Description of the “On this day” module */
92        return I18N::translate('A list of the anniversaries that occur today.');
93    }
94
95    /**
96     * Generate the HTML content of this block.
97     *
98     * @param Tree     $tree
99     * @param int      $block_id
100     * @param string   $ctype
101     * @param string[] $cfg
102     *
103     * @return string
104     */
105    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
106    {
107        $calendar_service = new CalendarService();
108
109        $default_events = implode(',', self::DEFAULT_EVENTS);
110
111        $filter    = (bool) $this->getBlockSetting($block_id, 'filter', '1');
112        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
113        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
114        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
115
116        extract($cfg, EXTR_OVERWRITE);
117
118        $event_array = explode(',', $events);
119
120        // If we are only showing living individuals, then we don't need to search for DEAT events.
121        if ($filter) {
122            $event_array  = array_diff($event_array, Gedcom::DEATH_EVENTS);
123        }
124
125        $events_filter = implode('|', $event_array);
126
127        $startjd = WT_CLIENT_JD;
128        $endjd   = WT_CLIENT_JD;
129
130        $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
131
132        if (empty($facts)) {
133            $content = view('modules/todays_events/empty');
134        } elseif ($infoStyle === 'list') {
135            $content = view('modules/todays_events/list', [
136                'facts' => $facts,
137            ]);
138        } else {
139            $content = view('modules/todays_events/table', [
140                'facts' => $facts,
141            ]);
142        }
143
144        if ($ctype !== '') {
145            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
146                $config_url = route('tree-page-block-edit', [
147                    'block_id' => $block_id,
148                    'ged'      => $tree->name(),
149                ]);
150            } elseif ($ctype === 'user' && Auth::check()) {
151                $config_url = route('user-page-block-edit', [
152                    'block_id' => $block_id,
153                    'ged'      => $tree->name(),
154                ]);
155            } else {
156                $config_url = '';
157            }
158
159            return view('modules/block-template', [
160                'block'      => str_replace('_', '-', $this->getName()),
161                'id'         => $block_id,
162                'config_url' => $config_url,
163                'title'      => $this->getTitle(),
164                'content'    => $content,
165            ]);
166        }
167
168        return $content;
169    }
170
171    /** {@inheritdoc} */
172    public function loadAjax(): bool
173    {
174        return true;
175    }
176
177    /** {@inheritdoc} */
178    public function isUserBlock(): bool
179    {
180        return true;
181    }
182
183    /** {@inheritdoc} */
184    public function isGedcomBlock(): bool
185    {
186        return true;
187    }
188
189    /**
190     * Update the configuration for a block.
191     *
192     * @param Request $request
193     * @param int     $block_id
194     *
195     * @return void
196     */
197    public function saveBlockConfiguration(Request $request, int $block_id)
198    {
199        $this->setBlockSetting($block_id, 'filter', $request->get('filter', '1'));
200        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', 'table'));
201        $this->setBlockSetting($block_id, 'sortStyle', $request->get('sortStyle', 'alpha'));
202        $this->setBlockSetting($block_id, 'events', implode(',', (array) $request->get('events')));
203    }
204
205    /**
206     * An HTML form to edit block settings
207     *
208     * @param Tree $tree
209     * @param int  $block_id
210     *
211     * @return void
212     */
213    public function editBlockConfiguration(Tree $tree, int $block_id)
214    {
215        $default_events = implode(',', self::DEFAULT_EVENTS);
216
217        $filter    = $this->getBlockSetting($block_id, 'filter', '1');
218        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
219        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
220        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
221
222        $event_array = explode(',', $events);
223
224        $all_events = [];
225        foreach (self::ALL_EVENTS as $event) {
226            $all_events[$event] = GedcomTag::getLabel($event);
227        }
228
229        $info_styles = [
230            /* I18N: An option in a list-box */
231            'list'  => I18N::translate('list'),
232            /* I18N: An option in a list-box */
233            'table' => I18N::translate('table'),
234        ];
235
236        $sort_styles = [
237            /* I18N: An option in a list-box */
238            'alpha' => I18N::translate('sort by name'),
239            /* I18N: An option in a list-box */
240            'anniv' => I18N::translate('sort by date'),
241        ];
242
243        echo view('modules/todays_events/config', [
244            'all_events'  => $all_events,
245            'event_array' => $event_array,
246            'filter'      => $filter,
247            'infoStyle'   => $infoStyle,
248            'info_styles' => $info_styles,
249            'sortStyle'   => $sortStyle,
250            'sort_styles' => $sort_styles,
251        ]);
252    }
253}
254