xref: /webtrees/index.php (revision 1e71bdc0ba6fc5add8fed9a3beb51cfca09e47dd)
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 Tree $WT_TREE
23 */
24global $WT_TREE;
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_TREE->getTreeId());
44}
45
46$active_blocks = Module::getActiveBlocks($WT_TREE);
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		return;
64	}
65	if (array_key_exists($module_name, $active_blocks)) {
66		echo $active_blocks[$module_name]->getBlock($block_id);
67	}
68
69	return;
70}
71
72$controller = new PageController;
73if ($ctype === 'user') {
74	$controller->restrictAccess(Auth::check());
75}
76$controller
77	->setPageTitle($ctype === 'user' ? I18N::translate('My page') : $WT_TREE->getTitle())
78	->setMetaRobots('index,follow')
79	->setCanonicalUrl(WT_SCRIPT_NAME . '?ctype=' . $ctype . '&amp;ged=' . $WT_TREE->getNameHtml())
80	->pageHeader()
81	// By default jQuery modifies AJAX URLs to disable caching, causing JS libraries to be loaded many times.
82	->addInlineJavascript('jQuery.ajaxSetup({cache:true});');
83
84if ($ctype === 'user') {
85	echo '<div id="my-page">';
86	echo '<h1 class="center">', I18N::translate('My page'), '</h1>';
87} else {
88	echo '<div id="home-page">';
89}
90if ($blocks['main']) {
91	if ($blocks['side']) {
92		echo '<div id="index_main_blocks">';
93	} else {
94		echo '<div id="index_full_blocks">';
95	}
96	foreach ($blocks['main'] as $block_id => $module_name) {
97		if (array_key_exists($module_name, $active_blocks)) {
98			if (Auth::isSearchEngine() || !$active_blocks[$module_name]->loadAjax()) {
99				// Load the block directly
100				echo $active_blocks[$module_name]->getBlock($block_id);
101			} else {
102				// Load the block asynchronously
103				echo '<div id="block_', $block_id, '"><div class="loading-image">&nbsp;</div></div>';
104				$controller->addInlineJavascript(
105					'jQuery("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '");'
106				);
107			}
108		}
109	}
110	echo '</div>';
111}
112if ($blocks['side']) {
113	if ($blocks['main']) {
114		echo '<div id="index_small_blocks">';
115	} else {
116		echo '<div id="index_full_blocks">';
117	}
118	foreach ($blocks['side'] as $block_id => $module_name) {
119		if (array_key_exists($module_name, $active_blocks)) {
120			if (Auth::isSearchEngine() || !$active_blocks[$module_name]->loadAjax()) {
121				// Load the block directly
122				echo $active_blocks[$module_name]->getBlock($block_id);
123			} else {
124				// Load the block asynchronously
125				echo '<div id="block_', $block_id, '"><div class="loading-image">&nbsp;</div></div>';
126				$controller->addInlineJavascript(
127					'jQuery("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '");'
128				);
129			}
130		}
131	}
132	echo '</div>';
133}
134echo '</div>';
135