/** * Builds a date input field. * * @param string the name of the input field * @param string initial value (ISO datetime (YYYY-MM-DD HH:MM:SS) * or erroneous if the field is in error state) * @param string label displayed in front of the field * @param array Optional params. Additionally to {@link $_common_params} you can use: * - date_format: Format of the date (string, PHP format, default taken from {@link locale_datefmt()}) * - add_date_format_note: If true, date format note gets prepended to the field's note * @return mixed true (if output) or the generated HTML if not outputting */ function date_input($field_name, $field_value, $field_label, $field_params = array()) { global $month, $weekday_letter; if (empty($field_params['date_format'])) { // Use locale date format: $date_format = locale_datefmt(); } else { $date_format = $field_params['date_format']; } // Don't keep that attrib in the list: unset($field_params['date_format']); // Convert PHP date format to JS library date format: // NOTE: when editing/extending this here, you probably also have to adjust param_check_date()! $js_date_format = preg_replace_callback('~(\\\\)?(\\w)~', create_function('$m', ' if( $m[1] == "\\\\" ) return "\\\\".$m[0]; // leave escaped switch( $m[2] ) { case "d": return "dd"; // day, 01-31 case "j": return "d"; // day, 1-31 case "l": return "EE"; // weekday (name) case "D": return "E"; // weekday (abbr) case "e": return ""; // weekday letter, not supported case "m": return "MM"; // month, 01-12 case "n": return "M"; // month, 1-12 case "F": return "MMM"; // full month name; "name or abbr" in date.js case "M": return "NNN"; // month name abbr case "y": return "yy"; // year, 00-99 case "Y": return "yyyy"; // year, XXXX default: return $m[0]; }'), $date_format); $field_params['type'] = 'text'; if (param_has_error($field_name) && !preg_match('~^\\d\\d\\d\\d-\\d\\d-\\d\\d(?: \\d\\d:\\d\\d:\\d\\d)?$~', $field_value)) { // There is an error message for this field: // We do not try to format the date, we keep the erroneous date (if it is not obviously valid). // We could have used param_error() ourself (e.g. "date outside of range"), and the erroneous // field should have the correct format. //echo 'error on '.$field_name.' keep erroneous entry intact '; // Keep original value, but strip off the time part (if any). $field_params['value'] = preg_replace('~ \\d\\d:\\d\\d:\\d\\d$~', '', $field_value); } else { // Make the date value clean for display: // The date value may be compact, in this case we have to decompact it if (preg_match('/^[0-9]+$/', $field_value)) { // The date is compact, so we decompact it $field_value = decompact_date($field_value); } // Get DATE part of datetime and format it to locale format: $field_params['value'] = mysql2date($date_format, $field_value); if (!$field_params['value']) { // Conversion failed (e.g. for dates before ~1902 / PHP < 5.1 (Windows) / 32bit / without DateTime support), use the original value (better than 1970-01-01 anyway!). $field_params['value'] = $field_value; } } if (!empty($field_params['add_date_format_note'])) { // Prepend $date_format to note $field_params['note'] = empty($field_params['note']) ? '(' . $date_format . ')' : '(' . $date_format . ') ' . $field_params['note']; } unset($field_params['add_date_format_note']); if (!isset($field_params['size'])) { // Get size out of $date_format if not explicitly set $field_params['size'] = strlen($js_date_format); } /* dh> do not use maxlength by default. Makes no sense IMHO and fails with dateformats like "j \d\e F, Y" if( !isset($field_params['maxlength']) ) { $field_params['maxlength'] = $field_params['size']; } */ /* Afwas > In the existing locales only d m y and Y are used. Currently the jQuery Datepicker can't handle other dateformats. I will see to some basic check or enable all. @TODO ^^ fp> It might make sense to have 2 date formats for locales: 1 for display and 1 for inputs. Input formats could be forced to used numeric data only. */ // Give it a class, so it can be selected for CSS in IE6 $field_params['class'] = (empty($field_params['class']) ? '' : $field_params['class'] . ' ') . 'form_date_input form-control'; $this->handle_common_params($field_params, $field_name, $field_label); $r = $this->begin_field() . $this->get_input_element($field_params, false); $r .= $this->end_field(); return $this->display_or_return($r); }
/** * Builds a date input field. * * @param string the name of the input field * @param string initial value (ISO datetime (YYYY-MM-DD HH:MM:SS) * or erroneous if the field is in error state) * @param string label displayed in front of the field * @param array Optional params. Additionally to {@link $_common_params} you can use: * - date_format: Format of the date (string, PHP format, default taken from {@link locale_datefmt()}) * - add_date_format_note: If true, date format note gets prepended to the field's note * @return mixed true (if output) or the generated HTML if not outputting */ function date_input($field_name, $field_value, $field_label, $field_params = array()) { global $month, $weekday_letter; if (empty($field_params['date_format'])) { // Use locale date format: $date_format = locale_datefmt(); } else { $date_format = $field_params['date_format']; } // Don't keep that attrib in the list: unset($field_params['date_format']); // Convert PHP date format to JS library date format (date.js): // NOTE: when editing/extending this here, you probably also have to adjust param_check_date()! $js_date_format = preg_replace_callback('~(\\\\)?(\\w)~', create_function('$m', ' if( $m[1] == "\\\\" ) return "\\\\".$m[0]; // leave escaped switch( $m[2] ) { case "d": return "dd"; // day, 01-31 case "j": return "d"; // day, 1-31 case "l": return "EE"; // weekday (name) case "D": return "E"; // weekday (abbr) case "e": return ""; // weekday letter, not supported case "m": return "MM"; // month, 01-12 case "n": return "M"; // month, 1-12 case "F": return "MMM"; // full month name; "name or abbr" in date.js case "M": return "NNN"; // month name abbr case "y": return "yy"; // year, 00-99 case "Y": return "yyyy"; // year, XXXX default: return $m[0]; }'), $date_format); #pre_dump( $js_date_format ); if (param_has_error($field_name)) { // There is an error message for this field: // We do not try to format the date, we keep the erroneous date. //echo 'error on '.$field_name.' keep erroneous entry intact '; $field_params['value'] = trim(substr($field_value, 0, 10)); } else { // Make the date value clean for display: // The date value may be compact, in this case we have to decompact it if (preg_match('/^[0-9]+$/', $field_value)) { // The date is compact, so we decompact it $field_value = decompact_date($field_value); } // Get DATE part of datetime and format it to locale format: $field_params['value'] = mysql2date($date_format, $field_value); } if (!empty($field_params['add_date_format_note'])) { // Prepend $date_format to note $field_params['note'] = empty($field_params['note']) ? '(' . $date_format . ')' : '(' . $date_format . ') ' . $field_params['note']; } unset($field_params['add_date_format_note']); if (!isset($field_params['size'])) { // Get size out of $date_format if not explicitly set $field_params['size'] = strlen($js_date_format); } /* dh> do not use maxlength by default. Makes no sense IMHO and fails with dateformats like "j \d\e F, Y" if( !isset($field_params['maxlength']) ) { $field_params['maxlength'] = $field_params['size']; } */ // Give it a class, so it can be selected for CSS in IE6 if (empty($field_params['class'])) { $field_params['class'] = 'form_date_input'; } else { $field_params['class'] .= ' form_date_input'; } $this->handle_common_params($field_params, $field_name, $field_label); $r = $this->begin_field() . '<script type="text/javascript"> //<![CDATA[ var cal_' . $field_name . ' = new CalendarPopup(); cal_' . $field_name . '.showYearNavigation(); cal_' . $field_name . '.showNavigationDropdowns(); // cal_' . $field_name . '.showYearNavigationInput(); // MonthNames get set through MONTH_NAMES cal_' . $field_name . '.setDayHeaders( ' . "'" . T_($weekday_letter[0]) . "'," . "'" . T_($weekday_letter[1]) . "'," . "'" . T_($weekday_letter[2]) . "'," . "'" . T_($weekday_letter[3]) . "'," . "'" . T_($weekday_letter[4]) . "'," . "'" . T_($weekday_letter[5]) . "'," . "'" . T_($weekday_letter[6]) . "' );\n" . ' cal_' . $field_name . '.setWeekStartDay(' . locale_startofweek() . '); cal_' . $field_name . ".setTodayText('" . TS_('Today') . "');\r\n\t\t\t\t\t\t//]]>\r\n\t\t\t\t\t</script>\n" . $this->get_input_element($field_params, false) . '<a href="#" onclick="cal_' . $field_name . ".select(document.getElementById('" . $field_name . "'), 'anchor_" . $field_name . "', '" . $js_date_format . "');" . ' return false;" name="anchor_' . $field_name . '" id="anchor_' . $this->get_valid_id($field_name) . '" title="' . T_('Select date') . '">' . get_icon('calendar', 'imgtag', array('title' => T_('Select date'))) . '</a>'; $r .= $this->end_field(); return $this->display_or_return($r); }