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 */ 21*c1010edaSGreg Roachclass View 22*c1010edaSGreg Roach{ 23f3281ad6SGreg Roach /** 24f3281ad6SGreg Roach * @var string The (file) name of the view. 25f3281ad6SGreg Roach */ 26f3281ad6SGreg Roach private $name; 27f3281ad6SGreg Roach 28f3281ad6SGreg Roach /** 29f3281ad6SGreg Roach * @var mixed[] Data to be inserted into the view. 30f3281ad6SGreg Roach */ 31f3281ad6SGreg Roach private $data; 32f3281ad6SGreg Roach 33f3281ad6SGreg Roach /** 3408d24c7aSGreg Roach * @var mixed[] Data to be inserted into all views. 3508d24c7aSGreg Roach */ 3608d24c7aSGreg Roach private static $shared_data = []; 3708d24c7aSGreg Roach 3808d24c7aSGreg Roach /** 39ecf66805SGreg Roach * @var string Implementation of Blade "stacks". 40ecf66805SGreg Roach */ 41ecf66805SGreg Roach private static $stack; 42ecf66805SGreg Roach 43ecf66805SGreg Roach /** 44ecf66805SGreg Roach * @var array[] Implementation of Blade "stacks". 45ecf66805SGreg Roach */ 46ecf66805SGreg Roach private static $stacks = []; 47ecf66805SGreg Roach 48ecf66805SGreg Roach /** 49f3281ad6SGreg Roach * Createa view from a template name and optional data. 50f3281ad6SGreg Roach * 51f3281ad6SGreg Roach * @param $name 52f3281ad6SGreg Roach * @param array $data 53f3281ad6SGreg Roach */ 54*c1010edaSGreg Roach public function __construct($name, $data = []) 55*c1010edaSGreg Roach { 56f3281ad6SGreg Roach $this->name = $name; 57f3281ad6SGreg Roach $this->data = $data; 58f3281ad6SGreg Roach } 59f3281ad6SGreg Roach 60f3281ad6SGreg Roach /** 6108d24c7aSGreg Roach * Shared data that is available to all views. 62962e29c9SGreg Roach * 63962e29c9SGreg Roach * @param string $key 64962e29c9SGreg Roach * @param mixed $value 6508d24c7aSGreg Roach */ 66*c1010edaSGreg Roach public static function share(string $key, $value) 67*c1010edaSGreg Roach { 6808d24c7aSGreg Roach self::$shared_data[$key] = $value; 6908d24c7aSGreg Roach } 7008d24c7aSGreg Roach 7108d24c7aSGreg Roach /** 72ecf66805SGreg Roach * Implementation of Blade "stacks". 73ecf66805SGreg Roach * 74ecf66805SGreg Roach * @see https://laravel.com/docs/5.5/blade#stacks 75962e29c9SGreg Roach * 76962e29c9SGreg Roach * @param string $stack 77ecf66805SGreg Roach */ 78*c1010edaSGreg Roach public static function push(string $stack) 79*c1010edaSGreg Roach { 80ecf66805SGreg Roach self::$stack = $stack; 81ecf66805SGreg Roach ob_start(); 82ecf66805SGreg Roach } 83ecf66805SGreg Roach 84ecf66805SGreg Roach /** 85ecf66805SGreg Roach * Implementation of Blade "stacks". 86ecf66805SGreg Roach */ 87*c1010edaSGreg Roach public static function endpush() 88*c1010edaSGreg Roach { 89ecf66805SGreg Roach self::$stacks[self::$stack][] = ob_get_clean(); 90ecf66805SGreg Roach } 91ecf66805SGreg Roach 92ecf66805SGreg Roach /** 93ecf66805SGreg Roach * Implementation of Blade "stacks". 94ecf66805SGreg Roach * 95962e29c9SGreg Roach * @param string $stack 96962e29c9SGreg Roach * 97ecf66805SGreg Roach * @return string 98ecf66805SGreg Roach */ 99*c1010edaSGreg Roach public static function stack(string $stack): string 100*c1010edaSGreg Roach { 101ecf66805SGreg Roach $content = implode('', self::$stacks[$stack] ?? []); 102ecf66805SGreg Roach 103ecf66805SGreg Roach self::$stacks[$stack] = []; 104ecf66805SGreg Roach 105ecf66805SGreg Roach return $content; 106ecf66805SGreg Roach } 107ecf66805SGreg Roach 108ecf66805SGreg Roach /** 109f3281ad6SGreg Roach * Render a view. 110f3281ad6SGreg Roach * 111f3281ad6SGreg Roach * @return string 112f3281ad6SGreg Roach */ 113*c1010edaSGreg Roach public function render() 114*c1010edaSGreg Roach { 11508d24c7aSGreg Roach extract($this->data + self::$shared_data); 116f3281ad6SGreg Roach 117f3281ad6SGreg Roach ob_start(); 118bc241c54SGreg Roach // Do not use require, so we can catch errors for missing files 119bc241c54SGreg Roach include $this->getFilenameForView($this->name); 12075d70144SGreg Roach 121f3281ad6SGreg Roach return ob_get_clean(); 122f3281ad6SGreg Roach } 123f3281ad6SGreg Roach 124f3281ad6SGreg Roach /** 12575d70144SGreg Roach * Allow a theme to override the default views. 126f3281ad6SGreg Roach * 127f3281ad6SGreg Roach * @param string $view_name 128f3281ad6SGreg Roach * 12975d70144SGreg Roach * @return string 130f3281ad6SGreg Roach */ 131*c1010edaSGreg Roach public static function getFilenameForView($view_name) 132*c1010edaSGreg Roach { 133f21917b2SGreg Roach $view_file = '/resources/views/' . $view_name . '.php'; 134f21917b2SGreg Roach $theme_view = WT_ROOT . WT_THEMES_DIR . Theme::theme()->themeId() . $view_file; 13575d70144SGreg Roach 136f21917b2SGreg Roach if (is_file($theme_view)) { 137f21917b2SGreg Roach return $theme_view; 138f21917b2SGreg Roach } else { 139f21917b2SGreg Roach return WT_ROOT . $view_file; 140f21917b2SGreg Roach } 141f3281ad6SGreg Roach } 142f3281ad6SGreg Roach 143f3281ad6SGreg Roach /** 144f3281ad6SGreg Roach * Cerate and render a view in a single operation. 145f3281ad6SGreg Roach * 146f3281ad6SGreg Roach * @param string $name 147f3281ad6SGreg Roach * @param mixed[] $data 148f3281ad6SGreg Roach * 149f3281ad6SGreg Roach * @return string 150f3281ad6SGreg Roach */ 151*c1010edaSGreg Roach public static function make($name, $data = []) 152*c1010edaSGreg Roach { 153f3281ad6SGreg Roach $view = new static($name, $data); 154f3281ad6SGreg Roach 15544116e73SGreg Roach DebugBar::addView($name, $data); 15644116e73SGreg Roach 157f3281ad6SGreg Roach return $view->render(); 158f3281ad6SGreg Roach } 159f3281ad6SGreg Roach} 160