summaryrefslogtreecommitdiff
path: root/app/Controller/PageController.php
blob: e24aa6a2f973eeee7ddc597bc7c67b00d56a53d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
 * webtrees: online genealogy
 * Copyright (C) 2015 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
namespace Fisharebest\Webtrees\Controller;

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Database;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\Filter;
use Fisharebest\Webtrees\Functions\Functions;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Theme;

/**
 * Controller for full-page, themed HTML responses
 */
class PageController extends BaseController {
	/** @var string Some pages have multiple URLs (TODO really?) */
	private $canonical_url = '';

	/** @var string Most pages are not intended for robots */
	private $meta_robots = 'noindex,nofollow';

	/** @var string <head><title> $page_title </title></head> */
	private $page_title = WT_WEBTREES;

	/** @var bool Is this a popup window? */
	private $popup;

	/**
	 * What should this page show in the browser’s title bar?
	 *
	 * @param string  $page_title
	 *
	 * @return $this
	 */
	public function setPageTitle($page_title) {
		$this->page_title = $page_title;

		return $this;
	}

	/**
	 * Some pages will want to display this as <h1> $page_title </h1>
	 *
	 * @return string
	 */
	public function getPageTitle() {
		return $this->page_title;
	}

	/**
	 * What is the preferred URL for this page?
	 *
	 * @param string $canonical_url
	 *
	 * @return $this
	 */
	public function setCanonicalUrl($canonical_url) {
		$this->canonical_url = $canonical_url;

		return $this;
	}

	/**
	 * What is the preferred URL for this page?
	 *
	 * @return string
	 */
	public function getCanonicalUrl() {
		return $this->canonical_url;
	}

	/**
	 * Should robots index this page?
	 *
	 * @param string $meta_robots
	 *
	 * @return $this
	 */
	public function setMetaRobots($meta_robots) {
		$this->meta_robots = $meta_robots;

		return $this;
	}

	/**
	 * Should robots index this page?
	 *
	 * @return string
	 */
	public function getMetaRobots() {
		return $this->meta_robots;
	}

	/**
	 * Restrict access
	 *
	 * @param bool $condition
	 *
	 * @return $this
	 */
	public function restrictAccess($condition) {
		if ($condition !== true) {
			header('Location: ' . WT_LOGIN_URL . '?url=' . rawurlencode(Functions::getQueryUrl()));
			exit;
		}

		return $this;
	}

	/**
	 * Print the page footer, using the theme
	 */
	public function pageFooter() {
		echo
			Theme::theme()->footerContainer() .
			'<!--[if lt IE 9]><script src="' . WT_JQUERY_JS_URL . '"></script><![endif]-->' .
			'<!--[if gte IE 9]><!--><script src="' . WT_JQUERY2_JS_URL . '"></script><!--<![endif]-->' .
			'<script src="' . WT_JQUERYUI_JS_URL . '"></script>' .
			'<script src="' . WT_WEBTREES_JS_URL . '"></script>' .
			$this->getJavascript() .
			Theme::theme()->hookFooterExtraJavascript() .
			(WT_DEBUG_SQL ? Database::getQueryLog() : '') .
			'</body>' .
			'</html>' . PHP_EOL .
			'<!-- webtrees: ' . WT_VERSION . ' -->' .
			'<!-- Execution time: ' . I18N::number(microtime(true) - WT_START_TIME, 3) . ' seconds -->' .
			'<!-- Memory: ' . I18N::number(memory_get_peak_usage(true) / 1024) . ' KB -->' .
			'<!-- SQL queries: ' . I18N::number(Database::getQueryCount()) . ' -->';
	}

	/**
	 * Print the page footer, using the theme
	 * Note that popup windows are deprecated
	 */
	public function pageFooterPopupWindow() {
		echo
			Theme::theme()->footerContainerPopupWindow() .
			'<!--[if lt IE 9]><script src="' . WT_JQUERY_JS_URL . '"></script><![endif]-->' .
			'<!--[if gte IE 9]><!--><script src="' . WT_JQUERY2_JS_URL . '"></script><!--<![endif]-->' .
			'<script src="' . WT_JQUERYUI_JS_URL . '">"</script>' .
			'<script src="' . WT_WEBTREES_JS_URL . '">"</script>' .
			$this->getJavascript() .
			Theme::theme()->hookFooterExtraJavascript() .
			(WT_DEBUG_SQL ? Database::getQueryLog() : '') .
			'</body>' .
			'</html>' . PHP_EOL .
			'<!-- webtrees: ' . WT_VERSION . ' -->' .
			'<!-- Execution time: ' . I18N::number(microtime(true) - WT_START_TIME, 3) . ' seconds -->' .
			'<!-- Memory: ' . I18N::number(memory_get_peak_usage(true) / 1024) . ' KB -->' .
			'<!-- SQL queries: ' . I18N::number(Database::getQueryCount()) . ' -->';
	}

