forked from rtp-ch/smarty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.tx_smarty_div.php
150 lines (135 loc) · 4.83 KB
/
class.tx_smarty_div.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
<?php
/***************************************************************
* Copyright notice
*
* (c) 2007 Simon Tuck <stu@rtpartner.ch>, Rueegg Tuck Partner GmbH
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* @copyright 2007 Rueegg Tuck Partner GmbH
* @author Simon Tuck <stu@rtpartner.ch>
* @link http://www.rtpartner.ch/
* @package Smarty (smarty)
**/
// Include TypoScript parser class
require_once(PATH_t3lib.'class.t3lib_tsparser.php');
class tx_smarty_div {
private static $_tsParser = null;
private function _getTsParser()
{
if(is_null(self::$_tsParser)) {
self::$_tsParser = t3lib_div::makeInstance('t3lib_tsparser');
}
return self::$_tsParser;
}
// Turns yes/no, true/false, on/off into booleans. !IMPORANT
function booleanize($value)
{
if(is_scalar($value)) {
if (preg_match("/^(on|true|yes)$/i", trim($value))) {
$value = true;
} elseif (preg_match("/^(off|false|no)$/i", trim($value))) {
$value = false;
}
}
return $value;
}
// Checks against valid TYPO3 instance
function validateTypo3Instance($instances = null)
{
if(empty($instances) || !defined('TYPO3_MODE')) return false;
$instances = t3lib_div::trimExplode(',', strtoupper($instances), 1);
if(in_array(TYPO3_MODE, $instances)) return true;
return false;
}
// Retrieves a TypoScript object from the global setup scope ($GLOBALS['TSFE']->tmpl->setup)
function getTypoScriptFromTMPL($key)
{
if(!$key) return false;
if ($setup = self::_getTsParser()->getVal($key, $GLOBALS['TSFE']->tmpl->setup)) return $setup;
return false;
}
// Turns an array (assumed to be TypoScript Parameters) into
// text.
function makeTypoScriptFromArray($params)
{
$return = null;
foreach($params as $param => $value) {
$return .= $param . ' = ' . $value . chr(10);
}
return $return;
}
// Parses a block of text (assumed to be TypoScript) and, if successful,
// returns an array:
// $setup[0] The TypoScript object, e.g. IMAGE or TEXT etc.
// $setup[1] The configuration array of the object
function parseTypoScript($typoscript)
{
if(!$typoscript) return false;
if (is_array($typoscript)) $typoscript = tx_smarty_div::makeTypoScriptFromArray($typoscript);
if(!is_null($typoscript)) {
self::_getTsParser()->parse($typoscript);
if($setup = self::_getTsParser()->setup) return $setup;
}
return false;
}
// General function to return TypoScript from plugin params
// The special param 'setup' is assumed to reference a TypoScript object from the global scope
function getTypoScriptFromParams($params) {
unset(self::_getTsParser()->setup); // Unset previous TS
if($params['setup']) {
$setup = tx_smarty_div::getTypoScriptFromTMPL($params['setup']);
unset($params['setup']);
}
if($params) $params = tx_smarty_div::parseTypoScript($params); // Create a TypoScript array from $params
if($params && $setup) $setup[1] = t3lib_div::array_merge_recursive_overrule($setup[1], $params, false, true); // Merge $setup & $params
return $setup ? $setup : array(1 => $params);
}
// Get an absolute file/dir reference (trailing slashes are stripped)
function getFileAbsName($filename) {
$location = t3lib_div::getFileAbsFileName($filename,0);
if(@is_readable($location)) {
return substr($location, -1) == DIRECTORY_SEPARATOR ? substr($location, 0, -1) : $location;
}
return $filename;
}
/**
* Note: This function is required but not available in earlier TYPO3 versions
* Removes dots "." from end of a key identifier of TypoScript styled array.
* array('key.' => array('property.' => 'value')) --> array('key' => array('property' => 'value'))
*
* @param array $ts: TypoScript configuration array
* @return array TypoScript configuration array without dots at the end of all keys
*/
function removeDotsFromTS($ts) {
$out = array();
if (is_array($ts)) {
foreach ($ts as $key => $value) {
if (is_array($value)) {
$key = rtrim($key, '.');
$out[$key] = tx_smarty_div::removeDotsFromTS($value);
} else {
$out[$key] = $value;
}
}
}
return $out;
}
}
?>