xref: /webtrees/app/Module/TopPageViewsModule.php (revision 39ca88ba08cefcfcaf891abfcf748f9c808eb326)
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;
25a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class TopPageViewsModule
298c2e8227SGreg Roach */
3037eb8894SGreg Roachclass TopPageViewsModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
3249a243cbSGreg Roach    use ModuleBlockTrait;
3349a243cbSGreg Roach
3476692c8bSGreg Roach    /**
3576692c8bSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
3676692c8bSGreg Roach     *
3776692c8bSGreg Roach     * @return string
3876692c8bSGreg Roach     */
3949a243cbSGreg Roach    public function title(): string
40c1010edaSGreg Roach    {
41bbb76c12SGreg Roach        /* I18N: Name of a module */
42bbb76c12SGreg Roach        return I18N::translate('Most viewed pages');
438c2e8227SGreg Roach    }
448c2e8227SGreg Roach
4576692c8bSGreg Roach    /**
4676692c8bSGreg Roach     * A sentence describing what this module does.
4776692c8bSGreg Roach     *
4876692c8bSGreg Roach     * @return string
4976692c8bSGreg Roach     */
5049a243cbSGreg Roach    public function description(): string
51c1010edaSGreg Roach    {
5286c75e30SGreg Roach        /* I18N: Description of the “Most viewed pages” module */
53bbb76c12SGreg Roach        return I18N::translate('A list of the pages that have been viewed the most number of times.');
548c2e8227SGreg Roach    }
558c2e8227SGreg Roach
5676692c8bSGreg Roach    /**
5776692c8bSGreg Roach     * Generate the HTML content of this block.
5876692c8bSGreg Roach     *
59e490cd80SGreg Roach     * @param Tree     $tree
6076692c8bSGreg Roach     * @param int      $block_id
615f2ae573SGreg Roach     * @param string   $ctype
62727f238cSGreg Roach     * @param string[] $cfg
6376692c8bSGreg Roach     *
6476692c8bSGreg Roach     * @return string
6576692c8bSGreg Roach     */
665f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
67c1010edaSGreg 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
7386c75e30SGreg Roach        $top10 = DB::table('hit_counter')
7486c75e30SGreg Roach            ->where('gedcom_id', '=', $tree->id())
7586c75e30SGreg Roach            ->whereIn('page_name', ['individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php'])
7686c75e30SGreg Roach            ->orderByDesc('page_count')
7786c75e30SGreg Roach            ->limit((int) $num)
7886c75e30SGreg Roach            ->pluck('page_count', 'page_parameter');
798c2e8227SGreg Roach
80f7f4b984SGreg Roach        $content = '<table>';
818c2e8227SGreg Roach        foreach ($top10 as $id => $count) {
82e490cd80SGreg Roach            $record = GedcomRecord::getInstance($id, $tree);
838c2e8227SGreg Roach            if ($record && $record->canShow()) {
84a86dd8b1SGreg Roach                $content .= '<tr>';
858c2e8227SGreg Roach                if ($count_placement == 'before') {
86a86dd8b1SGreg Roach                    $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
878c2e8227SGreg Roach                }
88*39ca88baSGreg Roach                $content .= '<td class="name2" ><a href="' . e($record->url()) . '">' . $record->fullName() . '</a></td>';
898c2e8227SGreg Roach                if ($count_placement == 'after') {
90a86dd8b1SGreg Roach                    $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
918c2e8227SGreg Roach                }
928c2e8227SGreg Roach                $content .= '</tr>';
938c2e8227SGreg Roach            }
948c2e8227SGreg Roach        }
957a6ee1acSGreg Roach        $content .= '</table>';
968c2e8227SGreg Roach
976a8879feSGreg Roach        if ($ctype !== '') {
98e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
99c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
100c1010edaSGreg Roach                    'block_id' => $block_id,
101aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
102c1010edaSGreg Roach                ]);
103397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
104c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
105c1010edaSGreg Roach                    'block_id' => $block_id,
106aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
107c1010edaSGreg Roach                ]);
1088cbbfdceSGreg Roach            } else {
1098cbbfdceSGreg Roach                $config_url = '';
1108cbbfdceSGreg Roach            }
1118cbbfdceSGreg Roach
112147e99aaSGreg Roach            return view('modules/block-template', [
11326684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1149c6524dcSGreg Roach                'id'         => $block_id,
1158cbbfdceSGreg Roach                'config_url' => $config_url,
11649a243cbSGreg Roach                'title'      => $this->title(),
1179c6524dcSGreg Roach                'content'    => $content,
1189c6524dcSGreg Roach            ]);
1198c2e8227SGreg Roach        }
120b2ce94c6SRico Sonntag
121b2ce94c6SRico Sonntag        return $content;
1228c2e8227SGreg Roach    }
1238c2e8227SGreg Roach
12476692c8bSGreg Roach    /**
12576692c8bSGreg Roach     * Should this block load asynchronously using AJAX?
12676692c8bSGreg Roach     *
12776692c8bSGreg Roach     * Simple blocks are faster in-line, more comples ones
12876692c8bSGreg Roach     * can be loaded later.
12976692c8bSGreg Roach     *
13076692c8bSGreg Roach     * @return bool
13176692c8bSGreg Roach     */
132c1010edaSGreg Roach    public function loadAjax(): bool
133c1010edaSGreg Roach    {
1348c2e8227SGreg Roach        return true;
1358c2e8227SGreg Roach    }
1368c2e8227SGreg Roach
13776692c8bSGreg Roach    /**
13876692c8bSGreg Roach     * Can this block be shown on the user’s home page?
13976692c8bSGreg Roach     *
14076692c8bSGreg Roach     * @return bool
14176692c8bSGreg Roach     */
142c1010edaSGreg Roach    public function isUserBlock(): bool
143c1010edaSGreg Roach    {
1448c2e8227SGreg Roach        return false;
1458c2e8227SGreg Roach    }
1468c2e8227SGreg Roach
14776692c8bSGreg Roach    /**
14876692c8bSGreg Roach     * Can this block be shown on the tree’s home page?
14976692c8bSGreg Roach     *
15076692c8bSGreg Roach     * @return bool
15176692c8bSGreg Roach     */
15263276d8fSGreg Roach    public function isTreeBlock(): bool
153c1010edaSGreg Roach    {
1548c2e8227SGreg Roach        return true;
1558c2e8227SGreg Roach    }
1568c2e8227SGreg Roach
15776692c8bSGreg Roach    /**
158a45f9889SGreg Roach     * Update the configuration for a block.
159a45f9889SGreg Roach     *
160a45f9889SGreg Roach     * @param Request $request
161a45f9889SGreg Roach     * @param int     $block_id
162a45f9889SGreg Roach     *
163a45f9889SGreg Roach     * @return void
164a45f9889SGreg Roach     */
165a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
166a45f9889SGreg Roach    {
167a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'num', $request->get('num', '10'));
168a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'count_placement', $request->get('count_placement', 'before'));
169a45f9889SGreg Roach    }
170a45f9889SGreg Roach
171a45f9889SGreg Roach    /**
17276692c8bSGreg Roach     * An HTML form to edit block settings
17376692c8bSGreg Roach     *
174e490cd80SGreg Roach     * @param Tree $tree
17576692c8bSGreg Roach     * @param int  $block_id
176a9430be8SGreg Roach     *
177a9430be8SGreg Roach     * @return void
17876692c8bSGreg Roach     */
179a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
180c1010edaSGreg Roach    {
181e2a378d3SGreg Roach        $num             = $this->getBlockSetting($block_id, 'num', '10');
182e2a378d3SGreg Roach        $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
1838c2e8227SGreg Roach
184c385536dSGreg Roach        $options = [
185bbb76c12SGreg Roach            /* I18N: An option in a list-box */
186bbb76c12SGreg Roach            'before' => I18N::translate('before'),
187bbb76c12SGreg Roach            /* I18N: An option in a list-box */
188bbb76c12SGreg Roach            'after'  => I18N::translate('after'),
189c385536dSGreg Roach        ];
1908c2e8227SGreg Roach
191147e99aaSGreg Roach        echo view('modules/top10_pageviews/config', [
192c385536dSGreg Roach            'count_placement' => $count_placement,
193c385536dSGreg Roach            'num'             => $num,
194c385536dSGreg Roach            'options'         => $options,
195c385536dSGreg Roach        ]);
1968c2e8227SGreg Roach    }
1978c2e8227SGreg Roach}
198