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

Source for file form_field.class.php

Documentation is available at form_field.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Form_Field_Date
  8.  *
  9.  * @package tangra_lib
  10.  * @subpackage form
  11.  */
  12.  
  13.  
  14. /**
  15.  *
  16.  */
  17. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_already_exists.class.php');
  18. /**
  19.  *
  20.  */
  21. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_not_exists.class.php');
  22.  
  23.  
  24. /**
  25.  * Represents abstract concept of HTML form field
  26.  *
  27.  * @package tangra_lib
  28.  * @subpackage form
  29.  */
  30. abstract class Form_Field extends Tangra_Class {
  31.     /**
  32.      * Name of the field
  33.      *
  34.      * @var string 
  35.      * @internal
  36.      */
  37.     private $name;
  38.     /**
  39.      * Is field required?
  40.      *
  41.      * @var boolean 
  42.      * @internal
  43.      */
  44.     private $required;
  45.     /**
  46.      * Value of the field
  47.      *
  48.      * @var mixed 
  49.      * @internal
  50.      */
  51.     private $value;
  52.     /**
  53.      * HTML value of the field
  54.      *
  55.      * @var string 
  56.      * @internal
  57.      */
  58.     private $html_value;
  59.     /**
  60.      * Default value
  61.      *
  62.      * @var mixed 
  63.      * @internal
  64.      */
  65.     private $default_value;
  66.     /**
  67.      * Type of the field. Contains class name of the field object
  68.      *
  69.      * @var string 
  70.      * @internal
  71.      */
  72.     private $type;
  73.     /**
  74.      * Array with potential errors
  75.      *
  76.      * @var array 
  77.      * @internal
  78.      */
  79.     private $potential_errors = array();
  80.     /**
  81.      * Array with current errors
  82.      *
  83.      * @var array 
  84.      * @internal
  85.      */
  86.     private $current_errors = array();
  87.  
  88.  
  89.     /**
  90.      * Constructor
  91.      *
  92.      * @param string $name Name of the field
  93.      * @param mixed $value Default value.
  94.      * @param boolean $required Is field required. Default is false
  95.      */
  96.     function __construct($name$value$required false{
  97.         $this->set_name($name);
  98.         $this->set_required($required);
  99.         $this->set_value($value);
  100.         $this->set_default_value($value);
  101.         $this->type = get_class($this);
  102.  
  103.         $this->add_potential_error('mandatory_missing');
  104.     }
  105.  
  106.  
  107.     /**
  108.      * Sets field's name
  109.      *
  110.      * @param string $name 
  111.      */
  112.     protected final function set_name($name{
  113.         if (ereg("^[a-z]{1}[a-z0-9_]{1,50}$"$name)) {
  114.             $this->name = strtolower($name);
  115.         else {
  116.             throw new Tangra_Exception('Invalid field name. Must be alphanumeric (underscore allowed), starting with letter, 50 characters max.');
  117.         }
  118.  
  119.     }
  120.  
  121.  
  122.     /**
  123.      * Returns field name
  124.      *
  125.      * @return string 
  126.      */
  127.     public final function get_name({
  128.         return $this->name;
  129.     }
  130.  
  131.  
  132.     /**
  133.      * Sets field's value
  134.      *
  135.      * @param mixed $value 
  136.      */
  137.     public function set_value($value{
  138.         $this->value = $value;
  139.     }
  140.  
  141.  
  142.     /**
  143.      * Returns field's value
  144.      *
  145.      * @return mixed 
  146.      */
  147.     public function get_value({
  148.         return $this->value;
  149.     }
  150.  
  151.  
  152.     /**
  153.      * Sets will the field be required
  154.      *
  155.      * @param boolean $required 
  156.      */
  157.     public function set_required($required{
  158.         $this->required = $required true false;
  159.     }
  160.  
  161.  
  162.     /**
  163.      * Returns if the field is required
  164.      *
  165.      * @return unknown 
  166.      */
  167.     public function get_required({
  168.         return $this->required;
  169.     }
  170.  
  171.     /**
  172.      * Returns type of the field (class name)
  173.      *
  174.      * @return string 
  175.      */
  176.     public function get_type({
  177.         return $this->type;
  178.     }
  179.  
  180.  
  181.     /**
  182.      * Sets default value
  183.      *
  184.      * @param mixed $default_value 
  185.      */
  186.     public function set_default_value($default_value{
  187.         $this->default_value = $default_value;
  188.     }
  189.  
  190.  
  191.     /**
  192.      * Returns default value
  193.      *
  194.      * @return mixed 
  195.      */
  196.     public function get_default_value({
  197.         return $this->default_value;
  198.     }
  199.  
  200.  
  201.     /**
  202.      * Set field's value to default value
  203.      *
  204.      */
  205.     public function set_to_default({
  206.         $this->value = $this->set_value($this->default_value);
  207.     }
  208.  
  209.  
  210.     /**
  211.      * Performs basic check for validity
  212.      *
  213.      * @return boolean 
  214.      * @internal
  215.      */
  216.     public function basic_check({
  217.         $has_errors false;
  218.  
  219.         if ($this->required && $this->get_html_value(== NULL{
  220.             $has_errors true;
  221.             $this->set_error('mandatory_missing');
  222.         else {
  223.             $this->clear_errors();
  224.         }
  225.  
  226.         return $has_errors;
  227.     }
  228.  
  229.  
  230.     /**
  231.      * Captures submit
  232.      *
  233.      * @param string $form_name 
  234.      * @param array $submit_array 
  235.      * @internal
  236.      */
  237.     public function capture_submit($form_name&$submit_array{
  238.         if (array_key_exists($form_name.'_'.$this->get_name()$submit_array)) {
  239.             $this->set_html_value($submit_array[$form_name.'_'.$this->get_name()]);
  240.         else {
  241.             $this->set_html_value(NULL);
  242.         }
  243.     }
  244.  
  245.  
  246.     /**
  247.      * Accepts submit data
  248.      * @internal
  249.      */
  250.     public function accept_submit({
  251.         if (!$this->get_errors()) {
  252.             $this->translate_value_html2app();
  253.         }
  254.     }
  255.  
  256.  
  257.     /**
  258.      * Adds potential error for the field
  259.      *
  260.      * @param string $error_name Name of the error
  261.      */
  262.     public function add_potential_error($error_name{
  263.         if (ereg("^[a-z]{1}[a-z0-9_]{1,50}$"$error_name)) {
  264.             if (!array_key_exists($error_name$this->potential_errors)) {
  265.                 $this->potential_errors[$error_name$error_name;
  266.             else {
  267.                 throw new TE_Key_Already_Exists($error_name);
  268.             }
  269.         else {
  270.             throw new Tangra_Exception('Invalid error name. Must be alphanumeric, starting with letter. Underscore is allowed.');
  271.         }
  272.     }
  273.  
  274.  
  275.     /**
  276.      * Returns array with field properties
  277.      *
  278.      * @return array 
  279.      * @internal
  280.      */
  281.     public function get_properties_array({
  282.         $ret['name'$this->get_name();
  283.         $ret['required'$this->get_required();
  284.         $ret['type'strtolower($this->get_type());
  285.         $ret['value'$this->is_field_in_error($this->get_html_value($this->get_value();
  286.         $ret['p_errors'array();
  287.         foreach($this->potential_errors as $err{
  288.             array_push($ret['p_errors']$err);
  289.         }
  290.  
  291.         foreach($this->current_errors as $err_name{
  292.             $ret['current_errors'][$err_name$this->potential_errors[$err_name];
  293.         }
  294.  
  295.         return $ret;
  296.     }
  297.  
  298.  
  299.     /**
  300.      * Nonsense function. Candidate for removal
  301.      *
  302.      * @param string $error_name 
  303.      * @return string 
  304.      * @internal
  305.      */
  306.     public function get_potential_error($error_name{
  307.         $ret false;
  308.  
  309.         if (array_key_exists($error_name$this->potential_errors)) {
  310.             $ret $this->potential_errors[$error_name];
  311.         else {
  312.             throw new TE_Key_Not_Exists($error_name);
  313.         }
  314.  
  315.         return $ret;
  316.     }
  317.  
  318.  
  319.     /**
  320.      * Sets field current error
  321.      *
  322.      * @param string $potential_error_name Name of the error
  323.      */
  324.     public function set_error($potential_error_name{
  325.         array_push($this->current_errors$this->get_potential_error($potential_error_name));
  326.     }
  327.  
  328.  
  329.     /**
  330.      * Return field's current errors
  331.      *
  332.      * @return unknown 
  333.      */
  334.     public function get_errors({
  335.         return $this->current_errors;
  336.     }
  337.  
  338.  
  339.     /**
  340.      * Clear current errors
  341.      *
  342.      */
  343.     public function clear_errors({
  344.         $this->current_errors = array();
  345.     }
  346.  
  347.  
  348.     /**
  349.      * Transfers data from html_value to value
  350.      * @internal
  351.      */
  352.     protected function translate_value_html2app({
  353.         $this->value = $this->get_html_value();
  354.     }
  355.  
  356.  
  357.     /**
  358.      * Sets html_value
  359.      *
  360.      * @param mixed $html_value 
  361.      */
  362.     protected function set_html_value($html_value{
  363.         $this->html_value = $html_value;
  364.     }
  365.  
  366.  
  367.     /**
  368.      * Returns html_value
  369.      *
  370.      * @return mixed 
  371.      */
  372.     public function get_html_value({
  373.         return $this->html_value;
  374.     }
  375.  
  376.  
  377.     /**
  378.      * Checks if field is in error
  379.      *
  380.      * @return boolean 
  381.      * @internal
  382.      */
  383.     protected function is_field_in_error({
  384.         $field_errors $this->get_errors();
  385.  
  386.         return $field_errors true false;
  387.     }
  388. }