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