/[corp_html]/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

Diff of /inc/Smarty.addons.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.26