|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
trparky Smarty Regular
Joined: 25 May 2006 Posts: 36
|
Posted: Thu Jun 10, 2010 1:40 am Post subject: CSS Element ID Patch for html_select_date function |
|
|
Code: | <?php
/**
* Smarty plugin
*
* @package Smarty
* @subpackage PluginsFunction
*/
/**
* 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)
* - 1.3.5 added the ability to have CSS ID elements added along
* with form element name
*
* @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}
(Smarty online manual)
* @version 1.3.5
* @author Andrei Zmievski
* @author Monte Ohrt <monte at ohrt dot com>
* @param array $params parameters
* @param object $smarty Smarty object
* @param object $template template object
* @return string
*/
function smarty_function_html_select_date($params, $smarty, $template)
{
require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
require_once(SMARTY_PLUGINS_DIR . 'function.html_options.php');
//$smarty->loadPlugin('Smarty_shared_escape_special_chars');
//$smarty->loadPlugin('Smarty_shared_make_timestamp');
//$smarty->loadPlugin('Smarty_function_html_options');
/* Default values. */
$prefix = "Date_";
$start_year = strftime("%Y");
$end_year = $start_year;
$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";
/* Write day values using this format MB */
$day_value_format = "%d";
$year_as_text = false;
/* Display years in reverse order? Ie. 2000,1999,.... */
$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;
/* <select size>'s of the different <select> tags.
If not set, uses default dropdown. */
$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;
/* Separate attributes for the tags. */
$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';
/* 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 = '';
foreach ($params as $_key => $_value) {
switch ($_key) {
case 'prefix':
case 'time':
case 'start_year':
case 'end_year':
case 'month_format':
case 'day_format':
case 'day_value_format':
case 'field_array':
case 'day_size':
case 'month_size':
case 'year_size':
case 'all_extra':
case 'day_extra':
case 'month_extra':
case 'year_extra':
case 'field_order':
case 'field_separator':
case 'month_value_format':
case 'month_empty':
case 'day_empty':
case 'year_empty':
$$_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':
case 'year_as_text':
case 'reverse_years':
$$_key = (bool)$_value;
break;
default:
if (!is_array($_value)) {
$extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
} else {
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];
} else {
$end_year = strftime('%Y') - $match[2];
}
}
if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
if ($match[1] == '+') {
$start_year = strftime('%Y') + $match[2];
} else {
$start_year = strftime('%Y') - $match[2];
}
}
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];
}
}
$field_order = strtoupper($field_order);
$html_result = $month_result = $day_result = $year_result = "";
$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));
}
$month_result .= '<select name=';
if (null !== $field_array) {
$month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
} else {
$month_result .= '"' . $prefix . 'Month"';
}
$month_result .= ' id=';
if (null !== $field_array) {
$month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
} 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;
}
$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, $template);
$month_result .= '</select>';
}
if ($display_days) {
$field_separator_count++;
$days = array();
if (isset($day_empty)) {
$days[''] = $day_empty;
$day_values[''] = '';
}
for ($i = 1; $i <= 31; $i++) {
$days[] = sprintf($day_format, $i);
$day_values[] = sprintf($day_value_format, $i);
}
$day_result .= '<select name=';
if (null !== $field_array) {
$day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
} else {
$day_result .= '"' . $prefix . 'Day"';
}
$day_result .= ' id=';
if (null !== $field_array) {
$day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
} else {
$day_result .= '"' . $prefix . 'Day"';
}
if (null !== $day_size) {
$day_result .= ' size="' . $day_size . '"';
}
if (null !== $all_extra) {
$day_result .= ' ' . $all_extra;
}
if (null !== $day_extra) {
$day_result .= ' ' . $day_extra;
}
$day_result .= $extra_attrs . '>' . "\n";
$day_result .= smarty_function_html_options(array('output' => $days,
'values' => $day_values,
'selected' => $time[2],
'print_result' => false),
$smarty, $template);
$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 ($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 . '" id="' . $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, $template);
$year_result .= '</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;
case 'Y':
$html_result .= $year_result;
break;
}
// Add the field seperator
if ($i < $field_separator_count) {
$html_result .= $field_separator;
}
}
return $html_result;
}
?> |
_________________ Tom Parkison |
|
Back to top |
|
trparky Smarty Regular
Joined: 25 May 2006 Posts: 36
|
Posted: Thu Jul 01, 2010 6:21 pm Post subject: |
|
|
Any way we can have this patch merged into the official release code? I can't be the only one that needs this particular patched module. _________________ Tom Parkison |
|
Back to top |
|
baskettcase Smarty Rookie
Joined: 19 Jan 2011 Posts: 15
|
Posted: Wed Jan 19, 2011 7:03 am Post subject: Yes please |
|
|
I also would like this to be merged with the official release code. ID's really are needed these days. Is there a way that we are assured that an admin has seen this thread? |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Wed Jan 19, 2011 4:56 pm Post subject: |
|
|
Hey there,
I already had rewriting {html_select_date} and {html_select_time} on my ToDo for 3.1. Although the focus was to simplify and speed up the code, I could easily introduce new arguments.
day_id, month_id and year_id will be included because of your post here. If you specify a string, it will be used directly. If you specify a boolean true it will be filled with the value of the name attribute (as your patch does).
Also I'll introduce month_names which will allow you to specify a list of names to be injected into the month <select> (sort of a problem in another thread).
Regards,
Rod |
|
Back to top |
|
baskettcase Smarty Rookie
Joined: 19 Jan 2011 Posts: 15
|
Posted: Wed Jan 19, 2011 6:27 pm Post subject: |
|
|
I just use facebook too much. I wanted to click "Like" on your post! Sounds great! Looking forward to it! |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
|
Back to top |
|
baskettcase Smarty Rookie
Joined: 19 Jan 2011 Posts: 15
|
Posted: Fri Jan 21, 2011 4:22 pm Post subject: |
|
|
Like! like! |
|
Back to top |
|
baskettcase Smarty Rookie
Joined: 19 Jan 2011 Posts: 15
|
Posted: Mon Jan 31, 2011 12:54 am Post subject: |
|
|
I set all_id=true and it is using 1name as the id. So for example if the name is birth_ and month name is birth_Month then id is 1birth_Month. Im guessing that wasn't what you had in mind? :O)
Right now I need to set: year_id=null month_id=null day_id=null for it to pull the name attribute as the id value. |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Mon Jan 31, 2011 7:46 am Post subject: |
|
|
well, yes. To trigger "set name as ID" you need to set *_id=null. |
|
Back to top |
|
baskettcase Smarty Rookie
Joined: 19 Jan 2011 Posts: 15
|
Posted: Mon Jan 31, 2011 7:50 am Post subject: |
|
|
how about name_is_id=true or something like that? it would cut down on all that code. Or just by default do that.. that seems like the best way. |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Mon Jan 31, 2011 7:55 am Post subject: |
|
|
IDs have to be unique. And because of that I would not want any plugin to use any IDs _it_ deems necessary. You can also set *_id="" which would make more sense… |
|
Back to top |
|
baskettcase Smarty Rookie
Joined: 19 Jan 2011 Posts: 15
|
Posted: Mon Jan 31, 2011 7:58 am Post subject: |
|
|
year_id=null month_id=null day_id=null
vs
name_is_id=true
Unless you literally mean *_id=null although I didn't see that in the code My guess is that even you don't want to type it all out! |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Mon Jan 31, 2011 8:02 am Post subject: |
|
|
all_id, year_id, month_id, day_id. anything that evaluates to true will be used as the string, anything that evaluates to false but is not null will trigger the name being used as ID. |
|
Back to top |
|
baskettcase Smarty Rookie
Joined: 19 Jan 2011 Posts: 15
|
Posted: Mon Jan 31, 2011 8:04 am Post subject: |
|
|
ah ok, so then if I use all_id=false then that should do it. Ill try it out. thanks! |
|
Back to top |
|
baskettcase Smarty Rookie
Joined: 19 Jan 2011 Posts: 15
|
Posted: Tue Feb 01, 2011 6:12 am Post subject: |
|
|
That worked. I think there might be a bug in the code since my month dropdown is showing as so:
Code: | <select name="birth_Month" id="birth_Month">
<option value="">Month</option>
<option value="01">December</option>
<option value="02" selected="selected">January</option>
<option value="03">February</option>
<option value="04">March</option>
<option value="05">April</option>
<option value="06">May</option>
<option value="07">June</option>
<option value="08">July</option>
<option value="09">August</option>
<option value="10">September</option>
<option value="11">October</option>
<option value="12">November</option>
</select> |
Im going to take a look at the code and add the old file in to see if that changes it, or maybe Im doing something wrong. Anyways I thought I would give you a heads up on that. Im also having trouble having it select the "empty" value by default.
Ok looks like the old code works and the months and values are displayed correctly, but not this new patch.
I got it to default to the blank values by doing "time='0000-00-00'" seems like "time=''" should work also. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|