xref: /webtrees/app/Helpers/functions.php (revision 8b67c11a1199191915b4af08a3841e7ce9d528b6)
11f3fb95cSGreg Roach<?php
21f3fb95cSGreg Roach/**
31f3fb95cSGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
51f3fb95cSGreg Roach * This program is free software: you can redistribute it and/or modify
61f3fb95cSGreg Roach * it under the terms of the GNU General Public License as published by
71f3fb95cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
81f3fb95cSGreg Roach * (at your option) any later version.
91f3fb95cSGreg Roach * This program is distributed in the hope that it will be useful,
101f3fb95cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f3fb95cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
121f3fb95cSGreg Roach * GNU General Public License for more details.
131f3fb95cSGreg Roach * You should have received a copy of the GNU General Public License
141f3fb95cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
151f3fb95cSGreg Roach */
16*8b67c11aSGreg Roach
1778f07ab5SGreg Roachdeclare(strict_types=1);
181f3fb95cSGreg Roach
19*8b67c11aSGreg Roachuse Fisharebest\Webtrees\Application;
20*8b67c11aSGreg Roach
21*8b67c11aSGreg Roach/**
22*8b67c11aSGreg Roach * Get the IoC container, or fetch something from it.
23*8b67c11aSGreg Roach *
24*8b67c11aSGreg Roach * @param string|null $abstract
25*8b67c11aSGreg Roach *
26*8b67c11aSGreg Roach * @return Application|mixed
27*8b67c11aSGreg Roach */
28*8b67c11aSGreg Roachfunction app(string $abstract = null)
29*8b67c11aSGreg Roach{
30*8b67c11aSGreg Roach    if ($abstract === null) {
31*8b67c11aSGreg Roach        return Application::getInstance();
32*8b67c11aSGreg Roach    } else {
33*8b67c11aSGreg Roach        return Application::getInstance()->make($abstract);
34*8b67c11aSGreg Roach    }
35*8b67c11aSGreg Roach}
36*8b67c11aSGreg Roach
371f3fb95cSGreg Roach/**
38f97c7170SGreg Roach * Generate a CSRF token form field.
39f97c7170SGreg Roach *
40f97c7170SGreg Roach * @return string
41f97c7170SGreg Roach */
42c1010edaSGreg Roachfunction csrf_field()
43c1010edaSGreg Roach{
4430ea777aSGreg Roach    return '<input type="hidden" name="csrf" value="' . e(\Fisharebest\Webtrees\Session::getCsrfToken()) . '">';
45f97c7170SGreg Roach}
46f97c7170SGreg Roach
47f97c7170SGreg Roach/**
488655ee66SGreg Roach * Get the CSRF token value.
498655ee66SGreg Roach *
508655ee66SGreg Roach * @return string
518655ee66SGreg Roach */
52c1010edaSGreg Roachfunction csrf_token()
53c1010edaSGreg Roach{
54a45f9889SGreg Roach    return \Fisharebest\Webtrees\Session::getCsrfToken();
558655ee66SGreg Roach}
568655ee66SGreg Roach
578655ee66SGreg Roach/**
581f3fb95cSGreg Roach * Generate a URL for a named route.
591f3fb95cSGreg Roach *
601f3fb95cSGreg Roach * @param string $route
611f3fb95cSGreg Roach * @param array  $parameters
62571e6fcaSGreg Roach * @param bool   $absolute
631f3fb95cSGreg Roach *
641f3fb95cSGreg Roach * @return string
651f3fb95cSGreg Roach */
66c1010edaSGreg Roachfunction route(string $route, array $parameters = [], bool $absolute = true): string
67c1010edaSGreg Roach{
681f3fb95cSGreg Roach    $parameters = ['route' => $route] + $parameters;
691f3fb95cSGreg Roach
70571e6fcaSGreg Roach    if ($absolute) {
7130ea777aSGreg Roach        return \Fisharebest\Webtrees\Html::url(WT_BASE_URL . 'index.php', $parameters);
721f3fb95cSGreg Roach    }
73b2ce94c6SRico Sonntag
7430ea777aSGreg Roach    return \Fisharebest\Webtrees\Html::url('index.php', $parameters);
75571e6fcaSGreg Roach}
768655ee66SGreg Roach
778655ee66SGreg Roach/**
788655ee66SGreg Roach * Cerate and render a view in a single operation.
798655ee66SGreg Roach *
808655ee66SGreg Roach * @param string  $name
818655ee66SGreg Roach * @param mixed[] $data
828655ee66SGreg Roach *
838655ee66SGreg Roach * @return string
848655ee66SGreg Roach */
85c1010edaSGreg Roachfunction view(string $name, array $data = [])
86c1010edaSGreg Roach{
8730ea777aSGreg Roach    return \Fisharebest\Webtrees\View::make($name, $data);
888655ee66SGreg Roach}
89