xref: /webtrees/app/Module/RecentChangesModule.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg Roach * Copyright (C) 2019 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} */
555f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
56c1010edaSGreg Roach    {
5755664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
586e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
596e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
60047cb287SGreg Roach        $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
618c2e8227SGreg Roach
62c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
638c2e8227SGreg Roach
64e490cd80SGreg Roach        $records = $this->getRecentChanges($tree, $days);
658c2e8227SGreg Roach
660280f44aSGreg Roach        switch ($sortStyle) {
670280f44aSGreg Roach            case 'name':
6880e23601SGreg Roach                uasort($records, function (GedcomRecord $a, GedcomRecord $b): int {
6980e23601SGreg Roach                    return GedcomRecord::compare($a, $b) ?: $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true);
7080e23601SGreg Roach                });
718c2e8227SGreg Roach                break;
7280e23601SGreg Roach
730280f44aSGreg Roach            case 'date_asc':
7480e23601SGreg Roach                uasort($records, function (GedcomRecord $a, GedcomRecord $b): int {
7580e23601SGreg Roach                    return $a->lastChangeTimestamp(true) - $b->lastChangeTimestamp(true) ?: GedcomRecord::compare($b, $a);
7680e23601SGreg Roach                });
778c2e8227SGreg Roach                break;
7880e23601SGreg Roach
790280f44aSGreg Roach            case 'date_desc':
8080e23601SGreg Roach                uasort($records, function (GedcomRecord $a, GedcomRecord $b): int {
8180e23601SGreg Roach                    return $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true) ?: GedcomRecord::compare($a, $b);
8280e23601SGreg Roach                });
838c2e8227SGreg Roach        }
840280f44aSGreg Roach
850280f44aSGreg Roach        if (empty($records)) {
860280f44aSGreg 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));
870280f44aSGreg Roach        } elseif ($infoStyle === 'list') {
884d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-list', [
894d2d7d1aSGreg Roach                'records'   => $records,
904d2d7d1aSGreg Roach                'show_user' => $show_user,
914d2d7d1aSGreg Roach            ]);
920280f44aSGreg Roach        } else {
934d2d7d1aSGreg Roach            $content = view('modules/recent_changes/changes-table', [
940280f44aSGreg Roach                'records'   => $records,
950280f44aSGreg Roach                'show_user' => $show_user,
960280f44aSGreg Roach            ]);
978c2e8227SGreg Roach        }
988c2e8227SGreg Roach
996a8879feSGreg Roach        if ($ctype !== '') {
100e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
101c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
102c1010edaSGreg Roach                    'block_id' => $block_id,
103aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
104c1010edaSGreg Roach                ]);
105397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
106c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
107c1010edaSGreg Roach                    'block_id' => $block_id,
108aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
109c1010edaSGreg Roach                ]);
1108cbbfdceSGreg Roach            } else {
1118cbbfdceSGreg Roach                $config_url = '';
1128cbbfdceSGreg Roach            }
1138cbbfdceSGreg Roach
114147e99aaSGreg Roach            return view('modules/block-template', [
1159c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1169c6524dcSGreg Roach                'id'         => $block_id,
1178cbbfdceSGreg Roach                'config_url' => $config_url,
1188cbbfdceSGreg Roach                'title'      => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)),
1199c6524dcSGreg Roach                'content'    => $content,
1209c6524dcSGreg Roach            ]);
1218c2e8227SGreg Roach        }
122b2ce94c6SRico Sonntag
123b2ce94c6SRico Sonntag        return $content;
1248c2e8227SGreg Roach    }
1258c2e8227SGreg Roach
1268c2e8227SGreg Roach    /** {@inheritdoc} */
127c1010edaSGreg Roach    public function loadAjax(): bool
128c1010edaSGreg Roach    {
1298c2e8227SGreg Roach        return true;
1308c2e8227SGreg Roach    }
1318c2e8227SGreg Roach
1328c2e8227SGreg Roach    /** {@inheritdoc} */
133c1010edaSGreg Roach    public function isUserBlock(): bool
134c1010edaSGreg Roach    {
1358c2e8227SGreg Roach        return true;
1368c2e8227SGreg Roach    }
1378c2e8227SGreg Roach
1388c2e8227SGreg Roach    /** {@inheritdoc} */
139c1010edaSGreg Roach    public function isGedcomBlock(): bool
140c1010edaSGreg Roach    {
1418c2e8227SGreg Roach        return true;
1428c2e8227SGreg Roach    }
1438c2e8227SGreg Roach
14420ac4041SGreg Roach    /**
145a45f9889SGreg Roach     * Update the configuration for a block.
146a45f9889SGreg Roach     *
147a45f9889SGreg Roach     * @param Request $request
148a45f9889SGreg Roach     * @param int     $block_id
149a45f9889SGreg Roach     *
150a45f9889SGreg Roach     * @return void
151a45f9889SGreg Roach     */
152a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
153a45f9889SGreg Roach    {
154af2b6902SGreg Roach        $days = $request->get('days', self::DEFAULT_DAYS);
155af2b6902SGreg Roach
156af2b6902SGreg Roach        if ((int) $days > self::MAX_DAYS || (int) $days < 1) {
157af2b6902SGreg Roach            $days = self::DEFAULT_DAYS;
158af2b6902SGreg Roach        }
159af2b6902SGreg Roach
160af2b6902SGreg Roach        $this->setBlockSetting($block_id, 'days', $days);
161a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_INFO_STYLE));
162a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'sortStyle', $request->get('sortStyle', self::DEFAULT_SORT_STYLE));
163a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_user', $request->get('show_user', self::DEFAULT_SHOW_USER));
164a45f9889SGreg Roach    }
165a45f9889SGreg Roach
166a45f9889SGreg Roach    /**
16720ac4041SGreg Roach     * An HTML form to edit block settings
16820ac4041SGreg Roach     *
16920ac4041SGreg Roach     * @param Tree $tree
17020ac4041SGreg Roach     * @param int  $block_id
17120ac4041SGreg Roach     *
17220ac4041SGreg Roach     * @return void
17320ac4041SGreg Roach     */
174a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
175c1010edaSGreg Roach    {
17655664801SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
1776e45321fSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
1786e45321fSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
1796e45321fSGreg Roach        $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
1808c2e8227SGreg Roach
181c385536dSGreg Roach        $info_styles = [
182bbb76c12SGreg Roach            /* I18N: An option in a list-box */
183bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
184bbb76c12SGreg Roach            /* I18N: An option in a list-box */
185bbb76c12SGreg Roach            'table' => I18N::translate('table'),
186c385536dSGreg Roach        ];
1878c2e8227SGreg Roach
188c385536dSGreg Roach        $sort_styles = [
189bbb76c12SGreg Roach            /* I18N: An option in a list-box */
190bbb76c12SGreg Roach            'name'      => I18N::translate('sort by name'),
191bbb76c12SGreg Roach            /* I18N: An option in a list-box */
192bbb76c12SGreg Roach            'date_asc'  => I18N::translate('sort by date, oldest first'),
193bbb76c12SGreg Roach            /* I18N: An option in a list-box */
194bbb76c12SGreg Roach            'date_desc' => I18N::translate('sort by date, newest first'),
195c385536dSGreg Roach        ];
1968c2e8227SGreg Roach
197147e99aaSGreg Roach        echo view('modules/recent_changes/config', [
198c385536dSGreg Roach            'days'        => $days,
199c385536dSGreg Roach            'infoStyle'   => $infoStyle,
200c385536dSGreg Roach            'info_styles' => $info_styles,
201c385536dSGreg Roach            'max_days'    => self::MAX_DAYS,
202c385536dSGreg Roach            'sortStyle'   => $sortStyle,
203c385536dSGreg Roach            'sort_styles' => $sort_styles,
204c385536dSGreg Roach            'show_user'   => $show_user,
205c385536dSGreg Roach        ]);
2068c2e8227SGreg Roach    }
2078c2e8227SGreg Roach
2086e45321fSGreg Roach    /**
2096e45321fSGreg Roach     * Find records that have changed since a given julian day
2106e45321fSGreg Roach     *
2116e45321fSGreg Roach     * @param Tree $tree Changes for which tree
21224319d9dSGreg Roach     * @param int  $days Number of days
2136e45321fSGreg Roach     *
2146e45321fSGreg Roach     * @return GedcomRecord[] List of records with changes
2156e45321fSGreg Roach     */
21655664801SGreg Roach    private function getRecentChanges(Tree $tree, int $days): array
217c1010edaSGreg Roach    {
2186e45321fSGreg Roach        $sql =
21924319d9dSGreg Roach            "SELECT xref FROM `##change`" .
220b03b6f21SGreg Roach            " WHERE new_gedcom != '' AND change_time > DATE_SUB(NOW(), INTERVAL :days DAY) AND gedcom_id = :tree_id" .
22124319d9dSGreg Roach            " GROUP BY xref" .
22224319d9dSGreg Roach            " ORDER BY MAX(change_id) DESC";
2236e45321fSGreg Roach
22413abd6f3SGreg Roach        $vars = [
22524319d9dSGreg Roach            'days'    => $days,
22672cf66d4SGreg Roach            'tree_id' => $tree->id(),
22713abd6f3SGreg Roach        ];
2286e45321fSGreg Roach
2296e45321fSGreg Roach        $xrefs = Database::prepare($sql)->execute($vars)->fetchOneColumn();
2306e45321fSGreg Roach
23113abd6f3SGreg Roach        $records = [];
2326e45321fSGreg Roach        foreach ($xrefs as $xref) {
2336e45321fSGreg Roach            $record = GedcomRecord::getInstance($xref, $tree);
2348e8029e5SRico Sonntag            if ($record && $record->canShow()) {
2356e45321fSGreg Roach                $records[] = $record;
2366e45321fSGreg Roach            }
2376e45321fSGreg Roach        }
2386e45321fSGreg Roach
2396e45321fSGreg Roach        return $records;
2406e45321fSGreg Roach    }
2418c2e8227SGreg Roach}
242