/[pliva-si]/inc/Smarty.addons.php
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /inc/Smarty.addons.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Mon Jun 25 09:57:25 2001 UTC (22 years, 10 months ago) by ravilov
Branch: MAIN
Branch point for: pliva
Initial revision

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

  ViewVC Help
Powered by ViewVC 1.1.26