xref: /webtrees/app/View.php (revision ecf66805cdf435857ce4ed0eb577cfd1296f8989)
1f3281ad6SGreg Roach<?php
2f3281ad6SGreg Roach/**
3f3281ad6SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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	/**
33*ecf66805SGreg Roach	 * @var string Implementation of Blade "stacks".
34*ecf66805SGreg Roach	 */
35*ecf66805SGreg Roach	private static $stack;
36*ecf66805SGreg Roach
37*ecf66805SGreg Roach	/**
38*ecf66805SGreg Roach	 * @var array[] Implementation of Blade "stacks".
39*ecf66805SGreg Roach	 */
40*ecf66805SGreg Roach	private static $stacks = [];
41*ecf66805SGreg Roach
42*ecf66805SGreg Roach	/**
43f3281ad6SGreg Roach	 * Createa view from a template name and optional data.
44f3281ad6SGreg Roach	 *
45f3281ad6SGreg Roach	 * @param       $name
46f3281ad6SGreg Roach	 * @param array $data
47f3281ad6SGreg Roach	 */
48f3281ad6SGreg Roach	public function __construct($name, $data = []) {
49f3281ad6SGreg Roach		$this->name = $name;
50f3281ad6SGreg Roach		$this->data = $data;
51f3281ad6SGreg Roach	}
52f3281ad6SGreg Roach
53f3281ad6SGreg Roach	/**
54*ecf66805SGreg Roach	 * Implementation of Blade "stacks".
55*ecf66805SGreg Roach	 *
56*ecf66805SGreg Roach	 * @see https://laravel.com/docs/5.5/blade#stacks
57*ecf66805SGreg Roach	 */
58*ecf66805SGreg Roach	public static function push(string $stack) {
59*ecf66805SGreg Roach		self::$stack = $stack;
60*ecf66805SGreg Roach		ob_start();
61*ecf66805SGreg Roach	}
62*ecf66805SGreg Roach
63*ecf66805SGreg Roach	/**
64*ecf66805SGreg Roach	 * Implementation of Blade "stacks".
65*ecf66805SGreg Roach	 */
66*ecf66805SGreg Roach	public static function endpush() {
67*ecf66805SGreg Roach		self::$stacks[self::$stack][] = ob_get_clean();
68*ecf66805SGreg Roach	}
69*ecf66805SGreg Roach
70*ecf66805SGreg Roach	/**
71*ecf66805SGreg Roach	 * Implementation of Blade "stacks".
72*ecf66805SGreg Roach	 *
73*ecf66805SGreg Roach	 * @return string
74*ecf66805SGreg Roach	 */
75*ecf66805SGreg Roach	public static function stack(string $stack): string {
76*ecf66805SGreg Roach		$content = implode('', self::$stacks[$stack] ?? []);
77*ecf66805SGreg Roach
78*ecf66805SGreg Roach		self::$stacks[$stack] = [];
79*ecf66805SGreg Roach
80*ecf66805SGreg Roach		return $content;
81*ecf66805SGreg Roach	}
82*ecf66805SGreg Roach
83*ecf66805SGreg Roach	/**
84f3281ad6SGreg Roach	 * Render a view.
85f3281ad6SGreg Roach	 *
86f3281ad6SGreg Roach	 * @return string
87f3281ad6SGreg Roach	 */
88f3281ad6SGreg Roach	public function render() {
89f3281ad6SGreg Roach		extract($this->data);
90f3281ad6SGreg Roach
91f3281ad6SGreg Roach		ob_start();
9275d70144SGreg Roach		require $this->getFilenameForView($this->name);
9375d70144SGreg Roach
94f3281ad6SGreg Roach		return ob_get_clean();
95f3281ad6SGreg Roach	}
96f3281ad6SGreg Roach
97f3281ad6SGreg Roach	/**
9875d70144SGreg Roach	 * Allow a theme to override the default views.
99f3281ad6SGreg Roach	 *
100f3281ad6SGreg Roach	 * @param string $view_name
101f3281ad6SGreg Roach	 *
10275d70144SGreg Roach	 * @return string
103f3281ad6SGreg Roach	 */
10475d70144SGreg Roach	public static function getFilenameForView($view_name) {
10575d70144SGreg Roach		$view_file  = $view_name . '.php';
1065c4e0741SGreg Roach		//$theme_view = WT_THEMES_DIR . Theme::theme()->themeId() . '/resources/views/' . $view_file;
10775d70144SGreg Roach
1085c4e0741SGreg Roach		//if (file_exists($theme_view)) {
1095c4e0741SGreg Roach		//	return $theme_view;
1105c4e0741SGreg Roach		//} else {
11175d70144SGreg Roach			return WT_ROOT . 'resources/views/' . $view_file;
1125c4e0741SGreg Roach		//}
113f3281ad6SGreg Roach	}
114f3281ad6SGreg Roach
115f3281ad6SGreg Roach	/**
116f3281ad6SGreg Roach	 * Cerate and render a view in a single operation.
117f3281ad6SGreg Roach	 *
118f3281ad6SGreg Roach	 * @param string  $name
119f3281ad6SGreg Roach	 * @param mixed[] $data
120f3281ad6SGreg Roach	 *
121f3281ad6SGreg Roach	 * @return string
122f3281ad6SGreg Roach	 */
123f3281ad6SGreg Roach	public static function make($name, $data = []) {
124f3281ad6SGreg Roach		$view = new static($name, $data);
125f3281ad6SGreg Roach
12644116e73SGreg Roach		DebugBar::addView($name, $data);
12744116e73SGreg Roach
128f3281ad6SGreg Roach		return $view->render();
129f3281ad6SGreg Roach	}
130f3281ad6SGreg Roach}
131