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

Source for file form_field_select.class.php

Documentation is available at form_field_select.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6.  
  7. /**
  8.  * Contains class Form_Field_Select
  9.  *
  10.  * @package tangra_lib
  11.  * @subpackage form
  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.'exceptions/te_select_value_already_exists.class.php');
  22.  
  23.  
  24. /**
  25.  * Represents select field
  26.  *
  27.  * @package tangra_lib
  28.  * @subpackage form
  29.  */
  30. class Form_Field_Select extends Form_Field {
  31.     /**
  32.      * Array that contains options
  33.      *
  34.      * @var array 
  35.      * @internal
  36.      */
  37.     protected $options = array();
  38.     /**
  39.      * Empty value
  40.      *
  41.      * @var integer 
  42.      * @internal
  43.      */
  44.     protected $empty_value = NULL;
  45.  
  46.  
  47.     /**
  48.      * Constructor
  49.      *
  50.      * @param string $name Name of the field
  51.      * @param integer $value Default value
  52.      * @param boolean $required Is field required. Default is false
  53.      * @param integer $empty_value Value of empty value
  54.      */
  55.     function __construct($name$value NULL$required false$empty_value NULL{
  56.         parent::__construct($name$value$required);
  57.         $this->set_empty_value($empty_value);
  58.  
  59.         $this->add_potential_error('invalid_value');
  60.     }
  61.  
  62.  
  63.     /**
  64.      * Sets empty value
  65.      *
  66.      * @param integer $empty_value 
  67.      */
  68.     public function set_empty_value($empty_value{
  69.         $this->empty_value = $empty_value;
  70.     }
  71.  
  72.  
  73.     /**
  74.      * Returns empty value
  75.      *
  76.      * @return unknown 
  77.      */
  78.     public function get_empty_value({
  79.         return $this->empty_value;
  80.     }
  81.  
  82.  
  83.     /**
  84.      * Adds new option
  85.      *
  86.      * @param integer $value Value of the option
  87.      * @throws TE_Select_Value_Already_Exists
  88.      */
  89.     public function add_option($value{
  90.         if ($this->is_unique_value($value)) {
  91.             if ($value == $this->get_value()) {
  92.                 $selected true;
  93.             else {
  94.                 $selected false;
  95.             }
  96.  
  97.             $c count($this->options);
  98.             $this->options[$c]['value'$value;
  99.             $this->options[$c]['selected'$selected;
  100.             $this->options[$c]['html_value'$c 1;
  101.         else {
  102.             throw new TE_Select_Value_Already_Exists($value);
  103.         }
  104.  
  105.     }
  106.  
  107.  
  108.     /**
  109.      * Sets all options at once using array
  110.      *
  111.      * @param array $options_arr 
  112.      */
  113.     public function set_options($options_arr{
  114.         $this->options = array();
  115.         if (!is_array($options_arr)) {
  116.             throw new Tangra_Exception('$options_arr parameter is not an array');
  117.         }
  118.  
  119.         foreach($options_arr as $key => $value{
  120.             $this->add_option($value);
  121.         }
  122.     }
  123.  
  124.  
  125.     /**
  126.      * Performs basic check for validity
  127.      *
  128.      * @return boolean 
  129.      * @internal
  130.      */
  131.     public function basic_check({
  132.         $has_errors parent::basic_check();
  133.         $ok false;
  134.  
  135.         if (!$has_errors{
  136.             $current_value null;
  137.             foreach($this->options as $option{
  138.                 if ($option['html_value'== $this->get_html_value()) {
  139.                     $current_value $option['value'];
  140.                     $ok true;
  141.                     break;
  142.                 }
  143.             }
  144.  
  145.             if ($this->get_html_value(== $current_value{
  146.                 $ok true;
  147.             }
  148.  
  149.             if (!$ok{
  150.                 $this->set_error('invalid_value');
  151.                 $has_errors true;
  152.             else {
  153.                 if ($this->get_empty_value(!== NULL{
  154.                     if ($current_value == $this->get_empty_value(&& $this->get_required()) {
  155.                         $this->set_value($this->get_empty_value());
  156.                         $this->set_error('mandatory_missing');
  157.                         $has_errors true;
  158.                     }
  159.                 }
  160.             }
  161.         }
  162.  
  163.         return $has_errors;
  164.     }
  165.  
  166.  
  167.     /**
  168.      * Sets current value
  169.      *
  170.      * @param integer $value 
  171.      */
  172.     public function set_value($value{
  173.         $this->_set_value($value);
  174.         $this->set_option_selected($value);
  175.     }
  176.  
  177.  
  178.     /**
  179.      * Returns array with field properties
  180.      *
  181.      * @return array 
  182.      * @internal
  183.      */
  184.     public function get_properties_array({
  185.         $ret parent::get_properties_array();
  186.         $ret['options'$this->options;
  187.  
  188.         return $ret;
  189.     }
  190.  
  191.  
  192.     /**
  193.      * Returns all options as array
  194.      *
  195.      * @return array 
  196.      */
  197.     public function get_options({
  198.         return $this->options;
  199.     }
  200.  
  201.  
  202.     /**
  203.      * Sets current value
  204.      *
  205.      * @param integer $value Value of the option
  206.      * @internal
  207.      */
  208.     protected function _set_value($value{
  209.         parent::set_value($value);
  210.     }
  211.  
  212.  
  213.     /**
  214.      * marks option as selected
  215.      *
  216.      * @param integer $value Value of the option
  217.      * @internal
  218.      */
  219.     private function set_option_selected($value{
  220.         foreach($this->options as &$option{
  221.             if ($option['value'!= $value{
  222.                 $option['selected'false;
  223.             else {
  224.                 $option['selected'true;
  225.             }
  226.         }
  227.     }
  228.  
  229.  
  230.     /**
  231.      * Checks if there is option with same value
  232.      *
  233.      * @param integer $value Value of the option
  234.      * @return boolean 
  235.      * @internal
  236.      */
  237.     private function is_unique_value($value{
  238.         $ret true;
  239.  
  240.         foreach($this->options as $option{
  241.             if ($value == $option['value']{
  242.                 $ret false;
  243.                 break;
  244.             }
  245.         }
  246.  
  247.         return $ret;
  248.     }
  249.  
  250.  
  251.     /**
  252.      * Transfers data from html_value to value
  253.      * @internal
  254.      */
  255.     protected function translate_value_html2app({
  256.         foreach($this->options as $option{
  257.             if ($option['html_value'== $this->get_html_value()) {
  258.                 $this->set_value($option['value']);
  259.                 break;
  260.             }
  261.         }
  262.     }
  263.  
  264.     /**
  265.      * Sets default value
  266.      *
  267.      * Makes option with that value selected
  268.      *
  269.      * @param mixed $default_value 
  270.      */
  271.     public function set_default_value($default_value{
  272.         parent::set_default_value($default_value);
  273.         $this->set_option_selected($default_value);
  274.     }
  275. }