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

Source for file static_html_form_generator.class.php

Documentation is available at static_html_form_generator.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6.  
  7. /**
  8.  * Contains class Static_HTML_Form_Generator
  9.  *
  10.  * @package  tangra_lib
  11.  * @subpackage  form
  12.  */
  13.  
  14.  
  15. /**
  16.  *
  17.  */
  18. require_once(TANGRA_MAIN_DIR.'form/fields/hidden/form_field_hidden.class.php');
  19.  
  20. /**
  21.  *
  22.  */
  23. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_already_exists.class.php');
  24.  
  25. /**
  26.  *
  27.  */
  28. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_not_exists.class.php');
  29.  
  30.  
  31. /**
  32.  * Generates HTML for "static" forms
  33.  *
  34.  * Static forms are forms that once created remain the same in temrs of count of fields and their type.
  35.  * In contrary, dynamic form may can be manipulated at runtime and receive new fields (with usual add_field() method), or remove fields from it.
  36.  *
  37.  * @package  tangra_lib
  38.  * @subpackage  form
  39.  */
  40.     /**
  41.      * Form object
  42.      *
  43.      * @var Form 
  44.      * @internal
  45.      */
  46.     private $form;
  47.  
  48.     /**
  49.      * Path to template dir
  50.      *
  51.      * @var string 
  52.      */
  53.     private $tpl_path;
  54.  
  55.     /**
  56.      * Form language (in term of native language)
  57.      *
  58.      * @var string 
  59.      */
  60.     private $language;
  61.  
  62.     /**
  63.      * Map for fields templates
  64.      *
  65.      * @var array 
  66.      */
  67.     private $fields_tpl_map  = array();
  68.  
  69.     /**
  70.      * Map for fields' errors templates
  71.      *
  72.      * @var array 
  73.      */
  74.     private $errs_tpl_map = array();
  75.  
  76.  
  77.     /**
  78.      * Constructor
  79.      *
  80.      * @param Form $form 
  81.      * @param string $tpl_path 
  82.      * @param string $language 
  83.      */
  84.     function __construct(Form $form$tpl_path$language{
  85.         $this->tpl_path = tangra_normalize_path($tpl_path);
  86.         $this->form = $form;
  87.         $this->language = $language;
  88.     }
  89.  
  90.  
  91.     /**
  92.      * Returns template path
  93.      *
  94.      * @return unknown 
  95.      */
  96.     public function get_tpl_path({
  97.         return $this->tpl_path;
  98.     }
  99.  
  100.  
  101.     /**
  102.      * Returns Form object
  103.      *
  104.      * @return Form 
  105.      */
  106.     public function get_form({
  107.         return $this->form;
  108.     }
  109.  
  110.  
  111.     /**
  112.      * Returns form language
  113.      *
  114.      * @return string 
  115.      */
  116.     public function get_language({
  117.         return $this->language;
  118.     }
  119.  
  120.  
  121.     /**
  122.      * Returns generated HTML code
  123.      *
  124.      * @return string 
  125.      */
  126.     public function get_static_html({
  127.         $tple new Tple();
  128.  
  129.         $fields $this->form->get_fields();
  130.         $fields $this->sort_fields($fields);
  131.  
  132.         $c 0;
  133.         $f_arr array();
  134.         foreach($fields as $f{
  135.             $tmp_arr $f->get_properties_array();
  136.             $tmp_arr['tpl_file'$this->get_fields_tpl_map_entry_value($f->get_name());
  137.             $tmp_arr['err_tpl_file_arr'$this->errs_tpl_map[$f->get_name()];
  138.             $f_arr[$c$tmp_arr;
  139.  
  140.             $c++;
  141.         }
  142.  
  143.         $tple->set_delimiters('{@''@}');
  144.  
  145.  
  146. //        print_r_pre($f_arr);
  147.  
  148.         $tple->assign('fields'$f_arr);
  149.         $tple->assign('form_name'$this->form->get_name());
  150.         $tple->assign('submit'$this->form->get_submit_name());
  151.         $method $this->form->get_method(==  Tangra_Form_Submit_Method::GET
  152.                                 ? 'get'
  153.                                 : 'post';
  154.         $tple->assign('method'$method);
  155.         $tple->assign('enctype'$this->form->get_enctype());
  156.         $tple->assign('action'$this->form->get_action());
  157.         $tple->assign('_language'$this->get_language());
  158.  
  159.         $path $this->get_tpl_path();
  160.  
  161.         $tple->assign('path'$path);
  162.  
  163.  
  164.         return $tple->fetch($path.'form.tpl');
  165.     }
  166.  
  167.  
  168.     /**
  169.      * Creates default template maps for fields and fields potential errors
  170.      *
  171.      */
  172.     public function create_default_tpl_maps({
  173.         $fields $this->form->get_fields();
  174.  
  175.         foreach($fields as $f{
  176.             $field_tpl_path $this->get_tpl_path().'/fields/'.strtolower($f->get_type()).'.tpl';
  177.             $this->add_tpl_map_entry($f->get_name()$field_tpl_path);
  178.  
  179.             $this->errs_tpl_map[$f->get_name()$this->create_field_errors_tpl_map($f);
  180.         }
  181.     }
  182.  
  183.  
  184.     /**
  185.      * Changes existying map entry for template
  186.      *
  187.      * @param unknown_type $field_name 
  188.      * @param unknown_type $tpl_path 
  189.      */
  190.     public function change_tpl_map_entry($field_name$tpl_path{
  191.         if (array_key_exists($field_name$this->fields_tpl_map)) {
  192.             $this->fields_tpl_map[$field_name$tpl_path;
  193.         else {
  194.             throw new TE_Key_Not_Exists($field_name);
  195.         }
  196.     }
  197.  
  198.  
  199.     /**
  200.      * Changes map entry for field error template
  201.      *
  202.      * @param string $field_name Name of the field
  203.      * @param string $p_error_name Name of the error
  204.      * @param string $tpl_path  Path to template file
  205.      */
  206.     public function change_field_p_error_map_entry($field_name$p_error_name$tpl_path{
  207.         if (array_key_exists($field_name$this->errs_tpl_map)) {
  208.             if (array_key_exists($p_error_name$this->errs_tpl_map[$field_name])) {
  209.                 $this->errs_tpl_map[$field_name][$p_error_name$tpl_path;
  210.  
  211.             else {
  212.                 throw new TE_Key_Not_Exists('Field name: '.$field_name.'. Potential error name: '.$p_error_name);
  213.             }
  214.         else {
  215.             throw new TE_Key_Not_Exists('Field name: '.$field_name);
  216.         }
  217.     }
  218.  
  219.  
  220.     /**
  221.      * Enter description here...
  222.      *
  223.      * @param unknown_type $field_name 
  224.      * @return unknown 
  225.      */
  226.     public function get_fields_tpl_map_entry_value($field_name{
  227.         $ret false;
  228.  
  229.         if (array_key_exists($field_name$this->fields_tpl_map)) {
  230.             $ret $this->fields_tpl_map[$field_name];
  231.         else {
  232.             throw new TE_Key_Not_Exists($field_name);
  233.         }
  234.  
  235.         return $ret;
  236.     }
  237.  
  238.  
  239.     /**
  240.      * Adds new map entry for templates
  241.      *
  242.      * @param unknown_type $field_name 
  243.      * @param unknown_type $tpl_path 
  244.      * @internal
  245.      */
  246.     protected function add_tpl_map_entry($field_name$tpl_path{
  247.         if (!array_key_exists($field_name$this->fields_tpl_map)) {
  248.             $this->fields_tpl_map[$field_name$tpl_path;
  249.         else {
  250.             throw new TE_Key_Already_Exists($field_name);
  251.         }
  252.     }
  253.  
  254.  
  255.     /**
  256.      * Sorts fields array
  257.      *
  258.      * Purpose is to move all hidden fields at the end of the result array. That way hidden
  259.      * fields does not bother the sequence of odd/even rows when HTML is generated (e.g. if
  260.      * diferent styles are used for odd and even rows (and if template uses one row per field))
  261.      *
  262.      * @param array $fields 
  263.      * @return array 
  264.      * @internal
  265.      */
  266.     protected function sort_fields(&$fields{
  267.         $normal_fields array();
  268.         $hidden_fields array();
  269.  
  270.         foreach ($fields as $key => $value{
  271.             if ($value instanceof Form_Field_Hidden{
  272.                 $hidden_fields[$key$value;
  273.             else {
  274.                 $normal_fields[$key$value;
  275.             }
  276.         }
  277.  
  278.         $ret array_merge($normal_fields$hidden_fields);
  279.  
  280.         return $ret;
  281.     }
  282.  
  283.  
  284.     /**
  285.      * Creates default map for fields' potential errors
  286.      *
  287.      * @return unknown 
  288.      * @internal
  289.      */
  290.     private function create_field_errors_tpl_map($f{
  291.         $ret array();
  292.  
  293.         $f_p_arr $f->get_properties_array();
  294.         $f_err_arr $f_p_arr['p_errors'];
  295.         foreach($f_err_arr as $err{
  296.             $ret[$err$this->get_tpl_path().'/fields/stock_errors/'.$err.'.tpl';
  297.         }
  298.  
  299.         return $ret;
  300.     }
  301. }