xref: /webtrees/app/Module/TopPageViewsModule.php (revision 6a8879fe9c08e13848197f33eb77fde0b2371399)
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
595f2ae573SGreg Roach     * @param string   $ctype
60727f238cSGreg Roach     * @param string[] $cfg
6176692c8bSGreg Roach     *
6276692c8bSGreg Roach     * @return string
6376692c8bSGreg Roach     */
645f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
65c1010edaSGreg 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([
7872cf66d4SGreg Roach            'tree_id' => $tree->id(),
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
99*6a8879feSGreg Roach        if ($ctype !== '') {
100e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
101c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
102c1010edaSGreg Roach                    'block_id' => $block_id,
103aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
104c1010edaSGreg Roach                ]);
105397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
106c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
107c1010edaSGreg Roach                    'block_id' => $block_id,
108aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
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        }
122b2ce94c6SRico Sonntag
123b2ce94c6SRico Sonntag        return $content;
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    /**
160a45f9889SGreg Roach     * Update the configuration for a block.
161a45f9889SGreg Roach     *
162a45f9889SGreg Roach     * @param Request $request
163a45f9889SGreg Roach     * @param int     $block_id
164a45f9889SGreg Roach     *
165a45f9889SGreg Roach     * @return void
166a45f9889SGreg Roach     */
167a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
168a45f9889SGreg Roach    {
169a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'num', $request->get('num', '10'));
170a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'count_placement', $request->get('count_placement', 'before'));
171a45f9889SGreg Roach    }
172a45f9889SGreg Roach
173a45f9889SGreg Roach    /**
17476692c8bSGreg Roach     * An HTML form to edit block settings
17576692c8bSGreg Roach     *
176e490cd80SGreg Roach     * @param Tree $tree
17776692c8bSGreg Roach     * @param int  $block_id
178a9430be8SGreg Roach     *
179a9430be8SGreg Roach     * @return void
18076692c8bSGreg Roach     */
181a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
182c1010edaSGreg Roach    {
183e2a378d3SGreg Roach        $num             = $this->getBlockSetting($block_id, 'num', '10');
184e2a378d3SGreg Roach        $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
1858c2e8227SGreg Roach
186c385536dSGreg Roach        $options = [
187bbb76c12SGreg Roach            /* I18N: An option in a list-box */
188bbb76c12SGreg Roach            'before' => I18N::translate('before'),
189bbb76c12SGreg Roach            /* I18N: An option in a list-box */
190bbb76c12SGreg Roach            'after'  => I18N::translate('after'),
191c385536dSGreg Roach        ];
1928c2e8227SGreg Roach
193147e99aaSGreg Roach        echo view('modules/top10_pageviews/config', [
194c385536dSGreg Roach            'count_placement' => $count_placement,
195c385536dSGreg Roach            'num'             => $num,
196c385536dSGreg Roach            'options'         => $options,
197c385536dSGreg Roach        ]);
1988c2e8227SGreg Roach    }
1998c2e8227SGreg Roach}
200