xref: /webtrees/app/Module/RecentChangesModule.php (revision 72cf66d48ef1f917238d9b0939a8aa33f257e274)
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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
216e45321fSGreg Roachuse Fisharebest\Webtrees\Database;
226e45321fSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
246e45321fSGreg Roachuse Fisharebest\Webtrees\Tree;
25a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class RecentChangesModule
298c2e8227SGreg Roach */
30c1010edaSGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
326e45321fSGreg Roach    const DEFAULT_BLOCK      = '1';
3355664801SGreg Roach    const DEFAULT_DAYS       = '7';
346e45321fSGreg Roach    const DEFAULT_HIDE_EMPTY = '0';
356e45321fSGreg Roach    const DEFAULT_SHOW_USER  = '1';
366e45321fSGreg Roach    const DEFAULT_SORT_STYLE = 'date_desc';
376e45321fSGreg Roach    const DEFAULT_INFO_STYLE = 'table';
38af2b6902SGreg Roach    const MAX_DAYS           = 90;
398c2e8227SGreg Roach
408c2e8227SGreg Roach    /** {@inheritdoc} */
418f53f488SRico Sonntag    public function getTitle(): string
42c1010edaSGreg Roach    {
43bbb76c12SGreg Roach        /* I18N: Name of a module */
44bbb76c12SGreg Roach        return I18N::translate('Recent changes');
458c2e8227SGreg Roach    }
468c2e8227SGreg Roach
478c2e8227SGreg Roach    /** {@inheritdoc} */
488f53f488SRico Sonntag    public function getDescription(): string
49c1010edaSGreg Roach    {
50bbb76c12SGreg Roach        /* I18N: Description of the “Recent changes” module */
51bbb76c12SGreg Roach        return I18N::translate('A list of records that have been updated recently.');
528c2e8227SGreg Roach    }
538c2e8227SGreg Roach
546e45321fSGreg Roach    /** {@inheritdoc} */
55c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
56c1010edaSGreg Roach    {
57e490cd80SGreg Roach        global $ctype;
588c2e8227SGreg Roach
5955664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
606e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
616e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
62047cb287SGreg Roach        $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
638c2e8227SGreg Roach
64c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
658c2e8227SGreg Roach
66e490cd80SGreg Roach        $records = $this->getRecentChanges($tree, $days);
678c2e8227SGreg Roach
680280f44aSGreg Roach        switch ($sortStyle) {
690280f44aSGreg Roach            case 'name':
7080e23601SGreg Roach                uasort($records, function (GedcomRecord $a, GedcomRecord $b): int {
7180e23601SGreg Roach                    return GedcomRecord::compare($a, $b) ?: $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true);
7280e23601SGreg Roach                });
738c2e8227SGreg Roach                break;
7480e23601SGreg Roach
750280f44aSGreg Roach            case 'date_asc':
7680e23601SGreg Roach                uasort($records, function (GedcomRecord $a, GedcomRecord $b): int {
7780e23601SGreg Roach                    return $a->lastChangeTimestamp(true) - $b->lastChangeTimestamp(true) ?: GedcomRecord::compare($b, $a);
7880e23601SGreg Roach                });
798c2e8227SGreg Roach                break;
8080e23601SGreg Roach
810280f44aSGreg Roach            case 'date_desc':
8280e23601SGreg Roach                uasort($records, function (GedcomRecord $a, GedcomRecord $b): int {
8380e23601SGreg Roach                    return $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true) ?: GedcomRecord::compare($a, $b);
8480e23601SGreg Roach                });
858c2e8227SGreg Roach        }
860280f44aSGreg Roach
870280f44aSGreg Roach        if (empty($records)) {
880280f44aSGreg Roach            $content = I18N::plural('There have been no changes within the last %s day.', 'There have been no changes within the last %s days.', $days, I18N::number($days));
890280f44aSGreg Roach        } elseif ($infoStyle === 'list') {
904d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-list', [
914d2d7d1aSGreg Roach                'records'   => $records,
924d2d7d1aSGreg Roach                'show_user' => $show_user,
934d2d7d1aSGreg Roach            ]);
940280f44aSGreg Roach        } else {
954d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-table', [
960280f44aSGreg Roach                'records'   => $records,
970280f44aSGreg Roach                'show_user' => $show_user,
980280f44aSGreg Roach            ]);
998c2e8227SGreg Roach        }
1008c2e8227SGreg Roach
1018c2e8227SGreg Roach        if ($template) {
102e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
103c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
104c1010edaSGreg Roach                    'block_id' => $block_id,
105c1010edaSGreg Roach                    'ged'      => $tree->getName(),
106c1010edaSGreg Roach                ]);
107397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
108c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
109c1010edaSGreg Roach                    'block_id' => $block_id,
110c1010edaSGreg Roach                    'ged'      => $tree->getName(),
111c1010edaSGreg Roach                ]);
1128cbbfdceSGreg Roach            } else {
1138cbbfdceSGreg Roach                $config_url = '';
1148cbbfdceSGreg Roach            }
1158cbbfdceSGreg Roach
116147e99aaSGreg Roach            return view('modules/block-template', [
1179c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1189c6524dcSGreg Roach                'id'         => $block_id,
1198cbbfdceSGreg Roach                'config_url' => $config_url,
1208cbbfdceSGreg Roach                'title'      => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)),
1219c6524dcSGreg Roach                'content'    => $content,
1229c6524dcSGreg Roach            ]);
1238c2e8227SGreg Roach        }
124b2ce94c6SRico Sonntag
125b2ce94c6SRico Sonntag        return $content;
1268c2e8227SGreg Roach    }
1278c2e8227SGreg Roach
1288c2e8227SGreg Roach    /** {@inheritdoc} */
129c1010edaSGreg Roach    public function loadAjax(): bool
130c1010edaSGreg Roach    {
1318c2e8227SGreg Roach        return true;
1328c2e8227SGreg Roach    }
1338c2e8227SGreg Roach
1348c2e8227SGreg Roach    /** {@inheritdoc} */
135c1010edaSGreg Roach    public function isUserBlock(): bool
136c1010edaSGreg Roach    {
1378c2e8227SGreg Roach        return true;
1388c2e8227SGreg Roach    }
1398c2e8227SGreg Roach
1408c2e8227SGreg Roach    /** {@inheritdoc} */
141c1010edaSGreg Roach    public function isGedcomBlock(): bool
142c1010edaSGreg Roach    {
1438c2e8227SGreg Roach        return true;
1448c2e8227SGreg Roach    }
1458c2e8227SGreg Roach
14620ac4041SGreg Roach    /**
147a45f9889SGreg Roach     * Update the configuration for a block.
148a45f9889SGreg Roach     *
149a45f9889SGreg Roach     * @param Request $request
150a45f9889SGreg Roach     * @param int     $block_id
151a45f9889SGreg Roach     *
152a45f9889SGreg Roach     * @return void
153a45f9889SGreg Roach     */
154a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
155a45f9889SGreg Roach    {
156af2b6902SGreg Roach        $days = $request->get('days', self::DEFAULT_DAYS);
157af2b6902SGreg Roach
158af2b6902SGreg Roach        if ((int) $days > self::MAX_DAYS || (int) $days < 1) {
159af2b6902SGreg Roach            $days = self::DEFAULT_DAYS;
160af2b6902SGreg Roach        }
161af2b6902SGreg Roach
162af2b6902SGreg Roach        $this->setBlockSetting($block_id, 'days', $days);
163a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_INFO_STYLE));
164a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'sortStyle', $request->get('sortStyle', self::DEFAULT_SORT_STYLE));
165a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_user', $request->get('show_user', self::DEFAULT_SHOW_USER));
166a45f9889SGreg Roach    }
167a45f9889SGreg Roach
168a45f9889SGreg Roach    /**
16920ac4041SGreg Roach     * An HTML form to edit block settings
17020ac4041SGreg Roach     *
17120ac4041SGreg Roach     * @param Tree $tree
17220ac4041SGreg Roach     * @param int  $block_id
17320ac4041SGreg Roach     *
17420ac4041SGreg Roach     * @return void
17520ac4041SGreg Roach     */
176a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
177c1010edaSGreg Roach    {
17855664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
1796e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
1806e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
1816e45321fSGreg Roach        $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
1828c2e8227SGreg Roach
183c385536dSGreg Roach        $info_styles = [
184bbb76c12SGreg Roach            /* I18N: An option in a list-box */
185bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
186bbb76c12SGreg Roach            /* I18N: An option in a list-box */
187bbb76c12SGreg Roach            'table' => I18N::translate('table'),
188c385536dSGreg Roach        ];
1898c2e8227SGreg Roach
190c385536dSGreg Roach        $sort_styles = [
191bbb76c12SGreg Roach            /* I18N: An option in a list-box */
192bbb76c12SGreg Roach            'name'      => I18N::translate('sort by name'),
193bbb76c12SGreg Roach            /* I18N: An option in a list-box */
194bbb76c12SGreg Roach            'date_asc'  => I18N::translate('sort by date, oldest first'),
195bbb76c12SGreg Roach            /* I18N: An option in a list-box */
196bbb76c12SGreg Roach            'date_desc' => I18N::translate('sort by date, newest first'),
197c385536dSGreg Roach        ];
1988c2e8227SGreg Roach
199147e99aaSGreg Roach        echo view('modules/recent_changes/config', [
200c385536dSGreg Roach            'days'        => $days,
201c385536dSGreg Roach            'infoStyle'   => $infoStyle,
202c385536dSGreg Roach            'info_styles' => $info_styles,
203c385536dSGreg Roach            'max_days'    => self::MAX_DAYS,
204c385536dSGreg Roach            'sortStyle'   => $sortStyle,
205c385536dSGreg Roach            'sort_styles' => $sort_styles,
206c385536dSGreg Roach            'show_user'   => $show_user,
207c385536dSGreg Roach        ]);
2088c2e8227SGreg Roach    }
2098c2e8227SGreg Roach
2106e45321fSGreg Roach    /**
2116e45321fSGreg Roach     * Find records that have changed since a given julian day
2126e45321fSGreg Roach     *
2136e45321fSGreg Roach     * @param Tree $tree Changes for which tree
21424319d9dSGreg Roach     * @param int  $days Number of days
2156e45321fSGreg Roach     *
2166e45321fSGreg Roach     * @return GedcomRecord[] List of records with changes
2176e45321fSGreg Roach     */
21855664801SGreg Roach    private function getRecentChanges(Tree $tree, int $days): array
219c1010edaSGreg Roach    {
2206e45321fSGreg Roach        $sql =
22124319d9dSGreg Roach            "SELECT xref FROM `##change`" .
222b03b6f21SGreg Roach            " WHERE new_gedcom != '' AND change_time > DATE_SUB(NOW(), INTERVAL :days DAY) AND gedcom_id = :tree_id" .
22324319d9dSGreg Roach            " GROUP BY xref" .
22424319d9dSGreg Roach            " ORDER BY MAX(change_id) DESC";
2256e45321fSGreg Roach
22613abd6f3SGreg Roach        $vars = [
22724319d9dSGreg Roach            'days'    => $days,
228*72cf66d4SGreg Roach            'tree_id' => $tree->id(),
22913abd6f3SGreg Roach        ];
2306e45321fSGreg Roach
2316e45321fSGreg Roach        $xrefs = Database::prepare($sql)->execute($vars)->fetchOneColumn();
2326e45321fSGreg Roach
23313abd6f3SGreg Roach        $records = [];
2346e45321fSGreg Roach        foreach ($xrefs as $xref) {
2356e45321fSGreg Roach            $record = GedcomRecord::getInstance($xref, $tree);
2368e8029e5SRico Sonntag            if ($record && $record->canShow()) {
2376e45321fSGreg Roach                $records[] = $record;
2386e45321fSGreg Roach            }
2396e45321fSGreg Roach        }
2406e45321fSGreg Roach
2416e45321fSGreg Roach        return $records;
2426e45321fSGreg Roach    }
2438c2e8227SGreg Roach}
244