xref: /webtrees/app/Module/TopPageViewsModule.php (revision c1010eda29c0909ed4d5d463f32d32bfefdd4dfe)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Database;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\GedcomRecord;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Tree;
24
25/**
26 * Class TopPageViewsModule
27 */
28class TopPageViewsModule extends AbstractModule implements ModuleBlockInterface
29{
30    /**
31     * How should this module be labelled on tabs, menus, etc.?
32     *
33     * @return string
34     */
35    public function getTitle()
36    {
37        return /* I18N: Name of a module */
38            I18N::translate('Most viewed pages');
39    }
40
41    /**
42     * A sentence describing what this module does.
43     *
44     * @return string
45     */
46    public function getDescription()
47    {
48        return /* I18N: Description of the “Most visited pages” module */
49            I18N::translate('A list of the pages that have been viewed the most number of times.');
50    }
51
52    /**
53     * Generate the HTML content of this block.
54     *
55     * @param Tree     $tree
56     * @param int      $block_id
57     * @param bool     $template
58     * @param string[] $cfg
59     *
60     * @return string
61     */
62    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
63    {
64        global $ctype;
65
66        $num             = $this->getBlockSetting($block_id, 'num', '10');
67        $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
68
69        extract($cfg, EXTR_OVERWRITE);
70
71        // load the lines from the file
72        $top10 = Database::prepare(
73            "SELECT page_parameter, page_count" .
74            " FROM `##hit_counter`" .
75            " WHERE gedcom_id = :tree_id AND page_name IN ('individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php')" .
76            " ORDER BY page_count DESC LIMIT :limit"
77        )->execute([
78            'tree_id' => $tree->getTreeId(),
79            'limit'   => (int)$num,
80        ])->fetchAssoc();
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->getFullName() . '</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 ($template) {
100            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
101                $config_url = route('tree-page-block-edit', [
102                    'block_id' => $block_id,
103                    'ged'      => $tree->getName(),
104                ]);
105            } elseif ($ctype === 'user' && Auth::check()) {
106                $config_url = route('user-page-block-edit', [
107                    'block_id' => $block_id,
108                    'ged'      => $tree->getName(),
109                ]);
110            } else {
111                $config_url = '';
112            }
113
114            return view('modules/block-template', [
115                'block'      => str_replace('_', '-', $this->getName()),
116                'id'         => $block_id,
117                'config_url' => $config_url,
118                'title'      => $this->getTitle(),
119                'content'    => $content,
120            ]);
121        } else {
122            return $content;
123        }
124    }
125
126    /**
127     * Should this block load asynchronously using AJAX?
128     *
129     * Simple blocks are faster in-line, more comples ones
130     * can be loaded later.
131     *
132     * @return bool
133     */
134    public function loadAjax(): bool
135    {
136        return true;
137    }
138
139    /**
140     * Can this block be shown on the user’s home page?
141     *
142     * @return bool
143     */
144    public function isUserBlock(): bool
145    {
146        return false;
147    }
148
149    /**
150     * Can this block be shown on the tree’s home page?
151     *
152     * @return bool
153     */
154    public function isGedcomBlock(): bool
155    {
156        return true;
157    }
158
159    /**
160     * An HTML form to edit block settings
161     *
162     * @param Tree $tree
163     * @param int  $block_id
164     *
165     * @return void
166     */
167    public function configureBlock(Tree $tree, int $block_id)
168    {
169        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
170            $this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
171            $this->setBlockSetting($block_id, 'count_placement', Filter::post('count_placement', 'before|after', 'before'));
172
173            return;
174        }
175
176        $num             = $this->getBlockSetting($block_id, 'num', '10');
177        $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
178
179        $options = [
180            'before' => /* I18N: An option in a list-box */
181                I18N::translate('before'),
182            'after'  => /* I18N: An option in a list-box */
183                I18N::translate('after'),
184        ];
185
186        echo view('modules/top10_pageviews/config', [
187            'count_placement' => $count_placement,
188            'num'             => $num,
189            'options'         => $options,
190        ]);
191    }
192}
193