xref: /webtrees/app/Module/TopPageViewsModule.php (revision 8c2e82270a639a3acf607b432e54721116dae723)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * Class TopPageViewsModule
21 */
22class TopPageViewsModule extends Module implements ModuleBlockInterface {
23	/** {@inheritdoc} */
24	public function getTitle() {
25		return /* I18N: Name of a module */ I18N::translate('Most viewed pages');
26	}
27
28	/** {@inheritdoc} */
29	public function getDescription() {
30		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.');
31	}
32
33	/** {@inheritdoc} */
34	public function getBlock($block_id, $template = true, $cfg = null) {
35		global $ctype;
36
37		$num             = get_block_setting($block_id, 'num', '10');
38		$count_placement = get_block_setting($block_id, 'count_placement', 'before');
39		$block           = get_block_setting($block_id, 'block', '0');
40
41		if ($cfg) {
42			foreach (array('count_placement', 'num', 'block') as $name) {
43				if (array_key_exists($name, $cfg)) {
44					$$name = $cfg[$name];
45				}
46			}
47		}
48
49		$id    = $this->getName() . $block_id;
50		$class = $this->getName() . '_block';
51		if ($ctype === 'gedcom' && WT_USER_GEDCOM_ADMIN || $ctype === 'user' && Auth::check()) {
52			$title = '<i class="icon-admin" title="' . I18N::translate('Configure') . '" onclick="modalDialog(\'block_edit.php?block_id=' . $block_id . '\', \'' . $this->getTitle() . '\');"></i>';
53		} else {
54			$title = '';
55		}
56		$title .= $this->getTitle();
57
58		$content = "";
59		// load the lines from the file
60		$top10 = Database::prepare(
61			"SELECT page_parameter, page_count" .
62			" FROM `##hit_counter`" .
63			" WHERE gedcom_id = :tree_id AND page_name IN ('individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php')" .
64			" ORDER BY page_count DESC LIMIT :limit"
65		)->execute(array(
66			'tree_id' => WT_GED_ID,
67			'limit'   => (int) $num,
68		))->FetchAssoc();
69
70
71		if ($block) {
72			$content .= "<table width=\"90%\">";
73		} else {
74			$content .= "<table>";
75		}
76		foreach ($top10 as $id=>$count) {
77			$record = GedcomRecord::getInstance($id);
78			if ($record && $record->canShow()) {
79				$content .= '<tr valign="top">';
80				if ($count_placement == 'before') {
81					$content .= '<td dir="ltr" align="right">[' . $count . ']</td>';
82				}
83				$content .= '<td class="name2" ><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>';
84				if ($count_placement == 'after') {
85					$content .= '<td dir="ltr" align="right">[' . $count . ']</td>';
86				}
87				$content .= '</tr>';
88			}
89		}
90		$content .= "</table>";
91
92		if ($template) {
93			if ($block) {
94				$class .= ' small_inner_block';
95			}
96			return Theme::theme()->formatBlock($id, $title, $class, $content);
97		} else {
98			return $content;
99		}
100	}
101
102	/** {@inheritdoc} */
103	public function loadAjax() {
104		return true;
105	}
106
107	/** {@inheritdoc} */
108	public function isUserBlock() {
109		return false;
110	}
111
112	/** {@inheritdoc} */
113	public function isGedcomBlock() {
114		return true;
115	}
116
117	/** {@inheritdoc} */
118	public function configureBlock($block_id) {
119		if (Filter::postBool('save') && Filter::checkCsrf()) {
120			set_block_setting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
121			set_block_setting($block_id, 'count_placement', Filter::post('count_placement', 'before|after', 'before'));
122			set_block_setting($block_id, 'block', Filter::postBool('block'));
123		}
124
125		$num             = get_block_setting($block_id, 'num', '10');
126		$count_placement = get_block_setting($block_id, 'count_placement', 'before');
127		$block           = get_block_setting($block_id, 'block', '0');
128
129		echo '<tr><td class="descriptionbox wrap width33">';
130		echo I18N::translate('Number of items to show');
131		echo '</td><td class="optionbox">';
132		echo '<input type="text" name="num" size="2" value="', $num, '">';
133		echo '</td></tr>';
134
135		echo "<tr><td class=\"descriptionbox wrap width33\">";
136		echo I18N::translate('Place counts before or after name?');
137		echo "</td><td class=\"optionbox\">";
138		echo select_edit_control('count_placement', array('before'=> I18N::translate('before'), 'after'=> I18N::translate('after')), null, $count_placement, '');
139		echo '</td></tr>';
140
141		echo '<tr><td class="descriptionbox wrap width33">';
142		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
143		echo '</td><td class="optionbox">';
144		echo edit_field_yes_no('block', $block);
145		echo '</td></tr>';
146	}
147}
148