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