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