xref: /webtrees/index.php (revision c87f84e39cd6c9aa4823bd922a5ee041f53af776)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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
18use Fisharebest\Webtrees\Controller\AjaxController;
19use Fisharebest\Webtrees\Controller\PageController;
20use Fisharebest\Webtrees\Functions\Functions;
21use Fisharebest\Webtrees\Functions\FunctionsDb;
22
23/** @global Tree $WT_TREE */
24global $WT_TREE;
25
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 = FunctionsDb::getUserBlocks(Auth::id());
41} else {
42	$blocks = FunctionsDb::getTreeBlocks($WT_TREE->getTreeId());
43}
44
45$active_blocks = Module::getActiveBlocks($WT_TREE);
46
47// The latest version is shown on the administration page. This updates it every day.
48Functions::fetchLatestVersion();
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		return;
63	}
64	if (array_key_exists($module_name, $active_blocks)) {
65		echo $active_blocks[$module_name]->getBlock($block_id);
66	}
67
68	return;
69}
70
71// Redirect search engines to the full URL
72if (Filter::get('ctype') !== $ctype || Filter::get('ged') !== $WT_TREE->getName()) {
73	header('Location: index.php?ctype=' . $ctype . '&ged=' . $WT_TREE->getNameUrl());
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->getTitle())
84	->setMetaRobots('index,follow')
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 '<h2 class="text-center">', $controller->getPageTitle(), '</h2>';
91}
92
93echo '<div class="row">';
94
95if ($blocks['main']) {
96	if ($blocks['side']) {
97		echo '<div class="col-sm-8 wt-main-blocks">';
98	} else {
99		echo '<div class="col-sm-12 wt-main-blocks">';
100	}
101	foreach ($blocks['main'] as $block_id => $module_name) {
102		if (array_key_exists($module_name, $active_blocks)) {
103			if ($active_blocks[$module_name]->loadAjax()) {
104				// Load the block asynchronously
105				echo '<div id="block_', $block_id, '"><div class="loading-image"></div></div>';
106				$controller->addInlineJavascript(
107					'$("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '");'
108				);
109			} else {
110				// Load the block directly
111				echo $active_blocks[$module_name]->getBlock($block_id);
112			}
113		}
114	}
115	echo '</div>';
116}
117if ($blocks['side']) {
118	if ($blocks['main']) {
119		echo '<div class="col-sm-4 wt-side-blocks">';
120	} else {
121		echo '<div class="col-sm-12 wt-side-blocks">';
122	}
123	foreach ($blocks['side'] as $block_id => $module_name) {
124		if (array_key_exists($module_name, $active_blocks)) {
125			if ($active_blocks[$module_name]->loadAjax()) {
126				// Load the block asynchronously
127				echo '<div id="block_', $block_id, '"><div class="loading-image"></div></div>';
128				$controller->addInlineJavascript(
129					'$("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '");'
130				);
131			} else {
132				// Load the block directly
133				echo $active_blocks[$module_name]->getBlock($block_id);
134			}
135		}
136	}
137	echo '</div>';
138}
139echo '</div>';
140