xref: /webtrees/index.php (revision 15d603e7c7c15d20f055d3d9c38d6b133453c5be)
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	// Activate components on the page
69	echo '<script>initialize();</script>';
70
71	return;
72}
73
74// Redirect search engines to the full URL
75if (Filter::get('ctype') !== $ctype || Filter::get('ged') !== $WT_TREE->getName()) {
76	header('Location: ' . WT_BASE_URL . 'index.php?ctype=' . $ctype . '&ged=' . $WT_TREE->getNameUrl());
77
78	return;
79}
80
81$controller = new PageController;
82if ($ctype === 'user') {
83	$controller->restrictAccess(Auth::check());
84}
85$controller
86	->setPageTitle($ctype === 'user' ? I18N::translate('My page') : $WT_TREE->getTitle())
87	->setMetaRobots('index,follow')
88	->pageHeader()
89	// By default jQuery modifies AJAX URLs to disable caching, causing JS libraries to be loaded many times.
90	->addInlineJavascript('jQuery.ajaxSetup({cache:true});');
91
92if ($ctype === 'user') {
93	echo '<h2 class="text-center">', $controller->getPageTitle(), '</h2>';
94}
95
96echo '<div class="row">';
97
98if ($blocks['main']) {
99	if ($blocks['side']) {
100		echo '<div class="col-sm-8 wt-main-blocks">';
101	} else {
102		echo '<div class="col-sm-12 wt-main-blocks">';
103	}
104	foreach ($blocks['main'] as $block_id => $module_name) {
105		if (array_key_exists($module_name, $active_blocks)) {
106			if ($active_blocks[$module_name]->loadAjax()) {
107				// Load the block asynchronously
108				echo '<div id="block_', $block_id, '"><div class="loading-image"></div></div>';
109				$controller->addInlineJavascript(
110					'$("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '");'
111				);
112			} else {
113				// Load the block directly
114				echo $active_blocks[$module_name]->getBlock($block_id);
115			}
116		}
117	}
118	echo '</div>';
119}
120if ($blocks['side']) {
121	if ($blocks['main']) {
122		echo '<div class="col-sm-4 wt-side-blocks">';
123	} else {
124		echo '<div class="col-sm-12 wt-side-blocks">';
125	}
126	foreach ($blocks['side'] as $block_id => $module_name) {
127		if (array_key_exists($module_name, $active_blocks)) {
128			if ($active_blocks[$module_name]->loadAjax()) {
129				// Load the block asynchronously
130				echo '<div id="block_', $block_id, '"><div class="loading-image"></div></div>';
131				$controller->addInlineJavascript(
132					'$("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '");'
133				);
134			} else {
135				// Load the block directly
136				echo $active_blocks[$module_name]->getBlock($block_id);
137			}
138		}
139	}
140	echo '</div>';
141}
142echo '</div>';
143