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