xref: /webtrees/app/Module/TopPageViewsModule.php (revision bbb76c12bd7338ebbb054916678efe20cb71ce1f)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
248c2e8227SGreg Roach
258c2e8227SGreg Roach/**
268c2e8227SGreg Roach * Class TopPageViewsModule
278c2e8227SGreg Roach */
28c1010edaSGreg Roachclass TopPageViewsModule extends AbstractModule implements ModuleBlockInterface
29c1010edaSGreg Roach{
3076692c8bSGreg Roach    /**
3176692c8bSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
3276692c8bSGreg Roach     *
3376692c8bSGreg Roach     * @return string
3476692c8bSGreg Roach     */
35c1010edaSGreg Roach    public function getTitle()
36c1010edaSGreg Roach    {
37*bbb76c12SGreg Roach        /* I18N: Name of a module */
38*bbb76c12SGreg Roach        return I18N::translate('Most viewed pages');
398c2e8227SGreg Roach    }
408c2e8227SGreg Roach
4176692c8bSGreg Roach    /**
4276692c8bSGreg Roach     * A sentence describing what this module does.
4376692c8bSGreg Roach     *
4476692c8bSGreg Roach     * @return string
4576692c8bSGreg Roach     */
46c1010edaSGreg Roach    public function getDescription()
47c1010edaSGreg Roach    {
48*bbb76c12SGreg Roach        /* I18N: Description of the “Most visited pages” module */
49*bbb76c12SGreg Roach        return I18N::translate('A list of the pages that have been viewed the most number of times.');
508c2e8227SGreg Roach    }
518c2e8227SGreg Roach
5276692c8bSGreg Roach    /**
5376692c8bSGreg Roach     * Generate the HTML content of this block.
5476692c8bSGreg Roach     *
55e490cd80SGreg Roach     * @param Tree     $tree
5676692c8bSGreg Roach     * @param int      $block_id
5776692c8bSGreg Roach     * @param bool     $template
58727f238cSGreg Roach     * @param string[] $cfg
5976692c8bSGreg Roach     *
6076692c8bSGreg Roach     * @return string
6176692c8bSGreg Roach     */
62c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
63c1010edaSGreg Roach    {
64e490cd80SGreg Roach        global $ctype;
658c2e8227SGreg Roach
66e2a378d3SGreg Roach        $num             = $this->getBlockSetting($block_id, 'num', '10');
67e2a378d3SGreg Roach        $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
688c2e8227SGreg Roach
69c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
708c2e8227SGreg Roach
718c2e8227SGreg Roach        // load the lines from the file
728c2e8227SGreg Roach        $top10 = Database::prepare(
738c2e8227SGreg Roach            "SELECT page_parameter, page_count" .
748c2e8227SGreg Roach            " FROM `##hit_counter`" .
758c2e8227SGreg Roach            " WHERE gedcom_id = :tree_id AND page_name IN ('individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php')" .
768c2e8227SGreg Roach            " ORDER BY page_count DESC LIMIT :limit"
7713abd6f3SGreg Roach        )->execute([
78e490cd80SGreg Roach            'tree_id' => $tree->getTreeId(),
798c2e8227SGreg Roach            'limit'   => (int)$num,
8013abd6f3SGreg Roach        ])->fetchAssoc();
818c2e8227SGreg Roach
82f7f4b984SGreg Roach        $content = '<table>';
838c2e8227SGreg Roach        foreach ($top10 as $id => $count) {
84e490cd80SGreg Roach            $record = GedcomRecord::getInstance($id, $tree);
858c2e8227SGreg Roach            if ($record && $record->canShow()) {
86a86dd8b1SGreg Roach                $content .= '<tr>';
878c2e8227SGreg Roach                if ($count_placement == 'before') {
88a86dd8b1SGreg Roach                    $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
898c2e8227SGreg Roach                }
90b1f1e4efSGreg Roach                $content .= '<td class="name2" ><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></td>';
918c2e8227SGreg Roach                if ($count_placement == 'after') {
92a86dd8b1SGreg Roach                    $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
938c2e8227SGreg Roach                }
948c2e8227SGreg Roach                $content .= '</tr>';
958c2e8227SGreg Roach            }
968c2e8227SGreg Roach        }
977a6ee1acSGreg Roach        $content .= '</table>';
988c2e8227SGreg Roach
998c2e8227SGreg Roach        if ($template) {
100e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
101c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
102c1010edaSGreg Roach                    'block_id' => $block_id,
103c1010edaSGreg Roach                    'ged'      => $tree->getName(),
104c1010edaSGreg Roach                ]);
105397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
106c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
107c1010edaSGreg Roach                    'block_id' => $block_id,
108c1010edaSGreg Roach                    'ged'      => $tree->getName(),
109c1010edaSGreg Roach                ]);
1108cbbfdceSGreg Roach            } else {
1118cbbfdceSGreg Roach                $config_url = '';
1128cbbfdceSGreg Roach            }
1138cbbfdceSGreg Roach
114147e99aaSGreg Roach            return view('modules/block-template', [
1159c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1169c6524dcSGreg Roach                'id'         => $block_id,
1178cbbfdceSGreg Roach                'config_url' => $config_url,
1189c6524dcSGreg Roach                'title'      => $this->getTitle(),
1199c6524dcSGreg Roach                'content'    => $content,
1209c6524dcSGreg Roach            ]);
1218c2e8227SGreg Roach        } else {
1228c2e8227SGreg Roach            return $content;
1238c2e8227SGreg Roach        }
1248c2e8227SGreg Roach    }
1258c2e8227SGreg Roach
12676692c8bSGreg Roach    /**
12776692c8bSGreg Roach     * Should this block load asynchronously using AJAX?
12876692c8bSGreg Roach     *
12976692c8bSGreg Roach     * Simple blocks are faster in-line, more comples ones
13076692c8bSGreg Roach     * can be loaded later.
13176692c8bSGreg Roach     *
13276692c8bSGreg Roach     * @return bool
13376692c8bSGreg Roach     */
134c1010edaSGreg Roach    public function loadAjax(): bool
135c1010edaSGreg Roach    {
1368c2e8227SGreg Roach        return true;
1378c2e8227SGreg Roach    }
1388c2e8227SGreg Roach
13976692c8bSGreg Roach    /**
14076692c8bSGreg Roach     * Can this block be shown on the user’s home page?
14176692c8bSGreg Roach     *
14276692c8bSGreg Roach     * @return bool
14376692c8bSGreg Roach     */
144c1010edaSGreg Roach    public function isUserBlock(): bool
145c1010edaSGreg Roach    {
1468c2e8227SGreg Roach        return false;
1478c2e8227SGreg Roach    }
1488c2e8227SGreg Roach
14976692c8bSGreg Roach    /**
15076692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
15176692c8bSGreg Roach     *
15276692c8bSGreg Roach     * @return bool
15376692c8bSGreg Roach     */
154c1010edaSGreg Roach    public function isGedcomBlock(): bool
155c1010edaSGreg Roach    {
1568c2e8227SGreg Roach        return true;
1578c2e8227SGreg Roach    }
1588c2e8227SGreg Roach
15976692c8bSGreg Roach    /**
16076692c8bSGreg Roach     * An HTML form to edit block settings
16176692c8bSGreg Roach     *
162e490cd80SGreg Roach     * @param Tree $tree
16376692c8bSGreg Roach     * @param int  $block_id
164a9430be8SGreg Roach     *
165a9430be8SGreg Roach     * @return void
16676692c8bSGreg Roach     */
167c1010edaSGreg Roach    public function configureBlock(Tree $tree, int $block_id)
168c1010edaSGreg Roach    {
169c385536dSGreg Roach        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
170e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
171e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'count_placement', Filter::post('count_placement', 'before|after', 'before'));
172c385536dSGreg Roach
173c385536dSGreg Roach            return;
1748c2e8227SGreg Roach        }
1758c2e8227SGreg Roach
176e2a378d3SGreg Roach        $num             = $this->getBlockSetting($block_id, 'num', '10');
177e2a378d3SGreg Roach        $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
1788c2e8227SGreg Roach
179c385536dSGreg Roach        $options = [
180*bbb76c12SGreg Roach            /* I18N: An option in a list-box */
181*bbb76c12SGreg Roach            'before' => I18N::translate('before'),
182*bbb76c12SGreg Roach            /* I18N: An option in a list-box */
183*bbb76c12SGreg Roach            'after'  => I18N::translate('after'),
184c385536dSGreg Roach        ];
1858c2e8227SGreg Roach
186147e99aaSGreg Roach        echo view('modules/top10_pageviews/config', [
187c385536dSGreg Roach            'count_placement' => $count_placement,
188c385536dSGreg Roach            'num'             => $num,
189c385536dSGreg Roach            'options'         => $options,
190c385536dSGreg Roach        ]);
1918c2e8227SGreg Roach    }
1928c2e8227SGreg Roach}
193