xref: /webtrees/app/Module/TopPageViewsModule.php (revision 22d65e5ad7724941da33d875027b68b86648a321)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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\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
625f2ae573SGreg Roach     * @param string   $ctype
63727f238cSGreg Roach     * @param string[] $cfg
6476692c8bSGreg Roach     *
6576692c8bSGreg Roach     * @return string
6676692c8bSGreg Roach     */
675f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): 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
72c385536dSGreg Roach        extract($cfg, 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>';
86*22d65e5aSGreg 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>';
90*22d65e5aSGreg 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
986a8879feSGreg Roach        if ($ctype !== '') {
99e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
100c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
101c1010edaSGreg Roach                    'block_id' => $block_id,
102aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
103c1010edaSGreg Roach                ]);
104397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
105c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
106c1010edaSGreg Roach                    'block_id' => $block_id,
107aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
108c1010edaSGreg Roach                ]);
1098cbbfdceSGreg Roach            } else {
1108cbbfdceSGreg Roach                $config_url = '';
1118cbbfdceSGreg Roach            }
1128cbbfdceSGreg Roach
113147e99aaSGreg Roach            return view('modules/block-template', [
1141e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1159c6524dcSGreg Roach                'id'         => $block_id,
1168cbbfdceSGreg Roach                'config_url' => $config_url,
11749a243cbSGreg Roach                'title'      => $this->title(),
1189c6524dcSGreg Roach                'content'    => $content,
1199c6524dcSGreg Roach            ]);
1208c2e8227SGreg Roach        }
121b2ce94c6SRico Sonntag
122b2ce94c6SRico Sonntag        return $content;
1238c2e8227SGreg Roach    }
1248c2e8227SGreg Roach
12576692c8bSGreg Roach    /**
12676692c8bSGreg Roach     * Should this block load asynchronously using AJAX?
12776692c8bSGreg Roach     *
12876692c8bSGreg Roach     * Simple blocks are faster in-line, more comples ones
12976692c8bSGreg Roach     * can be loaded later.
13076692c8bSGreg Roach     *
13176692c8bSGreg Roach     * @return bool
13276692c8bSGreg Roach     */
133c1010edaSGreg Roach    public function loadAjax(): bool
134c1010edaSGreg Roach    {
1358c2e8227SGreg Roach        return true;
1368c2e8227SGreg Roach    }
1378c2e8227SGreg Roach
13876692c8bSGreg Roach    /**
13976692c8bSGreg Roach     * Can this block be shown on the user’s home page?
14076692c8bSGreg Roach     *
14176692c8bSGreg Roach     * @return bool
14276692c8bSGreg Roach     */
143c1010edaSGreg Roach    public function isUserBlock(): bool
144c1010edaSGreg Roach    {
1458c2e8227SGreg Roach        return false;
1468c2e8227SGreg Roach    }
1478c2e8227SGreg Roach
14876692c8bSGreg Roach    /**
14976692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
15076692c8bSGreg Roach     *
15176692c8bSGreg Roach     * @return bool
15276692c8bSGreg Roach     */
15363276d8fSGreg Roach    public function isTreeBlock(): bool
154c1010edaSGreg Roach    {
1558c2e8227SGreg Roach        return true;
1568c2e8227SGreg Roach    }
1578c2e8227SGreg Roach
15876692c8bSGreg Roach    /**
159a45f9889SGreg Roach     * Update the configuration for a block.
160a45f9889SGreg Roach     *
1616ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
162a45f9889SGreg Roach     * @param int     $block_id
163a45f9889SGreg Roach     *
164a45f9889SGreg Roach     * @return void
165a45f9889SGreg Roach     */
1666ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
167a45f9889SGreg Roach    {
168a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'num', $request->get('num', '10'));
169a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'count_placement', $request->get('count_placement', 'before'));
170a45f9889SGreg Roach    }
171a45f9889SGreg Roach
172a45f9889SGreg Roach    /**
17376692c8bSGreg Roach     * An HTML form to edit block settings
17476692c8bSGreg Roach     *
175e490cd80SGreg Roach     * @param Tree $tree
17676692c8bSGreg Roach     * @param int  $block_id
177a9430be8SGreg Roach     *
178a9430be8SGreg Roach     * @return void
17976692c8bSGreg Roach     */
180e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
181c1010edaSGreg Roach    {
182e2a378d3SGreg Roach        $num             = $this->getBlockSetting($block_id, 'num', '10');
183e2a378d3SGreg Roach        $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
1848c2e8227SGreg Roach
185c385536dSGreg Roach        $options = [
186bbb76c12SGreg Roach            /* I18N: An option in a list-box */
187bbb76c12SGreg Roach            'before' => I18N::translate('before'),
188bbb76c12SGreg Roach            /* I18N: An option in a list-box */
189bbb76c12SGreg Roach            'after'  => I18N::translate('after'),
190c385536dSGreg Roach        ];
1918c2e8227SGreg Roach
192147e99aaSGreg Roach        echo view('modules/top10_pageviews/config', [
193c385536dSGreg Roach            'count_placement' => $count_placement,
194c385536dSGreg Roach            'num'             => $num,
195c385536dSGreg Roach            'options'         => $options,
196c385536dSGreg Roach        ]);
1978c2e8227SGreg Roach    }
1988c2e8227SGreg Roach}
199