forms
[ class tree: forms ] [ index: forms ] [ all elements ]

Source for file BirthdayDateTextInput.class.php

Documentation is available at BirthdayDateTextInput.class.php

  1. <?php
  2. /**
  3.  * For including this file you have to define the constant "CLASSPATH".
  4.  * Because every include in the framework depends on the CLASSPATH definition.
  5.  * The CLASSPATH means the relative path to the folder that contains the
  6.  * framework GilliGan.
  7.  *
  8.  * @package    forms
  9.  * @subpackage items
  10.  */
  11. if!defined"CLASSPATH" ) )
  12. {
  13.   echo "<h3>You have to define the constant CLASSPATH!</h3>\n";
  14.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\n";
  15.   exit();
  16. }
  17.  
  18. if defined"LANG" ) )
  19. {
  20.    /**
  21.     * Including language specific messages.
  22.     */
  23.    require_once CLASSPATH."forms/items/BirthdayDateTextInput/".LANG.".inc.php";
  24.    /**
  25.     * Including language specific messages.
  26.     */
  27.    require_once CLASSPATH."core/lang_spec_values/".LANG.".inc.php";
  28. }
  29. else
  30. {
  31.    echo "<h3>You have to define the constant LANG!</h3>\n";
  32.    echo "Example for german: define( 'LANG', 'de' );\n";
  33.    exit();
  34. }
  35.  
  36. /**
  37.  *
  38.  */
  39. require_once CLASSPATH."forms/items/HTMLTextInput.class.php";
  40. /**
  41.  *
  42.  */
  43. require_once CLASSPATH."html/JavaScript.class.php";
  44.  
  45. /**
  46.  * A class to generate text input fields to fill in Dates.
  47.  *
  48.  * @package    forms
  49.  * @subpackage items
  50.  * @version    0.1.81
  51.  *
  52.  * @author     Daniel Plücken <daniel@debakel.net>
  53.  * @license    http://www.gnu.org/copyleft/lesser.html
  54.  *              GNU Lesser General Public License
  55.  * @copyright  Copyright (C) 2003 Daniel Plücken <daniel@debakel.net>
  56.  *
  57.  *  This library is free software; you can redistribute it and/or
  58.  *  modify it under the terms of the GNU Lesser General Public
  59.  *  License as published by the Free Software Foundation; either
  60.  *  version 2.1 of the License.
  61.  *
  62.  *  This library is distributed in the hope that it will be useful,
  63.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  64.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  65.  *  GNU Lesser General Public License for more details.
  66.  *
  67.  *  You should have received a copy of the GNU Lesser General
  68.  *  Public License along with this library; if not, write to the
  69.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  70.  *  Boston, MA 02111-1307 USA
  71.  */
  72. {
  73.   /**
  74.    * The template for how to interpret the date.
  75.    * The standardvalue follows ISO 8601.
  76.    *
  77.    * Valid chars are:
  78.    *  - y for year
  79.    *  - m for month
  80.    *  - d for day
  81.    *
  82.    * @access private
  83.    * @var    string $template_date_format 
  84.    */
  85.   var $template_date_format = "";
  86.  
  87.   /**
  88.    * Stores the interpreted day
  89.    *
  90.    * @access private
  91.    * @var    integer $day 
  92.    */
  93.   var $day = 0;
  94.  
  95.   /**
  96.    * Stores the interpreted month
  97.    *
  98.    * @access private
  99.    * @var    integer $month 
  100.    */
  101.   var $month = 0;
  102.  
  103.   /**
  104.    * Stores the interpreted year
  105.    *
  106.    * @access private
  107.    * @var    integer $year 
  108.    */
  109.   var $year = 0;
  110.  
  111.   /**
  112.    * Stores whether this object has been output already.
  113.    *
  114.    * @access private
  115.    * @var    integer $year 
  116.    */
  117.   var $first_output = true;
  118.  
  119.   /**
  120.     * Constructor
  121.     *
  122.     * @access  public
  123.     * @version 1.12
  124.     * @since   0.1.0
  125.     *
  126.     * @param   string  $name  The value of the "name"-attribute of the text
  127.     *                          input field.
  128.     * @param   string  $value The value of the "value"-attribute of the text
  129.     *                          input field.
  130.     * @param   integer $size  The value of the "size"-attribute of the text
  131.     *                          input field.
  132.     * @param   string  $str_date_template Sets the template how to display date
  133.     *                                      in the text input. The rules follow
  134.     *                                      the parameter rules of the
  135.     *                                      date()-function.
  136.     * @return  BirthdayDateTextInput 
  137.     */
  138.   function BirthdayDateTextInput(
  139.                                   $name  "",
  140.                                   $value "",
  141.                                   $size  0,
  142.                                   $str_date_template ""
  143.                                 )
  144.   {
  145.     $this->HTMLTextInput$name$value$size10 );
  146.     $this->setDateTemplate$str_date_template );
  147.   }
  148.  
  149.  
  150.  
  151.    /**
  152.     * Interprets a given date on the basis of a template.
  153.     *
  154.     * @access  public
  155.     * @version 1.0
  156.     * @since   1.78
  157.     *
  158.     * @param   string  $str_date 
  159.     * @param   string  $str_template 
  160.     * @param   boolean $store_format 
  161.     *
  162.     * @return  array   The date interpreted in an array in index order: year,
  163.     *                   month, day
  164.     */
  165.     function interpretDate(
  166.                               $str_date "",
  167.                               $str_template "",
  168.                               $store_format false
  169.                           )
  170.     {
  171.         if @is_object$this ) )
  172.         {
  173.             if empty$str_template ) )
  174.             {  $str_template $this->template_date_format}
  175.             else
  176.             if $store_format )
  177.             {  $this->setDateTemplate$str_template )}
  178.  
  179.             if empty$str_date ) )
  180.             {  $str_date $this->value}
  181.         }
  182.  
  183.         $y_pos strpos$str_template"y" );
  184.         $m_pos strpos$str_template"m" );
  185.         $d_pos strpos$str_template"d" );
  186.  
  187.         $int_max max$y_pos$d_pos$m_pos );
  188.         $int_min min$y_pos$d_pos$m_pos );
  189.  
  190.         $d_regexp_pos $m_regexp_pos $y_regexp_pos 2;
  191.         switch $int_max )
  192.         {
  193.             case $y_pos:
  194.                  $y_regexp_pos 3;
  195.                  break;
  196.             case $m_pos:
  197.                  $m_regexp_pos 3;
  198.                  break;
  199.             case $d_pos:
  200.                  $d_regexp_pos 3;
  201.                  break;
  202.             default:
  203.                  break;
  204.         }
  205.  
  206.         switch $int_min )
  207.         {
  208.             case $y_pos:
  209.                  $y_regexp_pos 1;
  210.                  break;
  211.             case $m_pos:
  212.                  $m_regexp_pos 1;
  213.                  break;
  214.             case $d_pos:
  215.                  $d_regexp_pos 1;
  216.                  break;
  217.             default:
  218.                  break;
  219.         }
  220.  
  221.  
  222.         $str_template str_replace".""\."$str_template );
  223.         $str_template str_replace"d""(\d{1,2})"$str_template );
  224.         $str_template str_replace"m""(\d{1,2})"$str_template );
  225.         $str_template str_replace"y""(\d{4,4})"$str_template );
  226.  
  227.         $matches_arr array();
  228.         preg_match"!^".$str_template."$!"$str_date$matches_arr );
  229.  
  230.         if @is_object$this && $store_format )
  231.         {
  232.             $this->day   = $matches_arr[$d_regexp_pos];
  233.             $this->month = $matches_arr[$m_regexp_pos];
  234.             $this->year  = $matches_arr[$y_regexp_pos];
  235.         }
  236.  
  237.         return array(
  238.                 $matches_arr[$y_regexp_pos],
  239.                 $matches_arr[$m_regexp_pos],
  240.                 $matches_arr[$d_regexp_pos]
  241.                     );
  242.     }
  243.  
  244.  
  245.  
  246.    /**
  247.     * Returns the value of this item in ISO format.
  248.     *
  249.     * @access  public
  250.     * @version 1.0
  251.     * @since   0.1.75
  252.     *
  253.     * @return  string The value of this input in ISO format.
  254.     */
  255.     function getISODate()
  256.     {
  257.         if preg_match"!^\d{4,4}-\d{1,2}-\d{1,2}$!"$this->value ) )
  258.            return $this->value;
  259.         else
  260.         {
  261.            $str_lang LANG;
  262.            if !empty$this->template_date_format ) )
  263.               $str_template $this->template_date_format;
  264.            else
  265.            switch $str_lang )
  266.            {
  267.                 case "de":
  268.                      $str_template "d.m.y";
  269.                      break;
  270.                 case "en":
  271.                      $str_template "m.d.y";
  272.                      break;
  273.                 default:
  274.                      $str_template "y-m-d";
  275.                      break;
  276.            }
  277.  
  278.            $tmp_arr $this->interpretDate$this->value$str_templatefalse );
  279.  
  280.            return $tmp_arr[0]."-".$tmp_arr[1]."-".$tmp_arr[2];
  281.         }
  282.     }
  283.  
  284.  
  285.  
  286.    /**
  287.     * A overwritten method to forbid to set the maximal capacity of this special
  288.     * kind of input.
  289.     *
  290.     * @access  public
  291.     * @version 1.0
  292.     * @since   0.1.7
  293.     *
  294.     * @return  void 
  295.     */
  296.    function setMaxChars()
  297.    die"It is not allowed to set the maximum capacity of date input." )}
  298.  
  299.  
  300.  
  301.    /**
  302.     * Makes a clone of this object an returns a reference to this clone.
  303.     *
  304.     * @access  public
  305.     * @version 1.0
  306.     * @since   0.1.2
  307.     *
  308.     * @param   string $new_name 
  309.     * @param   string $selected_value 
  310.     * @param   string $size 
  311.     *
  312.     * @return  object 
  313.     */
  314.     function getClone(
  315.                        $new_name       "",
  316.                        $selected_value "",
  317.                        $size           0
  318.                      )
  319.     {
  320.       $newInstance parent::getClone(
  321.                                         $new_name,
  322.                                         $selected_value,
  323.                                         10,
  324.                                         $size
  325.                                      );
  326.  
  327.       return $newInstance;
  328.    }
  329.  
  330.  
  331.  
  332.    /**
  333.     * Returns a javascript functions to validate the correctness of a date.
  334.     *
  335.     * @access  public
  336.     * @version 1.1
  337.     * @since   0.1.0
  338.     *
  339.     * @return  string 
  340.     */
  341.    function getJSFunctionsToCheckDate()
  342.    {
  343.       static $JSOutputted 0;
  344.  
  345.       if$JSOutputted )
  346.       {
  347.          $JSOutputted ++;
  348.  
  349.          $tmp_js_validate JS_REGEXP_VALID_DATE;
  350.          $tmp_js_digit_separator JS_DATE_DIGIT_SEPARATOR;
  351.          $tmp_js_msg_invalid JS_ERR_INVALID_DATE_MSG;
  352.          $tmp_js_msg_day_zero JS_ERR_DAY_IS_ZERO_MSG;
  353.          $tmp_js_msg_day_gt_31 JS_ERR_DAY_GREATER_THAN_31_MSG;
  354.          $tmp_js_msg_month_zero JS_ERR_MONTH_IS_ZERO_MSG;
  355.          $tmp_js_msg_month_gt_twelve JS_EER_MONTH_GREATER_THAN_TWELVE;
  356.          $tmp_js_msg_day_gt_29_in_feb_of_leap =
  357.                    JS_ERR_DAY_GREATER_THAN_29_IN_FEBUARY_OF_A_LEAP_YEAR;
  358.          $tmp_js_msg_day_gt_28_in_feb_of_non_leap =
  359.                    JS_ERR_DAY_GREATER_THAN_28_IN_FEBUARY_OF_A_NON_LEAP_YEAR;
  360.          $tmp_js_msg_day_gt_30 JS_ERR_DAY_GREATER_THAN_30_MSG;
  361.  
  362.          $temp  =
  363. <<<JSCRIPT
  364.  
  365.  
  366.     <script language="javascript">
  367.     <!--
  368.  
  369.         function removeLeadingZeros( str )
  370.         {
  371.             while ( str.substr( 0, 1 ) == "0" )
  372.                 str = str.substr( 1, str.length - 1 );
  373.  
  374.             return str;
  375.         }
  376.  
  377.         function isLeapYear( y )
  378.         { return ((y % 4 == 0) && ((y % 100!= 0) || (y % 400 == 0))); }
  379.  
  380.         function checkTextDate( inputItem )
  381.         {
  382.             if ( inputItem.value == "" )
  383.                return true;
  384.  
  385.             regExpress = /${tmp_js_validate}/;
  386.  
  387.             if ( !regExpress.test( inputItem.value ) )
  388.             {
  389.                 alert( unescape( '${tmp_js_msg_invalid}' ) );
  390.                 return false;
  391.             }
  392.  
  393.  
  394.             var date_tokens = inputItem.value.split( "${tmp_js_digit_separator}" );
  395.  
  396.             day   = parseInt( removeLeadingZeros( date_tokens[0] ) );
  397.             month = parseInt( removeLeadingZeros( date_tokens[1] ) );
  398.             year  = parseInt( removeLeadingZeros( date_tokens[2] ) );
  399.  
  400.             if ( day == 0 )
  401.             {
  402.                 alert( unescape( '${tmp_js_msg_day_zero}' ) );
  403.                 day = 1;
  404.             }
  405.  
  406.             if ( day > 31 )
  407.             {
  408.                 alert( unescape( '${tmp_js_msg_day_gt_31}' ) );
  409.                 day = 31;
  410.             }
  411.  
  412.             if ( month == 0 )
  413.             {
  414.                 alert( unescape( '${tmp_js_msg_month_zero}' ) );
  415.                 month = 1;
  416.             }
  417.  
  418.             if ( month > 12 )
  419.             {
  420.                 alert( unescape( '${tmp_js_msg_month_gt_twelve}' ) );
  421.                 month = 12;
  422.             }
  423.  
  424.             if ( isLeapYear( year ) && ( month == 2 ) && ( day > 29 ) )
  425.             {
  426.                 alert( unescape ( '${tmp_js_msg_day_gt_29_in_feb_of_leap}' ) );
  427.                 day = 29;
  428.             }
  429.             else
  430.             if ( !isLeapYear( year ) && month == 2 && day > 28 )
  431.             {
  432.                 alert( unescape( '${tmp_js_msg_day_gt_28_in_feb_of_non_leap}' ) );
  433.                 day = 28;
  434.             }
  435.             else
  436.             {
  437.                 // Month with 30 days
  438.                 var errMonth = "";
  439.                 switch( month )
  440.                 {
  441.                     case 4:
  442.                          if ( day > 30 )
  443.                             errMonth = "{$GLOBALS["monthNames"][3]}";
  444.                          break;
  445.                     case 6:
  446.                          if ( day > 30 )
  447.                             errMonth = "{$GLOBALS["monthNames"][5]}";
  448.                          break;
  449.                     case 9:
  450.                          if ( day > 30 )
  451.                             errMonth = "{$GLOBALS["monthNames"][8]}";
  452.                          break;
  453.                     case 11:
  454.                     if ( day > 30 )
  455.                             errMonth = "{$GLOBALS["monthNames"][10]}";
  456.                          break;
  457.                 }
  458.  
  459.                 if( errMonth != "" )
  460.                 {
  461.                     alert( unescape( '${tmp_js_msg_day_gt_30}' ) );
  462.                     day = 30;
  463.                 }
  464.             }
  465.  
  466.             day   = ( day < 10 ) ? "0" + day : day;
  467.             month = ( month < 10 ) ? "0" + month : month;
  468.  
  469.             var str_tmp = inputItem.getAttribute( "dateTemplate" );
  470.  
  471.             var str_val = str_tmp.replace( /d/i, day );
  472.             str_val = str_val.replace( /m/i, month );
  473.             str_val = str_val.replace( /y/i, year );
  474.             inputItem.value = str_val;
  475.  
  476.             return true;
  477.         }
  478.  
  479.     // -->
  480.     </script>
  481.  
  482.  
  483. JSCRIPT;
  484.  
  485.          return $temp;
  486.       }
  487.    }
  488.  
  489.  
  490.  
  491.    /**
  492.     * Returns a javascript to check if the given value is valid for this kind of
  493.     * input.
  494.     *
  495.     * @access  public
  496.     * @version 1.0
  497.     * @since   0.1.0
  498.     *
  499.     * @return string 
  500.     */
  501.    function getValidationOrder()
  502.    {
  503.       $outputstr "checkTextDate( document."
  504.                                   .$parentform -> name."."
  505.                                   .$this -> name
  506.                               ." )";
  507.  
  508.       return $outputstr;
  509.    }
  510.  
  511.  
  512.  
  513.    /**
  514.     * Generates the sourcecode to build this object and returns it.
  515.     *
  516.     * @access  public
  517.     * @version 1.0
  518.     * @since   0.1.4
  519.     *
  520.     * @return  string 
  521.     */
  522.    function getPHPSource()
  523.    {
  524.       $outputstr  "unset( \x24value );\n";
  525.       $outputstr .= "\x24value = \x24_REQUEST[\"".$this -> name."\"]\n";
  526.       $outputstr .= "\x24input[] = new BirthdayDateTextInput(\n"
  527.                    ."                                       \"".$this -> name."\",\n"
  528.                    ."                                       \x24value,\n"
  529.                    ."                                       \"".$this -> size."\"\n"
  530.                    ."                                    );\n";
  531.  
  532.       $outputstr .= !empty$this -> value )
  533.                     ? "if( \x24value === \"\" )\n"
  534.                      ."  \x24input[count(\x24input)-1] "
  535.                        ."-> setValue( \"".$this -> value."\" );\n\n"
  536.                     : "\n";
  537.  
  538.       return $outputstr;
  539.    }
  540.  
  541.  
  542.  
  543.    /**
  544.     * Sets the template how to display date in the text input. The rules follow
  545.     * the parameter rules of the date()-function.
  546.     *
  547.     * @access  public
  548.     * @version 1.12
  549.     * @since   0.1.77
  550.     *
  551.     * @param   string $str_date 
  552.     *
  553.     * @return  void 
  554.     */
  555.     function setDateTemplate$str_date "" )
  556.     {
  557.         if !empty$str_date ) )
  558.            $this->template_date_format $str_date;
  559.         else
  560.         {
  561.            $str_lang LANG;
  562.            switch $str_lang )
  563.            {
  564.                case "de":
  565.                     $this->template_date_format "d.m.y";
  566.                     break;
  567.                case "en":
  568.                     $this->template_date_format "m.d.y";
  569.                     break;
  570.                default:
  571.                     $this->template_date_format "y-m-d";
  572.                     break;
  573.            }
  574.         }
  575.     }
  576.  
  577.  
  578.    /**
  579.     * Returns a generated string based on the attributes of this object.
  580.     *
  581.     * @access  public
  582.     * @version 1.15
  583.     * @since   0.1.0
  584.     *
  585.     * @return  string 
  586.     */
  587.     function get()
  588.     {
  589.         if preg_match"!^\d{4,4}-\d{1,2}-\d{1,2}$!"$this->value ) )
  590.         {
  591.             $tmp_arr $this->interpretDate$this->value"y-m-d"false );
  592.  
  593.             $this->value $this->template_date_format;
  594.             $this->value str_replace"y"$tmp_arr[0]$this->value );
  595.             $this->value str_replace"m"$tmp_arr[1]$this->value );
  596.             $this->value str_replace"d"$tmp_arr[2]$this->value );
  597.         }
  598.  
  599.         if $this->first_output )
  600.         {
  601.             if empty$this->other_attributes ) )
  602.                $this->other_attributes "dateTemplate=\""
  603.                                             .$this->template_date_format
  604.                                                      ."\"";
  605.             else
  606.                $this->other_attributes .= " dateTemplate=\""
  607.                                             .$this->template_date_format
  608.                                                        ."\"";
  609.  
  610.             $this->first_output false;
  611.         }
  612.  
  613.         parent::setOnBlur"checkTextDate( this );".$this->blur );
  614.  
  615.         return $this->getJSFunctionsToCheckDate()
  616.               .parent::get();
  617.     }
  618. // END of class BirthdayDateTextInput
  619. ?>

Documentation generated on Thu, 05 Jun 2008 19:10:20 +0200 by phpDocumentor 1.4.1