xref: /webtrees/app/Module/OnThisDayModule.php (revision e7f56f2af615447ab1a7646851f88b465ace9e04)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16*e7f56f2aSGreg Roachdeclare(strict_types=1);
17*e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
2112664b30SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService;
24e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
25a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class OnThisDayModule
298c2e8227SGreg Roach */
30c1010edaSGreg Roachclass OnThisDayModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
3212664b30SGreg Roach    // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
3312664b30SGreg Roach    const ALL_EVENTS = [
3412664b30SGreg Roach        'ADOP',
3512664b30SGreg Roach        'ANUL',
3612664b30SGreg Roach        'BAPM',
3712664b30SGreg Roach        'BARM',
3812664b30SGreg Roach        'BASM',
3912664b30SGreg Roach        'BIRT',
4012664b30SGreg Roach        'BLES',
4112664b30SGreg Roach        'BURI',
4212664b30SGreg Roach        'CHR',
4312664b30SGreg Roach        'CHRA',
4412664b30SGreg Roach        'CONF',
4512664b30SGreg Roach        'CREM',
4612664b30SGreg Roach        'DEAT',
4712664b30SGreg Roach        'DIV',
4812664b30SGreg Roach        'DIVF',
4912664b30SGreg Roach        'EMIG',
5012664b30SGreg Roach        'ENGA',
5112664b30SGreg Roach        'FCOM',
5212664b30SGreg Roach        'GRAD',
5312664b30SGreg Roach        'IMMI',
5412664b30SGreg Roach        'MARB',
5512664b30SGreg Roach        'MARC',
5612664b30SGreg Roach        'MARL',
5712664b30SGreg Roach        'MARR',
5812664b30SGreg Roach        'MARS',
5912664b30SGreg Roach        'NATU',
6012664b30SGreg Roach        'ORDN',
6112664b30SGreg Roach        'PROB',
6212664b30SGreg Roach        'RETI',
6312664b30SGreg Roach        'WILL',
6412664b30SGreg Roach    ];
6512664b30SGreg Roach
6612664b30SGreg Roach    const DEFAULT_EVENTS = [
6712664b30SGreg Roach        'BIRT',
6812664b30SGreg Roach        'MARR',
6912664b30SGreg Roach        'DEAT',
7012664b30SGreg Roach    ];
7112664b30SGreg Roach
7212664b30SGreg Roach    /**
7312664b30SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
7412664b30SGreg Roach     *
7512664b30SGreg Roach     * @return string
7612664b30SGreg Roach     */
778f53f488SRico Sonntag    public function getTitle(): string
78c1010edaSGreg Roach    {
79bbb76c12SGreg Roach        /* I18N: Name of a module */
80bbb76c12SGreg Roach        return I18N::translate('On this day');
818c2e8227SGreg Roach    }
828c2e8227SGreg Roach
8312664b30SGreg Roach    /**
8412664b30SGreg Roach     * A sentence describing what this module does.
8512664b30SGreg Roach     *
8612664b30SGreg Roach     * @return string
8712664b30SGreg Roach     */
888f53f488SRico Sonntag    public function getDescription(): string
89c1010edaSGreg Roach    {
90bbb76c12SGreg Roach        /* I18N: Description of the “On this day” module */
91bbb76c12SGreg Roach        return I18N::translate('A list of the anniversaries that occur today.');
928c2e8227SGreg Roach    }
938c2e8227SGreg Roach
9476692c8bSGreg Roach    /**
9576692c8bSGreg Roach     * Generate the HTML content of this block.
9676692c8bSGreg Roach     *
97e490cd80SGreg Roach     * @param Tree     $tree
9876692c8bSGreg Roach     * @param int      $block_id
9976692c8bSGreg Roach     * @param bool     $template
100727f238cSGreg Roach     * @param string[] $cfg
10176692c8bSGreg Roach     *
10276692c8bSGreg Roach     * @return string
10376692c8bSGreg Roach     */
104c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
105c1010edaSGreg Roach    {
106e490cd80SGreg Roach        global $ctype;
1078c2e8227SGreg Roach
108f0a11419SGreg Roach        $calendar_service = new CalendarService();
109f0a11419SGreg Roach
11012664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
11112664b30SGreg Roach
11212664b30SGreg Roach        $filter    = (bool) $this->getBlockSetting($block_id, 'filter', '1');
113e2a378d3SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
114e2a378d3SGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
11512664b30SGreg Roach        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
1168c2e8227SGreg Roach
117c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
1188c2e8227SGreg Roach
11912664b30SGreg Roach        $event_array = explode(',', $events);
12012664b30SGreg Roach
12112664b30SGreg Roach        // If we are only showing living individuals, then we don't need to search for DEAT events.
12212664b30SGreg Roach        if ($filter) {
12312664b30SGreg Roach            $death_events = explode('|', WT_EVENTS_DEAT);
12412664b30SGreg Roach            $event_array  = array_diff($event_array, $death_events);
12512664b30SGreg Roach        }
12612664b30SGreg Roach
12712664b30SGreg Roach        $events_filter = implode('|', $event_array);
12812664b30SGreg Roach
12912664b30SGreg Roach        $startjd = WT_CLIENT_JD;
13012664b30SGreg Roach        $endjd   = WT_CLIENT_JD;
13112664b30SGreg Roach
132f0a11419SGreg Roach        $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
13312664b30SGreg Roach
13412664b30SGreg Roach        if (empty($facts)) {
135147e99aaSGreg Roach            $content = view('modules/todays_events/empty');
136147e99aaSGreg Roach        } elseif ($infoStyle === 'list') {
137147e99aaSGreg Roach            $content = view('modules/todays_events/list', [
138147e99aaSGreg Roach                'facts' => $facts,
1390280f44aSGreg Roach            ]);
140d8189b6aSDavid Drury        } else {
141147e99aaSGreg Roach            $content = view('modules/todays_events/table', [
142d8189b6aSDavid Drury                'facts' => $facts,
1430280f44aSGreg Roach            ]);
1440280f44aSGreg Roach        }
1458c2e8227SGreg Roach
1468c2e8227SGreg Roach        if ($template) {
147e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
148c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
149c1010edaSGreg Roach                    'block_id' => $block_id,
150c1010edaSGreg Roach                    'ged'      => $tree->getName(),
151c1010edaSGreg Roach                ]);
152397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
153c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
154c1010edaSGreg Roach                    'block_id' => $block_id,
155c1010edaSGreg Roach                    'ged'      => $tree->getName(),
156c1010edaSGreg Roach                ]);
1579c6524dcSGreg Roach            } else {
1589c6524dcSGreg Roach                $config_url = '';
1599c6524dcSGreg Roach            }
1609c6524dcSGreg Roach
161147e99aaSGreg Roach            return view('modules/block-template', [
1629c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1639c6524dcSGreg Roach                'id'         => $block_id,
1649c6524dcSGreg Roach                'config_url' => $config_url,
1659c6524dcSGreg Roach                'title'      => $this->getTitle(),
1669c6524dcSGreg Roach                'content'    => $content,
16712664b30SGreg Roach            ]);
1688c2e8227SGreg Roach        }
169b2ce94c6SRico Sonntag
170b2ce94c6SRico Sonntag        return $content;
1718c2e8227SGreg Roach    }
1728c2e8227SGreg Roach
1738c2e8227SGreg Roach    /** {@inheritdoc} */
174c1010edaSGreg Roach    public function loadAjax(): bool
175c1010edaSGreg Roach    {
1768c2e8227SGreg Roach        return true;
1778c2e8227SGreg Roach    }
1788c2e8227SGreg Roach
1798c2e8227SGreg Roach    /** {@inheritdoc} */
180c1010edaSGreg Roach    public function isUserBlock(): bool
181c1010edaSGreg Roach    {
1828c2e8227SGreg Roach        return true;
1838c2e8227SGreg Roach    }
1848c2e8227SGreg Roach
1858c2e8227SGreg Roach    /** {@inheritdoc} */
186c1010edaSGreg Roach    public function isGedcomBlock(): bool
187c1010edaSGreg Roach    {
1888c2e8227SGreg Roach        return true;
1898c2e8227SGreg Roach    }
1908c2e8227SGreg Roach
19176692c8bSGreg Roach    /**
192a45f9889SGreg Roach     * Update the configuration for a block.
193a45f9889SGreg Roach     *
194a45f9889SGreg Roach     * @param Request $request
195a45f9889SGreg Roach     * @param int     $block_id
196a45f9889SGreg Roach     *
197a45f9889SGreg Roach     * @return void
198a45f9889SGreg Roach     */
199a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
200a45f9889SGreg Roach    {
201a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'filter', $request->get('filter', '1'));
202a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', 'table'));
203a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'sortStyle', $request->get('sortStyle', 'alpha'));
204a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'events', implode(',', (array) $request->get('events')));
205a45f9889SGreg Roach    }
206a45f9889SGreg Roach
207a45f9889SGreg Roach    /**
20876692c8bSGreg Roach     * An HTML form to edit block settings
20976692c8bSGreg Roach     *
210e490cd80SGreg Roach     * @param Tree $tree
21176692c8bSGreg Roach     * @param int  $block_id
212a9430be8SGreg Roach     *
213a9430be8SGreg Roach     * @return void
21476692c8bSGreg Roach     */
215a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
216c1010edaSGreg Roach    {
21712664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
21812664b30SGreg Roach
219e2a378d3SGreg Roach        $filter    = $this->getBlockSetting($block_id, 'filter', '1');
220e2a378d3SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
221e2a378d3SGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
22212664b30SGreg Roach        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
2238c2e8227SGreg Roach
22412664b30SGreg Roach        $event_array = explode(',', $events);
22512664b30SGreg Roach
22612664b30SGreg Roach        $all_events = [];
22712664b30SGreg Roach        foreach (self::ALL_EVENTS as $event) {
22812664b30SGreg Roach            $all_events[$event] = GedcomTag::getLabel($event);
22912664b30SGreg Roach        }
23012664b30SGreg Roach
231c385536dSGreg Roach        $info_styles = [
232bbb76c12SGreg Roach            /* I18N: An option in a list-box */
233bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
234bbb76c12SGreg Roach            /* I18N: An option in a list-box */
235bbb76c12SGreg Roach            'table' => I18N::translate('table'),
236c385536dSGreg Roach        ];
2378c2e8227SGreg Roach
238c385536dSGreg Roach        $sort_styles = [
239bbb76c12SGreg Roach            /* I18N: An option in a list-box */
240bbb76c12SGreg Roach            'alpha' => I18N::translate('sort by name'),
241bbb76c12SGreg Roach            /* I18N: An option in a list-box */
242bbb76c12SGreg Roach            'anniv' => I18N::translate('sort by date'),
243c385536dSGreg Roach        ];
244d8189b6aSDavid Drury
245147e99aaSGreg Roach        echo view('modules/todays_events/config', [
246c385536dSGreg Roach            'all_events'  => $all_events,
247c385536dSGreg Roach            'event_array' => $event_array,
248c385536dSGreg Roach            'filter'      => $filter,
249c385536dSGreg Roach            'infoStyle'   => $infoStyle,
250c385536dSGreg Roach            'info_styles' => $info_styles,
251c385536dSGreg Roach            'sortStyle'   => $sortStyle,
252c385536dSGreg Roach            'sort_styles' => $sort_styles,
253c385536dSGreg Roach        ]);
2548c2e8227SGreg Roach    }
2558c2e8227SGreg Roach}
256