xref: /webtrees/app/Module/TopPageViewsModule.php (revision a86dd8b13b4210d1576f0fdaf5e3c9e5653c4884)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2016 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\Database;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\Functions\FunctionsEdit;
22use Fisharebest\Webtrees\GedcomRecord;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Theme;
25
26/**
27 * Class TopPageViewsModule
28 */
29class TopPageViewsModule extends AbstractModule implements ModuleBlockInterface {
30	/**
31	 * How should this module be labelled on tabs, menus, etc.?
32	 *
33	 * @return string
34	 */
35	public function getTitle() {
36		return /* I18N: Name of a module */ I18N::translate('Most viewed pages');
37	}
38
39	/**
40	 * A sentence describing what this module does.
41	 *
42	 * @return string
43	 */
44	public function getDescription() {
45		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.');
46	}
47
48	/**
49	 * Generate the HTML content of this block.
50	 *
51	 * @param int      $block_id
52	 * @param bool     $template
53	 * @param string[] $cfg
54	 *
55	 * @return string
56	 */
57	public function getBlock($block_id, $template = true, $cfg = array()) {
58		global $ctype, $WT_TREE;
59
60		$num             = $this->getBlockSetting($block_id, 'num', '10');
61		$count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
62		$block           = $this->getBlockSetting($block_id, 'block', '0');
63
64		foreach (array('count_placement', 'num', 'block') 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 = '<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>';
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(array(
87			'tree_id' => $WT_TREE->getTreeId(),
88			'limit'   => (int) $num,
89		))->fetchAssoc();
90
91		if ($block) {
92			$content .= '<table width="90%">';
93		} else {
94			$content .= '<table>';
95		}
96		foreach ($top10 as $id => $count) {
97			$record = GedcomRecord::getInstance($id, $WT_TREE);
98			if ($record && $record->canShow()) {
99				$content .= '<tr>';
100				if ($count_placement == 'before') {
101					$content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
102				}
103				$content .= '<td class="name2" ><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>';
104				if ($count_placement == 'after') {
105					$content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
106				}
107				$content .= '</tr>';
108			}
109		}
110		$content .= "</table>";
111
112		if ($template) {
113			if ($block) {
114				$class .= ' small_inner_block';
115			}
116
117			return Theme::theme()->formatBlock($id, $title, $class, $content);
118		} else {
119			return $content;
120		}
121	}
122
123	/**
124	 * Should this block load asynchronously using AJAX?
125	 *
126	 * Simple blocks are faster in-line, more comples ones
127	 * can be loaded later.
128	 *
129	 * @return bool
130	 */
131	public function loadAjax() {
132		return true;
133	}
134
135	/**
136	 * Can this block be shown on the user’s home page?
137	 *
138	 * @return bool
139	 */
140	public function isUserBlock() {
141		return false;
142	}
143
144	/**
145	 * Can this block be shown on the tree’s home page?
146	 *
147	 * @return bool
148	 */
149	public function isGedcomBlock() {
150		return true;
151	}
152
153	/**
154	 * An HTML form to edit block settings
155	 *
156	 * @param int $block_id
157	 */
158	public function configureBlock($block_id) {
159		if (Filter::postBool('save') && Filter::checkCsrf()) {
160			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
161			$this->setBlockSetting($block_id, 'count_placement', Filter::post('count_placement', 'before|after', 'before'));
162			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
163		}
164
165		$num             = $this->getBlockSetting($block_id, 'num', '10');
166		$count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
167		$block           = $this->getBlockSetting($block_id, 'block', '0');
168
169		echo '<tr><td class="descriptionbox wrap width33">';
170		echo I18N::translate('Number of items to show');
171		echo '</td><td class="optionbox">';
172		echo '<input type="text" name="num" size="2" value="', $num, '">';
173		echo '</td></tr>';
174
175		echo "<tr><td class=\"descriptionbox wrap width33\">";
176		echo I18N::translate('Place counts before or after name?');
177		echo "</td><td class=\"optionbox\">";
178		echo FunctionsEdit::selectEditControl('count_placement', array('before' => I18N::translate('before'), 'after' => I18N::translate('after')), null, $count_placement, '');
179		echo '</td></tr>';
180
181		echo '<tr><td class="descriptionbox wrap width33">';
182		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
183		echo '</td><td class="optionbox">';
184		echo FunctionsEdit::editFieldYesNo('block', $block);
185		echo '</td></tr>';
186	}
187}
188