xref: /webtrees/app/Module/TopPageViewsModule.php (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\DB;
23use Fisharebest\Webtrees\GedcomRecord;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Tree;
27use Fisharebest\Webtrees\Validator;
28use Illuminate\Support\Str;
29use Psr\Http\Message\ServerRequestInterface;
30
31use function count;
32
33/**
34 * Class TopPageViewsModule
35 */
36class TopPageViewsModule extends AbstractModule implements ModuleBlockInterface
37{
38    use ModuleBlockTrait;
39
40    private const DEFAULT_NUMBER_TO_SHOW = '10';
41
42    private const PAGES = ['individual.php', 'family.php', 'source.php', 'repo.php', 'note.php', 'mediaviewer.php'];
43
44    /**
45     * How should this module be identified in the control panel, etc.?
46     *
47     * @return string
48     */
49    public function title(): string
50    {
51        /* I18N: Name of a module */
52        return I18N::translate('Most viewed pages');
53    }
54
55    /**
56     * A sentence describing what this module does.
57     *
58     * @return string
59     */
60    public function description(): string
61    {
62        /* I18N: Description of the “Most viewed pages” module */
63        return I18N::translate('A list of the pages that have been viewed the most number of times.');
64    }
65
66    /**
67     * Generate the HTML content of this block.
68     *
69     * @param Tree                 $tree
70     * @param int                  $block_id
71     * @param string               $context
72     * @param array<string,string> $config
73     *
74     * @return string
75     */
76    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
77    {
78        $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER_TO_SHOW);
79
80        extract($config, EXTR_OVERWRITE);
81
82        $query = DB::table('hit_counter')
83            ->where('gedcom_id', '=', $tree->id())
84            ->whereIn('page_name', self::PAGES)
85            ->select(['page_parameter', 'page_count'])
86            ->orderByDesc('page_count');
87
88        $results = [];
89        foreach ($query->cursor() as $row) {
90            $record = Registry::gedcomRecordFactory()->make($row->page_parameter, $tree);
91
92            if ($record instanceof GedcomRecord && $record->canShow()) {
93                $results[] = [
94                    'record' => $record,
95                    'count'  => (int) $row->page_count,
96                ];
97            }
98
99            if (count($results) === $num) {
100                break;
101            }
102        }
103
104        $content = view('modules/top10_pageviews/list', ['results' => $results]);
105
106        if ($context !== self::CONTEXT_EMBED) {
107            return view('modules/block-template', [
108                'block'      => Str::kebab($this->name()),
109                'id'         => $block_id,
110                'config_url' => $this->configUrl($tree, $context, $block_id),
111                'title'      => $this->title(),
112                'content'    => $content,
113            ]);
114        }
115
116        return $content;
117    }
118
119    /**
120     * Should this block load asynchronously using AJAX?
121     *
122     * Simple blocks are faster in-line, more complex ones can be loaded later.
123     *
124     * @return bool
125     */
126    public function loadAjax(): bool
127    {
128        return true;
129    }
130
131    /**
132     * Can this block be shown on the user’s home page?
133     *
134     * @return bool
135     */
136    public function isUserBlock(): bool
137    {
138        return false;
139    }
140
141    /**
142     * Can this block be shown on the tree’s home page?
143     *
144     * @return bool
145     */
146    public function isTreeBlock(): bool
147    {
148        return true;
149    }
150
151    /**
152     * Update the configuration for a block.
153     *
154     * @param ServerRequestInterface $request
155     * @param int     $block_id
156     *
157     * @return void
158     */
159    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
160    {
161        $num = Validator::parsedBody($request)->integer('num');
162
163        $this->setBlockSetting($block_id, 'num', (string) $num);
164    }
165
166    /**
167     * An HTML form to edit block settings
168     *
169     * @param Tree $tree
170     * @param int  $block_id
171     *
172     * @return string
173     */
174    public function editBlockConfiguration(Tree $tree, int $block_id): string
175    {
176        $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER_TO_SHOW);
177
178        return view('modules/top10_pageviews/config', [
179            'num' => $num,
180        ]);
181    }
182}
183