Location: PHPKode > projects > FiForms Framework > FiForms/FiForms-includes/FiForms_iDateText.inc.php
<?php

/*
*******************************************************************************

    FiForms -- A collection of PHP classes designed 
    to facilitate rapid development of web-database software

    Copyright (C) 2003-2008  Daniel McFeeters

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    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
    General Public License for more details.

    You should have received a copy of the GNU 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


    The original author of this library can be contacted at the following 
    address:

    Daniel McFeeters
    182 Baker Rd.
    Faubush, KY 42544-6526
    email:databases [at] fiforms [dot] org
    http://www.fiforms.org/


iDateText class
Project Started May 4, 2003
*******************************************************************************
FiForms_iDateText.inc.php

iDateText Definition File

Displays a three-part input for entering a date in the form Month/Day/Year.
Should be bound to a Date or Date/Time field. (Note that when a record
is updated, the time aspect of the date/time field will be lost.)

*******************************************************************************
*/

require_once("FiForms_iInput.inc.php");



require_once($GLOBALS['FIFORMS_CONFIG']['SCRIPT_PATH']."themes/".$FIFORMS_CONFIG['ICON_SET']."/iconset.php");
/* ?><code><?php */
$FIFORM_XML_INPUTS[] = 'f:iDateText';

class iDateText extends iInput
{
    var $month;
    var $day;
    var $year;
    var $autoDate;
        
    function iDateText()
    {
        $this->iInput(func_get_args());
        $this->autoDate = TRUE;
    } // function drawInput

    function getValueToSave($exit = false)
    {
        // Combines the MONTH, DAY, and YEAR parts of the date into
        // a single day.
        if(array_key_exists($this->dbField."_YEAR",$_POST))
        {
            // Get the values from POST
            $year = (int)$_POST[$this->dbField."_YEAR"];
            $month = (int)$_POST[$this->dbField."_MONTH"];
            $day = (int)$_POST[$this->dbField."_DAY"];

            if($this->autoDate)
            {
                if($year == 0)
                {
                $year = date("Y");
                }
                if($month == 0 && $day == 0)
                {
                $month = date("m");
                $day = date("d");
                }
            } // if autoDate
            // Check for and correct a 2-digit year
            // cut-off at 1950
            if($year <= 50 && $year != 0)
            {
                $year += 2000;
            }
            elseif($year < 100 && $year != 0)
            {
                $year += 1900;
            }
            if($year+$month+$day != 0 && $_POST[$this->dbField."_YEAR"] != '')
            {
                $this->valueToSave = 
                    sprintf("%04d-%02d-%02d", $year, $month, $day); 
                $dateCheck = mktime(0,0,0,$month,$day,$year);
                if((date("m",$dateCheck) != $month ||
                date("d",$dateCheck) != $day) && $year > 1970 && $year < 2038)
                {
                $this->errorMsg = "Bad Date.";
                return FALSE;
                }
            }
            else
            {
                $this->valueToSave = '';
            }

        }
        if($exit)
        {
            return(TRUE);
        }
    }


    function setupDate()
    {
        //Generate a three-part input to accept MONTH, DAY, and YEAR
        $this->month = new iText($this->dbField."_MONTH"," ");
        $this->day = new iText($this->dbField."_DAY"," ");
        $this->year = new iText($this->dbField."_YEAR"," ");
        $this->month->value = substr($this->value,5,2);
        $this->day->value = substr($this->value,8,2);
        $this->year->value = substr($this->value,0,4);
        $this->month->size = 2;
        $this->day->size = 2;
        $this->year->size = 4;
        $this->month->class = "no_border";
        $this->day->class = "no_border";
        $this->year->class = "no_border";
        $this->month->otherTags = 
            " onkeypress=\"return date_month_keypress(event,'".$this->dbField."');\"
            onclick=\"selectAll(this);\"
            onchange=\"refreshCalendar('".$this->dbField."');\"
            onblur=\"date_month_lostfocus('".$this->dbField."');\"";
        $this->day->otherTags = 
            " onkeypress=\"return date_day_keypress(event,'".$this->dbField."');\"
            onclick=\"selectAll(this);\"
            onchange=\"refreshCalendar('".$this->dbField."');\"
            onblur=\"date_day_lostfocus('".$this->dbField."');\"";
        $this->year->otherTags = 
            " onkeypress=\"return date_year_keypress(event,'".$this->dbField."');\"
            onclick=\"selectAll(this);\"
            onblur=\"date_year_lostfocus('".$this->dbField."');\"";
    }

    function drawInput()
    {
    $this->setupDate();
        // Output only the values if readonly
        if($this->readOnly == TRUE)
        {
            if($this->month->value == "")
            {
                return('');
            }
            else
            {
                return($this->month->value."/".$this->day->value.
                    "/".$this->year->value);
            }
        }
        // Output the controls in normal mode
        else
        {
            $this->icons = new iconset();
            return("<span class=\"text\">".$this->month->drawInput()."/".
                    $this->day->drawInput()."/".$this->year->drawInput().
                    "<span onclick=\"return showCalendar('".
                    $this->dbField."');\" name=\"select_".
                    $this->dbField."\" ".
                    "id=\"select_".$this->dbField."\">".
                    $this->icons->down."</span>".
                    "</span>");
        }

    } // function drawInput

    function buildFromXML($input)
    {
        parent::buildFromXML($input);
        if($input->attributes->getNamedItem('default')->nodeValue 
                == "today")
        {
                $this->value = date("Y-m-d");
        } // if default is "today"
    }

} // class iDateText

/* ?></code><?php */

?>
Return current item: FiForms Framework