Completed
Push — development ( eabfd0...d2f7b0 )
by Torben
04:13
created

FluidStandaloneService::getDatabaseConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service;
3
4
/*
5
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use TYPO3\CMS\Core\Database\DatabaseConnection;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
14
use TYPO3\CMS\Fluid\View\StandaloneView;
15
16
/**
17
 * FluidStandaloneService
18
 *
19
 * @author Torben Hansen <[email protected]>
20
 */
21
class FluidStandaloneService
22
{
23
    /**
24
     * The object manager
25
     *
26
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
27
     */
28
    protected $objectManager;
29
30
    /**
31
     * The configuration manager
32
     *
33
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
34
     */
35
    protected $configurationManager;
36
37
    /**
38
     * DI for $configurationManager
39
     *
40
     * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
41
     */
42
    public function injectConfigurationManager(
43
        \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
44
    ) {
45
        $this->configurationManager = $configurationManager;
46
    }
47
48
    /**
49
     * DI for $objectManager
50
     *
51
     * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
52
     */
53
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
54
    {
55
        $this->objectManager = $objectManager;
56
    }
57
58
    /**
59
     * Returns the template folders for the given part
60
     *
61
     * @param string $part
62
     * @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
63
     * @return array
64
     */
65
    public function getTemplateFolders($part = 'template')
66
    {
67
        $extbaseConfig = $this->configurationManager->getConfiguration(
68
            ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
69
            'SfEventMgt'
70
        );
71
72
        if (!empty($extbaseConfig['view'][$part . 'RootPaths'])) {
73
            $templatePaths = $extbaseConfig['view'][$part . 'RootPaths'];
74
            ksort($templatePaths);
75
        }
76
        if (empty($templatePaths)) {
77
            $path = $extbaseConfig['view'][$part . 'RootPath'];
78
            if (!empty($path)) {
79
                $templatePaths = [];
80
                $templatePaths[] = $path;
81
            }
82
        }
83
        if (empty($templatePaths)) {
84
            $templatePaths = [];
85
            $templatePaths[] = 'EXT:sf_event_mgt/Resources/Private/' . ucfirst($part) . 's/';
86
        }
87
88
        $absolutePaths = [];
89
        foreach ($templatePaths as $templatePath) {
90
            $absolutePaths[] = GeneralUtility::getFileAbsFileName($this->ensureSuffixedPath($templatePath));
91
        }
92
93
        return $absolutePaths;
94
    }
95
96
    /**
97
     * Makes sure the path ends with a slash
98
     *
99
     * @param string $path
100
     * @return string
101
     */
102
    protected function ensureSuffixedPath($path)
103
    {
104
        return rtrim($path, '/') . '/';
105
    }
106
107
    /**
108
     * Renders a fluid standlone view for the given template
109
     *
110
     * @param string $template
111
     * @param array $variables
112
     * @param string $extensionName
113
     * @param string $pluginName
114
     * @return string
115
     */
116
    public function renderTemplate($template, $variables, $extensionName = 'SfEventMgt', $pluginName = 'Pievent')
117
    {
118
        /** @var \TYPO3\CMS\Fluid\View\StandaloneView $emailView */
119
        $emailView = $this->objectManager->get(StandaloneView::class);
120
        $emailView->getRequest()->setControllerExtensionName($extensionName);
121
        $emailView->getRequest()->setPluginName($pluginName);
122
        $emailView->setFormat('html');
123
        $emailView->setTemplateRootPaths($this->getTemplateFolders('template'));
124
        $emailView->setLayoutRootPaths($this->getTemplateFolders('layout'));
125
        $emailView->setPartialRootPaths($this->getTemplateFolders('partial'));
126
        $emailView->setTemplate($template);
127
        $emailView->assignMultiple($variables);
128
        $emailBody = $emailView->render();
129
130
        return $emailBody;
131
    }
132
133
    /**
134
     * Parses the given string with Fluid View
135
     *
136
     * @param string $string Any string
137
     * @param array $variables Variables
138
     * @return string Parsed string
139
     */
140
    public function parseStringFluid($string, $variables = [])
141
    {
142
        if (empty($string)) {
143
            return $string;
144
        }
145
        /** @var StandaloneView $standaloneView */
146
        $standaloneView = $this->objectManager->get(StandaloneView::class);
147
        $standaloneView->setTemplateSource($string);
148
        $standaloneView->assignMultiple($variables);
149
150
        return $standaloneView->render();
151
    }
152
}
153