xref: /webtrees/app/View.php (revision 5c4e0741d1a96c6d1d19cb42e327e99a8eac671a)
1f3281ad6SGreg Roach<?php
2f3281ad6SGreg Roach/**
3f3281ad6SGreg Roach * webtrees: online genealogy
4f3281ad6SGreg Roach * Copyright (C) 2017 webtrees development team
5f3281ad6SGreg Roach * This program is free software: you can redistribute it and/or modify
6f3281ad6SGreg Roach * it under the terms of the GNU General Public License as published by
7f3281ad6SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8f3281ad6SGreg Roach * (at your option) any later version.
9f3281ad6SGreg Roach * This program is distributed in the hope that it will be useful,
10f3281ad6SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11f3281ad6SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12f3281ad6SGreg Roach * GNU General Public License for more details.
13f3281ad6SGreg Roach * You should have received a copy of the GNU General Public License
14f3281ad6SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15f3281ad6SGreg Roach */
16f3281ad6SGreg Roachnamespace Fisharebest\Webtrees;
17f3281ad6SGreg Roach
18f3281ad6SGreg Roach/**
19f3281ad6SGreg Roach * Simple view/template class.
20f3281ad6SGreg Roach */
21f3281ad6SGreg Roachclass View {
22f3281ad6SGreg Roach	/**
23f3281ad6SGreg Roach	 * @var string The (file) name of the view.
24f3281ad6SGreg Roach	 */
25f3281ad6SGreg Roach	private $name;
26f3281ad6SGreg Roach
27f3281ad6SGreg Roach	/**
28f3281ad6SGreg Roach	 * @var mixed[] Data to be inserted into the view.
29f3281ad6SGreg Roach	 */
30f3281ad6SGreg Roach	private $data;
31f3281ad6SGreg Roach
32f3281ad6SGreg Roach	/**
33f3281ad6SGreg Roach	 * Createa view from a template name and optional data.
34f3281ad6SGreg Roach	 *
35f3281ad6SGreg Roach	 * @param       $name
36f3281ad6SGreg Roach	 * @param array $data
37f3281ad6SGreg Roach	 */
38f3281ad6SGreg Roach	public function __construct($name, $data = []) {
39f3281ad6SGreg Roach		$this->name = $name;
40f3281ad6SGreg Roach		$this->data = $data;
41f3281ad6SGreg Roach	}
42f3281ad6SGreg Roach
43f3281ad6SGreg Roach	/**
44f3281ad6SGreg Roach	 * Render a view.
45f3281ad6SGreg Roach	 *
46f3281ad6SGreg Roach	 * @return string
47f3281ad6SGreg Roach	 */
48f3281ad6SGreg Roach	public function render() {
49f3281ad6SGreg Roach		extract($this->data);
50f3281ad6SGreg Roach
51f3281ad6SGreg Roach		ob_start();
5275d70144SGreg Roach		require $this->getFilenameForView($this->name);
5375d70144SGreg Roach
54f3281ad6SGreg Roach		return ob_get_clean();
55f3281ad6SGreg Roach	}
56f3281ad6SGreg Roach
57f3281ad6SGreg Roach	/**
5875d70144SGreg Roach	 * Allow a theme to override the default views.
59f3281ad6SGreg Roach	 *
60f3281ad6SGreg Roach	 * @param string $view_name
61f3281ad6SGreg Roach	 *
6275d70144SGreg Roach	 * @return string
63f3281ad6SGreg Roach	 */
6475d70144SGreg Roach	public static function getFilenameForView($view_name) {
6575d70144SGreg Roach		$view_file  = $view_name . '.php';
66*5c4e0741SGreg Roach		//$theme_view = WT_THEMES_DIR . Theme::theme()->themeId() . '/resources/views/' . $view_file;
6775d70144SGreg Roach
68*5c4e0741SGreg Roach		//if (file_exists($theme_view)) {
69*5c4e0741SGreg Roach		//	return $theme_view;
70*5c4e0741SGreg Roach		//} else {
7175d70144SGreg Roach			return WT_ROOT . 'resources/views/' . $view_file;
72*5c4e0741SGreg Roach		//}
73f3281ad6SGreg Roach	}
74f3281ad6SGreg Roach
75f3281ad6SGreg Roach	/**
76f3281ad6SGreg Roach	 * Cerate and render a view in a single operation.
77f3281ad6SGreg Roach	 *
78f3281ad6SGreg Roach	 * @param string  $name
79f3281ad6SGreg Roach	 * @param mixed[] $data
80f3281ad6SGreg Roach	 *
81f3281ad6SGreg Roach	 * @return string
82f3281ad6SGreg Roach	 */
83f3281ad6SGreg Roach	public static function make($name, $data = []) {
84f3281ad6SGreg Roach		$view = new static($name, $data);
85f3281ad6SGreg Roach
86f3281ad6SGreg Roach		return $view->render();
87f3281ad6SGreg Roach	}
88f3281ad6SGreg Roach}
89