xref: /webtrees/app/Module/OnThisDayModule.php (revision c1010eda29c0909ed4d5d463f32d32bfefdd4dfe)
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\Functions\FunctionsDb;
21use Fisharebest\Webtrees\GedcomTag;
22use Fisharebest\Webtrees\I18N;
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        return /* I18N: Name of a module */
78            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        return /* I18N: Description of the “On this day” module */
89            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        $default_events = implode(',', self::DEFAULT_EVENTS);
107
108        $filter    = (bool)$this->getBlockSetting($block_id, 'filter', '1');
109        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
110        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
111        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
112
113        extract($cfg, EXTR_OVERWRITE);
114
115        $event_array = explode(',', $events);
116
117        // If we are only showing living individuals, then we don't need to search for DEAT events.
118        if ($filter) {
119            $death_events = explode('|', WT_EVENTS_DEAT);
120            $event_array  = array_diff($event_array, $death_events);
121        }
122
123        $events_filter = implode('|', $event_array);
124
125        $startjd = WT_CLIENT_JD;
126        $endjd   = WT_CLIENT_JD;
127
128        $facts = FunctionsDb::getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
129
130        if (empty($facts)) {
131            $content = view('modules/todays_events/empty');
132        } elseif ($infoStyle === 'list') {
133            $content = view('modules/todays_events/list', [
134                'facts' => $facts,
135            ]);
136        } else {
137            $content = view('modules/todays_events/table', [
138                'facts' => $facts,
139            ]);
140        }
141
142        if ($template) {
143            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
144                $config_url = route('tree-page-block-edit', [
145                    'block_id' => $block_id,
146                    'ged'      => $tree->getName(),
147                ]);
148            } elseif ($ctype === 'user' && Auth::check()) {
149                $config_url = route('user-page-block-edit', [
150                    'block_id' => $block_id,
151                    'ged'      => $tree->getName(),
152                ]);
153            } else {
154                $config_url = '';
155            }
156
157            return view('modules/block-template', [
158                'block'      => str_replace('_', '-', $this->getName()),
159                'id'         => $block_id,
160                'config_url' => $config_url,
161                'title'      => $this->getTitle(),
162                'content'    => $content,
163            ]);
164        } else {
165            return $content;
166        }
167    }
168
169    /** {@inheritdoc} */
170    public function loadAjax(): bool
171    {
172        return true;
173    }
174
175    /** {@inheritdoc} */
176    public function isUserBlock(): bool
177    {
178        return true;
179    }
180
181    /** {@inheritdoc} */
182    public function isGedcomBlock(): bool
183    {
184        return true;
185    }
186
187    /**
188     * An HTML form to edit block settings
189     *
190     * @param Tree $tree
191     * @param int  $block_id
192     *
193     * @return void
194     */
195    public function configureBlock(Tree $tree, int $block_id)
196    {
197        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
198            $this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
199            $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
200            $this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
201            $this->setBlockSetting($block_id, 'events', implode(',', Filter::postArray('events')));
202
203            return;
204        }
205
206        $default_events = implode(',', self::DEFAULT_EVENTS);
207
208        $filter    = $this->getBlockSetting($block_id, 'filter', '1');
209        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
210        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
211        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
212
213        $event_array = explode(',', $events);
214
215        $all_events = [];
216        foreach (self::ALL_EVENTS as $event) {
217            $all_events[$event] = GedcomTag::getLabel($event);
218        }
219
220        $info_styles = [
221            'list'  => /* I18N: An option in a list-box */
222                I18N::translate('list'),
223            'table' => /* I18N: An option in a list-box */
224                I18N::translate('table'),
225        ];
226
227        $sort_styles = [
228            'alpha' => /* I18N: An option in a list-box */
229                I18N::translate('sort by name'),
230            'anniv' => /* I18N: An option in a list-box */
231                I18N::translate('sort by date'),
232        ];
233
234        echo view('modules/todays_events/config', [
235            'all_events'  => $all_events,
236            'event_array' => $event_array,
237            'filter'      => $filter,
238            'infoStyle'   => $infoStyle,
239            'info_styles' => $info_styles,
240            'sortStyle'   => $sortStyle,
241            'sort_styles' => $sort_styles,
242        ]);
243    }
244}
245