18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 4*1062a142SGreg 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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1776692c8bSGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 1915d603e7SGreg Roachuse Fisharebest\Webtrees\Bootstrap4; 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 238cbbfdceSGreg Roachuse Fisharebest\Webtrees\Html; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 259c6524dcSGreg Roachuse Fisharebest\Webtrees\View; 268c2e8227SGreg Roach 278c2e8227SGreg Roach/** 288c2e8227SGreg Roach * Class TopPageViewsModule 298c2e8227SGreg Roach */ 30e2a378d3SGreg Roachclass TopPageViewsModule extends AbstractModule implements ModuleBlockInterface { 3176692c8bSGreg Roach /** 3276692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 3376692c8bSGreg Roach * 3476692c8bSGreg Roach * @return string 3576692c8bSGreg Roach */ 368c2e8227SGreg Roach public function getTitle() { 378c2e8227SGreg Roach return /* I18N: Name of a module */ I18N::translate('Most viewed pages'); 388c2e8227SGreg Roach } 398c2e8227SGreg Roach 4076692c8bSGreg Roach /** 4176692c8bSGreg Roach * A sentence describing what this module does. 4276692c8bSGreg Roach * 4376692c8bSGreg Roach * @return string 4476692c8bSGreg Roach */ 458c2e8227SGreg Roach public function getDescription() { 468c2e8227SGreg Roach return /* I18N: Description of the “Most visited pages” module */ I18N::translate('A list of the pages that have been viewed the most number of times.'); 478c2e8227SGreg Roach } 488c2e8227SGreg Roach 4976692c8bSGreg Roach /** 5076692c8bSGreg Roach * Generate the HTML content of this block. 5176692c8bSGreg Roach * 5276692c8bSGreg Roach * @param int $block_id 5376692c8bSGreg Roach * @param bool $template 54727f238cSGreg Roach * @param string[] $cfg 5576692c8bSGreg Roach * 5676692c8bSGreg Roach * @return string 5776692c8bSGreg Roach */ 58a9430be8SGreg Roach public function getBlock($block_id, $template = true, $cfg = []): string { 594b9ff166SGreg Roach global $ctype, $WT_TREE; 608c2e8227SGreg Roach 61e2a378d3SGreg Roach $num = $this->getBlockSetting($block_id, 'num', '10'); 62e2a378d3SGreg Roach $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before'); 638c2e8227SGreg Roach 6413abd6f3SGreg Roach foreach (['count_placement', 'num'] as $name) { 658c2e8227SGreg Roach if (array_key_exists($name, $cfg)) { 668c2e8227SGreg Roach $$name = $cfg[$name]; 678c2e8227SGreg Roach } 688c2e8227SGreg Roach } 698c2e8227SGreg Roach 708c2e8227SGreg Roach // load the lines from the file 718c2e8227SGreg Roach $top10 = Database::prepare( 728c2e8227SGreg Roach "SELECT page_parameter, page_count" . 738c2e8227SGreg Roach " FROM `##hit_counter`" . 748c2e8227SGreg Roach " WHERE gedcom_id = :tree_id AND page_name IN ('individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php')" . 758c2e8227SGreg Roach " ORDER BY page_count DESC LIMIT :limit" 7613abd6f3SGreg Roach )->execute([ 7724ec66ceSGreg Roach 'tree_id' => $WT_TREE->getTreeId(), 788c2e8227SGreg Roach 'limit' => (int) $num, 7913abd6f3SGreg Roach ])->fetchAssoc(); 808c2e8227SGreg Roach 81f7f4b984SGreg Roach $content = '<table>'; 828c2e8227SGreg Roach foreach ($top10 as $id => $count) { 8324ec66ceSGreg Roach $record = GedcomRecord::getInstance($id, $WT_TREE); 848c2e8227SGreg Roach if ($record && $record->canShow()) { 85a86dd8b1SGreg Roach $content .= '<tr>'; 868c2e8227SGreg Roach if ($count_placement == 'before') { 87a86dd8b1SGreg Roach $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>'; 888c2e8227SGreg Roach } 89b1f1e4efSGreg Roach $content .= '<td class="name2" ><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></td>'; 908c2e8227SGreg 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 988c2e8227SGreg Roach if ($template) { 998cbbfdceSGreg Roach if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 1008cbbfdceSGreg Roach $config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 1018cbbfdceSGreg Roach } else { 1028cbbfdceSGreg Roach $config_url = ''; 1038cbbfdceSGreg Roach } 1048cbbfdceSGreg Roach 1059c6524dcSGreg Roach return View::make('blocks/template', [ 1069c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1079c6524dcSGreg Roach 'id' => $block_id, 1088cbbfdceSGreg Roach 'config_url' => $config_url, 1099c6524dcSGreg Roach 'title' => $this->getTitle(), 1109c6524dcSGreg Roach 'content' => $content, 1119c6524dcSGreg Roach ]); 1128c2e8227SGreg Roach } else { 1138c2e8227SGreg Roach return $content; 1148c2e8227SGreg Roach } 1158c2e8227SGreg Roach } 1168c2e8227SGreg Roach 11776692c8bSGreg Roach /** 11876692c8bSGreg Roach * Should this block load asynchronously using AJAX? 11976692c8bSGreg Roach * 12076692c8bSGreg Roach * Simple blocks are faster in-line, more comples ones 12176692c8bSGreg Roach * can be loaded later. 12276692c8bSGreg Roach * 12376692c8bSGreg Roach * @return bool 12476692c8bSGreg Roach */ 125a9430be8SGreg Roach public function loadAjax(): bool { 1268c2e8227SGreg Roach return true; 1278c2e8227SGreg Roach } 1288c2e8227SGreg Roach 12976692c8bSGreg Roach /** 13076692c8bSGreg Roach * Can this block be shown on the user’s home page? 13176692c8bSGreg Roach * 13276692c8bSGreg Roach * @return bool 13376692c8bSGreg Roach */ 134a9430be8SGreg Roach public function isUserBlock(): bool { 1358c2e8227SGreg Roach return false; 1368c2e8227SGreg Roach } 1378c2e8227SGreg Roach 13876692c8bSGreg Roach /** 13976692c8bSGreg Roach * Can this block be shown on the tree’s home page? 14076692c8bSGreg Roach * 14176692c8bSGreg Roach * @return bool 14276692c8bSGreg Roach */ 143a9430be8SGreg Roach public function isGedcomBlock(): bool { 1448c2e8227SGreg Roach return true; 1458c2e8227SGreg Roach } 1468c2e8227SGreg Roach 14776692c8bSGreg Roach /** 14876692c8bSGreg Roach * An HTML form to edit block settings 14976692c8bSGreg Roach * 15076692c8bSGreg Roach * @param int $block_id 151a9430be8SGreg Roach * 152a9430be8SGreg Roach * @return void 15376692c8bSGreg Roach */ 154be9a728cSGreg Roach public function configureBlock($block_id) { 1558c2e8227SGreg Roach if (Filter::postBool('save') && Filter::checkCsrf()) { 156e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10)); 157e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'count_placement', Filter::post('count_placement', 'before|after', 'before')); 1588c2e8227SGreg Roach } 1598c2e8227SGreg Roach 160e2a378d3SGreg Roach $num = $this->getBlockSetting($block_id, 'num', '10'); 161e2a378d3SGreg Roach $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before'); 1628c2e8227SGreg Roach 16315d603e7SGreg Roach echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="num">'; 1644ba103aeSGreg Roach echo /* I18N: ... to show in a list */ I18N::translate('Number of pages'); 16515d603e7SGreg Roach echo '</label><div class="col-sm-9">'; 1668c2e8227SGreg Roach echo '<input type="text" name="num" size="2" value="', $num, '">'; 16715d603e7SGreg Roach echo '</div></div>'; 1688c2e8227SGreg Roach 16915d603e7SGreg Roach echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="count_placement">'; 170a4d5a9c2SGreg Roach echo /* I18N: Label for a configuration option */ I18N::translate('Show counts before or after name'); 17115d603e7SGreg Roach echo '</label><div class="col-sm-9">'; 17215d603e7SGreg Roach echo Bootstrap4::select(['before' => I18N::translate('before'), 'after' => I18N::translate('after')], $count_placement, ['id' => 'count_placement', 'name' => 'count_placement']); 17315d603e7SGreg Roach echo '</div></div>'; 1748c2e8227SGreg Roach } 1758c2e8227SGreg Roach} 176