aboutsummaryrefslogtreecommitdiffstats
path: root/include/smarty/libs/plugins/function.html_select_date.php
diff options
context:
space:
mode:
authorrvelices <rv-github@modusoptimus.com>2013-06-20 03:38:47 +0000
committerrvelices <rv-github@modusoptimus.com>2013-06-20 03:38:47 +0000
commit6fc07742f8fca9d32db23243d374ea27e8ee4c1e (patch)
treebc7240c53a1c6bdff6c785153deb6306585c4062 /include/smarty/libs/plugins/function.html_select_date.php
parent9843eb362dd7d1b9d762871777ef76fa84e8202a (diff)
smarty 3 - first pass for tests
git-svn-id: http://piwigo.org/svn/trunk@23384 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to 'include/smarty/libs/plugins/function.html_select_date.php')
-rw-r--r--include/smarty/libs/plugins/function.html_select_date.php537
1 files changed, 300 insertions, 237 deletions
diff --git a/include/smarty/libs/plugins/function.html_select_date.php b/include/smarty/libs/plugins/function.html_select_date.php
index ede9c7be1..13c500354 100644
--- a/include/smarty/libs/plugins/function.html_select_date.php
+++ b/include/smarty/libs/plugins/function.html_select_date.php
@@ -1,99 +1,142 @@
<?php
/**
* Smarty plugin
+ *
* @package Smarty
- * @subpackage plugins
+ * @subpackage PluginsFunction
*/
/**
+ * @ignore
+ */
+require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
+/**
+ * @ignore
+ */
+require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
+
+/**
* Smarty {html_select_date} plugin
- *
+ *
* Type: function<br>
* Name: html_select_date<br>
* Purpose: Prints the dropdowns for date selection.
- *
- * ChangeLog:<br>
- * - 1.0 initial release
- * - 1.1 added support for +/- N syntax for begin
- * and end year values. (Monte)
- * - 1.2 added support for yyyy-mm-dd syntax for
- * time value. (Jan Rosier)
- * - 1.3 added support for choosing format for
- * month values (Gary Loescher)
- * - 1.3.1 added support for choosing format for
- * day values (Marcus Bointon)
- * - 1.3.2 support negative timestamps, force year
- * dropdown to include given date unless explicitly set (Monte)
- * - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that
- * of 0000-00-00 dates (cybot, boots)
- * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}
+ *
+ * ChangeLog:
+ * <pre>
+ * - 1.0 initial release
+ * - 1.1 added support for +/- N syntax for begin
+ * and end year values. (Monte)
+ * - 1.2 added support for yyyy-mm-dd syntax for
+ * time value. (Jan Rosier)
+ * - 1.3 added support for choosing format for
+ * month values (Gary Loescher)
+ * - 1.3.1 added support for choosing format for
+ * day values (Marcus Bointon)
+ * - 1.3.2 support negative timestamps, force year
+ * dropdown to include given date unless explicitly set (Monte)
+ * - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that
+ * of 0000-00-00 dates (cybot, boots)
+ * - 2.0 complete rewrite for performance,
+ * added attributes month_names, *_id
+ * </pre>
+ *
+ * @link http://www.smarty.net/manual/en/language.function.html.select.date.php {html_select_date}
* (Smarty online manual)
- * @version 1.3.4
- * @author Andrei Zmievski
- * @author Monte Ohrt <monte at ohrt dot com>
- * @param array
- * @param Smarty
- * @return string
+ * @version 2.0
+ * @author Andrei Zmievski
+ * @author Monte Ohrt <monte at ohrt dot com>
+ * @author Rodney Rehm
+ * @param array $params parameters
+ * @param Smarty_Internal_Template $template template object
+ * @return string
*/
-function smarty_function_html_select_date($params, &$smarty)
+function smarty_function_html_select_date($params, $template)
{
- require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
- require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
- require_once $smarty->_get_plugin_filepath('function','html_options');
+ // generate timestamps used for month names only
+ static $_month_timestamps = null;
+ static $_current_year = null;
+ if ($_month_timestamps === null) {
+ $_current_year = date('Y');
+ $_month_timestamps = array();
+ for ($i = 1; $i <= 12; $i++) {
+ $_month_timestamps[$i] = mktime(0, 0, 0, $i, 1, 2000);
+ }
+ }
+
/* Default values. */
- $prefix = "Date_";
- $start_year = strftime("%Y");
- $end_year = $start_year;
- $display_days = true;
- $display_months = true;
- $display_years = true;
- $month_format = "%B";
+ $prefix = "Date_";
+ $start_year = null;
+ $end_year = null;
+ $display_days = true;
+ $display_months = true;
+ $display_years = true;
+ $month_format = "%B";
/* Write months as numbers by default GL */
$month_value_format = "%m";
- $day_format = "%02d";
+ $day_format = "%02d";
/* Write day values using this format MB */
$day_value_format = "%d";
- $year_as_text = false;
+ $year_as_text = false;
/* Display years in reverse order? Ie. 2000,1999,.... */
- $reverse_years = false;
+ $reverse_years = false;
/* Should the select boxes be part of an array when returned from PHP?
e.g. setting it to "birthday", would create "birthday[Day]",
"birthday[Month]" & "birthday[Year]". Can be combined with prefix */
- $field_array = null;
+ $field_array = null;
/* <select size>'s of the different <select> tags.
If not set, uses default dropdown. */
- $day_size = null;
- $month_size = null;
- $year_size = null;
+ $day_size = null;
+ $month_size = null;
+ $year_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;
+ $all_extra = null;
/* Separate attributes for the tags. */
- $day_extra = null;
- $month_extra = null;
- $year_extra = null;
+ $day_extra = null;
+ $month_extra = null;
+ $year_extra = null;
/* Order in which to display the fields.
"D" -> day, "M" -> month, "Y" -> year. */
- $field_order = 'MDY';
+ $field_order = 'MDY';
/* String printed between the different fields. */
$field_separator = "\n";
- $time = time();
- $all_empty = null;
- $day_empty = null;
- $month_empty = null;
- $year_empty = null;
- $extra_attrs = '';
+ $option_separator = "\n";
+ $time = null;
+ // $all_empty = null;
+ // $day_empty = null;
+ // $month_empty = null;
+ // $year_empty = null;
+ $extra_attrs = '';
+ $all_id = null;
+ $day_id = null;
+ $month_id = null;
+ $year_id = null;
- foreach ($params as $_key=>$_value) {
+ foreach ($params as $_key => $_value) {
switch ($_key) {
- case 'prefix':
case 'time':
+ if (!is_array($_value) && $_value !== null) {
+ $time = smarty_make_timestamp($_value);
+ }
+ break;
+
+ case 'month_names':
+ if (is_array($_value) && count($_value) == 12) {
+ $$_key = $_value;
+ } else {
+ trigger_error("html_select_date: month_names must be an array of 12 strings", E_USER_NOTICE);
+ }
+ break;
+
+ case 'prefix':
+ case 'field_array':
case 'start_year':
case 'end_year':
- case 'month_format':
case 'day_format':
case 'day_value_format':
- case 'field_array':
+ case 'month_format':
+ case 'month_value_format':
case 'day_size':
case 'month_size':
case 'year_size':
@@ -103,18 +146,18 @@ function smarty_function_html_select_date($params, &$smarty)
case 'year_extra':
case 'field_order':
case 'field_separator':
- case 'month_value_format':
+ case 'option_separator':
+ case 'all_empty':
case 'month_empty':
case 'day_empty':
case 'year_empty':
+ case 'all_id':
+ case 'month_id':
+ case 'day_id':
+ case 'year_id':
$$_key = (string)$_value;
break;
- case 'all_empty':
- $$_key = (string)$_value;
- $day_empty = $month_empty = $year_empty = $all_empty;
- break;
-
case 'display_days':
case 'display_months':
case 'display_years':
@@ -124,208 +167,228 @@ function smarty_function_html_select_date($params, &$smarty)
break;
default:
- if(!is_array($_value)) {
- $extra_attrs .= ' '.$_key.'="'.smarty_function_escape_special_chars($_value).'"';
+ if (!is_array($_value)) {
+ $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
} else {
- $smarty->trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
- }
+ trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
+ }
break;
- }
- }
-
- if (preg_match('!^-\d+$!', $time)) {
- // negative timestamp, use date()
- $time = date('Y-m-d', $time);
- }
- // If $time is not in format yyyy-mm-dd
- if (preg_match('/^(\d{0,4}-\d{0,2}-\d{0,2})/', $time, $found)) {
- $time = $found[1];
- } else {
- // use smarty_make_timestamp to get an unix timestamp and
- // strftime to make yyyy-mm-dd
- $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
+ }
}
- // Now split this in pieces, which later can be used to set the select
- $time = explode("-", $time);
-
- // make syntax "+N" or "-N" work with start_year and end_year
- if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
- if ($match[1] == '+') {
- $end_year = strftime('%Y') + $match[2];
+
+ // Note: date() is faster than strftime()
+ // Note: explode(date()) is faster than date() date() date()
+ if (isset($params['time']) && is_array($params['time'])) {
+ if (isset($params['time'][$prefix . 'Year'])) {
+ // $_REQUEST[$field_array] given
+ foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) {
+ $_variableName = '_' . strtolower($_elementName);
+ $$_variableName = isset($params['time'][$prefix . $_elementName])
+ ? $params['time'][$prefix . $_elementName]
+ : date($_elementKey);
+ }
+ $time = mktime(0, 0, 0, $_month, $_day, $_year);
+ } elseif (isset($params['time'][$field_array][$prefix . 'Year'])) {
+ // $_REQUEST given
+ foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) {
+ $_variableName = '_' . strtolower($_elementName);
+ $$_variableName = isset($params['time'][$field_array][$prefix . $_elementName])
+ ? $params['time'][$field_array][$prefix . $_elementName]
+ : date($_elementKey);
+ }
+ $time = mktime(0, 0, 0, $_month, $_day, $_year);
} else {
- $end_year = strftime('%Y') - $match[2];
+ // no date found, use NOW
+ list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
}
- }
- if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
- if ($match[1] == '+') {
- $start_year = strftime('%Y') + $match[2];
+ } elseif ($time === null) {
+ if (array_key_exists('time', $params)) {
+ $_year = $_month = $_day = $time = null;
} else {
- $start_year = strftime('%Y') - $match[2];
+ list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
}
+ } else {
+ list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d', $time));
}
- if (strlen($time[0]) > 0) {
- if ($start_year > $time[0] && !isset($params['start_year'])) {
- // force start year to include given date if not explicitly set
- $start_year = $time[0];
- }
- if($end_year < $time[0] && !isset($params['end_year'])) {
- // force end year to include given date if not explicitly set
- $end_year = $time[0];
+
+ // make syntax "+N" or "-N" work with $start_year and $end_year
+ // Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr
+ foreach (array('start', 'end') as $key) {
+ $key .= '_year';
+ $t = $$key;
+ if ($t === null) {
+ $$key = (int)$_current_year;
+ } else if ($t[0] == '+') {
+ $$key = (int)($_current_year + trim(substr($t, 1)));
+ } else if ($t[0] == '-') {
+ $$key = (int)($_current_year - trim(substr($t, 1)));
+ } else {
+ $$key = (int)$$key;
}
}
- $field_order = strtoupper($field_order);
-
- $html_result = $month_result = $day_result = $year_result = "";
+ // flip for ascending or descending
+ if (($start_year > $end_year && !$reverse_years) || ($start_year < $end_year && $reverse_years)) {
+ $t = $end_year;
+ $end_year = $start_year;
+ $start_year = $t;
+ }
- $field_separator_count = -1;
- if ($display_months) {
- $field_separator_count++;
- $month_names = array();
- $month_values = array();
- if(isset($month_empty)) {
- $month_names[''] = $month_empty;
- $month_values[''] = '';
- }
- for ($i = 1; $i <= 12; $i++) {
- $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
- $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
+ // generate year <select> or <input>
+ if ($display_years) {
+ $_html_years = '';
+ $_extra = '';
+ $_name = $field_array ? ($field_array . '[' . $prefix . 'Year]') : ($prefix . 'Year');
+ if ($all_extra) {
+ $_extra .= ' ' . $all_extra;
+ }
+ if ($year_extra) {
+ $_extra .= ' ' . $year_extra;
}
-
- $month_result .= '<select name=';
- if (null !== $field_array){
- $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
+
+ if ($year_as_text) {
+ $_html_years = '<input type="text" name="' . $_name . '" value="' . $_year . '" size="4" maxlength="4"' . $_extra . $extra_attrs . ' />';
} else {
- $month_result .= '"' . $prefix . 'Month"';
- }
- if (null !== $month_size){
- $month_result .= ' size="' . $month_size . '"';
- }
- if (null !== $month_extra){
- $month_result .= ' ' . $month_extra;
- }
- if (null !== $all_extra){
- $month_result .= ' ' . $all_extra;
+ $_html_years = '<select name="' . $_name . '"';
+ if ($year_id !== null || $all_id !== null) {
+ $_html_years .= ' id="' . smarty_function_escape_special_chars(
+ $year_id !== null ? ( $year_id ? $year_id : $_name ) : ( $all_id ? ($all_id . $_name) : $_name )
+ ) . '"';
+ }
+ if ($year_size) {
+ $_html_years .= ' size="' . $year_size . '"';
+ }
+ $_html_years .= $_extra . $extra_attrs . '>' . $option_separator;
+
+ if (isset($year_empty) || isset($all_empty)) {
+ $_html_years .= '<option value="">' . ( isset($year_empty) ? $year_empty : $all_empty ) . '</option>' . $option_separator;
+ }
+
+ $op = $start_year > $end_year ? -1 : 1;
+ for ($i=$start_year; $op > 0 ? $i <= $end_year : $i >= $end_year; $i += $op) {
+ $_html_years .= '<option value="' . $i . '"'
+ . ($_year == $i ? ' selected="selected"' : '')
+ . '>' . $i . '</option>' . $option_separator;
+ }
+
+ $_html_years .= '</select>';
}
- $month_result .= $extra_attrs . '>'."\n";
-
- $month_result .= smarty_function_html_options(array('output' => $month_names,
- 'values' => $month_values,
- 'selected' => (int)$time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '',
- 'print_result' => false),
- $smarty);
- $month_result .= '</select>';
}
-
- if ($display_days) {
- $field_separator_count++;
- $days = array();
- if (isset($day_empty)) {
- $days[''] = $day_empty;
- $day_values[''] = '';
+
+ // generate month <select> or <input>
+ if ($display_months) {
+ $_html_month = '';
+ $_extra = '';
+ $_name = $field_array ? ($field_array . '[' . $prefix . 'Month]') : ($prefix . 'Month');
+ if ($all_extra) {
+ $_extra .= ' ' . $all_extra;
+ }
+ if ($month_extra) {
+ $_extra .= ' ' . $month_extra;
}
- for ($i = 1; $i <= 31; $i++) {
- $days[] = sprintf($day_format, $i);
- $day_values[] = sprintf($day_value_format, $i);
+
+ $_html_months = '<select name="' . $_name . '"';
+ if ($month_id !== null || $all_id !== null) {
+ $_html_months .= ' id="' . smarty_function_escape_special_chars(
+ $month_id !== null ? ( $month_id ? $month_id : $_name ) : ( $all_id ? ($all_id . $_name) : $_name )
+ ) . '"';
}
-
- $day_result .= '<select name=';
- if (null !== $field_array){
- $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
- } else {
- $day_result .= '"' . $prefix . 'Day"';
+ if ($month_size) {
+ $_html_months .= ' size="' . $month_size . '"';
+ }
+ $_html_months .= $_extra . $extra_attrs . '>' . $option_separator;
+
+ if (isset($month_empty) || isset($all_empty)) {
+ $_html_months .= '<option value="">' . ( isset($month_empty) ? $month_empty : $all_empty ) . '</option>' . $option_separator;
}
- if (null !== $day_size){
- $day_result .= ' size="' . $day_size . '"';
+
+ for ($i = 1; $i <= 12; $i++) {
+ $_val = sprintf('%02d', $i);
+ $_text = isset($month_names) ? smarty_function_escape_special_chars($month_names[$i]) : ($month_format == "%m" ? $_val : strftime($month_format, $_month_timestamps[$i]));
+ $_value = $month_value_format == "%m" ? $_val : strftime($month_value_format, $_month_timestamps[$i]);
+ $_html_months .= '<option value="' . $_value . '"'
+ . ($_val == $_month ? ' selected="selected"' : '')
+ . '>' . $_text . '</option>' . $option_separator;
}
- if (null !== $all_extra){
- $day_result .= ' ' . $all_extra;
+
+ $_html_months .= '</select>';
+ }
+
+ // generate day <select> or <input>
+ if ($display_days) {
+ $_html_day = '';
+ $_extra = '';
+ $_name = $field_array ? ($field_array . '[' . $prefix . 'Day]') : ($prefix . 'Day');
+ if ($all_extra) {
+ $_extra .= ' ' . $all_extra;
+ }
+ if ($day_extra) {
+ $_extra .= ' ' . $day_extra;
}
- if (null !== $day_extra){
- $day_result .= ' ' . $day_extra;
+
+ $_html_days = '<select name="' . $_name . '"';
+ if ($day_id !== null || $all_id !== null) {
+ $_html_days .= ' id="' . smarty_function_escape_special_chars(
+ $day_id !== null ? ( $day_id ? $day_id : $_name ) : ( $all_id ? ($all_id . $_name) : $_name )
+ ) . '"';
}
- $day_result .= $extra_attrs . '>'."\n";
- $day_result .= smarty_function_html_options(array('output' => $days,
- 'values' => $day_values,
- 'selected' => $time[2],
- 'print_result' => false),
- $smarty);
- $day_result .= '</select>';
- }
-
- if ($display_years) {
- $field_separator_count++;
- if (null !== $field_array){
- $year_name = $field_array . '[' . $prefix . 'Year]';
- } else {
- $year_name = $prefix . 'Year';
+ if ($day_size) {
+ $_html_days .= ' size="' . $day_size . '"';
+ }
+ $_html_days .= $_extra . $extra_attrs . '>' . $option_separator;
+
+ if (isset($day_empty) || isset($all_empty)) {
+ $_html_days .= '<option value="">' . ( isset($day_empty) ? $day_empty : $all_empty ) . '</option>' . $option_separator;
}
- if ($year_as_text) {
- $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
- if (null !== $all_extra){
- $year_result .= ' ' . $all_extra;
- }
- if (null !== $year_extra){
- $year_result .= ' ' . $year_extra;
- }
- $year_result .= '>';
- } else {
- $years = range((int)$start_year, (int)$end_year);
- if ($reverse_years) {
- rsort($years, SORT_NUMERIC);
- } else {
- sort($years, SORT_NUMERIC);
- }
- $yearvals = $years;
- if(isset($year_empty)) {
- array_unshift($years, $year_empty);
- array_unshift($yearvals, '');
- }
- $year_result .= '<select name="' . $year_name . '"';
- if (null !== $year_size){
- $year_result .= ' size="' . $year_size . '"';
- }
- if (null !== $all_extra){
- $year_result .= ' ' . $all_extra;
- }
- if (null !== $year_extra){
- $year_result .= ' ' . $year_extra;
- }
- $year_result .= $extra_attrs . '>'."\n";
- $year_result .= smarty_function_html_options(array('output' => $years,
- 'values' => $yearvals,
- 'selected' => $time[0],
- 'print_result' => false),
- $smarty);
- $year_result .= '</select>';
+
+ for ($i = 1; $i <= 31; $i++) {
+ $_val = sprintf('%02d', $i);
+ $_text = $day_format == '%02d' ? $_val : sprintf($day_format, $i);
+ $_value = $day_value_format == '%02d' ? $_val : sprintf($day_value_format, $i);
+ $_html_days .= '<option value="' . $_value . '"'
+ . ($_val == $_day ? ' selected="selected"' : '')
+ . '>' . $_text . '</option>' . $option_separator;
}
+
+ $_html_days .= '</select>';
}
- // Loop thru the field_order field
- for ($i = 0; $i <= 2; $i++){
- $c = substr($field_order, $i, 1);
- switch ($c){
- case 'D':
- $html_result .= $day_result;
- break;
-
- case 'M':
- $html_result .= $month_result;
- break;
-
+ // order the fields for output
+ $_html = '';
+ for ($i=0; $i <= 2; $i++) {
+ switch ($field_order[$i]) {
case 'Y':
- $html_result .= $year_result;
- break;
- }
- // Add the field seperator
- if($i < $field_separator_count) {
- $html_result .= $field_separator;
+ case 'y':
+ if (isset($_html_years)) {
+ if ($_html) {
+ $_html .= $field_separator;
+ }
+ $_html .= $_html_years;
+ }
+ break;
+
+ case 'm':
+ case 'M':
+ if (isset($_html_months)) {
+ if ($_html) {
+ $_html .= $field_separator;
+ }
+ $_html .= $_html_months;
+ }
+ break;
+
+ case 'd':
+ case 'D':
+ if (isset($_html_days)) {
+ if ($_html) {
+ $_html .= $field_separator;
+ }
+ $_html .= $_html_days;
+ }
+ break;
}
}
-
- return $html_result;
+ return $_html;
}
-/* vim: set expandtab: */
-
-?>
+?> \ No newline at end of file