xref: /webtrees/app/Module/TopPageViewsModule.php (revision 3d7a8a4ca809135634f38216b734b15acff479f7)
18c2e8227SGreg Roach<?php
20e62c4b8SGreg Roachnamespace Fisharebest\Webtrees\Module;
38c2e8227SGreg Roach
48c2e8227SGreg Roach/**
58c2e8227SGreg Roach * webtrees: online genealogy
68c2e8227SGreg Roach * Copyright (C) 2015 webtrees development team
78c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
88c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
98c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
108c2e8227SGreg Roach * (at your option) any later version.
118c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
128c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
138c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148c2e8227SGreg Roach * GNU General Public License for more details.
158c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
168c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
178c2e8227SGreg Roach */
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
21*3d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
258c2e8227SGreg Roach
268c2e8227SGreg Roach/**
278c2e8227SGreg Roach * Class TopPageViewsModule
288c2e8227SGreg Roach */
29e2a378d3SGreg Roachclass TopPageViewsModule extends AbstractModule implements ModuleBlockInterface {
308c2e8227SGreg Roach	/** {@inheritdoc} */
318c2e8227SGreg Roach	public function getTitle() {
328c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Most viewed pages');
338c2e8227SGreg Roach	}
348c2e8227SGreg Roach
358c2e8227SGreg Roach	/** {@inheritdoc} */
368c2e8227SGreg Roach	public function getDescription() {
378c2e8227SGreg 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.');
388c2e8227SGreg Roach	}
398c2e8227SGreg Roach
408c2e8227SGreg Roach	/** {@inheritdoc} */
418c2e8227SGreg Roach	public function getBlock($block_id, $template = true, $cfg = null) {
424b9ff166SGreg Roach		global $ctype, $WT_TREE;
438c2e8227SGreg Roach
44e2a378d3SGreg Roach		$num             = $this->getBlockSetting($block_id, 'num', '10');
45e2a378d3SGreg Roach		$count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
46e2a378d3SGreg Roach		$block           = $this->getBlockSetting($block_id, 'block', '0');
478c2e8227SGreg Roach
488c2e8227SGreg Roach		if ($cfg) {
498c2e8227SGreg Roach			foreach (array('count_placement', 'num', 'block') as $name) {
508c2e8227SGreg Roach				if (array_key_exists($name, $cfg)) {
518c2e8227SGreg Roach					$$name = $cfg[$name];
528c2e8227SGreg Roach				}
538c2e8227SGreg Roach			}
548c2e8227SGreg Roach		}
558c2e8227SGreg Roach
568c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
578c2e8227SGreg Roach		$class = $this->getName() . '_block';
584b9ff166SGreg Roach		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
599353052eSGreg Roach			$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>';
608c2e8227SGreg Roach		} else {
618c2e8227SGreg Roach			$title = '';
628c2e8227SGreg Roach		}
638c2e8227SGreg Roach		$title .= $this->getTitle();
648c2e8227SGreg Roach
658c2e8227SGreg Roach		$content = "";
668c2e8227SGreg Roach		// load the lines from the file
678c2e8227SGreg Roach		$top10 = Database::prepare(
688c2e8227SGreg Roach			"SELECT page_parameter, page_count" .
698c2e8227SGreg Roach			" FROM `##hit_counter`" .
708c2e8227SGreg Roach			" WHERE gedcom_id = :tree_id AND page_name IN ('individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php')" .
718c2e8227SGreg Roach			" ORDER BY page_count DESC LIMIT :limit"
728c2e8227SGreg Roach		)->execute(array(
7324ec66ceSGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
748c2e8227SGreg Roach			'limit'   => (int) $num,
758c2e8227SGreg Roach		))->FetchAssoc();
768c2e8227SGreg Roach
778c2e8227SGreg Roach		if ($block) {
788c2e8227SGreg Roach			$content .= "<table width=\"90%\">";
798c2e8227SGreg Roach		} else {
808c2e8227SGreg Roach			$content .= "<table>";
818c2e8227SGreg Roach		}
828c2e8227SGreg Roach		foreach ($top10 as $id => $count) {
8324ec66ceSGreg Roach			$record = GedcomRecord::getInstance($id, $WT_TREE);
848c2e8227SGreg Roach			if ($record && $record->canShow()) {
858c2e8227SGreg Roach				$content .= '<tr valign="top">';
868c2e8227SGreg Roach				if ($count_placement == 'before') {
878c2e8227SGreg Roach					$content .= '<td dir="ltr" align="right">[' . $count . ']</td>';
888c2e8227SGreg Roach				}
898c2e8227SGreg Roach				$content .= '<td class="name2" ><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>';
908c2e8227SGreg Roach				if ($count_placement == 'after') {
918c2e8227SGreg Roach					$content .= '<td dir="ltr" align="right">[' . $count . ']</td>';
928c2e8227SGreg Roach				}
938c2e8227SGreg Roach				$content .= '</tr>';
948c2e8227SGreg Roach			}
958c2e8227SGreg Roach		}
968c2e8227SGreg Roach		$content .= "</table>";
978c2e8227SGreg Roach
988c2e8227SGreg Roach		if ($template) {
998c2e8227SGreg Roach			if ($block) {
1008c2e8227SGreg Roach				$class .= ' small_inner_block';
1018c2e8227SGreg Roach			}
102cbc1590aSGreg Roach
1038c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1048c2e8227SGreg Roach		} else {
1058c2e8227SGreg Roach			return $content;
1068c2e8227SGreg Roach		}
1078c2e8227SGreg Roach	}
1088c2e8227SGreg Roach
1098c2e8227SGreg Roach	/** {@inheritdoc} */
1108c2e8227SGreg Roach	public function loadAjax() {
1118c2e8227SGreg Roach		return true;
1128c2e8227SGreg Roach	}
1138c2e8227SGreg Roach
1148c2e8227SGreg Roach	/** {@inheritdoc} */
1158c2e8227SGreg Roach	public function isUserBlock() {
1168c2e8227SGreg Roach		return false;
1178c2e8227SGreg Roach	}
1188c2e8227SGreg Roach
1198c2e8227SGreg Roach	/** {@inheritdoc} */
1208c2e8227SGreg Roach	public function isGedcomBlock() {
1218c2e8227SGreg Roach		return true;
1228c2e8227SGreg Roach	}
1238c2e8227SGreg Roach
1248c2e8227SGreg Roach	/** {@inheritdoc} */
1258c2e8227SGreg Roach	public function configureBlock($block_id) {
1268c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
127e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
128e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'count_placement', Filter::post('count_placement', 'before|after', 'before'));
129e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
1308c2e8227SGreg Roach		}
1318c2e8227SGreg Roach
132e2a378d3SGreg Roach		$num             = $this->getBlockSetting($block_id, 'num', '10');
133e2a378d3SGreg Roach		$count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
134e2a378d3SGreg Roach		$block           = $this->getBlockSetting($block_id, 'block', '0');
1358c2e8227SGreg Roach
1368c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
1378c2e8227SGreg Roach		echo I18N::translate('Number of items to show');
1388c2e8227SGreg Roach		echo '</td><td class="optionbox">';
1398c2e8227SGreg Roach		echo '<input type="text" name="num" size="2" value="', $num, '">';
1408c2e8227SGreg Roach		echo '</td></tr>';
1418c2e8227SGreg Roach
1428c2e8227SGreg Roach		echo "<tr><td class=\"descriptionbox wrap width33\">";
1438c2e8227SGreg Roach		echo I18N::translate('Place counts before or after name?');
1448c2e8227SGreg Roach		echo "</td><td class=\"optionbox\">";
145*3d7a8a4cSGreg Roach		echo FunctionsEdit::selectEditControl('count_placement', array('before' => I18N::translate('before'), 'after' => I18N::translate('after')), null, $count_placement, '');
1468c2e8227SGreg Roach		echo '</td></tr>';
1478c2e8227SGreg Roach
1488c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
1498c2e8227SGreg Roach		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
1508c2e8227SGreg Roach		echo '</td><td class="optionbox">';
151*3d7a8a4cSGreg Roach		echo FunctionsEdit::editFieldYesNo('block', $block);
1528c2e8227SGreg Roach		echo '</td></tr>';
1538c2e8227SGreg Roach	}
1548c2e8227SGreg Roach}
155