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

Source for file form_field_radio_group.class.php

Documentation is available at form_field_radio_group.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Form_Field_Radio_Group
  8.  *
  9.  * @package tangra_lib
  10.  * @subpackage form
  11.  */
  12.  
  13. /**
  14.  *
  15.  */
  16. require_once(TANGRA_MAIN_DIR.'form/fields/form_field.class.php');
  17. /**
  18.  *
  19.  */
  20. require_once(TANGRA_MAIN_DIR.'exceptions/te_radio_value_already_exists.class.php');
  21.  
  22.  
  23. /**
  24.  * Represents group of radio buttons
  25.  *
  26.  * @package tangra_lib
  27.  * @subpackage form
  28.  */
  29.     /**
  30.      * Array that contains radio buttons
  31.      *
  32.      * @var array 
  33.      * @internal
  34.      */
  35.     private $radios = array();
  36.  
  37.  
  38.     /**
  39.      * Constructor
  40.      *
  41.      * @param string $name Name of the field
  42.      * @param integer $value Default value.
  43.      * @param boolean $required Is field required. Default is false
  44.      */
  45.     function __construct($name$value NULL$required false{
  46.         parent::__construct($name$value$required);
  47.  
  48.         $this->add_potential_error('invalid_value');
  49.     }
  50.  
  51.  
  52.     /**
  53.      * Adds radio button
  54.      *
  55.      * @param integer $value Value of the radio button
  56.      * @throws  TE_Radio_Value_Already_Exists
  57.      */
  58.     public function add_radio($value{
  59.         if ($this->is_unique_value($value)) {
  60.             if ($value == $this->get_value()) {
  61.                 $checked true;
  62.             else {
  63.                 $checked false;
  64.             }
  65.  
  66.             $c count($this->radios);
  67.             $this->radios[$c]['value'$value;
  68.             $this->radios[$c]['checked'$checked;
  69.             $this->radios[$c]['html_value'$c 1;
  70.         else {
  71.             throw new TE_Radio_Value_Already_Exists($value);
  72.         }
  73.     }
  74.  
  75.  
  76.     /**
  77.      * Sets all radios at once using array
  78.      *
  79.      * @param array $radios_arr 
  80.      */
  81.     public function set_radios($radios_arr{
  82.         $this->radios = array();
  83.  
  84.         foreach($radios_arr as $key => $value{
  85.             $this->add_radio($value);
  86.         }
  87.     }
  88.  
  89.  
  90.     /**
  91.      * Performs basic check for validity
  92.      *
  93.      * @return boolean 
  94.      * @internal
  95.      */
  96.     public function basic_check({
  97.         $has_errors parent::basic_check();
  98.         $ok false;
  99.         if (!$has_errors{
  100.             if ($this->get_required()) {
  101.                 $ok $this->is_value_valid($this->get_html_value());
  102.                 if (!$ok{
  103.                     $this->set_error('invalid_value');
  104.                     $has_errors true;
  105.                 }
  106.             else {
  107.                 if ($this->get_html_value(!= NULL{
  108.                     $ok $this->is_value_valid($this->get_html_value());
  109.                     if (!$ok{
  110.                         $this->set_error('invalid_value');
  111.                         $has_errors true;
  112.                     }
  113.                 }
  114.             }
  115.         }
  116.  
  117.         return $has_errors;
  118.     }
  119.  
  120.  
  121.     /**
  122.      * Checks if there is radio button with that value
  123.      *
  124.      * @param integer $value 
  125.      * @return unknown 
  126.      */
  127.     private function is_value_valid($value{
  128.         $ret false;
  129.  
  130.         foreach($this->radios as $radio{
  131.             if ($radio['html_value'== $value{
  132.                 $ret true;
  133.                 break;
  134.             }
  135.         }
  136.  
  137.         return $ret;
  138.     }
  139.  
  140.  
  141.     /**
  142.      * Sets value
  143.      *
  144.      * @param integer $value 
  145.      */
  146.     public function set_value($value{
  147.         parent::set_value($value);
  148.         $this->set_radio_checked($value);
  149.     }
  150.  
  151.  
  152.     /**
  153.      * Returns array with field properties
  154.      *
  155.      * @return array 
  156.      * @internal
  157.      */
  158.     public function get_properties_array({
  159.         $ret parent::get_properties_array();
  160.         $ret['radios'$this->radios;
  161.         $ret['missing'true;
  162.  
  163.         return $ret;
  164.     }
  165.  
  166.  
  167.     /**
  168.      * Returns all radios as array
  169.      *
  170.      * @return array 
  171.      */
  172.     public function get_radios({
  173.         return $this->radios;
  174.     }
  175.  
  176.  
  177.     /**
  178.      * Checks if there is a radio with same value
  179.      *
  180.      * @param integer $value 
  181.      * @return boolean 
  182.      * @internal
  183.      */
  184.     private function is_unique_value($value{
  185.         $ret true;
  186.  
  187.         foreach($this->radios as $radio{
  188.             if ($value == $radio['value']{
  189.                 $ret false;
  190.                 break;
  191.             }
  192.         }
  193.  
  194.         return $ret;
  195.     }
  196.  
  197.  
  198.     /**
  199.      * Marks radio as checked
  200.      *
  201.      * @param integer $value Value of radio that will be marked as checked
  202.      */
  203.     private function set_radio_checked($value{
  204.         foreach($this->radios as &$radio{
  205.             if ($radio['value'!= $value{
  206.                 $radio['checked'false;
  207.             else {
  208.                 $radio['checked'true;
  209.             }
  210.         }
  211.     }
  212.  
  213.  
  214.     /**
  215.      * Transfers data from html_value to value
  216.      * @internal
  217.      */
  218.     protected function translate_value_html2app({
  219.         foreach($this->radios as $radio{
  220.             if ($radio['html_value'== $this->get_html_value()) {
  221.                 $this->set_value($radio['value']);
  222.                 break;
  223.             }
  224.         }
  225.     }
  226. }