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

Source for file form_view.class.php

Documentation is available at form_view.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. // $Id$
  4. //
  5.  
  6.  
  7. /**
  8.  * Contains class Form_View
  9.  *
  10.  * @package  tangra_lib
  11.  * @subpackage  form
  12.  */
  13.  
  14.  
  15. /**
  16.  *
  17.  */
  18. require_once(TANGRA_MAIN_DIR.'interfaces/i_static_viewable.class.php');
  19.  
  20. /**
  21.  *
  22.  */
  23. require_once(TANGRA_MAIN_DIR.'web_site/tple_exports.class.php');
  24.  
  25. /**
  26.  *
  27.  */
  28. require_once(TANGRA_MAIN_DIR.'form/fields/form_field_view.class.php');
  29.  
  30. /**
  31.  *
  32.  */
  33. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_already_exists.class.php');
  34.  
  35. /**
  36.  *
  37.  */
  38. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_not_exists.class.php');
  39.  
  40. /**
  41.  * Form_View is used for static HTML representation of Form objects
  42.  *
  43.  * @package  tangra_lib
  44.  * @subpackage  form
  45.  */
  46. class Form_View extends Tangra_Class implements I_Static_Viewable {
  47.     /**
  48.      * Form for which is current view
  49.      *
  50.      * @var Form 
  51.      * @internal
  52.      */
  53.     private $form;
  54.  
  55.     /**
  56.      * Views of form fields
  57.      *
  58.      * @var array 
  59.      * @internal
  60.      *
  61.      */
  62.     private $fields_views = array();
  63.  
  64.  
  65.  
  66.     /**
  67.      * Charset that will be used to prepare data of the fields (escaping with htmleentities)
  68.      * Default value is UTF-8
  69.      * @var string 
  70.      */
  71.     protected $charset = 'UTF-8';
  72.  
  73.  
  74.  
  75.     /**
  76.      * Constructor
  77.      *
  78.      * @param Form $form 
  79.      */
  80.     function __construct(Form &$form{
  81.         $this->form = $form;
  82.     }
  83.  
  84.  
  85.     /**
  86.      * Returns TPLE exports of the form
  87.      *
  88.      * Returns object of class TPLE_Exports that contains all "assigns" required for
  89.      * correct HTML presentation of the form
  90.      *
  91.      * @return TPLE_Exports 
  92.      */
  93.     public function get_tple_exports({
  94.         $exports new TPLE_Exports();
  95.  
  96.         $fields $this->form->get_fields();
  97.  
  98.         foreach($fields as $f{
  99.             $exports_tmp $this->get_field_view_exports($f->get_name());
  100.             $exports->merge($exports_tmp);
  101.         }
  102.  
  103.         $form_name $this->form->get_name();
  104.  
  105.         return $exports;
  106.     }
  107.  
  108.  
  109.     /**
  110.      * Adds field's view object
  111.      *
  112.      * Each form field have to have own field view object.
  113.      *
  114.      * @param strinfg$field_name Name of the field
  115.      * @param Form_Field_View $field_view - view object
  116.      * @throws TE_Key_Already_Exists
  117.      */
  118.     public function add_field_view($field_nameForm_Field_View $field_view{
  119.         if (!array_key_exists($field_name$this->fields_views)) {
  120.             $this->fields_views[$field_name$field_view;
  121.         else {
  122.             throw new TE_Key_Already_Exists($field_name);
  123.         }
  124.     }
  125.  
  126.  
  127.     /**
  128.      * Returns exports of field view
  129.      *
  130.      * @param string $field_name Name of the field
  131.      * @return TPLE_Exports 
  132.      * @internal
  133.      */
  134.     private function get_field_view_exports($field_name{
  135.         if (array_key_exists($field_name$this->fields_views)) {
  136.             $exports $this->fields_views[$field_name]->get_tple_exports();
  137.         else {
  138.             throw new TE_Key_Not_Exists($field_name);
  139.         }
  140.  
  141.         return $exports;
  142.     }
  143.  
  144.  
  145.     /**
  146.      * Returns Form_Field_View object of field specified with $field_view
  147.      *
  148.      * @param string $field_name Name of the field
  149.      * @return Form_Field_View 
  150.      */
  151.     public function &get_field_view($field_name{
  152.         if (array_key_exists($field_name$this->fields_views)) {
  153.             return $this->fields_views[$field_name];
  154.         else {
  155.             throw new TE_Key_Not_Exists($field_name);
  156.         }
  157.     }
  158.  
  159.  
  160.     /**
  161.      * Sets charset that field views will use for escaping field data (i.e. htmlentities)
  162.      *
  163.      * @param string $charset 
  164.      */
  165.     public function set_charset($charset{
  166.         $this->charset = $charset;
  167.         //TODO - check against list of usable charsets
  168.     }
  169.  
  170.  
  171.     /**
  172.      * Returns charset
  173.      *
  174.      * @return string 
  175.      */
  176.     public function get_charset({
  177.         return $this->charset;
  178.     }
  179.  
  180.  
  181.     /**
  182.      * Sets charset of the form view and cycles all field views to pass them the charset (calling their set_charset method)
  183.      * Default value for parameter $charset is UTF-8
  184.      * @param string $charset 
  185.      */
  186.     public function set_charset_cascade($charset 'UTF-8'{
  187.         $this->set_charset($charset);
  188.  
  189.         foreach($this->fields_views as &$fv{
  190.             $fv->set_charset($charset);
  191.         }
  192.     }
  193.  
  194.  
  195. }