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

Source for file form_field_select_multiple.class.php

Documentation is available at form_field_select_multiple.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. require_once(TANGRA_MAIN_DIR.'form/fields/select/form_field_select.class.php');
  17. /**
  18.  *
  19.  */
  20. require_once(TANGRA_MAIN_DIR.'exceptions/te_select_multiple_option_not_exists.class.php');
  21. /**
  22.  *
  23.  */
  24. require_once(TANGRA_MAIN_DIR.'exceptions/te_select_multiple_value_not_an_array.class.php');
  25.  
  26.  
  27. /**
  28.  * Represents select field with multiple choice
  29.  *
  30.  * @package tangra_lib
  31.  * @subpackage form
  32.  */
  33.     /**
  34.      * Enter description here...
  35.      *
  36.      * @param string $name Name of the field
  37.      * @param integer $value Array containing option values to be selected
  38.      * @param boolean $required Is field required. Default is false
  39.      */
  40.     function __construct($name$value NULL$required false{
  41.         parent::__construct($name$value$required);
  42.     }
  43.  
  44.  
  45.     /**
  46.      * Sets which options are selected
  47.      *
  48.      * @param array $value  Array that contains values of options to be selected
  49.      * @throws TE_Select_Multiple_Value_Not_An_Array
  50.      */
  51.     public function set_value($value{
  52.         if ($value != NULL{
  53.             if (is_array($value)) {
  54.                 parent::set_value(array());
  55.                 $tmp_value array();
  56.                 foreach($value as $v{
  57.                     $this->select_option($v);
  58.                     array_push($tmp_value$v);
  59.                 }
  60.                 parent::_set_value($tmp_value);
  61.             else {
  62.                 throw new TE_Select_Multiple_Value_Not_An_Array();
  63.             }
  64.         else {
  65.             $this->deselect_all_options();
  66.         }
  67.  
  68.     }
  69.  
  70.  
  71.     /**
  72.      * Marks option as selected
  73.      *
  74.      * @param integer $value Value of the option
  75.      * @throws TE_Select_Multiple_Option_Not_Exists
  76.      */
  77.     public function select_option($value{
  78.         $ok false;
  79.  
  80.         foreach($this->options as &$option{
  81.             if ($option['value'== $value{
  82.                 $option['selected'true;
  83.                 $ok true;
  84.                 break;
  85.             }
  86.         }
  87.  
  88.         if (!$ok{
  89.             throw new TE_Select_Multiple_Option_Not_Exists('Value: '.$value);
  90.         }
  91.     }
  92.  
  93.  
  94.     /**
  95.      * Marks options as not selected
  96.      *
  97.      * @param unknown_type $value Value of the option
  98.      * @throws TE_Select_Multiple_Option_Not_Exists
  99.      */
  100.     public function deselect_option($value{
  101.         $ok false;
  102.  
  103.         foreach($this->options as &$option{
  104.             if ($option['value'== $value{
  105.                 $option['selected'false;
  106.                 $ok true;
  107.                 break;
  108.             }
  109.         }
  110.  
  111.         if (!$ok{
  112.             throw new TE_Select_Multiple_Option_Not_Exists('Value: '.$value);
  113.         }
  114.     }
  115.  
  116.  
  117.     /**
  118.      * Captures submit
  119.      *
  120.      * @param unknown_type $form_name 
  121.      * @param unknown_type $submit_array 
  122.      * @internal
  123.      */
  124.     public function capture_submit($form_name&$submit_array{
  125.         if (array_key_exists($form_name.'_'.$this->get_name()$submit_array)) {
  126.             $this->set_html_value($submit_array[$form_name.'_'.$this->get_name()]);
  127.         else {
  128.             $this->set_html_value(array());
  129.         }
  130.     }
  131.  
  132.  
  133.     /**
  134.      * Performs basic check for validity
  135.      *
  136.      * @return boolean 
  137.      * @internal
  138.      */
  139.     public function basic_check({
  140.         $has_errors false;
  141.  
  142.         if ($this->get_required(&& $this->get_html_value(== NULL{
  143.             $has_errors true;
  144.             $this->set_error('mandatory_missing');
  145.         else {
  146.             $this->clear_errors();
  147.         }
  148.  
  149.         return $has_errors;
  150.     }
  151.  
  152.  
  153.     /**
  154.      * Transfers data from html_value to value
  155.      * @internal
  156.      */
  157.     public function translate_value_html2app({
  158.         $tmp_value array();
  159.  
  160.         $this->deselect_all_options();
  161.  
  162.         if (count($this->get_html_value())) {
  163.             foreach($this->get_html_value(as $html_value{
  164.                 array_push($tmp_value$this->options[$this->find_option_by_html_value($html_value)]['value']);
  165.     //            $this->select_option($this->options[$this->find_option_by_html_value($html_value)]['value']);
  166.             }
  167.         else {
  168.             $tmp_value array();
  169.         }
  170.  
  171.         $this->set_value($tmp_value);
  172.     }
  173.  
  174.  
  175.     /**
  176.      * Deselects all options
  177.      * @internal
  178.      *
  179.      */
  180.     private function deselect_all_options({
  181.         foreach($this->options as $option{
  182.             $this->deselect_option($option['value']);
  183.         }
  184.     }
  185.  
  186.  
  187.     /**
  188.      * Returns option value for option with html_value = <var>$html_value</var>
  189.      *
  190.      * @param integer $html_value 
  191.      * @return integer 
  192.      */
  193.     private function find_option_by_html_value($html_value{
  194.         $ret false;
  195.  
  196.         foreach($this->options as $key => $option {
  197.             if ($option['html_value'== $html_value{
  198.                 $ret $key;
  199.                 break;
  200.             }
  201.         }
  202.  
  203.         if ($ret === false{
  204.             throw new TE_Select_Multiple_Option_Not_Exists('html_value: '.$html_value);
  205.         }
  206.  
  207.         return $ret;
  208.     }
  209.  
  210. }