xref: /webtrees/index.php (revision 0a016d046f63b3b1ece7ec913216782c17f0cbad)
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$ajax = (bool) Filter::get('ajax');
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 ($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				$ajax_url = Html::url('index.php', ['ctype' => $ctype, 'block_id' => $block_id, 'ajax' => 1]);
106				echo '<div class="wt-ajax-load" data-ajax-url="' . Html::escape($ajax_url) . '"></div>';
107			} else {
108				// Load the block directly
109				echo $active_blocks[$module_name]->getBlock($block_id);
110			}
111		}
112	}
113	echo '</div>';
114}
115if ($blocks['side']) {
116	if ($blocks['main']) {
117		echo '<div class="col-sm-4 wt-side-blocks">';
118	} else {
119		echo '<div class="col-sm-12 wt-side-blocks">';
120	}
121	foreach ($blocks['side'] as $block_id => $module_name) {
122		if (array_key_exists($module_name, $active_blocks)) {
123			if ($active_blocks[$module_name]->loadAjax()) {
124				// Load the block asynchronously
125				$ajax_url = Html::url('index.php', ['ctype' => $ctype, 'block_id' => $block_id, 'ajax' => 1]);
126				echo '<div class="wt-ajax-load" data-ajax-url="' . Html::escape($ajax_url) . '"></div>';
127			} else {
128				// Load the block directly
129				echo $active_blocks[$module_name]->getBlock($block_id);
130			}
131		}
132	}
133	echo '</div>';
134}
135echo '</div>';
136