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