FalViewHelper::renderStatic()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 29
rs 9.3222
1
<?php
2
namespace Qbus\Qbtools\ViewHelpers;
3
4
use TYPO3\CMS\Core\Utility\GeneralUtility;
5
use TYPO3\CMS\Core\Resource\FileRepository;
6
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
7
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
8
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
9
10
/* * *************************************************************
11
 *  Copyright notice
12
 *
13
 *  (c) 2014 Benjamin Franzke <[email protected]>, Qbus Werbeagentur GmbH
14
 *
15
 *  All rights reserved
16
 *
17
 *  This script is part of the TYPO3 project. The TYPO3 project is
18
 *  free software; you can redistribute it and/or modify
19
 *  it under the terms of the GNU General Public License as published by
20
 *  the Free Software Foundation; either version 3 of the License, or
21
 *  (at your option) any later version.
22
 *
23
 *  The GNU General Public License can be found at
24
 *  http://www.gnu.org/copyleft/gpl.html.
25
 *
26
 *  This script is distributed in the hope that it will be useful,
27
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 *  GNU General Public License for more details.
30
 *
31
 *  This copyright notice MUST APPEAR in all copies of the script!
32
 * ************************************************************* */
33
34
/**
35
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
36
 */
37
38
class FalViewHelper extends AbstractViewHelper
39
{
40
    use CompileWithRenderStatic;
41
42
    /**
43
     * @var bool
44
     */
45
    protected $escapeOutput = false;
46
47
    /**
48
     * Initialize arguments
49
     */
50
    public function initializeArguments()
51
    {
52
        $this->registerArgument('object', 'mixed', '', true);
53
        $this->registerArgument('property', 'string', '', false, 'image');
54
        $this->registerArgument('table', 'string', 'ignored if $object is a DomainObject', false, 'tt_content');
55
        $this->registerArgument('as', 'string', '', false, 'files');
56
    }
57
58
    /**
59
     * @param array $arguments
60
     * @param \Closure $renderChildrenClosure
61
     * @param RenderingContextInterface $renderingContext
62
     * @return string
63
     */
64
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
65
    {
66
        $object = $arguments['object'];
67
        $property = $arguments['property'];
68
        $table = $arguments['table'];
69
        $as = $arguments['as'];
70
71
        $uid = -1;
72
        $files = array();
73
74
        if (is_array($object)) {
75
            $uid = (int) $object['uid'];
76
            if ($object[$property] < 1) {
77
                $uid = -1;
78
            }
79
        } elseif (is_numeric($object)) {
80
            $uid = (int) $object;
81
        }
82
83
        if ($uid >= 0) {
84
            $fileRepository = GeneralUtility::makeInstance(FileRepository::class);
85
            $files = $fileRepository->findByRelation($table, $property, $uid);
86
        }
87
88
        $renderingContext->getVariableProvider()->add($as, $files);
89
        $content = $renderChildrenClosure();
90
        $renderingContext->getVariableProvider()->remove($as);
91
92
        return $content;
93
    }
94
}
95