tangra logo
   
[ class tree: tangra_lib ] [ index: tangra_lib ] [ all elements ]
 

Source for file form_field_date_triad.class.php

Documentation is available at form_field_date_triad.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Form_Field_Date_Triad
  8.  *
  9.  * @package  tangra_lib
  10.  * @subpackage  form
  11.  */
  12.  
  13.  
  14. /**
  15.  *
  16.  */
  17. require_once(TANGRA_MAIN_DIR.'form/fields/form_field.class.php');
  18. /**
  19.  *
  20.  */
  21. require_once(TANGRA_MAIN_DIR.'nls/date.inc.php');
  22.  
  23.  
  24. /**
  25.  * Represents 3 HTML text fields.
  26.  *
  27.  * @package tangra_lib
  28.  * @subpackage form
  29.  */
  30. class Form_Field_Date_Triad extends Form_Field {
  31.     /**
  32.      * Day holder
  33.      *
  34.      * @var integer 
  35.      * @internal
  36.      */
  37.     private $day;
  38.     /**
  39.      * Month holder
  40.      *
  41.      * @var integer 
  42.      * @internal
  43.      */
  44.     private $month;
  45.     /**
  46.      * Year holder
  47.      *
  48.      * @var integer 
  49.      * @internal
  50.      */
  51.     private $year;
  52.  
  53.  
  54.     /**
  55.      * Constructor
  56.      *
  57.      * @param string $name Name of the field
  58.      * @param boolean $required Is field required?
  59.      * @param string $value Default value. Must be valid date in YYYY-MM-DD format or 0000-00-00
  60.      */
  61.     function __construct($name$required false$value '0000-00-00'{
  62.         parent::__construct($name$value$required);
  63.  
  64.         $this->add_potential_error('invalid_date');
  65.     }
  66.  
  67.  
  68.     /**
  69.      * Sets field value
  70.      *
  71.      * @param string $value. Must be valid date in YYYY-MM-DD format or 0000-00-00
  72.      */
  73.     public function set_value($value{
  74.         if ($value == '0000-00-00'{
  75.             $this->_set_value($value);
  76.         else {
  77.             if (is_valid_date($value)) {
  78.                 $this->_set_value($value);
  79.             else {
  80.                 throw new Tangra_Exception('Invalid date.');
  81.             }
  82.         }
  83.     }
  84.  
  85.  
  86.     /**
  87.      * Captures submit
  88.      *
  89.      * @param form_name $form_name 
  90.      * @param array $submit_array 
  91.      * @internal
  92.      */
  93.     public function capture_submit($form_name&$submit_array{
  94.         if (array_key_exists($form_name.'_'.$this->get_name().'_day'$submit_array)) {
  95.             $this->day = $submit_array[$form_name.'_'.$this->get_name().'_day'];
  96.             $this->day = str_pad($this->day2'0'STR_PAD_LEFT);
  97.         else {
  98.             $this->day = NULL;
  99.         }
  100.  
  101.         if (array_key_exists($form_name.'_'.$this->get_name().'_month'$submit_array)) {
  102.             $this->month = $submit_array[$form_name.'_'.$this->get_name().'_month'];
  103.             $this->month = str_pad($this->month2'0'STR_PAD_LEFT);
  104.         else {
  105.             $this->month = NULL;
  106.         }
  107.  
  108.         if (array_key_exists($form_name.'_'.$this->get_name().'_year'$submit_array)) {
  109.             $this->year = $submit_array[$form_name.'_'.$this->get_name().'_year'];
  110.             $this->year = str_pad($this->year4'0'STR_PAD_LEFT);
  111.         else {
  112.             $this->year = NULL;
  113.         }
  114.  
  115.         if ($this->day || $this->month || $this->year{
  116.             $this->set_html_value($this->year.'-'.$this->month.'-'.$this->day);
  117.         else {
  118.             $this->set_html_value(NULL);
  119.         }
  120.     }
  121.  
  122.  
  123.     /**
  124.      * Performs basic check for validity
  125.      *
  126.      * @return boolean 
  127.      * @internal
  128.      */
  129.     public function basic_check({
  130.         $has_errors false;
  131.  
  132.         if (!($this->get_required(&& $this->get_html_value(== '0000-00-00')) {
  133.             if ($this->get_html_value(!= '0000-00-00'{
  134.                 if (!is_valid_date($this->get_html_value())) {
  135.                     $this->set_error('invalid_date');
  136.                     $has_errors true;
  137.                 }
  138.             }
  139.         else {
  140.             $this->set_error('mandatory_missing');
  141.             $has_errors true;
  142.         }
  143.  
  144.         return $has_errors;
  145.     }
  146.  
  147.  
  148.     /**
  149.      * Returns day
  150.      *
  151.      * @return integer 
  152.      */
  153.     public function get_day({
  154.         return $this->day;
  155.     }
  156.  
  157.  
  158.     /**
  159.      * Returns month
  160.      *
  161.      * @return integer 
  162.      */
  163.     public function get_month({
  164.         return $this->month;
  165.     }
  166.  
  167.  
  168.     /**
  169.      * Returns year
  170.      *
  171.      * @return integer 
  172.      */
  173.     public function get_year({
  174.         return $this->year;
  175.     }
  176.  
  177.  
  178.     /**
  179.      * Sets field value
  180.      *
  181.      * @param string $value 
  182.      * @internal
  183.      */
  184.     private function _set_value($value{
  185.         $this->year = substr($value04);
  186.         $this->month = substr($value52);
  187.         $this->day = substr($value82);
  188.     }
  189. }