Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Content
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 renderTranslated
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Content View Helper to resolve translated pages.
5 * This is basically a wrapper around the PageLocator.
6 *
7 * PHP version 8
8 *
9 * Copyright (C) Villanova University 2021.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  View_Helpers
26 * @author   Mario Trojan <mario.trojan@uni-tuebingen.de>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development Wiki
29 */
30
31namespace VuFind\View\Helper\Root;
32
33use Laminas\View\Helper\AbstractHelper;
34use VuFind\ContentBlock\TemplateBased;
35
36/**
37 * Content View Helper to resolve translated pages.
38 * This is basically a wrapper around the PageLocator.
39 *
40 * @category VuFind
41 * @package  View_Helpers
42 * @author   Mario Trojan <mario.trojan@uni-tuebingen.de>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org/wiki/development Wiki
45 */
46class Content extends AbstractHelper
47{
48    /**
49     * TemplateBased instance to resolve translated pages.
50     *
51     * @var TemplateBased
52     */
53    protected $templateBasedBlock;
54
55    /**
56     * Context View Helper instance to resolve translated pages.
57     *
58     * @var Context
59     */
60    protected $contextHelper;
61
62    /**
63     * Constructor
64     *
65     * @param TemplateBased $block         TemplateBased ContentBlock
66     * @param Context       $contextHelper Context view helper
67     */
68    public function __construct(
69        TemplateBased $block,
70        Context $contextHelper
71    ) {
72        $this->templateBasedBlock = $block;
73        $this->contextHelper = $contextHelper;
74    }
75
76    /**
77     * Search for a translated template and render it using a temporary context.
78     *
79     * @param string $pageName    Name of the page
80     * @param string $pathPrefix  Path where the template should be located
81     * @param array  $context     Optional array of context variables
82     * @param array  $pageDetails Optional output variable for additional info
83     * @param string $pattern     Optional file system pattern to search page
84     *
85     * @return string            Rendered template output
86     */
87    public function renderTranslated(
88        string $pageName,
89        string $pathPrefix = 'content',
90        array $context = [],
91        ?array &$pageDetails = [],
92        ?string $pattern = null
93    ) {
94        if (!str_ends_with($pathPrefix, '/')) {
95            $pathPrefix .= '/';
96        }
97        $pathPrefix = 'templates/' . $pathPrefix;
98        $pageDetails = $this->templateBasedBlock->getContext(
99            $pathPrefix,
100            $pageName,
101            $pattern
102        );
103        return $this->contextHelper->renderInContext(
104            'ContentBlock/TemplateBased.phtml',
105            $context + $pageDetails
106        );
107    }
108}