diff --git a/app/Util/Twig.php b/app/Util/Twig.php index ba0f437f8..33a8edbdb 100644 --- a/app/Util/Twig.php +++ b/app/Util/Twig.php @@ -208,6 +208,15 @@ class Twig { 'UTF-8' ))); + $twig->addFunction(new \Twig\TwigFunction('build_url', function (string $base, array $args): string { + $delim = str_contains($base, '?') ? '&' : '?'; + foreach ($args as $k => $v) { + $base .= $delim . $k . '=' . $v; + $delim = '&'; + } + return $base; + })); + $twig->addFunction(new \Twig\TwigFunction('donor_icon', fn($icon) => new \Twig\Markup(image_cache_encode($icon), 'UTF-8'))); $twig->addFunction(new \Twig\TwigFunction('ipaddr', fn(string $ipaddr) => new \Twig\Markup( diff --git a/templates/report_auto/index.twig b/templates/report_auto/index.twig index cd140a5c7..8d177e47b 100644 --- a/templates/report_auto/index.twig +++ b/templates/report_auto/index.twig @@ -11,7 +11,7 @@

Reports by type

@@ -21,7 +21,7 @@

Reports by user

diff --git a/tests/phpunit/Util/TwigTest.php b/tests/phpunit/Util/TwigTest.php index 5b9509e56..3eb307d0d 100644 --- a/tests/phpunit/Util/TwigTest.php +++ b/tests/phpunit/Util/TwigTest.php @@ -214,9 +214,33 @@ END; ]); global $SessionID; $SessionID = $current['SessionID']; - $this->assertStringStartsWith('', self::twig('{{ header("page") }}')->render(), 'twig-function-header'); $this->assertStringEndsWith("\n\n", self::twig('{{ footer() }}')->render(), 'twig-function-footer'); + $this->assertEquals( + 'http://localhost/?arg=abc', + self::twig('{{ build_url("http://localhost/", {"arg": arg}) }}') + ->render(['arg' => 'abc']), + 'twig-function-build-url-1' + ); + $this->assertEquals( + 'http://localhost/?arg=abc&id=10', + self::twig('{{ build_url("http://localhost/", {"arg": arg, "id": id}) }}') + ->render(['arg' => 'abc', 'id' => 10]), + 'twig-function-build-url-2' + ); + $this->assertEquals( + 'http://localhost/index?x=123&arg=def', + self::twig('{{ build_url("http://localhost/index?x=123", {"arg": arg}) }}') + ->render(['arg' => 'def']), + 'twig-function-build-url-3' + ); + $this->assertEquals( + 'http://localhost/index?x=123&arg=def&id=20', + self::twig('{{ build_url("http://localhost/index?x=123", {"arg": arg, "id": id}) }}') + ->render(['arg' => 'def', 'id' => 20]), + 'twig-function-build-url-4' + ); + $this->assertEquals( '127.0.0.1 S', self::twig('{{ ipaddr(ip) }}')->render(['ip' => '127.0.0.1']),