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