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