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