/
FixedPostVarsConfigurationUtility.php
210 lines (179 loc) · 6.13 KB
/
FixedPostVarsConfigurationUtility.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
namespace T3kit\themeT3kit\Utility;
use Doctrine\DBAL\Query\QueryBuilder;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class FixedPostVarsConfigurationUtility
* @package T3kit\themeT3kit\Utility
*/
class FixedPostVarsConfigurationUtility
{
/**
* Update configuration file of fixed post vars
*
* @return void
*/
public function updateConfiguration()
{
$filePath = $this->getSaveFilePath();
if (!$this->canWriteConfiguration($filePath)) {
throw new \RuntimeException(
$filePath . ' is not writable.',
1485349703
);
}
$configurations = $this->getConfiguration();
$varNameToPageFixedUids = [];
$pages = $this->getFixedPagesUids();
$content = '<?php' . LF;
$content .= '$init = function () {' . LF;
// generate fixed post var config vars
foreach ($pages as $page) {
$key = $page['tx_themet3kit_fixed_post_var_conf'];
if (array_key_exists($key, $configurations)) {
if (!array_key_exists($key, $varNameToPageFixedUids)) {
$varName = '$' . GeneralUtility::underscoredToLowerCamelCase($key);
$content .= ' ' . $varName . ' = ';
$content .= $this->fixIndent(
ArrayUtility::arrayExport(
$configurations[$key]['configuration']
)
);
$content .= ';' . LF;
// save var name
$varNameToPageFixedUids[$key] = [
'varName' => $varName,
'fixedUids' => [$page['uid']]
];
} else {
$varNameToPageFixedUids[$key]['fixedUids'][] = $page['uid'];
}
}
}
$fixedPostVarLine = ' ';
$fixedPostVarLine .= '$GLOBALS[\'TYPO3_CONF_VARS\'][\'EXTCONF\'][\'realurl\'][\'_DEFAULT\'][\'fixedPostVars\']';
foreach ($varNameToPageFixedUids as $key => $varNameToPageFixedUid) {
// first write configuration
$content .= $fixedPostVarLine;
$content .= sprintf(
'[\'%s\'] = %s;',
$key,
$varNameToPageFixedUid['varName']
);
$content .= LF;
// now add it for each page
foreach ($varNameToPageFixedUid['fixedUids'] as $fixedUid) {
$content .= $fixedPostVarLine;
$content .= sprintf(
'[\'%s\'] = \'%s\';',
$fixedUid,
$key
);
$content .= LF;
}
$content .= LF;
}
$content .= '};' . LF;
$content .= '$init();' . LF;
$content .= 'unset($init);' . LF;
GeneralUtility::writeFile($filePath, $content, true);
}
/**
* Fix indent for configuration array
*
* @param $string
* @return string
*/
protected function fixIndent($string)
{
$lines = explode(PHP_EOL, $string);
foreach ($lines as &$line) {
$line = ' ' . $line;
}
return ltrim(implode(LF, $lines));
}
/**
* Generate configuration array with associative keys
*
* @return array
*/
protected function getConfiguration()
{
$configuration = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['theme_t3kit']['fixedPostVars'];
$processedConfiguration = [];
if (is_array($configuration)) {
foreach ($configuration as $item) {
$processedConfiguration[$item['key']] = $item;
}
}
return $processedConfiguration;
}
/**
* Get list of pages with fixed post var configuration
*
* @return array
*/
protected function getFixedPagesUids()
{
$field = 'tx_themet3kit_fixed_post_var_conf';
if (version_compare(TYPO3_version, '8.0', '<')) {
/** @var DatabaseConnection $dbConnection */
$dbConnection = $GLOBALS['TYPO3_DB'];
$pages = $dbConnection->exec_SELECTgetRows(
'uid, ' . $field,
'pages',
$field . ' != \'0\' AND ' . $field . ' != \'\''
. BackendUtility::deleteClause('pages')
);
} else {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
$pages = $queryBuilder
->select('uid', 'tx_themet3kit_fixed_post_var_conf')
->from('pages')
->where(
$queryBuilder->expr()->neq(
$field,
$queryBuilder->createNamedParameter('')
),
$queryBuilder->expr()->neq(
$field,
$queryBuilder->createNamedParameter('0')
)
)
->execute()
->fetchAll();
}
return $pages;
}
/**
* Checks if the configuration can be written.
*
* @param string $fileLocation
* @return bool
*/
protected function canWriteConfiguration($fileLocation)
{
$typo3confFolder = PATH_typo3conf;
return @is_writable($typo3confFolder) && (!file_exists($fileLocation) || @is_writable($fileLocation));
}
/**
* Get path where to save configuration
*
* @return string
*/
public function getSaveFilePath()
{
$extConf = HelperUtility::getExtConf();
if (is_array($extConf) && $extConf['fixedPostVarsSaveFilePath']) {
$filePath = PATH_site . trim($extConf['fixedPostVarsSaveFilePath']);
} else {
$filePath = PATH_site . 'typo3conf/realurl_fixedPostVars_conf.php';
}
return $filePath;
}
}