xref: /webtrees/app/Module/TopPageViewsModule.php (revision 4ba103ae40a7ab568f7922c704062c6f9200bbda)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4369c0ce6SGreg Roach * Copyright (C) 2016 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
213d7a8a4cSGreg 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 {
3076692c8bSGreg Roach	/**
3176692c8bSGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
3276692c8bSGreg Roach	 *
3376692c8bSGreg Roach	 * @return string
3476692c8bSGreg Roach	 */
358c2e8227SGreg Roach	public function getTitle() {
368c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Most viewed pages');
378c2e8227SGreg Roach	}
388c2e8227SGreg Roach
3976692c8bSGreg Roach	/**
4076692c8bSGreg Roach	 * A sentence describing what this module does.
4176692c8bSGreg Roach	 *
4276692c8bSGreg Roach	 * @return string
4376692c8bSGreg Roach	 */
448c2e8227SGreg Roach	public function getDescription() {
458c2e8227SGreg 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.');
468c2e8227SGreg Roach	}
478c2e8227SGreg Roach
4876692c8bSGreg Roach	/**
4976692c8bSGreg Roach	 * Generate the HTML content of this block.
5076692c8bSGreg Roach	 *
5176692c8bSGreg Roach	 * @param int      $block_id
5276692c8bSGreg Roach	 * @param bool     $template
53727f238cSGreg Roach	 * @param string[] $cfg
5476692c8bSGreg Roach	 *
5576692c8bSGreg Roach	 * @return string
5676692c8bSGreg Roach	 */
5776692c8bSGreg Roach	public function getBlock($block_id, $template = true, $cfg = array()) {
584b9ff166SGreg Roach		global $ctype, $WT_TREE;
598c2e8227SGreg Roach
60e2a378d3SGreg Roach		$num             = $this->getBlockSetting($block_id, 'num', '10');
61e2a378d3SGreg Roach		$count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
628c2e8227SGreg Roach
63838cc89dSGreg Roach		foreach (array('count_placement', 'num') as $name) {
648c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
658c2e8227SGreg Roach				$$name = $cfg[$name];
668c2e8227SGreg Roach			}
678c2e8227SGreg Roach		}
688c2e8227SGreg Roach
698c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
708c2e8227SGreg Roach		$class = $this->getName() . '_block';
714b9ff166SGreg Roach		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
729353052eSGreg 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>';
738c2e8227SGreg Roach		} else {
748c2e8227SGreg Roach			$title = '';
758c2e8227SGreg Roach		}
768c2e8227SGreg Roach		$title .= $this->getTitle();
778c2e8227SGreg Roach
78a86dd8b1SGreg Roach		$content = '';
798c2e8227SGreg Roach		// load the lines from the file
808c2e8227SGreg Roach		$top10 = Database::prepare(
818c2e8227SGreg Roach			"SELECT page_parameter, page_count" .
828c2e8227SGreg Roach			" FROM `##hit_counter`" .
838c2e8227SGreg Roach			" WHERE gedcom_id = :tree_id AND page_name IN ('individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php')" .
848c2e8227SGreg Roach			" ORDER BY page_count DESC LIMIT :limit"
858c2e8227SGreg Roach		)->execute(array(
8624ec66ceSGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
878c2e8227SGreg Roach			'limit'   => (int) $num,
887820e4d7SGreg Roach		))->fetchAssoc();
898c2e8227SGreg Roach
90a86dd8b1SGreg Roach		$content .= '<table>';
918c2e8227SGreg Roach		foreach ($top10 as $id => $count) {
9224ec66ceSGreg Roach			$record = GedcomRecord::getInstance($id, $WT_TREE);
938c2e8227SGreg Roach			if ($record && $record->canShow()) {
94a86dd8b1SGreg Roach				$content .= '<tr>';
958c2e8227SGreg Roach				if ($count_placement == 'before') {
96a86dd8b1SGreg Roach					$content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
978c2e8227SGreg Roach				}
988c2e8227SGreg Roach				$content .= '<td class="name2" ><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>';
998c2e8227SGreg Roach				if ($count_placement == 'after') {
100a86dd8b1SGreg Roach					$content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>';
1018c2e8227SGreg Roach				}
1028c2e8227SGreg Roach				$content .= '</tr>';
1038c2e8227SGreg Roach			}
1048c2e8227SGreg Roach		}
1058c2e8227SGreg Roach		$content .= "</table>";
1068c2e8227SGreg Roach
1078c2e8227SGreg Roach		if ($template) {
1088c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1098c2e8227SGreg Roach		} else {
1108c2e8227SGreg Roach			return $content;
1118c2e8227SGreg Roach		}
1128c2e8227SGreg Roach	}
1138c2e8227SGreg Roach
11476692c8bSGreg Roach	/**
11576692c8bSGreg Roach	 * Should this block load asynchronously using AJAX?
11676692c8bSGreg Roach	 *
11776692c8bSGreg Roach	 * Simple blocks are faster in-line, more comples ones
11876692c8bSGreg Roach	 * can be loaded later.
11976692c8bSGreg Roach	 *
12076692c8bSGreg Roach	 * @return bool
12176692c8bSGreg Roach	 */
1228c2e8227SGreg Roach	public function loadAjax() {
1238c2e8227SGreg Roach		return true;
1248c2e8227SGreg Roach	}
1258c2e8227SGreg Roach
12676692c8bSGreg Roach	/**
12776692c8bSGreg Roach	 * Can this block be shown on the user’s home page?
12876692c8bSGreg Roach	 *
12976692c8bSGreg Roach	 * @return bool
13076692c8bSGreg Roach	 */
1318c2e8227SGreg Roach	public function isUserBlock() {
1328c2e8227SGreg Roach		return false;
1338c2e8227SGreg Roach	}
1348c2e8227SGreg Roach
13576692c8bSGreg Roach	/**
13676692c8bSGreg Roach	 * Can this block be shown on the tree’s home page?
13776692c8bSGreg Roach	 *
13876692c8bSGreg Roach	 * @return bool
13976692c8bSGreg Roach	 */
1408c2e8227SGreg Roach	public function isGedcomBlock() {
1418c2e8227SGreg Roach		return true;
1428c2e8227SGreg Roach	}
1438c2e8227SGreg Roach
14476692c8bSGreg Roach	/**
14576692c8bSGreg Roach	 * An HTML form to edit block settings
14676692c8bSGreg Roach	 *
14776692c8bSGreg Roach	 * @param int $block_id
14876692c8bSGreg Roach	 */
1498c2e8227SGreg Roach	public function configureBlock($block_id) {
1508c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
151e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
152e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'count_placement', Filter::post('count_placement', 'before|after', 'before'));
1538c2e8227SGreg Roach		}
1548c2e8227SGreg Roach
155e2a378d3SGreg Roach		$num             = $this->getBlockSetting($block_id, 'num', '10');
156e2a378d3SGreg Roach		$count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before');
1578c2e8227SGreg Roach
1588c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
159*4ba103aeSGreg Roach		echo /* I18N: ... to show in a list */ I18N::translate('Number of pages');
1608c2e8227SGreg Roach		echo '</td><td class="optionbox">';
1618c2e8227SGreg Roach		echo '<input type="text" name="num" size="2" value="', $num, '">';
1628c2e8227SGreg Roach		echo '</td></tr>';
1638c2e8227SGreg Roach
1648c2e8227SGreg Roach		echo "<tr><td class=\"descriptionbox wrap width33\">";
165a4d5a9c2SGreg Roach		echo /* I18N: Label for a configuration option */ I18N::translate('Show counts before or after name');
1668c2e8227SGreg Roach		echo "</td><td class=\"optionbox\">";
1673d7a8a4cSGreg Roach		echo FunctionsEdit::selectEditControl('count_placement', array('before' => I18N::translate('before'), 'after' => I18N::translate('after')), null, $count_placement, '');
1688c2e8227SGreg Roach		echo '</td></tr>';
1698c2e8227SGreg Roach	}
1708c2e8227SGreg Roach}
171