	/**
	 * Print the page header, using the theme
	 *
	 * @param bool $popup Is this a popup window
	 *
	 * @return $this
	 */
	public function pageHeader($popup = false) {
		global $WT_TREE;

		$this->popup = $popup;

		// Give Javascript access to some PHP constants
		$this->addInlineJavascript('
			var WT_STATIC_URL  = "' . Filter::escapeJs(WT_STATIC_URL) . '";
			var WT_MODULES_DIR = "' . Filter::escapeJs(WT_MODULES_DIR) . '";
			var WT_GEDCOM      = "' . Filter::escapeJs($WT_TREE ? $WT_TREE->getName() : '') . '";
			var textDirection  = "' . Filter::escapeJs(I18N::direction()) . '";
			var WT_SCRIPT_NAME = "' . Filter::escapeJs(WT_SCRIPT_NAME) . '";
			var WT_LOCALE      = "' . Filter::escapeJs(WT_LOCALE) . '";
			var WT_CSRF_TOKEN  = "' . Filter::escapeJs(Filter::getCsrfToken()) . '";
		', self::JS_PRIORITY_HIGH);

		// Temporary fix for access to main menu hover elements on android/blackberry touch devices
		$this->addInlineJavascript('
			if(navigator.userAgent.match(/Android|PlayBook/i)) {
				jQuery(".primary-menu > li > a").attr("href", "#");
			}
		');

		Theme::theme()->sendHeaders();
		echo Theme::theme()->doctype();
		echo Theme::theme()->html();
		echo Theme::theme()->head($this);

		if ($this->popup) {
			echo Theme::theme()->bodyHeaderPopupWindow();
			// We've displayed the header - display the footer automatically
			register_shutdown_function(array($this, 'pageFooterPopupWindow'), $this->popup);

		} else {
			echo Theme::theme()->bodyHeader();
			// We've displayed the header - display the footer automatically
			register_shutdown_function(array($this, 'pageFooter'), $this->popup);

		}

		// Flush the output, so the browser can render the header and load javascript
		// while we are preparing data for the page
		if (ini_get('output_buffering')) {
			ob_flush();
		}
		flush();

		return $this;
	}

	/**
	 * Get significant information from this page, to allow other pages such as
	 * charts and reports to initialise with the same records
	 *
	 * @return Individual
	 */
	public function getSignificantIndividual() {
		global $WT_TREE;

		static $individual; // Only query the DB once.

		if (!$individual && $WT_TREE->getUserPreference(Auth::user(), 'rootid')) {
			$individual = Individual::getInstance($WT_TREE->getUserPreference(Auth::user(), 'rootid'), $WT_TREE);
		}
		if (!$individual && $WT_TREE->getUserPreference(Auth::user(), 'gedcomid')) {
			$individual = Individual::getInstance($WT_TREE->getUserPreference(Auth::user(), 'gedcomid'), $WT_TREE);
		}
		if (!$individual) {
			$individual = Individual::getInstance($WT_TREE->getPreference('PEDIGREE_ROOT_ID'), $WT_TREE);
		}
		if (!$individual) {
			$individual = Individual::getInstance(
				Database::prepare(
					"SELECT MIN(i_id) FROM `##individuals` WHERE i_file=?"
				)->execute(array($WT_TREE->getTreeId()))->fetchOne(),
				$WT_TREE
			);
		}
		if (!$individual) {
			// always return a record
			$individual = new Individual('I', '0 @I@ INDI', null, $WT_TREE);
		}

		return $individual;
	}

	/**
	 * Get significant information from this page, to allow other pages such as
	 * charts and reports to initialise with the same records
	 *
	 * @return Family
	 */
	public function getSignificantFamily() {
		$individual = $this->getSignificantIndividual();
		if ($individual) {
			foreach ($individual->getChildFamilies() as $family) {
				return $family;
			}
			foreach ($individual->getSpouseFamilies() as $family) {
				return $family;
			}
		}

		// always return a record
		return new Family('F', '0 @F@ FAM', null, $individual->getTree());
	}

	/**
	 * Get significant information from this page, to allow other pages such as
	 * charts and reports to initialise with the same records
	 *
	 * @return string
	 */
	public function getSignificantSurname() {
		return '';
	}
}