1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees: online genealogy |
5
|
|
|
* Copyright (C) 2021 webtrees development team |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* You should have received a copy of the GNU General Public License |
15
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\Module; |
21
|
|
|
|
22
|
|
|
use Fisharebest\Webtrees\Gedcom; |
23
|
|
|
use Fisharebest\Webtrees\I18N; |
24
|
|
|
use Fisharebest\Webtrees\Registry; |
25
|
|
|
use Fisharebest\Webtrees\Services\CalendarService; |
26
|
|
|
use Fisharebest\Webtrees\Tree; |
27
|
|
|
use Fisharebest\Webtrees\Validator; |
28
|
|
|
use Illuminate\Support\Str; |
29
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class UpcomingAnniversariesModule |
33
|
|
|
*/ |
34
|
|
|
class UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface |
35
|
|
|
{ |
36
|
|
|
use ModuleBlockTrait; |
37
|
|
|
|
38
|
|
|
private const SORT_STYLE_DATE = 'anniv'; |
39
|
|
|
private const SORT_STYLE_NAME = 'alpha'; |
40
|
|
|
|
41
|
|
|
private const LAYOUT_STYLE_LIST = 'list'; |
42
|
|
|
private const LAYOUT_STYLE_TABLE = 'table'; |
43
|
|
|
|
44
|
|
|
// Default values for new blocks. |
45
|
|
|
private const DEFAULT_DAYS = '7'; |
46
|
|
|
private const DEFAULT_FILTER = '1'; |
47
|
|
|
private const DEFAULT_SORT = self::SORT_STYLE_NAME; |
48
|
|
|
private const DEFAULT_STYLE = self::LAYOUT_STYLE_TABLE; |
49
|
|
|
|
50
|
|
|
// Initial sorting for datatables |
51
|
|
|
private const DATATABLES_ORDER = [ |
52
|
|
|
self::SORT_STYLE_NAME => [[0, 'asc']], |
53
|
|
|
self::SORT_STYLE_DATE => [[1, 'asc']], |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
// Can show this number of days into the future. |
57
|
|
|
private const MAX_DAYS = 30; |
58
|
|
|
|
59
|
|
|
// Pagination |
60
|
|
|
private const LIMIT_LOW = 10; |
61
|
|
|
private const LIMIT_HIGH = 20; |
62
|
|
|
|
63
|
|
|
// All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN |
64
|
|
|
private const ALL_EVENTS = [ |
65
|
|
|
'ADOP' => 'INDI:ADOP', |
66
|
|
|
'ANUL' => 'FAM:ANUL', |
67
|
|
|
'BAPM' => 'INDI:BAPM', |
68
|
|
|
'BARM' => 'INDI:BARM', |
69
|
|
|
'BASM' => 'INDI:BASM', |
70
|
|
|
'BIRT' => 'INDI:BIRT', |
71
|
|
|
'BLES' => 'INDI:BLES', |
72
|
|
|
'BURI' => 'INDI:BURI', |
73
|
|
|
'CHR' => 'INDI:CHR', |
74
|
|
|
'CHRA' => 'INDI:CHRA', |
75
|
|
|
'CONF' => 'INDI:CONF', |
76
|
|
|
'CREM' => 'INDI:CREM', |
77
|
|
|
'DEAT' => 'INDI:DEAT', |
78
|
|
|
'DIV' => 'FAM:DIV', |
79
|
|
|
'DIVF' => 'FAM:DIVF', |
80
|
|
|
'EMIG' => 'INDI:EMIG', |
81
|
|
|
'ENGA' => 'FAM:ENGA', |
82
|
|
|
'FCOM' => 'INDI:FCOM', |
83
|
|
|
'GRAD' => 'INDI:GRAD', |
84
|
|
|
'IMMI' => 'INDI:IMMI', |
85
|
|
|
'MARB' => 'FAM:MARB', |
86
|
|
|
'MARC' => 'FAM:MARC', |
87
|
|
|
'MARL' => 'FAM:MARL', |
88
|
|
|
'MARR' => 'FAM:MARR', |
89
|
|
|
'MARS' => 'FAM:MARS', |
90
|
|
|
'NATU' => 'INDI:NATU', |
91
|
|
|
'ORDN' => 'INDI:ORDN', |
92
|
|
|
'PROB' => 'INDI:PROB', |
93
|
|
|
'RETI' => 'INDI:RETI', |
94
|
|
|
'WILL' => 'INDI:WILL', |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
private const DEFAULT_EVENTS = [ |
98
|
|
|
'BIRT', |
99
|
|
|
'MARR', |
100
|
|
|
'DEAT', |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
private CalendarService $calendar_service; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param CalendarService $calendar_service |
107
|
|
|
*/ |
108
|
|
|
public function __construct(CalendarService $calendar_service) |
109
|
|
|
{ |
110
|
|
|
$this->calendar_service = $calendar_service; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* How should this module be identified in the control panel, etc.? |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function title(): string |
119
|
|
|
{ |
120
|
|
|
/* I18N: Name of a module */ |
121
|
|
|
return I18N::translate('Upcoming events'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* A sentence describing what this module does. |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function description(): string |
130
|
|
|
{ |
131
|
|
|
/* I18N: Description of the “Upcoming events” module */ |
132
|
|
|
return I18N::translate('A list of the anniversaries that will occur in the near future.'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Generate the HTML content of this block. |
137
|
|
|
* |
138
|
|
|
* @param Tree $tree |
139
|
|
|
* @param int $block_id |
140
|
|
|
* @param string $context |
141
|
|
|
* @param array<string,string> $config |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string |
146
|
|
|
{ |
147
|
|
|
$default_events = implode(',', self::DEFAULT_EVENTS); |
148
|
|
|
|
149
|
|
|
$days = (int)$this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); |
150
|
|
|
$filter = (bool)$this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER); |
151
|
|
|
$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); |
152
|
|
|
$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); |
153
|
|
|
$events = $this->getBlockSetting($block_id, 'events', $default_events); |
154
|
|
|
|
155
|
|
|
extract($config, EXTR_OVERWRITE); |
156
|
|
|
|
157
|
|
|
$event_array = explode(',', $events); |
158
|
|
|
|
159
|
|
|
// If we are only showing living individuals, then we don't need to search for DEAT events. |
160
|
|
|
if ($filter) { |
161
|
|
|
$event_array = array_diff($event_array, Gedcom::DEATH_EVENTS); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$events_filter = implode('|', $event_array); |
165
|
|
|
|
166
|
|
|
$startjd = Registry::timestampFactory()->now()->addDays(1)->julianDay(); |
167
|
|
|
$endjd = Registry::timestampFactory()->now()->addDays($days)->julianDay(); |
168
|
|
|
|
169
|
|
|
$facts = $this->calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree); |
170
|
|
|
|
171
|
|
|
if ($facts->isEmpty()) { |
172
|
|
|
if ($endjd === $startjd) { |
173
|
|
|
$content = view('modules/upcoming_events/empty', [ |
174
|
|
|
'message' => I18N::translate('No events exist for tomorrow.'), |
175
|
|
|
]); |
176
|
|
|
} else { |
177
|
|
|
/* I18N: translation for %s==1 is unused; it is translated separately as “tomorrow” */ |
178
|
|
|
$content = view('modules/upcoming_events/empty', [ |
179
|
|
|
'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)), |
180
|
|
|
]); |
181
|
|
|
} |
182
|
|
|
} elseif ($infoStyle === 'list') { |
183
|
|
|
$content = view('lists/anniversaries-list', [ |
184
|
|
|
'id' => $block_id, |
185
|
|
|
'facts' => $facts, |
186
|
|
|
'limit_low' => self::LIMIT_LOW, |
187
|
|
|
'limit_high' => self::LIMIT_HIGH, |
188
|
|
|
]); |
189
|
|
|
} else { |
190
|
|
|
$content = view('lists/anniversaries-table', [ |
191
|
|
|
'facts' => $facts, |
192
|
|
|
'limit_low' => self::LIMIT_LOW, |
193
|
|
|
'limit_high' => self::LIMIT_HIGH, |
194
|
|
|
'order' => self::DATATABLES_ORDER[$sortStyle], |
195
|
|
|
]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if ($context !== self::CONTEXT_EMBED) { |
199
|
|
|
return view('modules/block-template', [ |
200
|
|
|
'block' => Str::kebab($this->name()), |
201
|
|
|
'id' => $block_id, |
202
|
|
|
'config_url' => $this->configUrl($tree, $context, $block_id), |
203
|
|
|
'title' => $this->title(), |
204
|
|
|
'content' => $content, |
205
|
|
|
]); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $content; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Should this block load asynchronously using AJAX? |
213
|
|
|
* |
214
|
|
|
* Simple blocks are faster in-line, more complex ones can be loaded later. |
215
|
|
|
* |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
|
|
public function loadAjax(): bool |
219
|
|
|
{ |
220
|
|
|
return true; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Can this block be shown on the user’s home page? |
225
|
|
|
* |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
public function isUserBlock(): bool |
229
|
|
|
{ |
230
|
|
|
return true; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Can this block be shown on the tree’s home page? |
235
|
|
|
* |
236
|
|
|
* @return bool |
237
|
|
|
*/ |
238
|
|
|
public function isTreeBlock(): bool |
239
|
|
|
{ |
240
|
|
|
return true; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Update the configuration for a block. |
245
|
|
|
* |
246
|
|
|
* @param ServerRequestInterface $request |
247
|
|
|
* @param int $block_id |
248
|
|
|
* |
249
|
|
|
* @return void |
250
|
|
|
*/ |
251
|
|
|
public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void |
252
|
|
|
{ |
253
|
|
|
$days = Validator::parsedBody($request)->isBetween(1, self::MAX_DAYS)->integer('days'); |
254
|
|
|
$filter = Validator::parsedBody($request)->boolean('filter'); |
255
|
|
|
$info_style = Validator::parsedBody($request)->isInArrayKeys($this->infoStyles())->string('infoStyle'); |
256
|
|
|
$sort_style = Validator::parsedBody($request)->isInArrayKeys($this->sortStyles())->string('sortStyle'); |
257
|
|
|
$events = Validator::parsedBody($request)->array('events'); |
258
|
|
|
|
259
|
|
|
$this->setBlockSetting($block_id, 'days', (string)$days); |
260
|
|
|
$this->setBlockSetting($block_id, 'filter', (string)$filter); |
261
|
|
|
$this->setBlockSetting($block_id, 'infoStyle', $info_style); |
262
|
|
|
$this->setBlockSetting($block_id, 'sortStyle', $sort_style); |
263
|
|
|
$this->setBlockSetting($block_id, 'events', implode(',', $events)); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* An HTML form to edit block settings |
268
|
|
|
* |
269
|
|
|
* @param Tree $tree |
270
|
|
|
* @param int $block_id |
271
|
|
|
* |
272
|
|
|
* @return string |
273
|
|
|
*/ |
274
|
|
|
public function editBlockConfiguration(Tree $tree, int $block_id): string |
275
|
|
|
{ |
276
|
|
|
$default_events = implode(',', self::DEFAULT_EVENTS); |
277
|
|
|
|
278
|
|
|
$days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); |
279
|
|
|
$filter = $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER); |
280
|
|
|
$info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); |
281
|
|
|
$sort_style = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); |
282
|
|
|
$events = $this->getBlockSetting($block_id, 'events', $default_events); |
283
|
|
|
|
284
|
|
|
$event_array = explode(',', $events); |
285
|
|
|
|
286
|
|
|
$all_events = []; |
287
|
|
|
foreach (self::ALL_EVENTS as $event => $tag) { |
288
|
|
|
$all_events[$event] = Registry::elementFactory()->make($tag)->label(); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
return view('modules/upcoming_events/config', [ |
292
|
|
|
'all_events' => $all_events, |
293
|
|
|
'days' => $days, |
294
|
|
|
'event_array' => $event_array, |
295
|
|
|
'filter' => $filter, |
296
|
|
|
'info_style' => $info_style, |
297
|
|
|
'info_styles' => $this->infoStyles(), |
298
|
|
|
'max_days' => self::MAX_DAYS, |
299
|
|
|
'sort_style' => $sort_style, |
300
|
|
|
'sort_styles' => $this->sortStyles(), |
301
|
|
|
]); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @return array<string,string> |
306
|
|
|
*/ |
307
|
|
|
private function infoStyles(): array |
308
|
|
|
{ |
309
|
|
|
return [ |
310
|
|
|
self::LAYOUT_STYLE_LIST => /* I18N: An option in a list-box */ I18N::translate('list'), |
311
|
|
|
self::LAYOUT_STYLE_TABLE => /* I18N: An option in a list-box */ I18N::translate('table'), |
312
|
|
|
]; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return array<string,string> |
317
|
|
|
*/ |
318
|
|
|
private function sortStyles(): array |
319
|
|
|
{ |
320
|
|
|
return [ |
321
|
|
|
self::SORT_STYLE_NAME => /* I18N: An option in a list-box */ I18N::translate('sort by name'), |
322
|
|
|
self::SORT_STYLE_DATE => /* I18N: An option in a list-box */ I18N::translate('sort by date'), |
323
|
|
|
]; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|