xref: /webtrees/app/Module/OnThisDayModule.php (revision f0a114195ac1eac96981ed21f4681b1f2d0de72c)
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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
2012664b30SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
22*f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService;
23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
248c2e8227SGreg Roach
258c2e8227SGreg Roach/**
268c2e8227SGreg Roach * Class OnThisDayModule
278c2e8227SGreg Roach */
28c1010edaSGreg Roachclass OnThisDayModule extends AbstractModule implements ModuleBlockInterface
29c1010edaSGreg Roach{
3012664b30SGreg Roach    // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
3112664b30SGreg Roach    const ALL_EVENTS = [
3212664b30SGreg Roach        'ADOP',
3312664b30SGreg Roach        'ANUL',
3412664b30SGreg Roach        'BAPM',
3512664b30SGreg Roach        'BARM',
3612664b30SGreg Roach        'BASM',
3712664b30SGreg Roach        'BIRT',
3812664b30SGreg Roach        'BLES',
3912664b30SGreg Roach        'BURI',
4012664b30SGreg Roach        'CHR',
4112664b30SGreg Roach        'CHRA',
4212664b30SGreg Roach        'CONF',
4312664b30SGreg Roach        'CREM',
4412664b30SGreg Roach        'DEAT',
4512664b30SGreg Roach        'DIV',
4612664b30SGreg Roach        'DIVF',
4712664b30SGreg Roach        'EMIG',
4812664b30SGreg Roach        'ENGA',
4912664b30SGreg Roach        'FCOM',
5012664b30SGreg Roach        'GRAD',
5112664b30SGreg Roach        'IMMI',
5212664b30SGreg Roach        'MARB',
5312664b30SGreg Roach        'MARC',
5412664b30SGreg Roach        'MARL',
5512664b30SGreg Roach        'MARR',
5612664b30SGreg Roach        'MARS',
5712664b30SGreg Roach        'NATU',
5812664b30SGreg Roach        'ORDN',
5912664b30SGreg Roach        'PROB',
6012664b30SGreg Roach        'RETI',
6112664b30SGreg Roach        'WILL',
6212664b30SGreg Roach    ];
6312664b30SGreg Roach
6412664b30SGreg Roach    const DEFAULT_EVENTS = [
6512664b30SGreg Roach        'BIRT',
6612664b30SGreg Roach        'MARR',
6712664b30SGreg Roach        'DEAT',
6812664b30SGreg Roach    ];
6912664b30SGreg Roach
7012664b30SGreg Roach    /**
7112664b30SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
7212664b30SGreg Roach     *
7312664b30SGreg Roach     * @return string
7412664b30SGreg Roach     */
75c1010edaSGreg Roach    public function getTitle()
76c1010edaSGreg Roach    {
77bbb76c12SGreg Roach        /* I18N: Name of a module */
78bbb76c12SGreg Roach        return I18N::translate('On this day');
798c2e8227SGreg Roach    }
808c2e8227SGreg Roach
8112664b30SGreg Roach    /**
8212664b30SGreg Roach     * A sentence describing what this module does.
8312664b30SGreg Roach     *
8412664b30SGreg Roach     * @return string
8512664b30SGreg Roach     */
86c1010edaSGreg Roach    public function getDescription()
87c1010edaSGreg Roach    {
88bbb76c12SGreg Roach        /* I18N: Description of the “On this day” module */
89bbb76c12SGreg Roach        return I18N::translate('A list of the anniversaries that occur today.');
908c2e8227SGreg Roach    }
918c2e8227SGreg Roach
9276692c8bSGreg Roach    /**
9376692c8bSGreg Roach     * Generate the HTML content of this block.
9476692c8bSGreg Roach     *
95e490cd80SGreg Roach     * @param Tree     $tree
9676692c8bSGreg Roach     * @param int      $block_id
9776692c8bSGreg Roach     * @param bool     $template
98727f238cSGreg Roach     * @param string[] $cfg
9976692c8bSGreg Roach     *
10076692c8bSGreg Roach     * @return string
10176692c8bSGreg Roach     */
102c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
103c1010edaSGreg Roach    {
104e490cd80SGreg Roach        global $ctype;
1058c2e8227SGreg Roach
106*f0a11419SGreg Roach        $calendar_service = new CalendarService();
107*f0a11419SGreg Roach
10812664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
10912664b30SGreg Roach
11012664b30SGreg Roach        $filter    = (bool)$this->getBlockSetting($block_id, 'filter', '1');
111e2a378d3SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
112e2a378d3SGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
11312664b30SGreg Roach        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
1148c2e8227SGreg Roach
115c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
1168c2e8227SGreg Roach
11712664b30SGreg Roach        $event_array = explode(',', $events);
11812664b30SGreg Roach
11912664b30SGreg Roach        // If we are only showing living individuals, then we don't need to search for DEAT events.
12012664b30SGreg Roach        if ($filter) {
12112664b30SGreg Roach            $death_events = explode('|', WT_EVENTS_DEAT);
12212664b30SGreg Roach            $event_array  = array_diff($event_array, $death_events);
12312664b30SGreg Roach        }
12412664b30SGreg Roach
12512664b30SGreg Roach        $events_filter = implode('|', $event_array);
12612664b30SGreg Roach
12712664b30SGreg Roach        $startjd = WT_CLIENT_JD;
12812664b30SGreg Roach        $endjd   = WT_CLIENT_JD;
12912664b30SGreg Roach
130*f0a11419SGreg Roach        $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
13112664b30SGreg Roach
13212664b30SGreg Roach        if (empty($facts)) {
133147e99aaSGreg Roach            $content = view('modules/todays_events/empty');
134147e99aaSGreg Roach        } elseif ($infoStyle === 'list') {
135147e99aaSGreg Roach            $content = view('modules/todays_events/list', [
136147e99aaSGreg Roach                'facts' => $facts,
1370280f44aSGreg Roach            ]);
138d8189b6aSDavid Drury        } else {
139147e99aaSGreg Roach            $content = view('modules/todays_events/table', [
140d8189b6aSDavid Drury                'facts' => $facts,
1410280f44aSGreg Roach            ]);
1420280f44aSGreg Roach        }
1438c2e8227SGreg Roach
1448c2e8227SGreg Roach        if ($template) {
145e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
146c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
147c1010edaSGreg Roach                    'block_id' => $block_id,
148c1010edaSGreg Roach                    'ged'      => $tree->getName(),
149c1010edaSGreg Roach                ]);
150397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
151c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
152c1010edaSGreg Roach                    'block_id' => $block_id,
153c1010edaSGreg Roach                    'ged'      => $tree->getName(),
154c1010edaSGreg Roach                ]);
1559c6524dcSGreg Roach            } else {
1569c6524dcSGreg Roach                $config_url = '';
1579c6524dcSGreg Roach            }
1589c6524dcSGreg Roach
159147e99aaSGreg Roach            return view('modules/block-template', [
1609c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1619c6524dcSGreg Roach                'id'         => $block_id,
1629c6524dcSGreg Roach                'config_url' => $config_url,
1639c6524dcSGreg Roach                'title'      => $this->getTitle(),
1649c6524dcSGreg Roach                'content'    => $content,
16512664b30SGreg Roach            ]);
1668c2e8227SGreg Roach        } else {
1678c2e8227SGreg Roach            return $content;
1688c2e8227SGreg Roach        }
1698c2e8227SGreg Roach    }
1708c2e8227SGreg Roach
1718c2e8227SGreg Roach    /** {@inheritdoc} */
172c1010edaSGreg Roach    public function loadAjax(): bool
173c1010edaSGreg Roach    {
1748c2e8227SGreg Roach        return true;
1758c2e8227SGreg Roach    }
1768c2e8227SGreg Roach
1778c2e8227SGreg Roach    /** {@inheritdoc} */
178c1010edaSGreg Roach    public function isUserBlock(): bool
179c1010edaSGreg Roach    {
1808c2e8227SGreg Roach        return true;
1818c2e8227SGreg Roach    }
1828c2e8227SGreg Roach
1838c2e8227SGreg Roach    /** {@inheritdoc} */
184c1010edaSGreg Roach    public function isGedcomBlock(): bool
185c1010edaSGreg Roach    {
1868c2e8227SGreg Roach        return true;
1878c2e8227SGreg Roach    }
1888c2e8227SGreg Roach
18976692c8bSGreg Roach    /**
19076692c8bSGreg Roach     * An HTML form to edit block settings
19176692c8bSGreg Roach     *
192e490cd80SGreg Roach     * @param Tree $tree
19376692c8bSGreg Roach     * @param int  $block_id
194a9430be8SGreg Roach     *
195a9430be8SGreg Roach     * @return void
19676692c8bSGreg Roach     */
197c1010edaSGreg Roach    public function configureBlock(Tree $tree, int $block_id)
198c1010edaSGreg Roach    {
199c385536dSGreg Roach        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
200e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
201e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
202e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
20312664b30SGreg Roach            $this->setBlockSetting($block_id, 'events', implode(',', Filter::postArray('events')));
204c385536dSGreg Roach
205c385536dSGreg Roach            return;
2068c2e8227SGreg Roach        }
2078c2e8227SGreg Roach
20812664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
20912664b30SGreg Roach
210e2a378d3SGreg Roach        $filter    = $this->getBlockSetting($block_id, 'filter', '1');
211e2a378d3SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
212e2a378d3SGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
21312664b30SGreg Roach        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
2148c2e8227SGreg Roach
21512664b30SGreg Roach        $event_array = explode(',', $events);
21612664b30SGreg Roach
21712664b30SGreg Roach        $all_events = [];
21812664b30SGreg Roach        foreach (self::ALL_EVENTS as $event) {
21912664b30SGreg Roach            $all_events[$event] = GedcomTag::getLabel($event);
22012664b30SGreg Roach        }
22112664b30SGreg Roach
222c385536dSGreg Roach        $info_styles = [
223bbb76c12SGreg Roach            /* I18N: An option in a list-box */
224bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
225bbb76c12SGreg Roach            /* I18N: An option in a list-box */
226bbb76c12SGreg Roach            'table' => I18N::translate('table'),
227c385536dSGreg Roach        ];
2288c2e8227SGreg Roach
229c385536dSGreg Roach        $sort_styles = [
230bbb76c12SGreg Roach            /* I18N: An option in a list-box */
231bbb76c12SGreg Roach            'alpha' => I18N::translate('sort by name'),
232bbb76c12SGreg Roach            /* I18N: An option in a list-box */
233bbb76c12SGreg Roach            'anniv' => I18N::translate('sort by date'),
234c385536dSGreg Roach        ];
235d8189b6aSDavid Drury
236147e99aaSGreg Roach        echo view('modules/todays_events/config', [
237c385536dSGreg Roach            'all_events'  => $all_events,
238c385536dSGreg Roach            'event_array' => $event_array,
239c385536dSGreg Roach            'filter'      => $filter,
240c385536dSGreg Roach            'infoStyle'   => $infoStyle,
241c385536dSGreg Roach            'info_styles' => $info_styles,
242c385536dSGreg Roach            'sortStyle'   => $sortStyle,
243c385536dSGreg Roach            'sort_styles' => $sort_styles,
244c385536dSGreg Roach        ]);
2458c2e8227SGreg Roach    }
2468c2e8227SGreg Roach}
247