• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FluidTYPO3 / vhs / 13566190336

27 Feb 2025 12:18PM UTC coverage: 72.127% (-0.6%) from 72.746%
13566190336

push

github

NamelessCoder
[TER] 7.1.0

5649 of 7832 relevant lines covered (72.13%)

20.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

36.45
/Classes/ViewHelpers/Resource/AbstractResourceViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Resource;
3

4
/*
5
 * This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10

11
use FluidTYPO3\Vhs\Proxy\DoctrineQueryProxy;
12
use FluidTYPO3\Vhs\Proxy\ResourceFactoryProxy;
13
use FluidTYPO3\Vhs\Traits\TagViewHelperCompatibility;
14
use FluidTYPO3\Vhs\Utility\ResourceUtility;
15
use TYPO3\CMS\Core\Database\Connection;
16
use TYPO3\CMS\Core\Database\ConnectionPool;
17
use TYPO3\CMS\Core\Resource\File;
18
use TYPO3\CMS\Core\Resource\ProcessedFile;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
21

22
/**
23
 * Base class for resource related view helpers.
24
 */
25
abstract class AbstractResourceViewHelper extends AbstractTagBasedViewHelper
26
{
27
    use TagViewHelperCompatibility;
28

29
    public function initializeArguments(): void
30
    {
31
        parent::initializeArguments();
14✔
32
        $this->registerArgument(
14✔
33
            'identifier',
14✔
34
            'mixed',
14✔
35
            'The FAL combined identifiers (either CSV, array or implementing Traversable).'
14✔
36
        );
14✔
37
        $this->registerArgument(
14✔
38
            'categories',
14✔
39
            'mixed',
14✔
40
            'The sys_category records to select the resources from (either CSV, array or implementing Traversable).'
14✔
41
        );
14✔
42
        $this->registerArgument(
14✔
43
            'treatIdAsUid',
14✔
44
            'boolean',
14✔
45
            'If TRUE, the identifier argument is treated as resource uids.',
14✔
46
            false,
14✔
47
            false
14✔
48
        );
14✔
49
        $this->registerArgument(
14✔
50
            'treatIdAsReference',
14✔
51
            'boolean',
14✔
52
            'If TRUE, the identifier argument is treated as reference uids and will be resolved to resources ' .
14✔
53
            'via sys_file_reference.',
14✔
54
            false,
14✔
55
            false
14✔
56
        );
14✔
57
    }
58

59
    /**
60
     * @param mixed $categories
61
     */
62
    public function getFiles(bool $onlyProperties = false, ?string $identifier = null, $categories = null): ?array
63
    {
64
        $identifier = $this->arrayForMixedArgument($identifier, 'identifier');
14✔
65
        $categories = $this->arrayForMixedArgument($categories, 'categories');
14✔
66
        $treatIdAsUid = (boolean) $this->arguments['treatIdAsUid'];
14✔
67
        $treatIdAsReference = (boolean) $this->arguments['treatIdAsReference'];
14✔
68

69
        if ($treatIdAsUid && $treatIdAsReference) {
14✔
70
            throw new \RuntimeException(
×
71
                'The arguments "treatIdAsUid" and "treatIdAsReference" may not both be TRUE.',
×
72
                1384604695
×
73
            );
×
74
        }
75

76
        if (empty($identifier) && empty($categories)) {
14✔
77
             return null;
14✔
78
        }
79

80
        foreach ($identifier as $key => $maybeUrl) {
×
81
            if (substr($maybeUrl, 0, 5) !== 't3://') {
×
82
                continue;
×
83
            }
84
            $parts = parse_url($maybeUrl);
×
85
            if (!isset($parts['host']) || $parts['host'] !== 'file' || !isset($parts['query'])) {
×
86
                continue;
×
87
            }
88
            parse_str($parts['query'], $queryParts);
×
89
            if (isset($queryParts['uid'])) {
×
90
                $identifier[$key] = $queryParts['uid'];
×
91
                $treatIdAsUid = true;
×
92
            }
93
        }
94

95
        $files = [];
×
96
        /** @var ResourceFactoryProxy $resourceFactory */
97
        $resourceFactory = GeneralUtility::makeInstance(ResourceFactoryProxy::class);
×
98

99
        if (!empty($categories)) {
×
100
            /** @var ConnectionPool $connectionPool */
101
            $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
×
102
            $queryBuilder = $connectionPool->getQueryBuilderForTable($this->getTablenameForSystemConfiguration());
×
103
            $queryBuilder->createNamedParameter(
×
104
                $this->getTablenameForSystemConfiguration(),
×
105
                Connection::PARAM_STR,
×
106
                ':tablenames'
×
107
            );
×
108
            $queryBuilder->createNamedParameter($categories, Connection::PARAM_STR_ARRAY, ':categories');
×
109

110
            $queryBuilder
×
111
                ->select('uid_foreign')
×
112
                ->from('sys_category_record_mm')
×
113
                ->where(
×
114
                    $queryBuilder->expr()->eq('tablenames', ':tablenames')
×
115
                )
×
116
                ->andWhere(
×
117
                    $queryBuilder->expr()->in('uid_local', ':categories')
×
118
                );
×
119
            $statement = DoctrineQueryProxy::executeQueryOnQueryBuilder($queryBuilder);
×
120
            $rows = DoctrineQueryProxy::fetchAllAssociative($statement);
×
121

122
            /** @var int[] $fileUids */
123
            $fileUids = array_unique(array_column($rows, 'uid_foreign'));
×
124

125
            if (empty($identifier)) {
×
126
                foreach ($fileUids as $fileUid) {
×
127
                    try {
128
                        $file = $resourceFactory->getFileObject($fileUid);
×
129

130
                        if ($onlyProperties) {
×
131
                            $file = ResourceUtility::getFileArray($file);
×
132
                        }
133

134
                        $files[] = $file;
×
135
                    } catch (\Exception $e) {
×
136
                        continue;
×
137
                    }
138
                }
139

140
                return $files;
×
141
            }
142
        }
143

144
        foreach ($identifier as $i) {
×
145
            try {
146
                if ($treatIdAsUid) {
×
147
                    $file = $resourceFactory->getFileObject(intval($i));
×
148
                } elseif ($treatIdAsReference) {
×
149
                    $fileReference = $resourceFactory->getFileReferenceObject(intval($i));
×
150
                    $file = $fileReference->getOriginalFile();
×
151
                } else {
152
                    $file = $resourceFactory->getFileObjectFromCombinedIdentifier($i);
×
153
                }
154

155
                /** @var File|ProcessedFile|null $file */
156
                if ($file === null) {
×
157
                    continue;
×
158
                }
159

160
                if (isset($fileUids) && !in_array($file->getUid(), $fileUids)) {
×
161
                    continue;
×
162
                }
163

164
                if ($onlyProperties) {
×
165
                    if ($file instanceof ProcessedFile) {
×
166
                        $file = $file->toArray();
×
167
                    } else {
168
                        $file = ResourceUtility::getFileArray($file);
×
169
                    }
170
                }
171

172
                $files[] = $file;
×
173
            } catch (\Exception $e) {
×
174
                continue;
×
175
            }
176
        }
177

178
        return $files;
×
179
    }
180

181
    /**
182
     * Mixed argument with CSV, array, Traversable
183
     *
184
     * @param mixed $argument
185
     */
186
    public function arrayForMixedArgument($argument, string $name): array
187
    {
188
        if (null === $argument) {
14✔
189
            $argument = $this->arguments[$name];
14✔
190
        }
191

192
        if ($argument instanceof \Traversable) {
14✔
193
            $argument = iterator_to_array($argument);
×
194
        } elseif (is_string($argument)) {
14✔
195
            $argument = GeneralUtility::trimExplode(',', $argument, true);
×
196
        } else {
197
            $argument = (array) $argument;
14✔
198
        }
199

200
        return $argument;
14✔
201
    }
202

203
    /**
204
     * This fuction decides if sys_file or sys_file_metadata is used for a query on sys_category_record_mm
205
     * This is neccessary because it depends on the TYPO3 version and the state of the extension filemetadata if
206
     * 'sys_file' should be used or 'sys_file_metadata'.
207
     */
208
    private function getTablenameForSystemConfiguration(): string
209
    {
210
        return 'sys_file_metadata';
×
211
    }
212
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc