xref: /webtrees/app/Module/OnThisDayModule.php (revision f0a114195ac1eac96981ed21f4681b1f2d0de72c)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Filter;
20use Fisharebest\Webtrees\GedcomTag;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Services\CalendarService;
23use Fisharebest\Webtrees\Tree;
24
25/**
26 * Class OnThisDayModule
27 */
28class OnThisDayModule extends AbstractModule implements ModuleBlockInterface
29{
30    // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
31    const ALL_EVENTS = [
32        'ADOP',
33        'ANUL',
34        'BAPM',
35        'BARM',
36        'BASM',
37        'BIRT',
38        'BLES',
39        'BURI',
40        'CHR',
41        'CHRA',
42        'CONF',
43        'CREM',
44        'DEAT',
45        'DIV',
46        'DIVF',
47        'EMIG',
48        'ENGA',
49        'FCOM',
50        'GRAD',
51        'IMMI',
52        'MARB',
53        'MARC',
54        'MARL',
55        'MARR',
56        'MARS',
57        'NATU',
58        'ORDN',
59        'PROB',
60        'RETI',
61        'WILL',
62    ];
63
64    const DEFAULT_EVENTS = [
65        'BIRT',
66        'MARR',
67        'DEAT',
68    ];
69
70    /**
71     * How should this module be labelled on tabs, menus, etc.?
72     *
73     * @return string
74     */
75    public function getTitle()
76    {
77        /* I18N: Name of a module */
78        return I18N::translate('On this day');
79    }
80
81    /**
82     * A sentence describing what this module does.
83     *
84     * @return string
85     */
86    public function getDescription()
87    {
88        /* I18N: Description of the “On this day” module */
89        return I18N::translate('A list of the anniversaries that occur today.');
90    }
91
92    /**
93     * Generate the HTML content of this block.
94     *
95     * @param Tree     $tree
96     * @param int      $block_id
97     * @param bool     $template
98     * @param string[] $cfg
99     *
100     * @return string
101     */
102    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
103    {
104        global $ctype;
105
106        $calendar_service = new CalendarService();
107
108        $default_events = implode(',', self::DEFAULT_EVENTS);
109
110        $filter    = (bool)$this->getBlockSetting($block_id, 'filter', '1');
111        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
112        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
113        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
114
115        extract($cfg, EXTR_OVERWRITE);
116
117        $event_array = explode(',', $events);
118
119        // If we are only showing living individuals, then we don't need to search for DEAT events.
120        if ($filter) {
121            $death_events = explode('|', WT_EVENTS_DEAT);
122            $event_array  = array_diff($event_array, $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 ($template) {
145            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
146                $config_url = route('tree-page-block-edit', [
147                    'block_id' => $block_id,
148                    'ged'      => $tree->getName(),
149                ]);
150            } elseif ($ctype === 'user' && Auth::check()) {
151                $config_url = route('user-page-block-edit', [
152                    'block_id' => $block_id,
153                    'ged'      => $tree->getName(),
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        } else {
167            return $content;
168        }
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     * An HTML form to edit block settings
191     *
192     * @param Tree $tree
193     * @param int  $block_id
194     *
195     * @return void
196     */
197    public function configureBlock(Tree $tree, int $block_id)
198    {
199        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
200            $this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
201            $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
202            $this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
203            $this->setBlockSetting($block_id, 'events', implode(',', Filter::postArray('events')));
204
205            return;
206        }
207
208        $default_events = implode(',', self::DEFAULT_EVENTS);
209
210        $filter    = $this->getBlockSetting($block_id, 'filter', '1');
211        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
212        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
213        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
214
215        $event_array = explode(',', $events);
216
217        $all_events = [];
218        foreach (self::ALL_EVENTS as $event) {
219            $all_events[$event] = GedcomTag::getLabel($event);
220        }
221
222        $info_styles = [
223            /* I18N: An option in a list-box */
224            'list'  => I18N::translate('list'),
225            /* I18N: An option in a list-box */
226            'table' => I18N::translate('table'),
227        ];
228
229        $sort_styles = [
230            /* I18N: An option in a list-box */
231            'alpha' => I18N::translate('sort by name'),
232            /* I18N: An option in a list-box */
233            'anniv' => I18N::translate('sort by date'),
234        ];
235
236        echo view('modules/todays_events/config', [
237            'all_events'  => $all_events,
238            'event_array' => $event_array,
239            'filter'      => $filter,
240            'infoStyle'   => $infoStyle,
241            'info_styles' => $info_styles,
242            'sortStyle'   => $sortStyle,
243            'sort_styles' => $sort_styles,
244        ]);
245    }
246}
247