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