-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.tx_bnstatictemplates_lib.php
246 lines (212 loc) · 8.37 KB
/
class.tx_bnstatictemplates_lib.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
class tx_bnstatictemplates_lib {
const PATH_TYPE_BASE = 0;
const PATH_TYPE_SITE = 1;
/**
* ItemsProcFunc for adding static templates from fileadmin.
*
* @param array $params
* @param object parentObject
*/
public static function addStaticTemplates(&$params, &$parentObject) {
if (!is_array($params['items'])) {
$params['items'] = array();
}
$baseStaticTemplates = self::getStaticTemplatesInBaseConfigurationPath();
if ($params['row']) {
$siteStaticTemplates = self::getStaticTemplatesInSiteConfigurationPath($params['row']['pid']);
} else {
$siteStaticTEmplates = array();
}
$mergedItems = array();
$mergedItems['items'] = array_merge($baseStaticTemplates['items'], $siteStaticTemplates['items']);
usort($mergedItems['items'], function($a, $b) {
return $a[0] > $b[0];
});
$params['items'] = array_merge($params['items'], $mergedItems['items']);
}
/**
* Gets the static template prefix from EXTCONF
*
* @return string
*/
protected static function getStaticTemplatePrefix() {
$extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['bn_statictemplates']);
return ($extConf['staticTemplatePrefix']) ? $extConf['staticTemplatePrefix'] : 'BN';
}
/**
* Gets the base configuration path from EXTCONF
*
* @return string
*/
protected static function getBaseConfigurationPath() {
$extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['bn_statictemplates']);
return $extConf['baseConfigurationPath'];
}
/**
* Gets the items array for static templates in the base configuration path
*
* @return array
*/
protected static function getStaticTemplatesInBaseConfigurationPath() {
$relativeConfigurationPath = self::getBaseConfigurationPath();
return self::getStaticTemplatesInPath(self::PATH_TYPE_BASE, $relativeConfigurationPath);
}
/**
* Gets the site configuration path for the given page ID
*
* @param integer $pageId
* @return string
*/
protected static function getSiteConfigurationPath($pageId) {
$tmpl = t3lib_div::makeInstance("t3lib_tsparser_ext");
$tmpl->tt_track = 0;
$tmpl->init();
// local template
$localTemplateRow = $tmpl->ext_getFirstTemplate($pageId);
if ($localTemplateRow['root']) {
return $localTemplateRow['tx_bnstatictemplates_path'];
} else {
// Gets the rootLine
$sys_page = t3lib_div::makeInstance("t3lib_pageSelect");
$rootLine = $sys_page->getRootLine($pageId);
$tmpl->runThroughTemplates($rootLine, $localTemplateRow['uid']);
// Use the root page found when walking the rootline
$templateRow = $tmpl->ext_getFirstTemplate($tmpl->rootId);
return $templateRow['tx_bnstatictemplates_path'];
}
}
/**
* Gets the items array for the static templates in the site configuration
*
* @return array
*/
protected static function getStaticTemplatesInSiteConfigurationPath($pid) {
$relativeConfigurationPath = self::getSiteConfigurationPath($pid);
return self::getStaticTemplatesInPath(self::PATH_TYPE_SITE, $relativeConfigurationPath);
}
/**
* Gets the items array for a given configuration type (base or site) and path.
*
* @param integer $configurationType
* @param string $relativeConfigurationPath
* @return array
*/
protected static function getStaticTemplatesInPath($configurationType, $relativeConfigurationPath) {
$params = array();
$params['items'] = array();
$name = basename(PATH_site . $relativeConfigurationPath);
$prefix = self::getStaticTemplatePrefix();
// Default TS
$pathToTS = trim($relativeConfigurationPath, '/') . '/Default/Configuration/TypoScript/';
if (@is_dir(PATH_site . $pathToTS)) {
switch($configurationType) {
case self::PATH_TYPE_BASE:
$configurationName = $configurationKey . ' (Base)';
break;
case self::PATH_TYPE_SITE:
$configurationName = $configurationKey . ' (' . $name . ')';
}
$params['items'][] = array($prefix . ': ' . $configurationName, $pathToTS);
}
// Extension TS
$configurations = t3lib_div::get_dirs(PATH_site . rtrim($relativeConfigurationPath, '/') . '/Extensions/');
foreach ((array) $configurations as $configurationKey) {
$pathToTS = trim($relativeConfigurationPath, '/') . '/Extensions/' . $configurationKey . '/Configuration/TypoScript/';
if (@is_dir(PATH_site . $pathToTS)) {
switch($configurationType) {
case self::PATH_TYPE_BASE:
$configurationName = $configurationKey . ' (Base)';
break;
case self::PATH_TYPE_SITE:
$configurationName = $configurationKey . ' (' . $name . ')';
}
$params['items'][] = array($prefix . ': EXT:' . $configurationName, $pathToTS);
}
}
// addStaticTemplates TS
$configurations = t3lib_div::get_dirs(PATH_site . rtrim($relativeConfigurationPath, '/') . '/StaticTemplates/');
foreach ((array) $configurations as $configurationKey) {
$pathToTS = trim($relativeConfigurationPath, '/') . '/StaticTemplates/' . $configurationKey . '/Configuration/TypoScript/';
if (@is_dir(PATH_site . $pathToTS)) {
switch($configurationType) {
case self::PATH_TYPE_BASE:
$configurationName = $configurationKey . ' (Base)';
break;
case self::PATH_TYPE_SITE:
$configurationName = $configurationKey . ' (' . $name . ')';
}
$params['items'][] = array($prefix . ': ' . $configurationName, $pathToTS);
}
}
return $params;
}
/**
* Includes static templates as part of the page rendering process.
*
* @param array $params
* @param object $parentObject
* @return void
*/
public static function includeStaticTemplates($params, $parentObject) {
$idList = $params['idList'];
$templateId = $params['templateId'];
$pid = $params['pid'];
$row = $params['row'];
if (trim($row['include_static_file'])) {
$include_static_fileArr = t3lib_div::trimExplode(',', $row['include_static_file'], TRUE);
foreach ((array) $include_static_fileArr as $ISF_filePath) {
// Specifically process static templates NOT coming from extensions and have not already been processed
if ((substr($ISF_filePath, 0, 4) !== 'EXT:') && !in_array('bnstatictemplate_' . $ISF_filePath, explode(',', $idList))) {
$title = $ISF_filePath;
$ISF_relFilePath = $ISF_filePath;
$ISF_filePath = PATH_site . $ISF_filePath;
if (@is_dir($ISF_filePath)) {
// Convert IncludeStaticFile.txt to an array
if (@is_file($ISF_filePath . 'IncludeStaticFile.txt')) {
$staticFilesIncludedFromTemplate = array_unique(explode(',', t3lib_div::getUrl($ISF_filePath . 'IncludeStaticFile.txt')));
} else {
$staticFilesIncludedFromTemplate = array();
}
$baseConfigurationPath = self::getBaseConfigurationPath();
if ($row['root']) {
$root = $row['pid'];
} elseif ($parentObject->rootId) {
$root = $parentObject->rootId;
}
if ($root) {
$siteConfigurationPath = self::getSiteConfigurationPath($root);
// If we're including something from site configuraton, look for a corresponding base configuration to include
if (strstr(rtrim(dirname($ISF_filePath), '/'), rtrim(PATH_site . $siteConfigurationPath, '/')) !== FALSE) {
$baseConfiguration = str_replace($siteConfigurationPath, $baseConfigurationPath, $ISF_relFilePath);
if (@is_dir(PATH_site . $baseConfiguration)) {
$staticFilesIncludedFromTemplate[] = $baseConfiguration;
}
}
}
$subrow = array(
'constants' => @is_file($ISF_filePath . 'Constants.ts') ? t3lib_div::getUrl($ISF_filePath . 'Constants.ts') : '',
'config' => @is_file($ISF_filePath . 'TypoScript.ts') ? t3lib_div::getUrl($ISF_filePath . 'TypoScript.ts') : '',
'include_static' => @is_file($ISF_filePath . 'IncludeStatic.txt') ? implode(',', array_unique(t3lib_div::intExplode(',', t3lib_div::getUrl($ISF_filePath . 'IncludeStatic.txt')))) : '',
'include_static_file' => implode(',', array_unique($staticFilesIncludedFromTemplate)),
'title' => $ISF_relFilePath,
'uid' => $ISF_relFilePath
);
$parentObject->processTemplate($subrow, $idList . ',bnstatictemplate_' . $ISF_relFilePath, $pid, 'bnstatictemplate_' . $ISF_relFilePath, $templateId);
}
}
}
}
}
/**
* Checks if the given path is within the site static TSConfig path
*
* @param string $path
* @return boolean
*/
protected static function isPathWithinSiteStaticTSConfigPath($path) {
$siteStaticTSConfigPath = self::getSiteStaticTSConfigPath();
return (strstr($path, $siteStaticTSConfigPath) !== FALSE);
}
}
?>