xref: /webtrees/index.php (revision b3d7c3220095c1070ca96a325986b254212a066e)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2015 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees;
17
18/**
19 * Defined in session.php
20 *
21 * @global Tree $WT_TREE
22 */
23global $WT_TREE;
24
25use Fisharebest\Webtrees\Controller\AjaxController;
26use Fisharebest\Webtrees\Controller\PageController;
27use Fisharebest\Webtrees\Functions\Functions;
28use Fisharebest\Webtrees\Functions\FunctionsDb;
29
30define('WT_SCRIPT_NAME', 'index.php');
31require './includes/session.php';
32
33// The only option for action is "ajax"
34$action = Filter::get('action');
35
36// The default view depends on whether we are logged in
37if (Auth::check()) {
38	$ctype = Filter::get('ctype', 'gedcom|user', 'user');
39} else {
40	$ctype = 'gedcom';
41}
42
43// Get the blocks list
44if ($ctype === 'user') {
45	$blocks = FunctionsDb::getUserBlocks(Auth::id());
46} else {
47	$blocks = FunctionsDb::getTreeBlocks($WT_TREE->getTreeId());
48}
49
50$active_blocks = Module::getActiveBlocks($WT_TREE);
51
52// The latest version is shown on the administration page.  This updates it every day.
53Functions::fetchLatestVersion();
54
55// We generate individual blocks using AJAX
56if ($action === 'ajax') {
57	$controller = new AjaxController;
58	$controller->pageHeader();
59
60	// Check we’re displaying an allowable block.
61	$block_id = Filter::getInteger('block_id');
62	if (array_key_exists($block_id, $blocks['main'])) {
63		$module_name = $blocks['main'][$block_id];
64	} elseif (array_key_exists($block_id, $blocks['side'])) {
65		$module_name = $blocks['side'][$block_id];
66	} else {
67		return;
68	}
69	if (array_key_exists($module_name, $active_blocks)) {
70		echo $active_blocks[$module_name]->getBlock($block_id);
71	}
72
73	return;
74}
75
76// Redirect search engines to the full URL
77if (Filter::get('ctype') !== $ctype || Filter::get('ged') !== $WT_TREE->getName()) {
78	header('Location: ' . WT_BASE_URL . 'index.php?ctype=' . $ctype . '&ged=' . $WT_TREE->getName());
79	return;
80}
81
82$controller = new PageController;
83if ($ctype === 'user') {
84	$controller->restrictAccess(Auth::check());
85}
86$controller
87	->setPageTitle($ctype === 'user' ? I18N::translate('My page') : $WT_TREE->getTitle())
88	->setMetaRobots('index,follow')
89	->pageHeader()
90	// By default jQuery modifies AJAX URLs to disable caching, causing JS libraries to be loaded many times.
91	->addInlineJavascript('jQuery.ajaxSetup({cache:true});');
92
93if ($ctype === 'user') {
94	echo '<div id="my-page">';
95	echo '<h1 class="center">', I18N::translate('My page'), '</h1>';
96} else {
97	echo '<div id="home-page">';
98}
99if ($blocks['main']) {
100	if ($blocks['side']) {
101		echo '<div id="index_main_blocks">';
102	} else {
103		echo '<div id="index_full_blocks">';
104	}
105	foreach ($blocks['main'] as $block_id => $module_name) {
106		if (array_key_exists($module_name, $active_blocks)) {
107			if (Auth::isSearchEngine() || !$active_blocks[$module_name]->loadAjax()) {
108				// Load the block directly
109				echo $active_blocks[$module_name]->getBlock($block_id);
110			} else {
111				// Load the block asynchronously
112				echo '<div id="block_', $block_id, '"><div class="loading-image">&nbsp;</div></div>';
113				$controller->addInlineJavascript(
114					'jQuery("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '");'
115				);
116			}
117		}
118	}
119	echo '</div>';
120}
121if ($blocks['side']) {
122	if ($blocks['main']) {
123		echo '<div id="index_small_blocks">';
124	} else {
125		echo '<div id="index_full_blocks">';
126	}
127	foreach ($blocks['side'] as $block_id => $module_name) {
128		if (array_key_exists($module_name, $active_blocks)) {
129			if (Auth::isSearchEngine() || !$active_blocks[$module_name]->loadAjax()) {
130				// Load the block directly
131				echo $active_blocks[$module_name]->getBlock($block_id);
132			} else {
133				// Load the block asynchronously
134				echo '<div id="block_', $block_id, '"><div class="loading-image">&nbsp;</div></div>';
135				$controller->addInlineJavascript(
136					'jQuery("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '");'
137				);
138			}
139		}
140	}
141	echo '</div>';
142}
143echo '</div>';
144