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