1 |
<?php |
2 |
/* |
3 |
* Project: Smarty: the PHP compiled template engine |
4 |
* File: Smarty.addons.php |
5 |
* Author: Monte Ohrt <monte@ispi.net> |
6 |
* Andrei Zmievski <andrei@ispi.net> |
7 |
* Version: 1.3.0 |
8 |
* Copyright: 2001 ispi of Lincoln, Inc. |
9 |
* |
10 |
* This library is free software; you can redistribute it and/or |
11 |
* modify it under the terms of the GNU Lesser General Public |
12 |
* License as published by the Free Software Foundation; either |
13 |
* version 2.1 of the License, or (at your option) any later version. |
14 |
* |
15 |
* This library is distributed in the hope that it will be useful, |
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 |
* Lesser General Public License for more details. |
19 |
* |
20 |
* You should have received a copy of the GNU Lesser General Public |
21 |
* License along with this library; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* |
24 |
* You may contact the authors of Smarty by e-mail at: |
25 |
* monte@ispi.net |
26 |
* andrei@ispi.net |
27 |
* |
28 |
* Or, write to: |
29 |
* Monte Ohrt |
30 |
* CTO, ispi |
31 |
* 237 S. 70th suite 220 |
32 |
* Lincoln, NE 68510 |
33 |
* |
34 |
* The latest version of Smarty can be obtained from: |
35 |
* http://www.phpinsider.com |
36 |
* |
37 |
*/ |
38 |
|
39 |
|
40 |
/*============================================*\ |
41 |
Inserts handler |
42 |
\*============================================*/ |
43 |
|
44 |
function _smarty_insert_handler($args, $caching, $delimiter) |
45 |
{ |
46 |
if ($caching) { |
47 |
$arg_string = serialize($args); |
48 |
return "$delimiter{insert_cache $arg_string}$delimiter"; |
49 |
} else { |
50 |
$function_name = 'insert_'.$args['name']; |
51 |
return $function_name($args); |
52 |
} |
53 |
} |
54 |
|
55 |
|
56 |
/*============================================*\ |
57 |
Modifiers |
58 |
\*============================================*/ |
59 |
|
60 |
function _smarty_mod_handler() |
61 |
{ |
62 |
$args = func_get_args(); |
63 |
list($func_name, $map_array) = array_splice($args, 0, 2); |
64 |
$var = $args[0]; |
65 |
|
66 |
if ($map_array && is_array($var)) { |
67 |
foreach ($var as $key => $val) { |
68 |
$args[0] = $val; |
69 |
$var[$key] = call_user_func_array($func_name, $args); |
70 |
} |
71 |
return $var; |
72 |
} else { |
73 |
return call_user_func_array($func_name, $args); |
74 |
} |
75 |
} |
76 |
|
77 |
|
78 |
/*======================================================================*\ |
79 |
Function: smarty_mod_escape |
80 |
Purpose: Escape the string according to escapement type |
81 |
\*======================================================================*/ |
82 |
function smarty_mod_escape($string, $esc_type = 'html') |
83 |
{ |
84 |
switch ($esc_type) { |
85 |
case 'html': |
86 |
return htmlspecialchars($string); |
87 |
|
88 |
case 'url': |
89 |
return urlencode($string); |
90 |
|
91 |
default: |
92 |
return $string; |
93 |
} |
94 |
} |
95 |
|
96 |
|
97 |
/*======================================================================*\ |
98 |
Function: smarty_mod_truncate |
99 |
Purpose: Truncate a string to a certain length if necessary, |
100 |
optionally splitting in the middle of a word, and |
101 |
appending the $etc string. |
102 |
\*======================================================================*/ |
103 |
function smarty_mod_truncate($string, $length = 80, $etc = '...', $break_words = false) |
104 |
{ |
105 |
if ($length == 0) |
106 |
return ''; |
107 |
|
108 |
if (strlen($string) > $length) { |
109 |
$length -= strlen($etc); |
110 |
$fragment = substr($string, 0, $length+1); |
111 |
if ($break_words) |
112 |
$fragment = substr($fragment, 0, -1); |
113 |
else |
114 |
$fragment = preg_replace('/\s+(\S+)?$/', '', $fragment); |
115 |
return $fragment.$etc; |
116 |
} else |
117 |
return $string; |
118 |
} |
119 |
|
120 |
|
121 |
function smarty_mod_spacify($string, $spacify_char = ' ') |
122 |
{ |
123 |
return implode($spacify_char, preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY)); |
124 |
} |
125 |
|
126 |
|
127 |
function smarty_mod_date_format($string, $format="%b %e, %Y") |
128 |
{ |
129 |
return strftime($format, $string); |
130 |
} |
131 |
|
132 |
|
133 |
function smarty_mod_string_format($string, $format) |
134 |
{ |
135 |
return sprintf($format, $string); |
136 |
} |
137 |
|
138 |
function smarty_mod_replace($string, $search, $replace) |
139 |
{ |
140 |
return str_replace($search, $replace, $string); |
141 |
} |
142 |
|
143 |
function smarty_mod_strip_tags($string, $replace_with_space = true) |
144 |
{ |
145 |
if ($replace_with_space) |
146 |
return preg_replace('!<[^>]*?>!', ' ', $string); |
147 |
else |
148 |
return strip_tags($string); |
149 |
} |
150 |
|
151 |
function smarty_mod_default($string, $default="") |
152 |
{ |
153 |
if(empty($string)) |
154 |
return $default; |
155 |
else |
156 |
return $string; |
157 |
} |
158 |
|
159 |
/*============================================*\ |
160 |
Custom tag functions |
161 |
\*============================================*/ |
162 |
|
163 |
/*======================================================================*\ |
164 |
Function: smarty_func_html_options |
165 |
Purpose: Prints the list of <option> tags generated from |
166 |
the passed parameters |
167 |
\*======================================================================*/ |
168 |
function smarty_func_html_options() |
169 |
{ |
170 |
$print_result = true; |
171 |
|
172 |
extract(func_get_arg(0)); |
173 |
|
174 |
$html_result = ""; |
175 |
|
176 |
settype($selected, 'array'); |
177 |
if (isset($options)) { |
178 |
settype($options, 'array'); |
179 |
foreach ($options as $key => $value) { |
180 |
$html_result .= "<option value=\"$key\""; |
181 |
if (in_array($key, $selected)) |
182 |
$html_result .= " selected"; |
183 |
$html_result .= ">$value</option>\n"; |
184 |
} |
185 |
} else { |
186 |
settype($output, 'array'); |
187 |
settype($values, 'array'); |
188 |
for ($i = 0; $i < count($output); $i++) { |
189 |
/* By default, check value against $selected */ |
190 |
$sel_check = $values[$i]; |
191 |
$html_result .= "<option"; |
192 |
if ($i < count($values)) |
193 |
$html_result .= " value=\"".$values[$i]."\""; |
194 |
else |
195 |
$sel_check = $output[$i]; /* if more outputs than values, then |
196 |
check output against $selected */ |
197 |
if (in_array($sel_check, $selected)) |
198 |
$html_result .= " selected"; |
199 |
$html_result .= ">".$output[$i]."</option>\n"; |
200 |
} |
201 |
} |
202 |
|
203 |
if ($print_result) |
204 |
print $html_result; |
205 |
else |
206 |
return $html_result; |
207 |
} |
208 |
|
209 |
|
210 |
/*======================================================================*\ |
211 |
Function: smarty_func_html_select_date |
212 |
Purpose: Prints the dropdowns for date selection. |
213 |
\*======================================================================*/ |
214 |
function smarty_func_html_select_date() |
215 |
{ |
216 |
/* Default values. */ |
217 |
$prefix = "Date_"; |
218 |
$time = time(); |
219 |
$start_year = strftime("%Y"); |
220 |
$end_year = $start_year; |
221 |
$display_days = true; |
222 |
$display_months = true; |
223 |
$display_years = true; |
224 |
$month_format = "%B"; |
225 |
$day_format = "%02d"; |
226 |
$year_as_text = false; |
227 |
|
228 |
extract(func_get_arg(0)); |
229 |
|
230 |
$html_result = ""; |
231 |
|
232 |
if ($display_months) { |
233 |
$month_names = array(); |
234 |
|
235 |
for ($i = 1; $i <= 12; $i++) |
236 |
$month_names[] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000)); |
237 |
|
238 |
$html_result .= '<select name="'.$prefix.'Month">'."\n"; |
239 |
$html_result .= smarty_func_html_options(array('output' => $month_names, |
240 |
'values' => range(1, 12), |
241 |
'selected' => strftime("%m", $time), |
242 |
'print_result' => false)); |
243 |
$html_result .= "</select>\n"; |
244 |
} |
245 |
|
246 |
if ($display_days) { |
247 |
$days = range(1, 31); |
248 |
array_walk($days, create_function('&$x', '$x = sprintf("'.$day_format.'", $x);')); |
249 |
|
250 |
$html_result .= '<select name="'.$prefix.'Day">'."\n"; |
251 |
$html_result .= smarty_func_html_options(array('output' => $days, |
252 |
'values' => range(1, 31), |
253 |
'selected' => strftime("%d", $time), |
254 |
'print_result' => false)); |
255 |
$html_result .= "</select>\n"; |
256 |
} |
257 |
|
258 |
if ($display_years) { |
259 |
if ($year_as_text) { |
260 |
$html_result .= '<input type="text" name="'.$prefix.'Year" value="'.strftime('%Y', $time).'" size="4" maxlength="4">'; |
261 |
} else { |
262 |
$years = range($start_year, $end_year); |
263 |
|
264 |
$html_result .= '<select name="'.$prefix.'Year">'."\n"; |
265 |
$html_result .= smarty_func_html_options(array('output' => $years, |
266 |
'values' => $years, |
267 |
'selected' => strftime("%Y", $time), |
268 |
'print_result' => false)); |
269 |
$html_result .= "</select>\n"; |
270 |
} |
271 |
} |
272 |
|
273 |
print $html_result; |
274 |
} |
275 |
|
276 |
/* vim: set expandtab: */ |
277 |
|
278 |
?> |