Completed
Push — development ( 178cf5...deb2fc )
by Torben
13:04
created

FluidStandaloneService::injectObjectManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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
     */
34
    protected $objectManager;
35
36
    /**
37
     * The configuration manager
38
     *
39
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
40
     */
41
    protected $configurationManager;
42
43
    /**
44
     * DI for $configurationManager
45
     *
46
     * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
47
     */
48
    public function injectConfigurationManager(
49
        \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
50
    ) {
51
        $this->configurationManager = $configurationManager;
52
    }
53
54
    /**
55
     * DI for $objectManager
56
     *
57
     * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
58
     */
59
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
60
    {
61
        $this->objectManager = $objectManager;
62
    }
63
64
    /**
65
     * Returns the template folders for the given part
66
     *
67
     * @param string $part
68
     * @return array
69
     * @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
70
     */
71
    public function getTemplateFolders($part = 'template')
72
    {
73
        $extbaseConfig = $this->configurationManager->getConfiguration(
74
            ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
75
            'SfEventMgt'
76
        );
77
78
        if (!empty($extbaseConfig['view'][$part . 'RootPaths'])) {
79
            $templatePaths = $extbaseConfig['view'][$part . 'RootPaths'];
80
            $templatePaths = array_values($templatePaths);
81
        }
82
        if (empty($templatePaths)) {
83
            $path = $extbaseConfig['view'][$part . 'RootPath'];
84
            if (!empty($path)) {
85
                $templatePaths = [];
86
                $templatePaths[] = $path;
87
            }
88
        }
89
        if (empty($templatePaths)) {
90
            $templatePaths = [];
91
            $templatePaths[] = 'EXT:sf_event_mgt/Resources/Private/' . ucfirst($part) . 's/';
92
        }
93
94
        $absolutePaths = [];
95
        foreach ($templatePaths as $templatePath) {
96
            $absolutePaths[] = GeneralUtility::getFileAbsFileName($this->ensureSuffixedPath($templatePath));
97
        }
98
        return $absolutePaths;
99
    }
100
101
    /**
102
     * Makes sure the path ends with a slash
103
     *
104
     * @param string $path
105
     * @return string
106
     */
107
    protected function ensureSuffixedPath($path)
108
    {
109
        return rtrim($path, '/') . '/';
110
    }
111
112
    /**
113
     * Return path and filename for a file or path.
114
     *        Only the first existing file/path will be returned.
115
     *        respect *RootPaths and *RootPath
116
     *
117
     * @param string $pathAndFilename e.g. Email/Name.html
118
     * @param string $part "template", "partial", "layout"
119
     * @return string Filename/path
120
     */
121
    public function getTemplatePath($pathAndFilename, $part = 'template')
122
    {
123
        $matches = $this->getTemplatePaths($pathAndFilename, $part);
124
        return !empty($matches) ? end($matches) : '';
125
    }
126
127
    /**
128
     * Return path and filename for one or many files/paths.
129
     *        Only existing files/paths will be returned.
130
     *        respect *RootPaths and *RootPath
131
     *
132
     * @param string $pathAndFilename Path/filename (Email/Name.html) or path
133
     * @param string $part "template", "partial", "layout"
134
     * @return array All existing matches found
135
     */
136
    protected function getTemplatePaths($pathAndFilename, $part = 'template')
137
    {
138
        $matches = [];
139
        $absolutePaths = $this->getTemplateFolders($part);
140
        foreach ($absolutePaths as $absolutePath) {
141
            if (file_exists($absolutePath . $pathAndFilename)) {
142
                $matches[] = $absolutePath . $pathAndFilename;
143
            }
144
        }
145
        return $matches;
146
    }
147
148
    /**
149
     * Renders a fluid standlone view for the given template
150
     *
151
     * @param string $template
152
     * @param array $variables
153
     * @param string $extensionName
154
     * @param string $pluginName
155
     * @return string
156
     */
157
    public function renderTemplate($template, $variables, $extensionName = 'SfEventMgt', $pluginName = 'Pievent')
158
    {
159
        /** @var \TYPO3\CMS\Fluid\View\StandaloneView $emailView */
160
        $emailView = $this->objectManager->get(StandaloneView::class);
161
        $emailView->getRequest()->setControllerExtensionName($extensionName);
162
        $emailView->getRequest()->setPluginName($pluginName);
163
        $emailView->setFormat('html');
164
        $emailView->setLayoutRootPaths($this->getTemplateFolders('layout'));
165
        $emailView->setPartialRootPaths($this->getTemplateFolders('partial'));
166
        $emailView->setTemplatePathAndFilename($template);
167
        $emailView->assignMultiple($variables);
168
        $emailBody = $emailView->render();
169
        return $emailBody;
170
    }
171
172
    /**
173
     * Parses the given string with Fluid View
174
     *
175
     * @param string $string Any string
176
     * @param array $variables Variables
177
     * @return string Parsed string
178
     */
179
    public function parseStringFluid($string, $variables = [])
180
    {
181
        if (empty($string) || empty(self::getDatabaseConnection())) {
182
            return $string;
183
        }
184
        /** @var StandaloneView $standaloneView */
185
        $standaloneView = $this->objectManager->get(StandaloneView::class);
186
        $standaloneView->setTemplateSource($string);
187
        $standaloneView->assignMultiple($variables);
188
        return $standaloneView->render();
189
    }
190
191
    /**
192
     * @return DatabaseConnection
193
     */
194
    protected static function getDatabaseConnection()
195
    {
196
        return $GLOBALS['TYPO3_DB'];
197
    }
198
}
199