fisharebest /
webtrees
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * webtrees: online genealogy |
||
| 5 | * Copyright (C) 2025 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\Auth; |
||
| 23 | use Fisharebest\Webtrees\Gedcom; |
||
| 24 | use Fisharebest\Webtrees\I18N; |
||
| 25 | use Fisharebest\Webtrees\Registry; |
||
| 26 | use Fisharebest\Webtrees\Services\CalendarService; |
||
| 27 | use Fisharebest\Webtrees\Tree; |
||
| 28 | use Fisharebest\Webtrees\Validator; |
||
| 29 | use Illuminate\Support\Str; |
||
| 30 | use Psr\Http\Message\ServerRequestInterface; |
||
| 31 | |||
| 32 | class OnThisDayModule extends AbstractModule implements ModuleBlockInterface |
||
| 33 | { |
||
| 34 | use ModuleBlockTrait; |
||
| 35 | |||
| 36 | // Pagination |
||
| 37 | private const int LIMIT_LOW = 10; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 38 | private const int LIMIT_HIGH = 20; |
||
| 39 | |||
| 40 | // Default values for new blocks. |
||
| 41 | private const string DEFAULT_SORT = 'date_desc'; |
||
| 42 | private const string DEFAULT_STYLE = 'date_desc'; |
||
| 43 | |||
| 44 | // Initial sorting for datatables |
||
| 45 | private const array DATATABLES_ORDER = [ |
||
| 46 | 'alpha' => [[0, 'asc']], |
||
| 47 | 'date_asc' => [[2, 'asc']], |
||
| 48 | 'date_desc' => [[2, 'desc']], |
||
| 49 | ]; |
||
| 50 | |||
| 51 | // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN |
||
| 52 | private const array ALL_EVENTS = [ |
||
| 53 | 'ADOP' => 'INDI:ADOP', |
||
| 54 | 'ANUL' => 'FAM:ANUL', |
||
| 55 | 'BAPM' => 'INDI:BAPM', |
||
| 56 | 'BARM' => 'INDI:BARM', |
||
| 57 | 'BASM' => 'INDI:BASM', |
||
| 58 | 'BIRT' => 'INDI:BIRT', |
||
| 59 | 'BLES' => 'INDI:BLES', |
||
| 60 | 'BURI' => 'INDI:BURI', |
||
| 61 | 'CHR' => 'INDI:CHR', |
||
| 62 | 'CHRA' => 'INDI:CHRA', |
||
| 63 | 'CONF' => 'INDI:CONF', |
||
| 64 | 'CREM' => 'INDI:CREM', |
||
| 65 | 'DEAT' => 'INDI:DEAT', |
||
| 66 | 'DIV' => 'FAM:DIV', |
||
| 67 | 'DIVF' => 'FAM:DIVF', |
||
| 68 | 'EMIG' => 'INDI:EMIG', |
||
| 69 | 'ENGA' => 'FAM:ENGA', |
||
| 70 | 'FCOM' => 'INDI:FCOM', |
||
| 71 | 'GRAD' => 'INDI:GRAD', |
||
| 72 | 'IMMI' => 'INDI:IMMI', |
||
| 73 | 'MARB' => 'FAM:MARB', |
||
| 74 | 'MARC' => 'FAM:MARC', |
||
| 75 | 'MARL' => 'FAM:MARL', |
||
| 76 | 'MARR' => 'FAM:MARR', |
||
| 77 | 'MARS' => 'FAM:MARS', |
||
| 78 | 'NATU' => 'INDI:NATU', |
||
| 79 | 'ORDN' => 'INDI:ORDN', |
||
| 80 | 'PROB' => 'INDI:PROB', |
||
| 81 | 'RETI' => 'INDI:RETI', |
||
| 82 | 'WILL' => 'INDI:WILL', |
||
| 83 | ]; |
||
| 84 | |||
| 85 | private const array DEFAULT_EVENTS = [ |
||
| 86 | 'BIRT', |
||
| 87 | 'MARR', |
||
| 88 | 'DEAT', |
||
| 89 | ]; |
||
| 90 | |||
| 91 | public function title(): string |
||
| 92 | { |
||
| 93 | /* I18N: Name of a module */ |
||
| 94 | return I18N::translate('On this day'); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function description(): string |
||
| 98 | { |
||
| 99 | /* I18N: Description of the “On this day” module */ |
||
| 100 | return I18N::translate('A list of the anniversaries that occur today.'); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Generate the HTML content of this block. |
||
| 105 | * |
||
| 106 | * @param Tree $tree |
||
| 107 | * @param int $block_id |
||
| 108 | * @param string $context |
||
| 109 | * @param array<string,string> $config |
||
| 110 | * |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string |
||
| 114 | { |
||
| 115 | $calendar_service = new CalendarService(); |
||
| 116 | |||
| 117 | $default_events = implode(',', self::DEFAULT_EVENTS); |
||
| 118 | |||
| 119 | $filter = (bool) $this->getBlockSetting($block_id, 'filter', '1'); |
||
| 120 | $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); |
||
| 121 | $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); |
||
| 122 | $events = $this->getBlockSetting($block_id, 'events', $default_events); |
||
| 123 | |||
| 124 | extract($config, EXTR_OVERWRITE); |
||
| 125 | |||
| 126 | $event_array = explode(',', $events); |
||
| 127 | |||
| 128 | // If we are only showing living individuals, then we don't need to search for DEAT events. |
||
| 129 | if ($filter) { |
||
| 130 | $event_array = array_diff($event_array, Gedcom::DEATH_EVENTS); |
||
| 131 | } |
||
| 132 | |||
| 133 | $events_filter = implode('|', $event_array); |
||
| 134 | |||
| 135 | $startjd = Registry::timestampFactory()->now()->julianDay(); |
||
| 136 | $endjd = $startjd; |
||
| 137 | |||
| 138 | $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree); |
||
| 139 | |||
| 140 | if ($facts->isEmpty()) { |
||
| 141 | if ($filter && Auth::check()) { |
||
| 142 | $message = I18N::translate('No events for living individuals exist for today.'); |
||
| 143 | } else { |
||
| 144 | $message = I18N::translate('No events exist for today.'); |
||
| 145 | } |
||
| 146 | $content = view('modules/todays_events/empty', ['message' => $message]); |
||
| 147 | } elseif ($infoStyle === 'list') { |
||
| 148 | $content = view('lists/anniversaries-list', [ |
||
| 149 | 'id' => $block_id, |
||
| 150 | 'facts' => $facts, |
||
| 151 | 'limit_low' => self::LIMIT_LOW, |
||
| 152 | 'limit_high' => self::LIMIT_HIGH, |
||
| 153 | ]); |
||
| 154 | } else { |
||
| 155 | $content = view('lists/anniversaries-table', [ |
||
| 156 | 'facts' => $facts, |
||
| 157 | 'limit_low' => self::LIMIT_LOW, |
||
| 158 | 'limit_high' => self::LIMIT_HIGH, |
||
| 159 | 'order' => self::DATATABLES_ORDER[$sortStyle] ?? self::DATATABLES_ORDER[self::DEFAULT_SORT], |
||
| 160 | ]); |
||
| 161 | } |
||
| 162 | |||
| 163 | if ($context !== self::CONTEXT_EMBED) { |
||
| 164 | return view('modules/block-template', [ |
||
| 165 | 'block' => Str::kebab($this->name()), |
||
| 166 | 'id' => $block_id, |
||
| 167 | 'config_url' => $this->configUrl($tree, $context, $block_id), |
||
| 168 | 'title' => $this->title(), |
||
| 169 | 'content' => $content, |
||
| 170 | ]); |
||
| 171 | } |
||
| 172 | |||
| 173 | return $content; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Should this block load asynchronously using AJAX? |
||
| 178 | * |
||
| 179 | * Simple blocks are faster in-line, more complex ones can be loaded later. |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function loadAjax(): bool |
||
| 184 | { |
||
| 185 | return true; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Can this block be shown on the user’s home page? |
||
| 190 | * |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function isUserBlock(): bool |
||
| 194 | { |
||
| 195 | return true; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Can this block be shown on the tree’s home page? |
||
| 200 | * |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | public function isTreeBlock(): bool |
||
| 204 | { |
||
| 205 | return true; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Update the configuration for a block. |
||
| 210 | * |
||
| 211 | * @param ServerRequestInterface $request |
||
| 212 | * @param int $block_id |
||
| 213 | * |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void |
||
| 217 | { |
||
| 218 | $filter = Validator::parsedBody($request)->string('filter'); |
||
| 219 | $info_style = Validator::parsedBody($request)->string('infoStyle'); |
||
| 220 | $sort_style = Validator::parsedBody($request)->string('sortStyle'); |
||
| 221 | $events = Validator::parsedBody($request)->array('events'); |
||
| 222 | |||
| 223 | $this->setBlockSetting($block_id, 'filter', $filter); |
||
| 224 | $this->setBlockSetting($block_id, 'infoStyle', $info_style); |
||
| 225 | $this->setBlockSetting($block_id, 'sortStyle', $sort_style); |
||
| 226 | $this->setBlockSetting($block_id, 'events', implode(',', $events)); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * An HTML form to edit block settings |
||
| 231 | * |
||
| 232 | * @param Tree $tree |
||
| 233 | * @param int $block_id |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public function editBlockConfiguration(Tree $tree, int $block_id): string |
||
| 238 | { |
||
| 239 | $default_events = implode(',', self::DEFAULT_EVENTS); |
||
| 240 | |||
| 241 | $filter = $this->getBlockSetting($block_id, 'filter', '1'); |
||
| 242 | $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); |
||
| 243 | $sort_style = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); |
||
| 244 | $events = $this->getBlockSetting($block_id, 'events', $default_events); |
||
| 245 | |||
| 246 | $event_array = explode(',', $events); |
||
| 247 | |||
| 248 | $all_events = []; |
||
| 249 | foreach (self::ALL_EVENTS as $event => $tag) { |
||
| 250 | $all_events[$event] = Registry::elementFactory()->make($tag)->label(); |
||
| 251 | } |
||
| 252 | |||
| 253 | $info_styles = [ |
||
| 254 | /* I18N: An option in a list-box */ |
||
| 255 | 'list' => I18N::translate('list'), |
||
| 256 | /* I18N: An option in a list-box */ |
||
| 257 | 'table' => I18N::translate('table'), |
||
| 258 | ]; |
||
| 259 | |||
| 260 | $sort_styles = [ |
||
| 261 | /* I18N: An option in a list-box */ |
||
| 262 | 'alpha' => I18N::translate('sort by name'), |
||
| 263 | /* I18N: An option in a list-box */ |
||
| 264 | 'anniv_asc' => I18N::translate('sort by date, oldest first'), |
||
| 265 | /* I18N: An option in a list-box */ |
||
| 266 | 'anniv_desc' => I18N::translate('sort by date, newest first'), |
||
| 267 | ]; |
||
| 268 | |||
| 269 | return view('modules/todays_events/config', [ |
||
| 270 | 'all_events' => $all_events, |
||
| 271 | 'event_array' => $event_array, |
||
| 272 | 'filter' => $filter, |
||
| 273 | 'info_style' => $info_style, |
||
| 274 | 'info_styles' => $info_styles, |
||
| 275 | 'sort_style' => $sort_style, |
||
| 276 | 'sort_styles' => $sort_styles, |
||
| 277 | ]); |
||
| 278 | } |
||
| 279 | } |
||
| 280 |