Source for file DateSelect.class.php
Documentation is available at DateSelect.class.php
* For including this file you have to define the constant "CLASSPATH".
* Because every include in the framework depends on the CLASSPATH definition.
* The CLASSPATH means the relative path to the folder that contains the
echo "<h3>You have to define the constant CLASSPATH!</h3>\n";
echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\n";
require_once CLASSPATH. "forms/items/HTMLSelect.class.php";
require_once CLASSPATH. "forms/items/ABSTHTMLFormItem.class.php";
require_once CLASSPATH. "core/DateFormatter.class.php";
include_once CLASSPATH. "core/Numbers.class.php";
include_once CLASSPATH. "core/Strings.class.php";
* @author Daniel Plücken <daniel@debakel.net>
* @license http://www.gnu.org/copyleft/lesser.html
* GNU Lesser General Public License
* @copyright Copyright (C) 2003 Daniel Plücken <daniel@debakel.net>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* @var string $standard_date_format
* @var HTMLSelect $secondSelect
* @var HTMLSelect $minuteSelect
* @var HTMLSelect $hourSelect
* @var HTMLSelect $daySelect
* @var HTMLSelect $weekdaySelect
* @var HTMLSelect $weeknumSelect
* @var HTMLSelect $monthSelect
* @var HTMLSelect $yearSelect
* @var boolean $onlyfuturedates
* @var string $onlyfutureJSerrMSG
* @var string $output_order_str
var $output_template_str = "{#WEEKDAY#}\r\n{#DAY#}\r\n{#MONTH#}\r\n{#WEEKNUM#}\r\n{#YEAR#}\r\n{#HOUR#}\r\n{#MINUTE#}\r\n{#SECOND#}\r\n";
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $minuteintervall
* @param integer $secondintervall
$this->SETTING["instance"] = $name;
if( !preg_match( "!^(?:\d+|0)$!", $minuteinterval ) )
$this->SETTING["minuteinterval"] = $minuteinterval;
if( !preg_match( "!^(?:\d+|0)$!", $secondinterval ) )
$this->SETTING["secondinterval"] = $secondinterval;
// stores which items were initialised
$this->SETTING["implementedSelects"]["SECOND"] = false;
$this->SETTING["implementedSelects"]["MINUTE"] = false;
$this->SETTING["implementedSelects"]["HOUR"] = false;
$this->SETTING["implementedSelects"]["DAY"] = false;
$this->SETTING["implementedSelects"]["WEEKDAYS"] = false;
$this->SETTING["implementedSelects"]["WEEKNUM"] = false;
$this->SETTING["implementedSelects"]["MONTH"] = false;
$this->SETTING["implementedSelects"]["YEAR"] = false;
// stores this items who should not appear
$this->SETTING["hidden"]["SECOND"] = false;
$this->SETTING["hidden"]["MINUTE"] = false;
$this->SETTING["hidden"]["HOUR"] = false;
$this->SETTING["hidden"]["DAY"] = false;
$this->SETTING["hidden"]["WEEKDAYS"] = false;
$this->SETTING["hidden"]["WEEKNUM"] = false;
$this->SETTING["hidden"]["MONTH"] = false;
$this->SETTING["hidden"]["YEAR"] = false;
* This sets how the selected date should be standardly handled. The Following
* - ISO - Means that the selected value of this widget will be put in
* a database relation in an ISO-Format, if it is a member of
* - TIMESTAMP - Means that the selected value of this widget will be put in
* a database relation in an UNIX-Timestamp-Format, if it is a
* member of a DBTfillingForm. This is the first standard if
* no other declaration was made.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $str_kind
* This sets in which order the selects should be output.
* You can use following placeholders:
* - {#WEEKDAY#} - For the weekdays select
* - {#DAY#} - For the days select.
* - {#MONTH#} - For the months select.
* - {#YEAR#} - For the years select.
* - {#HOUR#} - For the hours select.
* - {#MINUTE#} - For the minutes select.
* - {#SECOND#} - For the seconds select.
* - {#WEEKNUM#} - For the weeknums select.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $str_template
* Sets the values of the select group with the given timestamp.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $timestamp
$fortimes = 60 / $this->SETTING["secondinterval"];
# Get the right second to select
$fortimes2 = 60 / $this->SETTING["minuteinterval"];
# Get the right minute to select
* Sets the values of the select group with the given ISO date.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $str_iso_date
* Returns a clone of this object.
* @author Daniel Plücken <daniel@debakel.net>
$newInstance->SETTING["implementedSelects"]
= $this->SETTING["implementedSelects"];
$newInstance->hourSelect = $this->hourSelect->clone('hour'. $newName);
$newInstance->daySelect = $this->daySelect->clone( 'day'. $newName );
$newInstance->yearSelect = $this->yearSelect->clone( 'year'. $newName );
* Returns a generated string based on the attributes of this object's
* @author Daniel Plücken <daniel@debakel.net>
* Returns an iso date from a given timestamp or extracts the iso date from
* request data of a form component, that was generated with this class. In
* last case you only have to give in the name you used with the constructor
* to generate an object of this class. In both upper cases this method can be
* called statically. If no parameter was given, the method tries to get the
* date from class variable $timestamp, which can be set by the method
* setTimestamp() of this class.
* @param mixed $itemname_or_timestamp
function getISODate( $itemname_or_timestamp = "" )
if ( empty( $itemname_or_timestamp ) )
if ( preg_match( "!^\d+$!", $itemname_or_timestamp ) )
return date( "Y-m-d H:i:s", $itemname_or_timestamp );
$day_str = $_REQUEST[ "day". $itemname_or_timestamp ];
$month_str = $_REQUEST[ "month". $itemname_or_timestamp ];
$year_str = $_REQUEST[ "year". $itemname_or_timestamp ];
$hour_str = $_REQUEST[ "hour". $itemname_or_timestamp ];
$minute_str = $_REQUEST[ "minute". $itemname_or_timestamp ];
$second_str = $_REQUEST[ "second". $itemname_or_timestamp ];
* Returns the timestamp value that can compute from the values of the
* @author Daniel Plücken <daniel@debakel.net>
return mktime( $h, $m, $s, $mt, $d, $y );
* Returns a generated string based on the attributes of this object's
* @author Daniel Plücken <daniel@debakel.net>
* Returns a generated string based on the attributes of this object's
* @author Daniel Plücken <daniel@debakel.net>
* Returns a generated string based on the attributes of this object's
* @author Daniel Plücken <daniel@debakel.net>
* Returns a generated string based on the attributes of this object's
* @author Daniel Plücken <daniel@debakel.net>
* Returns a generated string based on the attributes of this object's
* @author Daniel Plücken <daniel@debakel.net>
* Returns a generated string based on the attributes of this object's
* @author Daniel Plücken <daniel@debakel.net>
* Returns a generated string based on the attributes of this object's
* @author Daniel Plücken <daniel@debakel.net>
* With this method you can disable the checkup of guilty dates by
* @author Daniel Plücken <daniel@debakel.net>
* With this method you can set the checkup of guilty dates in the future by
* @author Daniel Plücken <daniel@debakel.net>
* With this method you can set the message that should appear if the given
* date isn't a date in the future.
* @author Daniel Plücken <daniel@debakel.net>
* Returns the name suffix of this select group.
* @author Daniel Plücken <daniel@debakel.net>
{ return $this->SETTING["instance"]; }
* Sets up the item to selecting the second of a date.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $selectedValue
* @param boolean $show_emptylabel
$fortimes = 60 / $this->SETTING["secondinterval"];
for( $i = 0; $i < $fortimes + $tmp_len; $i++ )
$valueArr[$i] = "". ($i * $this->SETTING["secondinterval"]);
$labelArr[$i] = ( $valueArr[$i] < 10 )
? "0". $valueArr[$i] : $valueArr[$i];
# compute the right minute
if( preg_match( "!^(?:[0-5][0-9]|[0-9])$!", $selectedValue ) )
$this->SETTING["hidden"]["SECOND"] = $hidden;
$this->SETTING["implementedSelects"]["SECOND"] = true;
? "try { onlyFutureDate( this.name ); } "
. "catch (e) {} finally {}"
'second'. $this->SETTING["instance"],
* Sets up the item to selecting the minute of a date.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $selectedValue
* @param boolean $show_emptylabel
$fortimes = 60 / $this->SETTING["minuteinterval"];
for( $i = $tmp_len; $i < $fortimes + $tmp_len; $i++ )
$valueArr[$i] = "". (( $i - $tmp_len ) * $this->SETTING["minuteinterval"]);
$labelArr[$i] = $valueArr[$i] < 10
# compute the right minute
if( preg_match( "!^(?:[0-5][0-9]|[0-9])$!", $selectedValue ) )
$this->SETTING["hidden"]["MINUTE"] = $hidden;
$this->SETTING["implementedSelects"]["MINUTE"] = true;
? "try { onlyFutureDate( this.name ); } "
. "catch (e) {} finally {}"
"minute". $this->SETTING["instance"],
* Sets up the item to selecting the hour of a date.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $selectedValue
* @param boolean $show_emptylabel
$show_emptylabel = false,
$tmp = $begin + $tmp_len;
$valueArr[$tmp] = "". $begin;
$labelArr[$tmp] = $valueArr[$tmp] < 10
# probe whether the hour is in the right range of values.
if( !preg_match( "!^(?:[01]?[0-9]|2[0-3])$!", $selectedValue ) )
$this->SETTING["hidden"]["HOUR"] = $hidden;
$this->SETTING["implementedSelects"]["HOUR"] = true;
? "try { onlyFutureDate( this.name ); } "
. "catch (e) {} finally {}"
* Sets up the item to selecting the day of a date.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $selectedValue
for( $i = 0; $i < 31; $i++ )
$labelArr[$i] = $valueArr[$i] < 10
? "0". $valueArr[$i] : $valueArr[$i];
$this->SETTING["hidden"]["DAY"] = $hidden;
$this->SETTING["implementedSelects"]["DAY"] = true;
$onChangeOrder = ( $this->jscheck )
? "try { checkDay( this.name ); "
? "onlyFutureDate( this.name ); "
. "} catch (e) {} finally {}"
* Sets up the item to selecting the weekday of a date.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $selectedValue
* @param boolean $show_emptylabel
for( $i = 0; $i < 7; $i++ )
$labelArr[$i] = ( $valueArr[$i] + 1 < 10 )
? "0". ( $valueArr[$i] + 1 )
: "". ( $valueArr[$i] + 1 );
$labelArr[$i] = $dayNames[$i];
$this->SETTING["hidden"]["WEEKDAYS"] = $hidden;
$this->SETTING["implementedSelects"]["WEEKDAYS"] = true;
? "try { onlyFutureDate( this.name ); "
. "} catch (e) {} finally {}"
'weekday'. $this->SETTING["instance"],
// doesn't function in all php versions
// $this->weekdaySelect->setCharset( "UTF-8" );
* Sets up the item to selecting the weeknum of a given year.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $selectedValue
for( $i = 1; $i <= $weeknumCount; $i++ )
$labelArr[] = $valueArr[$i - 1] < 10
if( !preg_match( "!^(?:[0-4]?[0-9]|5[0-3])$!", $selectedValue ) )
$this->SETTING["hidden"]["WEEKNUM"] = $hidden;
$this->SETTING["implementedSelects"]["WEEKNUM"] = true;
? "try { onlyFutureDate( this.name ); "
. "} catch (e) {} finally {}"
'weeknum'. $this->SETTING["instance"],
* Sets up the item to selecting the month of a given year.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $selectedValue
for( $i = 0; $i < 12; $i++ )
$labelArr[$i] = ( $valueArr[$i] < 10 )
$labelArr[$i] = $monthNames[$i];
if( !preg_match( "!^(?:0?[1-9]|1[0-2])$!", $selectedValue ) )
$this->SETTING["hidden"]["MONTH"] = $hidden;
$this->SETTING["implementedSelects"]["MONTH"] = true;
$onChangeOrder = ( $this->jscheck )
? "try { checkDay( this.name ); "
? "onlyFutureDate( this.name ); "
. "} catch (e) {} finally {}"
'month'. $this->SETTING["instance"],
// doesn't function in all php versions
// $this->monthSelect->setCharset( "UTF-8" );
* Sets up the item to selecting the year of a date. You have to define the
* value on which it should start and this one on which it should end.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $yearBegin
* @param integer $yearEnd
* @param string $selectedValue
for( $i = $yearBegin; $i <= $yearEnd; $i++ )
$this->SETTING["hidden"]["YEAR"] = $hidden;
$this->SETTING["implementedSelects"]["YEAR"] = true;
? "try { onlyFutureDate( this.name ); } "
. "catch (e) {} finally {}"
* Sets up the item to selecting the year of date. It will start on tree
* years ago and two years int he future
* @author Daniel Plücken <daniel@debakel.net>
* @param string $selectedValue
( !empty( $selectedIndex ) )
* Gets the second of a given timestamp and rounds it to the nearest value of
* @author Daniel Plücken <daniel@debakel.net>
* @param string $selectedValue
$SETTING = $this->getSettingsArray();
$temp = date( "s", $timestamp );
(60 / $SETTING["secondintervall"])
* Gets the minute of a given timestamp and rounds it to the nearest value of
* @author Daniel Plücken <daniel@debakel.net>
* @param string $selectedValue
$SETTING = $this->getSettingsArray();
$temp = date( "i", $timestamp );
(60 / $SETTING["minuteintervall"])
* Returns a generated string based on the attributes of this object.
* @author Daniel Plücken <daniel@debakel.net>
function get( $str_format = "" )
if ( empty( $str_format ) )
if ( $this->SETTING["hidden"]["WEEKDAYS"] )
{ $out = str_replace( "{#WEEKDAY#}", $this->getweekdaySelect(), $out ); }
if ( $this->SETTING["hidden"]["DAY"] )
if ( $this->SETTING["hidden"]["MONTH"] )
if ( $this->SETTING["hidden"]["WEEKNUM"] )
if ( $this->SETTING["hidden"]["YEAR"] )
if ( $this->SETTING["hidden"]["HOUR"] )
if ( $this->SETTING["hidden"]["MINUTE"] )
if ( $this->SETTING["hidden"]["SECOND"] )
if ( !empty( $this->label_obj->style_class ) )
{ $str_style .= " class=\"{$this->label_obj->style_class}\" "; }
{ $str_style .= " style=\"{$this->label_obj->freestyle}\" "; }
$out = "<fieldset{$str_style}>"
. "<legend>{$this->label_obj->str_body}</legend> ". $out
* Creates javascript-functions to verify the date choosen by an user.
* @author Daniel Plücken <daniel@debakel.net>
static $JSFutureOutputted = 0;
&& $JSFutureOutputted < 1
&& $this->SETTING["implementedSelects"]["DAY"]
&& $this->SETTING["implementedSelects"]["MONTH"]
&& $this->SETTING["implementedSelects"]["YEAR"]
$out .= "\r\n\r\n<script language=\"javascript\">\r\n";
$out .= " function onlyFutureDate( callerSelectName )\r\n";
$out .= " var second = 0;\r\n";
$out .= " var minute = 0;\r\n";
$out .= " var hour = 0;\r\n";
$out .= " var day = 0;\r\n";
$out .= " var month = -1;\r\n";
$out .= " var year = 0;\r\n\r\n";
$out .= " callerSelectName = callerSelectName."
. "replace( /second/, \"\" );\r\n";
$out .= " callerSelectName = callerSelectName."
. "replace( /minute/, \"\" );\r\n";
$out .= " callerSelectName = callerSelectName."
. "replace( /hour/, \"\" );\r\n";
$out .= " callerSelectName = callerSelectName."
. "replace( /day/, \"\" );\r\n";
$out .= " callerSelectName = callerSelectName."
. "replace( /month/, \"\" );\r\n";
$out .= " callerSelectName = callerSelectName."
. "replace( /year/, \"\" );\r\n\r\n";
$out .= " for( i = 0; i < document.forms.length; i++ )\r\n";
$out .= " if( eval('document.forms[' + i + '].second' "
. "&& eval('document.forms[' + i + '].second' "
. "+ callerSelectName ).value != '' )\r\n";
$out .= " second = parseInt( eval('document.forms[' + i + ']."
. "second' + callerSelectName ).value );\r\n";
$out .= " if( eval('document.forms[' + i + '].minute' "
. "&& eval('document.forms[' + i + '].minute' "
. "+ callerSelectName ).value != '' )\r\n";
$out .= " minute = parseInt( eval('document.forms[' + i + ']."
. "minute' + callerSelectName ).value );\r\n";
$out .= " if( eval('document.forms[' + i + '].hour' "
. "&& eval('document.forms[' + i + '].hour' "
. "+ callerSelectName ).value != '' )\r\n";
$out .= " hour = parseInt( eval('document.forms[' + i + ']."
. "hour' + callerSelectName ).value );\r\n";
$out .= " if( eval('document.forms[' + i + '].day' "
. "+ callerSelectName ) )\r\n";
$out .= " day = parseInt( eval('document.forms[' + i + ']."
. "day' + callerSelectName ).value );\r\n";
$out .= " if( eval('document.forms[' + i + '].month' "
. "+ callerSelectName ) )\r\n";
$out .= " month = parseInt( eval('document.forms[' + i + ']."
. "month' + callerSelectName ).value ) - 1;\r\n";
$out .= " if( eval('document.forms[' + i + '].year' "
. "+ callerSelectName ) )\r\n";
$out .= " year = parseInt( eval('document.forms[' + i + ']."
. "year' + callerSelectName ).value );\r\n";
$out .= " var now = new Date();\r\n";
$out .= " var now_unix = now.getTime();\r\n";
$out .= " var choosendate = new Date( year, month, day, "
. "hour, minute, second );\r\n";
$out .= " var choosendate_unix = choosendate.getTime();\r\n\r\n";
$out .= " if( now_unix < choosendate_unix )\r\n";
$out .= " return true;\r\n";
$out .= " alert( unescape( '". $this
$out .= " return false;\r\n";
$out .= "</script>\r\n\r\n\r\n";
$INSTANCE = $this->SETTING["instance"];
if( $this->SETTING["implementedSelects"]["DAY"]
&& $this->SETTING["implementedSelects"]["MONTH"]
$out .= "\r\n\r\n<script language=\"javascript\">\r\n";
$out .= " function isLeapYear(y)\r\n";
$out .= " { return ((y % 4 == 0) "
. "|| (y % 400 == 0))); }\r\n\r\n";
$out .= " function check30DaysMonth( month, day, "
. "callerSelectName, k )\r\n";
$out .= " // Monate mit 30 Tagen\r\n";
$out .= " var errMonth = \"\";\r\n";
$out .= " switch( month )\r\n";
$out .= " if( day > 30 )\r\n";
$out .= " errMonth = \"April\";\r\n";
$out .= " if( day > 30 )\r\n";
$out .= " errMonth = \"Juni\";\r\n";
$out .= " if( day > 30 )\r\n";
$out .= " errMonth = \"September\";\r\n";
$out .= " if( day > 30 )\r\n";
$out .= " errMonth = \"November\";\r\n";
$out .= " if( errMonth != \"\" )\r\n";
$out .= " alert( unescape( 'Der Monat ' + errMonth + ' hat maximal 30 Tage, "
. "der Wert der Tag-Auswahl auf 30 zur%FCckgestellt.\\t\\t' ) );\r\n";
$out .= " eval('document.forms[' + k + '].day' + callerSelectName ).value = 30;\r\n";
$out .= " return false;\r\n";
$out .= " return true;\r\n";
$out .= " function checkDay( callerSelectName )\r\n";
$out .= " callerSelectName = callerSelectName."
. "replace( /day/, \"\" );\r\n";
$out .= " callerSelectName = callerSelectName."
. "replace( /month/, \"\" );\r\n";
$out .= " callerSelectName = callerSelectName."
. "replace( /year/, \"\" );\r\n\r\n";
$out .= " for( h = 0; h < document.forms.length; h++ )\r\n";
$out .= " if( document.forms[h]\r\n";
$out .= " && eval('document.forms[' + h + '].day' "
. "+ callerSelectName )\r\n";
$out .= " && eval('document.forms[' + h + '].month' "
. "+ callerSelectName ) )\r\n";
$out .= " var day = parseInt( eval('document.forms[' + h + ']."
. "day' + callerSelectName ).value );\r\n";
$out .= " var month = parseInt( eval('document.forms[' + h + ']."
. "month' + callerSelectName ).value );\r\n\r\n";
$out .= " if( eval('document.forms[' + h + '].year' "
. "+ callerSelectName ) )\r\n";
$out .= " var year = parseInt( eval('document.forms[' + h + ']."
. "year' + callerSelectName ).value );\r\n\r\n";
$out .= " // Schaltjahr (gregorianisch, 27 Sekunden pro Jahr ist durchaus verkraftbar)\r\n";
$out .= " if( isLeapYear( year )\r\n";
$out .= " && ( month == 2 )\r\n";
$out .= " && ( day > 29 ) )\r\n";
$out .= " alert( unescape ( 'Der Monat Febuar hat(te) im Jahre ' + year "
. "+ ' maximal 29 Tage,\\r\\n"
. "daher wird der Wert der Tag-Auswahl auf "
. "29 zur%FCckgestellt.\\t\\t' ) );\r\n";
$out .= " eval('document.forms[' + h + '].day' + callerSelectName ).value = 29;\r\n";
$out .= " return false;\r\n";
$out .= " else if( !isLeapYear( year )\r\n";
$out .= " && month == 2 && day > 28 )\r\n";
$out .= " alert( unescape( 'Der Monat Febuar hat(te) im Jahre ' + year "
. "+ ' maximal 28 Tage,\\r\\n"
. "daher wird der Wert der Tag-Auswahl auf "
. "28 zur%FCckgestellt.\\t\\t' ) );\r\n";
$out .= " eval('document.forms[' + h + '].day' + callerSelectName ).value = 28;\r\n";
$out .= " return false;\r\n";
$out .= " return check30DaysMonth( month, day, "
. "callerSelectName, h );\r\n";
$out .= " if( month == 2 && day > 29 )\r\n";
$out .= " alert( unescape( 'Der Monat Febuar hat maximal 29 Tage, daher wird\\r\\n"
. "der Wert der Tag-Auswahl auf 29 zur%FCckgestellt.\\t\\t' ) );\r\n";
$out .= " eval('document.forms[' + h + '].day' + callerSelectName ).value = 29;\r\n";
$out .= " return false;\r\n";
$out .= " return check30DaysMonth( month, day, "
. "callerSelectName, h );\r\n";
$out .= " return true;\r\n";
$out .= "</script>\r\n\r\n\r\n";
} // END of class DateSelect
|