Completed
Push — development ( 3fa72d...81d6fa )
by Torben
03:48
created

FluidStandaloneService::getTemplatePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 6
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\Database\DatabaseConnection;
18
use \TYPO3\CMS\Core\Utility\GeneralUtility;
19
use \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
20
use TYPO3\CMS\Fluid\View\StandaloneView;
21
22
/**
23
 * FluidStandaloneService
24
 *
25
 * @author Torben Hansen <[email protected]>
26
 */
27
class FluidStandaloneService
28
{
29
    /**
30
     * The object manager
31
     *
32
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
33
     * @inject
34
     */
35
    protected $objectManager;
36
37
    /**
38
     * The configuration manager
39
     *
40
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
41
     * @inject
42
     */
43
    protected $configurationManager;
44
45
    /**
46
     * Returns the template folders for the given part
47
     *
48
     * @param string $part
49
     * @return array
50
     * @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
51
     */
52
    public function getTemplateFolders($part = 'template')
53
    {
54
        $extbaseConfig = $this->configurationManager->getConfiguration(
55
            ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
56
            'SfEventMgt'
57
        );
58
59
        if (!empty($extbaseConfig['view'][$part . 'RootPaths'])) {
60
            $templatePaths = $extbaseConfig['view'][$part . 'RootPaths'];
61
            $templatePaths = array_values($templatePaths);
62
        }
63
        if (empty($templatePaths)) {
64
            $path = $extbaseConfig['view'][$part . 'RootPath'];
65
            if (!empty($path)) {
66
                $templatePaths = [];
67
                $templatePaths[] = $path;
68
            }
69
        }
70
        if (empty($templatePaths)) {
71
            $templatePaths = [];
72
            $templatePaths[] = 'EXT:sf_event_mgt/Resources/Private/' . ucfirst($part) . 's/';
73
        }
74
75
        $absolutePaths = [];
76
        foreach ($templatePaths as $templatePath) {
77
            $absolutePaths[] = GeneralUtility::getFileAbsFileName($this->ensureSuffixedPath($templatePath));
78
        }
79
        return $absolutePaths;
80
    }
81
82
    /**
83
     * Makes sure the path ends with a slash
84
     *
85
     * @param string $path
86
     * @return string
87
     */
88
    protected function ensureSuffixedPath($path)
89
    {
90
        return rtrim($path, '/') . '/';
91
    }
92
93
    /**
94
     * Return path and filename for a file or path.
95
     *        Only the first existing file/path will be returned.
96
     *        respect *RootPaths and *RootPath
97
     *
98
     * @param string $pathAndFilename e.g. Email/Name.html
99
     * @param string $part "template", "partial", "layout"
100
     * @return string Filename/path
101
     */
102
    public function getTemplatePath($pathAndFilename, $part = 'template')
103
    {
104
        $matches = $this->getTemplatePaths($pathAndFilename, $part);
105
        return !empty($matches) ? end($matches) : '';
106
    }
107
108
    /**
109
     * Return path and filename for one or many files/paths.
110
     *        Only existing files/paths will be returned.
111
     *        respect *RootPaths and *RootPath
112
     *
113
     * @param string $pathAndFilename Path/filename (Email/Name.html) or path
114
     * @param string $part "template", "partial", "layout"
115
     * @return array All existing matches found
116
     */
117
    protected function getTemplatePaths($pathAndFilename, $part = 'template')
118
    {
119
        $matches = [];
120
        $absolutePaths = $this->getTemplateFolders($part);
121
        foreach ($absolutePaths as $absolutePath) {
122
            if (file_exists($absolutePath . $pathAndFilename)) {
123
                $matches[] = $absolutePath . $pathAndFilename;
124
            }
125
        }
126
        return $matches;
127
    }
128
129
    /**
130
     * Renders a fluid standlone view for the given template
131
     *
132
     * @param string $template
133
     * @param array $variables
134
     * @param string $extensionName
135
     * @param string $pluginName
136
     * @return string
137
     */
138
    public function renderTemplate($template, $variables, $extensionName = 'SfEventMgt', $pluginName = 'Pievent')
139
    {
140
        /** @var \TYPO3\CMS\Fluid\View\StandaloneView $emailView */
141
        $emailView = $this->objectManager->get(StandaloneView::class);
142
        $emailView->getRequest()->setControllerExtensionName($extensionName);
143
        $emailView->getRequest()->setPluginName($pluginName);
144
        $emailView->setFormat('html');
145
        $emailView->setLayoutRootPaths($this->getTemplateFolders('layout'));
146
        $emailView->setPartialRootPaths($this->getTemplateFolders('partial'));
147
        $emailView->setTemplatePathAndFilename($template);
148
        $emailView->assignMultiple($variables);
149
        $emailBody = $emailView->render();
150
        return $emailBody;
151
    }
152
153
    /**
154
     * Parses the given string with Fluid View
155
     *
156
     * @param string $string Any string
157
     * @param array $variables Variables
158
     * @return string Parsed string
159
     */
160
    public function fluidParseString($string, $variables = [])
161
    {
162
        if (empty($string) || empty(self::getDatabaseConnection())) {
163
            return $string;
164
        }
165
        /** @var StandaloneView $standaloneView */
166
        $standaloneView = $this->objectManager->get(StandaloneView::class);
167
        $standaloneView->setTemplateSource($string);
168
        $standaloneView->assignMultiple($variables);
169
        return $standaloneView->render();
170
    }
171
172
    /**
173
     * @return DatabaseConnection
174
     */
175
    protected static function getDatabaseConnection()
176
    {
177
        return $GLOBALS['TYPO3_DB'];
178
    }
179
}
180