xref: /webtrees/index.php (revision b90d8acc82ea3080403ec948fc3f1106a64e74cc)
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 * Defined in session.php
21 *
22 * @global string $SEARCH_SPIDER
23 * @global Tree   $WT_TREE
24 */
25global $SEARCH_SPIDER, $WT_TREE;
26
27define('WT_SCRIPT_NAME', 'index.php');
28require './includes/session.php';
29
30// The only option for action is "ajax"
31$action = Filter::get('action');
32
33// The default view depends on whether we are logged in
34if (Auth::check()) {
35	$ctype = Filter::get('ctype', 'gedcom|user', 'user');
36} else {
37	$ctype = 'gedcom';
38}
39
40// Get the blocks list
41if ($ctype === 'user') {
42	$blocks = get_user_blocks(Auth::id());
43} else {
44	$blocks = get_gedcom_blocks(WT_GED_ID);
45}
46
47$all_blocks = Module::getActiveBlocks();
48
49// The latest version is shown on the administration page.  This updates it every day.
50fetch_latest_version();
51
52// We generate individual blocks using AJAX
53if ($action === 'ajax') {
54	$controller = new AjaxController;
55	$controller->pageHeader();
56
57	// Check we’re displaying an allowable block.
58	$block_id = Filter::getInteger('block_id');
59	if (array_key_exists($block_id, $blocks['main'])) {
60		$module_name = $blocks['main'][$block_id];
61	} elseif (array_key_exists($block_id, $blocks['side'])) {
62		$module_name = $blocks['side'][$block_id];
63	} else {
64
65		return;
66	}
67	if (array_key_exists($module_name, $all_blocks)) {
68		$class_name = __NAMESPACE__ . '\\' . $module_name . '_WT_Module';
69		$module     = new $class_name;
70		echo $module->getBlock($block_id);
71	}
72	if (WT_DEBUG_SQL) {
73		echo Database::getQueryLog();
74	}
75
76	return;
77}
78
79$controller = new PageController;
80if ($ctype === 'user') {
81	$controller->restrictAccess(Auth::check());
82}
83$controller
84	->setPageTitle($ctype === 'user' ? I18N::translate('My page') : WT_TREE_TITLE)
85	->setMetaRobots('index,follow')
86	->setCanonicalUrl(WT_SCRIPT_NAME . '?ctype=' . $ctype . '&amp;ged=' . WT_GEDCOM)
87	->pageHeader()
88	// By default jQuery modifies AJAX URLs to disable caching, causing JS libraries to be loaded many times.
89	->addInlineJavascript('jQuery.ajaxSetup({cache:true});');
90
91if ($ctype === 'user') {
92	echo '<div id="my-page">';
93	echo '<h1 class="center">', I18N::translate('My page'), '</h1>';
94} else {
95	echo '<div id="home-page">';
96}
97if ($blocks['main']) {
98	if ($blocks['side']) {
99		echo '<div id="index_main_blocks">';
100	} else {
101		echo '<div id="index_full_blocks">';
102	}
103	foreach ($blocks['main'] as $block_id => $module_name) {
104		$class_name = __NAMESPACE__ . '\\' . $module_name . '_WT_Module';
105		$module     = new $class_name;
106		if ($SEARCH_SPIDER || !$module->loadAjax()) {
107			// Load the block directly
108			echo $module->getBlock($block_id);
109		} else {
110			// Load the block asynchronously
111			echo '<div id="block_', $block_id, '"><div class="loading-image">&nbsp;</div></div>';
112			$controller->addInlineJavascript(
113				'jQuery("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '");'
114			);
115		}
116	}
117	echo '</div>';
118}
119if ($blocks['side']) {
120	if ($blocks['main']) {
121		echo '<div id="index_small_blocks">';
122	} else {
123		echo '<div id="index_full_blocks">';
124	}
125	foreach ($blocks['side'] as $block_id => $module_name) {
126		$class_name = __NAMESPACE__ . '\\' . $module_name . '_WT_Module';
127		$module     = new $class_name;
128		if ($SEARCH_SPIDER || !$module->loadAjax()) {
129			// Load the block directly
130			echo $module->getBlock($block_id);
131		} else {
132			// Load the block asynchronously
133			echo '<div id="block_', $block_id, '"><div class="loading-image">&nbsp;</div></div>';
134			$controller->addInlineJavascript(
135				'jQuery("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '");'
136			);
137		}
138	}
139	echo '</div>';
140}
141
142echo '<div id="link_change_blocks">';
143
144if ($ctype === 'user') {
145	echo '<a href="index_edit.php?user_id=' . Auth::id() . '">', I18N::translate('Change the blocks on this page'), '</a>';
146} elseif ($ctype === 'gedcom' && WT_USER_GEDCOM_ADMIN) {
147	echo '<a href="index_edit.php?gedcom_id=' . WT_GED_ID . '">', I18N::translate('Change the blocks on this page'), '</a>';
148}
149
150if ($WT_TREE->getPreference('SHOW_COUNTER')) {
151	echo '<span>' . I18N::translate('Hit count:') . ' ' . $hitCount . '</span>';
152}
153
154echo '</div></div>';
155