xref: /webtrees/app/Module/TopPageViewsModule.php (revision 6e9b2106ef8de9279faf02c6e02326f2ecbbf5a5)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Factory;
23use Fisharebest\Webtrees\GedcomRecord;
24use Fisharebest\Webtrees\I18N;
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    /**
40     * How should this module be identified in the control panel, etc.?
41     *
42     * @return string
43     */
44    public function title(): string
45    {
46        /* I18N: Name of a module */
47        return I18N::translate('Most viewed pages');
48    }
49
50    /**
51     * A sentence describing what this module does.
52     *
53     * @return string
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 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', ['individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php'])
80            ->orderByDesc('page_count');
81
82        $results = [];
83        foreach ($query->cursor() as $row) {
84            $record = Factory::gedcomRecord()->make($row->page_parameter, $tree);
85
86            if ($record instanceof GedcomRecord && $record->canShow()) {
87                $results[] = [
88                    'record' => $record,
89                    'count'  => $row->page_count,
90                ];
91            }
92
93            if (count($results) === $num) {
94                break;
95            }
96        }
97
98        $content = view('modules/top10_pageviews/list', ['results' => $results]);
99
100        if ($context !== self::CONTEXT_EMBED) {
101            return view('modules/block-template', [
102                'block'      => Str::kebab($this->name()),
103                'id'         => $block_id,
104                'config_url' => $this->configUrl($tree, $context, $block_id),
105                'title'      => $this->title(),
106                'content'    => $content,
107            ]);
108        }
109
110        return $content;
111    }
112
113    /**
114     * Should this block load asynchronously using AJAX?
115     *
116     * Simple blocks are faster in-line, more complex ones can be loaded later.
117     *
118     * @return bool
119     */
120    public function loadAjax(): bool
121    {
122        return true;
123    }
124
125    /**
126     * Can this block be shown on the user’s home page?
127     *
128     * @return bool
129     */
130    public function isUserBlock(): bool
131    {
132        return false;
133    }
134
135    /**
136     * Can this block be shown on the tree’s home page?
137     *
138     * @return bool
139     */
140    public function isTreeBlock(): bool
141    {
142        return true;
143    }
144
145    /**
146     * Update the configuration for a block.
147     *
148     * @param ServerRequestInterface $request
149     * @param int     $block_id
150     *
151     * @return void
152     */
153    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
154    {
155        $params = (array) $request->getParsedBody();
156
157        $this->setBlockSetting($block_id, 'num', $params['num']);
158    }
159
160    /**
161     * An HTML form to edit block settings
162     *
163     * @param Tree $tree
164     * @param int  $block_id
165     *
166     * @return string
167     */
168    public function editBlockConfiguration(Tree $tree, int $block_id): string
169    {
170        $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER_TO_SHOW);
171
172        return view('modules/top10_pageviews/config', [
173            'num' => $num,
174        ]);
175    }
176}
177