xref: /webtrees/app/Module/TopPageViewsModule.php (revision 15d603e7c7c15d20f055d3d9c38d6b133453c5be)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Bootstrap4;
20use Fisharebest\Webtrees\Database;
21use Fisharebest\Webtrees\Filter;
22use Fisharebest\Webtrees\FontAwesome;
23use Fisharebest\Webtrees\GedcomRecord;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Theme;
26
27/**
28 * Class TopPageViewsModule
29 */
30class TopPageViewsModule extends AbstractModule implements ModuleBlockInterface {
31	/**
32	 * How should this module be labelled on tabs, menus, etc.?
33	 *
34	 * @return string
35	 */
36	public function getTitle() {
37		return /* I18N: Name of a module */ I18N::translate('Most viewed pages');
38	}
39
40	/**
41	 * A sentence describing what this module does.
42	 *
43	 * @return string
44	 */
45	public function getDescription() {
46		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.');
47	}
48
49	/**
50	 * Generate the HTML content of this block.
51	 *
52	 * @param int      $block_id
53	 * @param bool     $template
54	 * @param string[] $cfg
55	 *
56	 * @return string
57	 */
58	public function getBlock($block_id, $template = true, $cfg = []) {
59		global $ctype, $WT_TREE;
60
61		$num             = $this->getBlockSetting($block_id, 'num', '10');
62		$count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
63
64		foreach (['count_placement', 'num'] as $name) {
65			if (array_key_exists($name, $cfg)) {
66				$$name = $cfg[$name];
67			}
68		}
69
70		$id    = $this->getName() . $block_id;
71		$class = $this->getName() . '_block';
72		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
73			$title = FontAwesome::linkIcon('preferences', I18N::translate('Preferences'), ['class' => 'btn btn-link', 'href' => 'block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype]) . ' ';
74		} else {
75			$title = '';
76		}
77		$title .= $this->getTitle();
78
79		$content = '';
80		// load the lines from the file
81		$top10 = Database::prepare(
82			"SELECT page_parameter, page_count" .
83			" FROM `##hit_counter`" .
84			" WHERE gedcom_id = :tree_id AND page_name IN ('individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php')" .
85			" ORDER BY page_count DESC LIMIT :limit"
86		)->execute([
87			'tree_id' => $WT_TREE->getTreeId(),
88			'limit'   => (int) $num,
89		])->fetchAssoc();
90
91		$content .= '<table>';
92		foreach ($top10 as $id => $count) {
93			$record = GedcomRecord::getInstance($id, $WT_TREE);
94			if ($record && $record->canShow()) {
95				$content .= '<tr>';
96				if ($count_placement == 'before') {
97					$content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
98				}
99				$content .= '<td class="name2" ><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>';
100				if ($count_placement == 'after') {
101					$content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
102				}
103				$content .= '</tr>';
104			}
105		}
106		$content .= '</table>';
107
108		if ($template) {
109			return Theme::theme()->formatBlock($id, $title, $class, $content);
110		} else {
111			return $content;
112		}
113	}
114
115	/**
116	 * Should this block load asynchronously using AJAX?
117	 *
118	 * Simple blocks are faster in-line, more comples ones
119	 * can be loaded later.
120	 *
121	 * @return bool
122	 */
123	public function loadAjax() {
124		return true;
125	}
126
127	/**
128	 * Can this block be shown on the user’s home page?
129	 *
130	 * @return bool
131	 */
132	public function isUserBlock() {
133		return false;
134	}
135
136	/**
137	 * Can this block be shown on the tree’s home page?
138	 *
139	 * @return bool
140	 */
141	public function isGedcomBlock() {
142		return true;
143	}
144
145	/**
146	 * An HTML form to edit block settings
147	 *
148	 * @param int $block_id
149	 */
150	public function configureBlock($block_id) {
151		if (Filter::postBool('save') && Filter::checkCsrf()) {
152			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
153			$this->setBlockSetting($block_id, 'count_placement', Filter::post('count_placement', 'before|after', 'before'));
154		}
155
156		$num             = $this->getBlockSetting($block_id, 'num', '10');
157		$count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
158
159		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="num">';
160		echo /* I18N: ... to show in a list */ I18N::translate('Number of pages');
161		echo '</label><div class="col-sm-9">';
162		echo '<input type="text" name="num" size="2" value="', $num, '">';
163		echo '</div></div>';
164
165		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="count_placement">';
166		echo /* I18N: Label for a configuration option */ I18N::translate('Show counts before or after name');
167		echo '</label><div class="col-sm-9">';
168		echo Bootstrap4::select(['before' => I18N::translate('before'), 'after' => I18N::translate('after')], $count_placement, ['id' => 'count_placement', 'name' => 'count_placement']);
169		echo '</div></div>';
170	}
171}
172