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