<?php
###################################################################
#
# displays the date (yyyy, mm, dd) and time (hh, min, ss) dropdowns
# in a form and automagically keeps track of the user changes
#
# The value is set in the field that is provided. It is assumed
# the the field will be a text box or a hidden field.
#
# tried with NS and IE .. surprise ... works!!
#
# interface functions:
# setFieldName ( frmName, fieldName ) : The formname should be
# fully qualified. e.g., "document.frmMain.frmSub"
# the field name where the selected date will be returned
# ( SHOULD BE THE FIRST FUNCTION CALLED )
# - setAMPM( boolean ) : If AM/PM date needs to be displayed
# - setValues( yyyy, mm, dd, hh, min, ss) : If not called the
# current datetime of the server is defaulted
# writeJS() : Writes the necessary JavaScript.
#
# displayYear, displayMonth, displayMonthName, displayDay,
# displayHour, displayMinute, displaySecond, displayType
# - these are for displaying the corresponding dropdowns
# and can be called at the necessary place in the HTML.
# note: displayType will display dropdown for AM/PM.
# this will not be displayed if setAMPM is not
# set to true. by default it is false.
#
###################################################################
class ooDateTime {
// this function writes all the necessary javascript code,
// so that everything runs fine! :)
function writeJS() {
$fs = $this->dt_fieldSuffix;
$frm= $this->dt_formName;
$doAMPMAdjust = "";
if ( $this->isAMPM == true ) {
$doAMPMAdjust =
"hhType = $frm.type$fs [$frm.type$fs.selectedIndex].value;".
"if ( hhType == 'AM' && hh == 12 ) hh = 0;".
"if ( hhType == 'PM' && hh != 12 ) hh = parseInt(hh) + 12;";
}
$jsDate = Date( "Y, m - 1, d, H, i, s", $this->value);
// weird Javascript starts month from 0!!!
printf(
"<script language='JavaScript'>\n".
"date$fs = new Date( $jsDate );\n".
"\n".
"function setField$fs () {\n".
" $this->dt_fieldName.value = date$fs.toLocaleString();\n".
"}\n".
"setField$fs();\n".
"\n".
"function dateChange$fs ( fieldType ) {\n".
" switch( fieldType ) {\n".
" case 'yyyy': date$fs.setFullYear( $frm.yyyy$fs [$frm.yyyy$fs.selectedIndex].value); break;\n".
" case 'mm': date$fs.setMonth( $frm.mm$fs [$frm.mm$fs.selectedIndex].value - 1); break;\n".
" case 'dd': date$fs.setDate( $frm.dd$fs [$frm.dd$fs.selectedIndex].value); break;\n".
" case 'hh': \n".
" case 'type' : \n".
" hh = $frm.hh$fs [$frm.hh$fs.selectedIndex].value;\n".
" $doAMPMAdjust\n".
" date$fs.setHours( hh ); break;\n".
" case 'min': date$fs.setMinutes( $frm.min$fs [$frm.min$fs.selectedIndex].value); break;\n".
" case 'ss': date$fs.setSeconds( $frm.ss$fs [$frm.ss$fs.selectedIndex].value); break;\n".
" }\n".
" setField$fs();\n".
" return false;\n".
"}".
"</script>"
);
}
function setFieldName( $formName, $fieldName) {
$this->dt_fieldName = $formName.".".$fieldName;
$this->dt_formName = $formName;
}
function setAMPM( $isAMPM ) {
$this->isAMPM = $isAMPM;
}
function setValues( $yyyy, $mm, $dd, $hh, $min, $ss ) {
$this->value = mktime( $hh, $min, $ss, $dd, $mm, $yyyy);
$this->dt_yyyy = $yyyy; $this->dt_mm = $mm; $this->dt_dd = $dd;
$this->dt_hh = $hh; $this->dt_min = $min; $this->dt_ss = $ss;
}
function displayYear() {
$yyyy = date( "Y", $this->value);
$this->displayList( "yyyy",
$this->generateArray( $yyyy - 5, $yyyy + 5), $yyyy);
}
function displayMonth() {
$this->displayList( "mm", $this->generateArray( 1, 12),
date( "m", $this->value));
}
function displayMonthName() {
$fs = $this->dt_fieldSuffix;
$mm = date( "m", $this->value);
printf( "<SELECT SIZE=1 name=mm$this->dt_fieldSuffix
onChange='return dateChange$fs ( \"mm\") '>\n");
for( $i = 1; $i <= 12; $i++ ) {
printf( "\t<OPTION VALUE=$i ");
if ( $mm == $i ) {
printf( "SELECTED" );
}
printf( ">%s</OPTION>\n",
date( 'F', mktime( 1, 1, 1, $i, 1, 2000)));
}
printf( "</SELECT>\n");
}
function displayDay() {
$dd = date( "d", $this->value);
$this->displayList( "dd", $this->generateArray( 1, 31), $dd);
}
function displayHour() {
if ( $this->isAMPM == true ) {
$this->displayList( "hh", $this->generateArray( 1, 12),
date( "h", $this->value));
} else {
$this->displayList( "hh", $this->generateArray( 0, 23),
date( "H", $this->value));
}
}
function displayMinute() {
$this->displayList( "min", $this->generateArray( 0, 59),
date( "i", $this->value));
}
function displaySecond() {
$this->displayList( "ss", $this->generateArray( 0, 59),
date( "s", $this->value));
}
function displayType() {
if ( $this->isAMPM ) {
$sType = date( "A", $this->value );
$this->displayList( "type", array( "AM", "PM") , $sType);
}
}
#################################################################
#
# private members and variables
#
#################################################################
var $value; // the date value
var $isAMPM = false; // if the display will be in AM/PM
var $dt_formName;
var $dt_fieldName;
var $dt_fieldSuffix;
function ooDateTime() {
$this->value = time(); // by default get the current time
$this->dt_fieldSuffix = $this->fieldSuffix(); // generate the random seed
$this->isAMPM = false;
}
function displayList( $fieldPrefix, $listData, $selectedData ) {
$fs = $this->dt_fieldSuffix;
$fieldName=$fieldPrefix.$this->dt_fieldSuffix;
printf( "<SELECT SIZE=1 name=$fieldName ".
"onChange='dateChange$fs ( \"$fieldPrefix\") '>\n");
while ( list( $k, $v) = each( $listData )) {
printf( "\t<OPTION VALUE=$v");
if ( $selectedData == $v ) printf( " SELECTED" );
printf( ">$v</OPTION>\n");
}
printf( "</SELECT>\n");
}
function generateArray( $startNo, $endNo ) {
$tarr = array();
for( $i = $startNo; $i <= $endNo; $i++ ) {
array_push( $tarr, $i);
}
return $tarr;
}
function fieldSuffix() {
mt_srand((double) microtime() * 1000000);
return mt_rand(10000, 99999 );
}
}
?> |