xref: /webtrees/app/Module/TopPageViewsModule.php (revision b0f2796f8c7f9d92d007e1ed61a612ac5595a95e)
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    public function description(): string
56    {
57        /* I18N: Description of the “Most viewed pages” module */
58        return I18N::translate('A list of the pages that have been viewed the most number of times.');
59    }
60
61    /**
62     * Generate the HTML content of this block.
63     *
64     * @param Tree                 $tree
65     * @param int                  $block_id
66     * @param string               $context
67     * @param array<string,string> $config
68     *
69     * @return string
70     */
71    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
72    {
73        $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER_TO_SHOW);
74
75        extract($config, EXTR_OVERWRITE);
76
77        $query = DB::table('hit_counter')
78            ->where('gedcom_id', '=', $tree->id())
79            ->whereIn('page_name', self::PAGES)
80            ->select(['page_parameter', 'page_count'])
81            ->orderByDesc('page_count');
82
83        $results = [];
84        foreach ($query->cursor() as $row) {
85            $record = Registry::gedcomRecordFactory()->make($row->page_parameter, $tree);
86
87            if ($record instanceof GedcomRecord && $record->canShow()) {
88                $results[] = [
89                    'record' => $record,
90                    'count'  => (int) $row->page_count,
91                ];
92            }
93
94            if (count($results) === $num) {
95                break;
96            }
97        }
98
99        $content = view('modules/top10_pageviews/list', ['results' => $results]);
100
101        if ($context !== self::CONTEXT_EMBED) {
102            return view('modules/block-template', [
103                'block'      => Str::kebab($this->name()),
104                'id'         => $block_id,
105                'config_url' => $this->configUrl($tree, $context, $block_id),
106                'title'      => $this->title(),
107                'content'    => $content,
108            ]);
109        }
110
111        return $content;
112    }
113
114    /**
115     * Should this block load asynchronously using AJAX?
116     *
117     * Simple blocks are faster in-line, more complex ones can be loaded later.
118     *
119     * @return bool
120     */
121    public function loadAjax(): bool
122    {
123        return true;
124    }
125
126    /**
127     * Can this block be shown on the user’s home page?
128     *
129     * @return bool
130     */
131    public function isUserBlock(): bool
132    {
133        return false;
134    }
135
136    /**
137     * Can this block be shown on the tree’s home page?
138     *
139     * @return bool
140     */
141    public function isTreeBlock(): bool
142    {
143        return true;
144    }
145
146    /**
147     * Update the configuration for a block.
148     *
149     * @param ServerRequestInterface $request
150     * @param int     $block_id
151     *
152     * @return void
153     */
154    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
155    {
156        $num = Validator::parsedBody($request)->integer('num');
157
158        $this->setBlockSetting($block_id, 'num', (string) $num);
159    }
160
161    /**
162     * An HTML form to edit block settings
163     *
164     * @param Tree $tree
165     * @param int  $block_id
166     *
167     * @return string
168     */
169    public function editBlockConfiguration(Tree $tree, int $block_id): string
170    {
171        $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER_TO_SHOW);
172
173        return view('modules/top10_pageviews/config', [
174            'num' => $num,
175        ]);
176    }
177}
178