xref: /webtrees/app/Module/TopPageViewsModule.php (revision aa6f03bb51be5a55a78fe8aed18753116689a6d3)
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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
24e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
25a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class TopPageViewsModule
298c2e8227SGreg Roach */
30c1010edaSGreg Roachclass TopPageViewsModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
3276692c8bSGreg Roach    /**
3376692c8bSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
3476692c8bSGreg Roach     *
3576692c8bSGreg Roach     * @return string
3676692c8bSGreg Roach     */
378f53f488SRico Sonntag    public function getTitle(): string
38c1010edaSGreg Roach    {
39bbb76c12SGreg Roach        /* I18N: Name of a module */
40bbb76c12SGreg Roach        return I18N::translate('Most viewed pages');
418c2e8227SGreg Roach    }
428c2e8227SGreg Roach
4376692c8bSGreg Roach    /**
4476692c8bSGreg Roach     * A sentence describing what this module does.
4576692c8bSGreg Roach     *
4676692c8bSGreg Roach     * @return string
4776692c8bSGreg Roach     */
488f53f488SRico Sonntag    public function getDescription(): string
49c1010edaSGreg Roach    {
50bbb76c12SGreg Roach        /* I18N: Description of the “Most visited pages” module */
51bbb76c12SGreg Roach        return I18N::translate('A list of the pages that have been viewed the most number of times.');
528c2e8227SGreg Roach    }
538c2e8227SGreg Roach
5476692c8bSGreg Roach    /**
5576692c8bSGreg Roach     * Generate the HTML content of this block.
5676692c8bSGreg Roach     *
57e490cd80SGreg Roach     * @param Tree     $tree
5876692c8bSGreg Roach     * @param int      $block_id
5976692c8bSGreg Roach     * @param bool     $template
60727f238cSGreg Roach     * @param string[] $cfg
6176692c8bSGreg Roach     *
6276692c8bSGreg Roach     * @return string
6376692c8bSGreg Roach     */
64c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
65c1010edaSGreg Roach    {
66e490cd80SGreg Roach        global $ctype;
678c2e8227SGreg Roach
68e2a378d3SGreg Roach        $num             = $this->getBlockSetting($block_id, 'num', '10');
69e2a378d3SGreg Roach        $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
708c2e8227SGreg Roach
71c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
728c2e8227SGreg Roach
738c2e8227SGreg Roach        // load the lines from the file
748c2e8227SGreg Roach        $top10 = Database::prepare(
758c2e8227SGreg Roach            "SELECT page_parameter, page_count" .
768c2e8227SGreg Roach            " FROM `##hit_counter`" .
778c2e8227SGreg Roach            " WHERE gedcom_id = :tree_id AND page_name IN ('individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php')" .
788c2e8227SGreg Roach            " ORDER BY page_count DESC LIMIT :limit"
7913abd6f3SGreg Roach        )->execute([
8072cf66d4SGreg Roach            'tree_id' => $tree->id(),
818c2e8227SGreg Roach            'limit'   => (int) $num,
8213abd6f3SGreg Roach        ])->fetchAssoc();
838c2e8227SGreg Roach
84f7f4b984SGreg Roach        $content = '<table>';
858c2e8227SGreg Roach        foreach ($top10 as $id => $count) {
86e490cd80SGreg Roach            $record = GedcomRecord::getInstance($id, $tree);
878c2e8227SGreg Roach            if ($record && $record->canShow()) {
88a86dd8b1SGreg Roach                $content .= '<tr>';
898c2e8227SGreg Roach                if ($count_placement == 'before') {
90a86dd8b1SGreg Roach                    $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
918c2e8227SGreg Roach                }
92b1f1e4efSGreg Roach                $content .= '<td class="name2" ><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></td>';
938c2e8227SGreg Roach                if ($count_placement == 'after') {
94a86dd8b1SGreg Roach                    $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
958c2e8227SGreg Roach                }
968c2e8227SGreg Roach                $content .= '</tr>';
978c2e8227SGreg Roach            }
988c2e8227SGreg Roach        }
997a6ee1acSGreg Roach        $content .= '</table>';
1008c2e8227SGreg Roach
1018c2e8227SGreg Roach        if ($template) {
102e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
103c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
104c1010edaSGreg Roach                    'block_id' => $block_id,
105*aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
106c1010edaSGreg Roach                ]);
107397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
108c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
109c1010edaSGreg Roach                    'block_id' => $block_id,
110*aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
111c1010edaSGreg Roach                ]);
1128cbbfdceSGreg Roach            } else {
1138cbbfdceSGreg Roach                $config_url = '';
1148cbbfdceSGreg Roach            }
1158cbbfdceSGreg Roach
116147e99aaSGreg Roach            return view('modules/block-template', [
1179c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1189c6524dcSGreg Roach                'id'         => $block_id,
1198cbbfdceSGreg Roach                'config_url' => $config_url,
1209c6524dcSGreg Roach                'title'      => $this->getTitle(),
1219c6524dcSGreg Roach                'content'    => $content,
1229c6524dcSGreg Roach            ]);
1238c2e8227SGreg Roach        }
124b2ce94c6SRico Sonntag
125b2ce94c6SRico Sonntag        return $content;
1268c2e8227SGreg Roach    }
1278c2e8227SGreg Roach
12876692c8bSGreg Roach    /**
12976692c8bSGreg Roach     * Should this block load asynchronously using AJAX?
13076692c8bSGreg Roach     *
13176692c8bSGreg Roach     * Simple blocks are faster in-line, more comples ones
13276692c8bSGreg Roach     * can be loaded later.
13376692c8bSGreg Roach     *
13476692c8bSGreg Roach     * @return bool
13576692c8bSGreg Roach     */
136c1010edaSGreg Roach    public function loadAjax(): bool
137c1010edaSGreg Roach    {
1388c2e8227SGreg Roach        return true;
1398c2e8227SGreg Roach    }
1408c2e8227SGreg Roach
14176692c8bSGreg Roach    /**
14276692c8bSGreg Roach     * Can this block be shown on the user’s home page?
14376692c8bSGreg Roach     *
14476692c8bSGreg Roach     * @return bool
14576692c8bSGreg Roach     */
146c1010edaSGreg Roach    public function isUserBlock(): bool
147c1010edaSGreg Roach    {
1488c2e8227SGreg Roach        return false;
1498c2e8227SGreg Roach    }
1508c2e8227SGreg Roach
15176692c8bSGreg Roach    /**
15276692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
15376692c8bSGreg Roach     *
15476692c8bSGreg Roach     * @return bool
15576692c8bSGreg Roach     */
156c1010edaSGreg Roach    public function isGedcomBlock(): bool
157c1010edaSGreg Roach    {
1588c2e8227SGreg Roach        return true;
1598c2e8227SGreg Roach    }
1608c2e8227SGreg Roach
16176692c8bSGreg Roach    /**
162a45f9889SGreg Roach     * Update the configuration for a block.
163a45f9889SGreg Roach     *
164a45f9889SGreg Roach     * @param Request $request
165a45f9889SGreg Roach     * @param int     $block_id
166a45f9889SGreg Roach     *
167a45f9889SGreg Roach     * @return void
168a45f9889SGreg Roach     */
169a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
170a45f9889SGreg Roach    {
171a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'num', $request->get('num', '10'));
172a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'count_placement', $request->get('count_placement', 'before'));
173a45f9889SGreg Roach    }
174a45f9889SGreg Roach
175a45f9889SGreg Roach    /**
17676692c8bSGreg Roach     * An HTML form to edit block settings
17776692c8bSGreg Roach     *
178e490cd80SGreg Roach     * @param Tree $tree
17976692c8bSGreg Roach     * @param int  $block_id
180a9430be8SGreg Roach     *
181a9430be8SGreg Roach     * @return void
18276692c8bSGreg Roach     */
183a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
184c1010edaSGreg Roach    {
185e2a378d3SGreg Roach        $num             = $this->getBlockSetting($block_id, 'num', '10');
186e2a378d3SGreg Roach        $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
1878c2e8227SGreg Roach
188c385536dSGreg Roach        $options = [
189bbb76c12SGreg Roach            /* I18N: An option in a list-box */
190bbb76c12SGreg Roach            'before' => I18N::translate('before'),
191bbb76c12SGreg Roach            /* I18N: An option in a list-box */
192bbb76c12SGreg Roach            'after'  => I18N::translate('after'),
193c385536dSGreg Roach        ];
1948c2e8227SGreg Roach
195147e99aaSGreg Roach        echo view('modules/top10_pageviews/config', [
196c385536dSGreg Roach            'count_placement' => $count_placement,
197c385536dSGreg Roach            'num'             => $num,
198c385536dSGreg Roach            'options'         => $options,
199c385536dSGreg Roach        ]);
2008c2e8227SGreg Roach    }
2018c2e8227SGreg Roach}
202