/
BTS2.php
253 lines (240 loc) · 5.94 KB
/
BTS2.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
247
248
249
250
251
252
253
<?php
/**
* BTS2 - Bill's Template System for PHP 5
*
* A very simple Smarty-like template sytem
*
* PHP Version 5
*
* @category HTML
* @package BTS2
* @author Bill Shupp <hostmaster@shupp.org>
* @copyright 2006-2008 Bill Shupp
* @license GPL 2.0 {@link http://www.gnu.org/licenses/gpl.txt}
* @link http://shupp.org/bts
*/
require_once 'BTS2/Exception.php';
/**
* BTS2
*
* @category HTML
* @package BTS2
* @author Bill Shupp <hostmaster@shupp.org>
* @copyright 2006-2008 Bill Shupp
* @license GPL 2.0 {@link http://www.gnu.org/licenses/gpl.txt}
* @link http://shupp.org/bts
*/
class BTS2
{
/**
* data
*
* Where all data is stored
*
* @var array
* @access public
*/
protected $data = array();
/**
* templateDir
*
* Location of template directory
*
* @var string
* @access protected
*/
protected $templateDir = null;
/**
* __constructor
*
* Setup
*
* @param string $templateDir Teamplates directory
*
* @access public
* @return void
*/
public function __construct($templateDir = './templates')
{
$this->templateDir = $templateDir;
$this->data['php_self'] = $_SERVER['PHP_SELF'];
}
/**
* __set
*
* @param mixed $key data array key
* @param mixed $val data array value
*
* @access public
* @return void
*/
public function __set($key, $val)
{
$this->data[$key] = $val;
}
/**
* __get
*
* @param mixed $key data array key
*
* @access public
* @return void
*/
public function __get($key)
{
if (isset($this->data[$key])) {
return $this->data[$key];
}
return null;
}
/**
* assign
*
* Assign a variable to $data
*
* @param mixed $name variable name
* @param mixed $value variable value
*
* @access public
* @return void
*/
public function assign($name, $value)
{
$this->data[$name] = $value;
}
/**
* getContents
*
* return the parsed PHP contents of a file
*
* @param string $file filename
*
* @access protected
* @throws BTS2_Exception if $file is not readable
* @return string
*/
protected function getContents($file)
{
$path = $this->templateDir . DIRECTORY_SEPARATOR . $file;
if (!is_readable($path)) {
throw new BTS2_Exception("BTS2 Error: Could not retrieve $path");
} else {
// Get contents and parse PHP
foreach ($this->data as $key => $val) {
$$key = $val;
}
ob_start();
include $path;
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
}
/**
* parse
*
* Parse template data
*
* @param mixed $data contents to parse
*
* @protected
* @return $data
*/
protected function parse($data)
{
// Replace Tags
foreach ($this->data as $key => $value) {
if (is_array($value)) {
$re = '/{[$]*' . trim($key) . '}/i';
$data = preg_replace($re, 'Array', $data);
foreach ($value as $ar_key => $ar_val) {
$re = '/{[$]*' . trim($key) . '.' . trim($ar_key) . '}/i';
$data = preg_replace($re, trim($ar_val), $data);
}
} else {
$re = '/{[$]*' . trim($key) .'}/i';
$data = preg_replace($re, trim($value), $data);
}
}
return $data;
}
/**
* display
*
* Output data using print() if second argument is 0.
* Otherwise, return the data to the caller
*
* @param mixed $file filename
* @param bool $return whether to return rather than display
*
* @access public
* @throws BTS2_Exception bubbles up from $this->getContents()
* @return mixed string $data or void
*/
public function display($file, $return = false)
{
$data = $this->getContents($file);
$data = $this->parse($data);
if ($return == true) {
return $data;
}
print ($data);
}
/**
* selectOptions
*
* Determinte which option is selected in an array, output
* as an <option> list
*
* @param mixed $opts options array
* @param mixed $selectedOpt selected option
*
* @access public
* @return void
*/
public function selectOptions($opts, $selectedOpt)
{
$out = '';
foreach ($opts as $key => $val) {
$selected = '';
if ($val == $selectedOpt) {
$selected = ' selected';
}
$out .= "<option$selected>$val\n";
}
return $out;
}
/**
* cycle
*
* Simple function for cycling items like bgcolor in a foreach loop
* Derived from the Smarty Plugin by Monte Ohrt
*
* @param mixed $values bgcolor values
* @param string $name name
*
* @access public
* @return void
*/
public function cycle($values, $name = 'default')
{
static $cycleVars;
if (isset($cycleVars[$name]['values'])
&& $cycleVars[$name]['values'] != $values) {
$cycleVars[$name]['index'] = 0;
}
$cycleVars[$name]['values'] = $values;
$cycleArray = explode(',', $cycleVars[$name]['values']);
if (!isset($cycleVars[$name]['index'])) {
$cycleVars[$name]['index'] = 0;
}
$retval = $cycleArray[$cycleVars[$name]['index']];
if ($cycleVars[$name]['index'] >= count($cycleArray) -1) {
$cycleVars[$name]['index'] = 0;
} else {
$cycleVars[$name]['index']++;
}
return $retval;
}
}
?>