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