xref: /webtrees/app/Module/TopPageViewsModule.php (revision 0e62c4b8d0ec6901bfaffd1dc763db37489518a4)
1<?php
2namespace Fisharebest\Webtrees\Module;
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 */
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Database;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\GedcomRecord;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Theme;
24
25/**
26 * Class TopPageViewsModule
27 */
28class TopPageViewsModule extends AbstractModule implements ModuleBlockInterface {
29	/** {@inheritdoc} */
30	public function getTitle() {
31		return /* I18N: Name of a module */ I18N::translate('Most viewed pages');
32	}
33
34	/** {@inheritdoc} */
35	public function getDescription() {
36		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.');
37	}
38
39	/** {@inheritdoc} */
40	public function getBlock($block_id, $template = true, $cfg = null) {
41		global $ctype, $WT_TREE;
42
43		$num             = $this->getBlockSetting($block_id, 'num', '10');
44		$count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
45		$block           = $this->getBlockSetting($block_id, 'block', '0');
46
47		if ($cfg) {
48			foreach (array('count_placement', 'num', 'block') as $name) {
49				if (array_key_exists($name, $cfg)) {
50					$$name = $cfg[$name];
51				}
52			}
53		}
54
55		$id    = $this->getName() . $block_id;
56		$class = $this->getName() . '_block';
57		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
58			$title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
59		} else {
60			$title = '';
61		}
62		$title .= $this->getTitle();
63
64		$content = "";
65		// load the lines from the file
66		$top10 = Database::prepare(
67			"SELECT page_parameter, page_count" .
68			" FROM `##hit_counter`" .
69			" WHERE gedcom_id = :tree_id AND page_name IN ('individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php')" .
70			" ORDER BY page_count DESC LIMIT :limit"
71		)->execute(array(
72			'tree_id' => $WT_TREE->getTreeId(),
73			'limit'   => (int) $num,
74		))->FetchAssoc();
75
76		if ($block) {
77			$content .= "<table width=\"90%\">";
78		} else {
79			$content .= "<table>";
80		}
81		foreach ($top10 as $id => $count) {
82			$record = GedcomRecord::getInstance($id, $WT_TREE);
83			if ($record && $record->canShow()) {
84				$content .= '<tr valign="top">';
85				if ($count_placement == 'before') {
86					$content .= '<td dir="ltr" align="right">[' . $count . ']</td>';
87				}
88				$content .= '<td class="name2" ><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>';
89				if ($count_placement == 'after') {
90					$content .= '<td dir="ltr" align="right">[' . $count . ']</td>';
91				}
92				$content .= '</tr>';
93			}
94		}
95		$content .= "</table>";
96
97		if ($template) {
98			if ($block) {
99				$class .= ' small_inner_block';
100			}
101
102			return Theme::theme()->formatBlock($id, $title, $class, $content);
103		} else {
104			return $content;
105		}
106	}
107
108	/** {@inheritdoc} */
109	public function loadAjax() {
110		return true;
111	}
112
113	/** {@inheritdoc} */
114	public function isUserBlock() {
115		return false;
116	}
117
118	/** {@inheritdoc} */
119	public function isGedcomBlock() {
120		return true;
121	}
122
123	/** {@inheritdoc} */
124	public function configureBlock($block_id) {
125		if (Filter::postBool('save') && Filter::checkCsrf()) {
126			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
127			$this->setBlockSetting($block_id, 'count_placement', Filter::post('count_placement', 'before|after', 'before'));
128			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
129		}
130
131		$num             = $this->getBlockSetting($block_id, 'num', '10');
132		$count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
133		$block           = $this->getBlockSetting($block_id, 'block', '0');
134
135		echo '<tr><td class="descriptionbox wrap width33">';
136		echo I18N::translate('Number of items to show');
137		echo '</td><td class="optionbox">';
138		echo '<input type="text" name="num" size="2" value="', $num, '">';
139		echo '</td></tr>';
140
141		echo "<tr><td class=\"descriptionbox wrap width33\">";
142		echo I18N::translate('Place counts before or after name?');
143		echo "</td><td class=\"optionbox\">";
144		echo select_edit_control('count_placement', array('before' => I18N::translate('before'), 'after' => I18N::translate('after')), null, $count_placement, '');
145		echo '</td></tr>';
146
147		echo '<tr><td class="descriptionbox wrap width33">';
148		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
149		echo '</td><td class="optionbox">';
150		echo edit_field_yes_no('block', $block);
151		echo '</td></tr>';
152	}
153}
154