xref: /webtrees/app/Module/TopPageViewsModule.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\GedcomRecord;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Tree;
26use Illuminate\Database\Capsule\Manager as DB;
27use Illuminate\Support\Str;
28use Psr\Http\Message\ServerRequestInterface;
29
30/**
31 * Class TopPageViewsModule
32 */
33class TopPageViewsModule extends AbstractModule implements ModuleBlockInterface
34{
35    use ModuleBlockTrait;
36
37    private const DEFAULT_NUMBER_TO_SHOW = '10';
38
39    private const PAGES = ['individual.php', 'family.php', 'source.php', 'repo.php', 'note.php', 'mediaviewer.php'];
40
41    /**
42     * How should this module be identified in the control panel, etc.?
43     *
44     * @return string
45     */
46    public function title(): string
47    {
48        /* I18N: Name of a module */
49        return I18N::translate('Most viewed pages');
50    }
51
52    /**
53     * A sentence describing what this module does.
54     *
55     * @return string
56     */
57    public function description(): string
58    {
59        /* I18N: Description of the “Most viewed pages” module */
60        return I18N::translate('A list of the pages that have been viewed the most number of times.');
61    }
62
63    /**
64     * Generate the HTML content of this block.
65     *
66     * @param Tree                 $tree
67     * @param int                  $block_id
68     * @param string               $context
69     * @param array<string,string> $config
70     *
71     * @return string
72     */
73    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
74    {
75        $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER_TO_SHOW);
76
77        extract($config, EXTR_OVERWRITE);
78
79        $query = DB::table('hit_counter')
80            ->where('gedcom_id', '=', $tree->id())
81            ->whereIn('page_name', self::PAGES)
82            ->orderByDesc('page_count');
83
84        $results = [];
85        foreach ($query->cursor() as $row) {
86            $record = Registry::gedcomRecordFactory()->make($row->page_parameter, $tree);
87
88            if ($record instanceof GedcomRecord && $record->canShow()) {
89                $results[] = [
90                    'record' => $record,
91                    'count'  => $row->page_count,
92                ];
93            }
94
95            if (count($results) === $num) {
96                break;
97            }
98        }
99
100        $content = view('modules/top10_pageviews/list', ['results' => $results]);
101
102        if ($context !== self::CONTEXT_EMBED) {
103            return view('modules/block-template', [
104                'block'      => Str::kebab($this->name()),
105                'id'         => $block_id,
106                'config_url' => $this->configUrl($tree, $context, $block_id),
107                'title'      => $this->title(),
108                'content'    => $content,
109            ]);
110        }
111
112        return $content;
113    }
114
115    /**
116     * Should this block load asynchronously using AJAX?
117     *
118     * Simple blocks are faster in-line, more complex ones can be loaded later.
119     *
120     * @return bool
121     */
122    public function loadAjax(): bool
123    {
124        return true;
125    }
126
127    /**
128     * Can this block be shown on the user’s home page?
129     *
130     * @return bool
131     */
132    public function isUserBlock(): bool
133    {
134        return false;
135    }
136
137    /**
138     * Can this block be shown on the tree’s home page?
139     *
140     * @return bool
141     */
142    public function isTreeBlock(): bool
143    {
144        return true;
145    }
146
147    /**
148     * Update the configuration for a block.
149     *
150     * @param ServerRequestInterface $request
151     * @param int     $block_id
152     *
153     * @return void
154     */
155    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
156    {
157        $params = (array) $request->getParsedBody();
158
159        $this->setBlockSetting($block_id, 'num', $params['num']);
160    }
161
162    /**
163     * An HTML form to edit block settings
164     *
165     * @param Tree $tree
166     * @param int  $block_id
167     *
168     * @return string
169     */
170    public function editBlockConfiguration(Tree $tree, int $block_id): string
171    {
172        $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER_TO_SHOW);
173
174        return view('modules/top10_pageviews/config', [
175            'num' => $num,
176        ]);
177    }
178}
179