Fixed html_select_date and html_select_time

From: Date: Sun, 02 Sep 2001 22:17:38 +0000
Subject: Fixed html_select_date and html_select_time
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-1416@lists.php.net to get a copy of this message
Hi! I fixed up the html_select_date and html_select_time functions again. First, I added all the options from html_select_date to html_select_time (where applicable). This makes those two functions be somewhat "in sync" again. Next, I added 3 options to html_select_date: /* Which date should be selected? */ /* Useful for "ancient" times (pre 1970) or */ /* very distant times (post 2031) */ /* If set, the value from XY_select is preferred */ $day_select = null; $month_select = null; $year_select = null; I did this, because I noticed that for early years, pre-selecting dates does not work. This is, because mktime() returns negative values for pre-1970 years. I haven't tested post-2031 (?) dates, but I suspect that there may be errors on some (32-bit) platforms as well. The patch contains diff's agains Smarty.addons.php and docs.sgm from the 31-Aug-2001 09:27 snapshot as can be downloaded from the Smarty website. Cheers, Alexander Skwar -- How to quote: http://learn.to/quote (german) http://quote.6x.to (english) Homepage: http://www.digitalprojects.com | http://www.iso-top.de iso-top.de - Die günstige Art an Linux Distributionen zu kommen Uptime: 7 hours 56 minutes

