xref: /webtrees/app/Module/TopPageViewsModule.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
18c2e8227SGreg Roach<?php
2*3976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2076692c8bSGreg Roach
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
2486c75e30SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
251e7a7a28SGreg Roachuse Illuminate\Support\Str;
266ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class TopPageViewsModule
308c2e8227SGreg Roach */
3137eb8894SGreg Roachclass TopPageViewsModule extends AbstractModule implements ModuleBlockInterface
32c1010edaSGreg Roach{
3349a243cbSGreg Roach    use ModuleBlockTrait;
3449a243cbSGreg Roach
3576692c8bSGreg Roach    /**
360cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
3776692c8bSGreg Roach     *
3876692c8bSGreg Roach     * @return string
3976692c8bSGreg Roach     */
4049a243cbSGreg Roach    public function title(): string
41c1010edaSGreg Roach    {
42bbb76c12SGreg Roach        /* I18N: Name of a module */
43bbb76c12SGreg Roach        return I18N::translate('Most viewed pages');
448c2e8227SGreg Roach    }
458c2e8227SGreg Roach
4676692c8bSGreg Roach    /**
4776692c8bSGreg Roach     * A sentence describing what this module does.
4876692c8bSGreg Roach     *
4976692c8bSGreg Roach     * @return string
5076692c8bSGreg Roach     */
5149a243cbSGreg Roach    public function description(): string
52c1010edaSGreg Roach    {
5386c75e30SGreg Roach        /* I18N: Description of the “Most viewed pages” module */
54bbb76c12SGreg Roach        return I18N::translate('A list of the pages that have been viewed the most number of times.');
558c2e8227SGreg Roach    }
568c2e8227SGreg Roach
5776692c8bSGreg Roach    /**
5876692c8bSGreg Roach     * Generate the HTML content of this block.
5976692c8bSGreg Roach     *
60e490cd80SGreg Roach     * @param Tree     $tree
6176692c8bSGreg Roach     * @param int      $block_id
623caaa4d2SGreg Roach     * @param string   $context
633caaa4d2SGreg Roach     * @param string[] $config
6476692c8bSGreg Roach     *
6576692c8bSGreg Roach     * @return string
6676692c8bSGreg Roach     */
673caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
68c1010edaSGreg Roach    {
69e2a378d3SGreg Roach        $num             = $this->getBlockSetting($block_id, 'num', '10');
70e2a378d3SGreg Roach        $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
718c2e8227SGreg Roach
723caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
738c2e8227SGreg Roach
7486c75e30SGreg Roach        $top10 = DB::table('hit_counter')
7586c75e30SGreg Roach            ->where('gedcom_id', '=', $tree->id())
7686c75e30SGreg Roach            ->whereIn('page_name', ['individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php'])
7786c75e30SGreg Roach            ->orderByDesc('page_count')
7886c75e30SGreg Roach            ->limit((int) $num)
7986c75e30SGreg Roach            ->pluck('page_count', 'page_parameter');
808c2e8227SGreg Roach
81f7f4b984SGreg Roach        $content = '<table>';
828c2e8227SGreg Roach        foreach ($top10 as $id => $count) {
83e490cd80SGreg Roach            $record = GedcomRecord::getInstance($id, $tree);
848c2e8227SGreg Roach            if ($record && $record->canShow()) {
85a86dd8b1SGreg Roach                $content .= '<tr>';
8622d65e5aSGreg Roach                if ($count_placement === 'before') {
87a86dd8b1SGreg Roach                    $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
888c2e8227SGreg Roach                }
8939ca88baSGreg Roach                $content .= '<td class="name2" ><a href="' . e($record->url()) . '">' . $record->fullName() . '</a></td>';
9022d65e5aSGreg Roach                if ($count_placement === 'after') {
91a86dd8b1SGreg Roach                    $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
928c2e8227SGreg Roach                }
938c2e8227SGreg Roach                $content .= '</tr>';
948c2e8227SGreg Roach            }
958c2e8227SGreg Roach        }
967a6ee1acSGreg Roach        $content .= '</table>';
978c2e8227SGreg Roach
983caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
99147e99aaSGreg Roach            return view('modules/block-template', [
1001e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1019c6524dcSGreg Roach                'id'         => $block_id,
1023caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
10349a243cbSGreg Roach                'title'      => $this->title(),
1049c6524dcSGreg Roach                'content'    => $content,
1059c6524dcSGreg Roach            ]);
1068c2e8227SGreg Roach        }
107b2ce94c6SRico Sonntag
108b2ce94c6SRico Sonntag        return $content;
1098c2e8227SGreg Roach    }
1108c2e8227SGreg Roach
11176692c8bSGreg Roach    /**
11276692c8bSGreg Roach     * Should this block load asynchronously using AJAX?
11376692c8bSGreg Roach     *
1143caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
11576692c8bSGreg Roach     *
11676692c8bSGreg Roach     * @return bool
11776692c8bSGreg Roach     */
118c1010edaSGreg Roach    public function loadAjax(): bool
119c1010edaSGreg Roach    {
1208c2e8227SGreg Roach        return true;
1218c2e8227SGreg Roach    }
1228c2e8227SGreg Roach
12376692c8bSGreg Roach    /**
12476692c8bSGreg Roach     * Can this block be shown on the user’s home page?
12576692c8bSGreg Roach     *
12676692c8bSGreg Roach     * @return bool
12776692c8bSGreg Roach     */
128c1010edaSGreg Roach    public function isUserBlock(): bool
129c1010edaSGreg Roach    {
1308c2e8227SGreg Roach        return false;
1318c2e8227SGreg Roach    }
1328c2e8227SGreg Roach
13376692c8bSGreg Roach    /**
13476692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
13576692c8bSGreg Roach     *
13676692c8bSGreg Roach     * @return bool
13776692c8bSGreg Roach     */
13863276d8fSGreg Roach    public function isTreeBlock(): bool
139c1010edaSGreg Roach    {
1408c2e8227SGreg Roach        return true;
1418c2e8227SGreg Roach    }
1428c2e8227SGreg Roach
14376692c8bSGreg Roach    /**
144a45f9889SGreg Roach     * Update the configuration for a block.
145a45f9889SGreg Roach     *
1466ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
147a45f9889SGreg Roach     * @param int     $block_id
148a45f9889SGreg Roach     *
149a45f9889SGreg Roach     * @return void
150a45f9889SGreg Roach     */
1516ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
152a45f9889SGreg Roach    {
153c50a90beSGreg Roach        $params = $request->getParsedBody();
154c50a90beSGreg Roach
155c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'num', $params['num']);
156c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'count_placement', $params['count_placement']);
157a45f9889SGreg Roach    }
158a45f9889SGreg Roach
159a45f9889SGreg 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     *
1653caaa4d2SGreg Roach     * @return string
16676692c8bSGreg Roach     */
1673caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
168c1010edaSGreg Roach    {
169e2a378d3SGreg Roach        $num             = $this->getBlockSetting($block_id, 'num', '10');
170e2a378d3SGreg Roach        $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
1718c2e8227SGreg Roach
172c385536dSGreg Roach        $options = [
173bbb76c12SGreg Roach            /* I18N: An option in a list-box */
174bbb76c12SGreg Roach            'before' => I18N::translate('before'),
175bbb76c12SGreg Roach            /* I18N: An option in a list-box */
176bbb76c12SGreg Roach            'after'  => I18N::translate('after'),
177c385536dSGreg Roach        ];
1788c2e8227SGreg Roach
1793caaa4d2SGreg Roach        return view('modules/top10_pageviews/config', [
180c385536dSGreg Roach            'count_placement' => $count_placement,
181c385536dSGreg Roach            'num'             => $num,
182c385536dSGreg Roach            'options'         => $options,
183c385536dSGreg Roach        ]);
1848c2e8227SGreg Roach    }
1858c2e8227SGreg Roach}
186