diff -ur smarty.original/Smarty.addons.php smarty.modified/Smarty.addons.php --- smarty.original/Smarty.addons.php Fri Aug 31 16:25:16 2001 +++ smarty.modified/Smarty.addons.php Sun Sep 2 23:53:00 2001 @@ -270,6 +270,13 @@ $month_format = "%B"; $day_format = "%02d"; $year_as_text = false; + /* Which date should be selected? */ + /* Useful for "ancient" times (pre 1970) or */ + /* very distant times (post 2031) */ + /* If set, the value from XY_select is preferred */ + $day_select = null; + $month_select = null; + $year_select = null; /* Display years in reverse order? Ie. 2000,1999,.... */ $reverse_years = false; /* Should the select boxes be part of an array when returned from PHP? @@ -323,9 +330,14 @@ $month_result .= ' ' . $all_extra; } $month_result .= '>'."\n"; - $month_result .= smarty_func_html_options(array('output' => $month_names, - 'values' => range(1, 12), - 'selected' => strftime("%m", $time), + if (null !== $month_select){ + $selected = $month_select; + } else { + $selected = strftime("%m", $time); + } + $month_result .= smarty_func_html_options(array('output' => $month_names, + 'values' => range(1, 12), + 'selected' => $selected, 'print_result' => false)); $month_result .= '</select>'; } @@ -351,9 +363,14 @@ $day_result .= ' ' . $day_extra; } $day_result .= '>'."\n"; - $day_result .= smarty_func_html_options(array('output' => $days, - 'values' => range(1, 31), - 'selected' => strftime("%d", $time), + if (null !== $day_select){ + $selected = $day_select; + } else { + $selected = strftime("%d", $time); + } + $day_result .= smarty_func_html_options(array('output' => $days, + 'values' => range(1, 31), + 'selected' => $selected, 'print_result' => false)); $day_result .= '</select>'; } @@ -364,8 +381,13 @@ } else { $year_name = $prefix . 'Year'; } + if (null !== $year_select){ + $selected = $year_select; + } else { + $selected = strftime("%Y", $time); + } if ($year_as_text) { - $year_result .= '<input type="text" name="' . $year_name . '" value="'.strftime('%Y', $time).'" size="4" maxlength="4"'; + $year_result .= '<input type="text" name="' . $year_name . '" value="' . $selected . '" size="4" maxlength="4"'; if (null !== $all_extra){ $year_result .= ' ' . $all_extra; } @@ -390,9 +412,9 @@ $year_result .= ' ' . $year_extra; } $year_result .= '>'."\n"; - $year_result .= smarty_func_html_options(array('output' => $years, - 'values' => $years, - 'selected' => strftime("%Y", $time), + $year_result .= smarty_func_html_options(array('output' => $years, + 'values' => $years, + 'selected' => $selected, 'print_result' => false)); $year_result .= '</select>'; } @@ -431,13 +453,35 @@ /* Default values. */ $prefix = "Time_"; $time = time(); - $display_hours = true; + $display_hours = true; $display_minutes = true; $display_seconds = true; $display_meridian = true; - $use_24_hours = true; + $use_24_hours = true; $minute_interval = 1; $second_interval = 1; + /* Should the select boxes be part of an array when returned from PHP? + e.g. setting it to "sunrise", would create "sunrise[Hour]", + "sunrise[Minute]", "sunrise[Second]" & "sunrise[Meridian]". + Can be combined with prefix */ + $field_array = null; + /* <select size>'s of the different <select> tags. + If not set, uses default dropdown. */ + $hours_size = null; + $minutes_size = null; + $seconds_size = null; + $meridian_size = null; + /* Unparsed attributes common to *ALL* the <select>/<input> tags. + An example might be in the template: all_extra='class="foo"'. */ + $all_extra = null; + /* Separate attributes for the tags. */ + $hours_extra = null; + $minutes_extra = null; + $seconds_extra = null; + $meridian_extra = null; + /* String printed between the different fields. */ + $field_separator = "\n"; + extract(func_get_arg(0)); @@ -446,16 +490,25 @@ $html_result = ''; if ($display_hours) { - $hours = $use_24_hours ? range(0, 23) : range(1, 12); + $hours = $use_24_hours ? range(0, 23) : range(1, 12); $hour_fmt = $use_24_hours ? '%H' : '%I'; for ($i = 0; $i < count($hours); $i++) $hours[$i] = sprintf('%02d', $hours[$i]); - $html_result .= '<select name="'.$prefix.'Hour">'."\n"; - $html_result .= smarty_func_html_options(array('output' => $hours, - 'values' => $hours, - 'selected' => strftime($hour_fmt, $time), + $html_result .= '<select name='; + if (null !== $field_array){ + $html_result .= '"' . $field_array . '[' . $prefix . 'Hour]"'; + } else { + $html_result .= '"' . $prefix . 'Hour"'; + } + if (null !== $minutes_size){ + $html_result .= ' size="' . $minutes_size . '"'; + } + $html_result .= ' ' . $hours_extra . ' ' . $all_extra . ">\n"; + $html_result .= smarty_func_html_options(array('output' => $hours, + 'values' => $hours, + 'selected' => strftime($hour_fmt, $time), 'print_result' => false)); - $html_result .= "</select>\n"; + $html_result .= '</select>' . $field_seperator; } if ($display_minutes) { @@ -463,12 +516,18 @@ for ($i = 0; $i < count($all_minutes); $i+= $minute_interval) $minutes[] = sprintf('%02d', $all_minutes[$i]); $selected = intval(floor(strftime('%M', $time) / $minute_interval) * $minute_interval); - $html_result .= '<select name="'.$prefix.'Minute">'."\n"; - $html_result .= smarty_func_html_options(array('output' => $minutes, - 'values' => $minutes, - 'selected' => $selected, + $html_result .= '<select name='; + if (null !== $field_array){ + $html_result .= '"' . $field_array . '[' . $prefix . 'Minute]"'; + } else { + $html_result .= '"' . $prefix . 'Minute"'; + } + $html_result .= ' ' . $minutes_extra . ' ' . $all_extra . ">\n"; + $html_result .= smarty_func_html_options(array('output' => $minutes, + 'values' => $minutes, + 'selected' => $selected, 'print_result' => false)); - $html_result .= "</select>\n"; + $html_result .= '</select>' . $field_seperator; } if ($display_seconds) { @@ -476,21 +535,33 @@ for ($i = 0; $i < count($all_seconds); $i+= $second_interval) $seconds[] = sprintf('%02d', $all_seconds[$i]); $selected = intval(floor(strftime('%S', $time) / $second_interval) * $second_interval); - $html_result .= '<select name="'.$prefix.'Second">'."\n"; - $html_result .= smarty_func_html_options(array('output' => $seconds, - 'values' => $seconds, - 'selected' => $selected, + $html_result .= '<select name='; + if (null !== $field_array){ + $html_result .= '"' . $field_array . '[' . $prefix . 'Second]"'; + } else { + $html_result .= '"' . $prefix . 'Second"'; + } + $html_result .= ' ' . $seconds_extra . ' ' . $all_extra . ">\n"; + $html_result .= smarty_func_html_options(array('output' => $seconds, + 'values' => $seconds, + 'selected' => $selected, 'print_result' => false)); - $html_result .= "</select>\n"; + $html_result .= '</select>' . $field_seperator; } if ($display_meridian && !$use_24_hours) { - $html_result .= '<select name="'.$prefix.'Meridian">'."\n"; - $html_result .= smarty_func_html_options(array('output' => array('AM', 'PM'), - 'values' => array('am', 'pm'), - 'selected' => strtolower(strftime('%p', $time)), + $html_result .= '<select name='; + if (null !== $field_array){ + $html_result .= '"' . $field_array . '[' . $prefix . 'Meridian]"'; + } else { + $html_result .= '"' . $prefix . 'Meridian"'; + } + $html_result .= ' ' . $meridian_extra . ' ' . $all_extra . ">\n"; + $html_result .= smarty_func_html_options(array('output' => array('AM', 'PM'), + 'values' => array('am', 'pm'), + 'selected' => strtolower(strftime('%p', $time)), 'print_result' => false)); - $html_result .= "</select>\n"; + $html_result .= '</select>' . $field_seperator; } print $html_result; diff -ur smarty.original/docs.sgm smarty.modified/docs.sgm --- smarty.original/docs.sgm Thu Aug 9 22:22:35 2001 +++ smarty.modified/docs.sgm Sun Sep 2 23:49:49 2001 @@ -3166,6 +3166,27 @@ <entry>current time</entry> <entry>what date/time to use</entry> </row> + <row> + <entry>day_select</entry> + <entry>integer</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Which day should be selected? Useful for "ancient" years (pre 1970) or very distant years (post 2031). If set, overrules value set for time.</entry> + </row> + <row> + <entry>month_select</entry> + <entry>integer</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Which month should be selected?</entry> + </row> + <row> + <entry>year_select</entry> + <entry>integer</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Which year should be selected?</entry> + </row> <row> <entry>start_year</entry> <entry>string</entry> @@ -3222,6 +3243,83 @@ <entry>false</entry> <entry>whether or not to display the year as text</entry> </row> + <row> + <entry>day_size</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Size to display the day select box in. If unset, no size="" parameter is printed</entry> + </row> + <row> + <entry>month_size</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Size to display the month select box in. If unset, no size="" parameter is printed</entry> + </row> + <row> + <entry>year_size</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Size to display the year select box in. If unset, no size="" parameter is printed</entry> + </row> + <row> + <entry>all_extra</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Additional arguments to all the &lt;select&gt;/&lt;input&gt; tags. An example might be in the template: all_extra='class="foo"'.</entry> + </row> + <row> + <entry>day_extra</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Additional arguments to the day select box</entry> + </row> + <row> + <entry>month_extra</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Additional arguments to the month select box</entry> + </row> + <row> + <entry>year_extra</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Additional arguments to the year select box</entry> + </row> + <row> + <entry>field_order</entry> + <entry>string</entry> + <entry>No</entry> + <entry>MDY</entry> + <entry>Order in which the fields are to be displayed. Possible values are all combinations of the letters D, M, Y. The letters stand for day (D), month (M) and year (Y)</entry> + </row> + <row> + <entry>field_seperator</entry> + <entry>string</entry> + <entry>No</entry> + <entry>\n</entry> + <entry>String which is to be printed between all the fields. The default is a line break.</entry> + </row> + <row> + <entry>reverse_years</entry> + <entry>boolean</entry> + <entry>No</entry> + <entry>FALSE</entry> + <entry>Should the years be displayed in reverse order? Specifying TRUE would return 2000, 1999, 1998...., while the default would be to return 1998, 1999, 2000</entry> + </row> + <row> + <entry>field_array</entry> + <entry>String</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Should the returned fields be part of an array? Specifying a value makes the Day/Month/Year values part of this array. Eg. setting it to "birthday", would create "birthday[Day]", "birthday[Month]", and "birthday[Year]". Can be combined with prefix.</entry> + </row> </tbody> </tgroup> </informaltable> @@ -3411,6 +3509,83 @@ <entry>1</entry> <entry>number interval in second dropdown</entry> </row> + <row> + <entry>hours_size</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Size to display the hour select box in. If unset, no size="" parameter is printed</entry> + </row> + <row> + <entry>minutes_size</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Size to display the minute select box in. If unset, no size="" parameter is printed</entry> + </row> + <row> + <entry>seconds_size</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Size to display the second select box in. If unset, no size="" parameter is printed</entry> + </row> + <row> + <entry>meridian_size</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Size to display the meridian select box in. If unset, no size="" parameter is printed</entry> + </row> + <row> + <entry>all_extra</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Additional arguments to all the &lt;select&gt; tags. An example might be in the template: all_extra='class="foo"'.</entry> + </row> + <row> + <entry>hours_extra</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Additional arguments to the hour select box</entry> + </row> + <row> + <entry>minutes_extra</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Additional arguments to the minute select box</entry> + </row> + <row> + <entry>seconds_extra</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Additional arguments to the second select box</entry> + </row> + <row> + <entry>meridian_extra</entry> + <entry>string</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Additional arguments to the meridian select box</entry> + </row> + <row> + <entry>field_seperator</entry> + <entry>string</entry> + <entry>No</entry> + <entry>\n</entry> + <entry>String which is to be printed between all the fields. The default is a line break.</entry> + </row> + <row> + <entry>field_array</entry> + <entry>String</entry> + <entry>No</entry> + <entry>Unset</entry> + <entry>Should the returned fields be part of an array? Specifying a value makes the Hour:Minute:Second Meridian values part of this array. Eg. setting it to "sunset", would create "sunset[Hour]", "sunset[Minute]", "sunset[Second]" and "sunset[Meridian]". Can be combined with prefix.</entry> + </row> </tbody> </tgroup> </informaltable>
« previous php.smarty.general (#1416) next »