Changeset 437

Show
Ignore:
Timestamp:
05/09/07 18:21:13 (2 years ago)
Author:
laurentj
Message:

worked on a html builder for jforms, and added unit tests on jforms

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/experimental/jforms/build/manifests/jelix-lib.mn

    r436 r437  
    111111  jForms.class.php 
    112112  jFormsBase.class.php 
     113  jFormsBuilderBase.class.php 
    113114  jFormsCompiler.class.php 
    114115  jFormsControl.class.php 
    115116  jFormsDataContainer.class.php 
    116117  jFormsDatasource.class.php 
     118  jforms_jstemplate.php 
    117119 
    118120cd lib/jelix/tpl/ 
  • branches/experimental/jforms/build/manifests/testapp.mn

    r436 r437  
    8484  utjaclusergroup.class.php 
    8585  test.iface.php 
     86  utjforms.class.php 
    8687cd testapp/modules/unittest/classes/tests 
    8788  foo.class.php 
     
    109110  tpl.classic.php 
    110111  acl.classic.php 
     112  jforms.classic.php 
    111113 
    112114cd testapp/var 
  • branches/experimental/jforms/lib/jelix-www/js/jforms.js

    r432 r437  
    3232*/ 
    3333 
     34 
     35var gForm, gControl; 
    3436 
    3537/* 
     
    234236    this.datatype = datatype; 
    235237    this.required = false; 
     238    this.readonly = false; 
    236239    this.errInvalid = ''; 
    237240    this.errRequired = ''; 
  • branches/experimental/jforms/lib/jelix/core/jSelector.class.php

    r436 r437  
    652652    } 
    653653 
    654     public function getCompiledBuildFilePath (){ 
    655         return JELIX_APP_TEMP_PATH.'compiled/'.$this->_dirname.$this->module.'~'.$this->resource.'_htmlbuilder'.$this->_cacheSuffix;
     654    public function getCompiledBuilderPath ($type){ 
     655        return JELIX_APP_TEMP_PATH.'compiled/'.$this->_dirname.$this->module.'~'.$this->resource.'_builder_'.$type.$this->_cacheSuffix
    656656    } 
    657657} 
  • branches/experimental/jforms/lib/jelix/docs/ns/jforms-controls.rng

    r436 r437  
    66     <attribute name="ref" /> 
    77     <ref name="label" /> 
    8      <zeroOrMore> 
     8     <!--<zeroOrMore> 
    99        <choice> 
    1010           <ref name="help" /> 
    1111           <ref name="hint" /> 
    1212           <ref name="alert" /> 
    13            <!--<ref name="actions" />--
     13           <ref name="actions" /
    1414        </choice> 
    15      </zeroOrMore> 
    16   </define> 
    17    
     15     </zeroOrMore>--> 
     16  </define> 
     17  <!-- 
    1818  <define name="UI.Common.Attrs"> 
    1919       
    20   </define> 
     20  </define>--> 
    2121   
    2222   
     
    2727  </define> 
    2828 
     29   
    2930  <define name="List.UI.Common"> 
    3031     <choice> 
     
    3435        <ref name="daodatasource" /> 
    3536    </choice> 
    36     <optional> 
     37    <!-- <optional> 
    3738         <attribute name="appearance"> 
    3839             <choice> 
     
    4243             </choice> 
    4344         </attribute> 
    44       </optional> 
    45   </define> 
    46  
     45      </optional> --> 
     46  </define> 
     47  <define name="item"> 
     48     <element name="item"> 
     49         <optional> 
     50            <choice> 
     51                <attribute name="label" /> 
     52                <attribute name="labellocale" /> 
     53             </choice> 
     54         </optional> 
     55         <attribute name="value" /> 
     56     </element> 
     57  </define> 
     58   
    4759  <define name="datatypes"> 
    48      <choice
     60     <!--<choice>--
    4961        <ref name="datatype.attr"/> 
    50         <element name="datatype"> 
     62     <!--   <element name="datatype"> 
    5163           <ref name="datatype.attr"/> 
    5264           <optional><attribute name="length" /></optional> 
     
    5769           <optional><attribute name="minvalue" /></optional> 
    5870           <empty /> 
    59         </element> 
     71        </element>--> 
    6072  </define> 
    6173 
     
    8496      <attribute name="daovalueproperty" /> 
    8597  </define> 
    86    
     98 
    8799  <!--  Controls       --> 
    88  
    89  
    90100   <define name="controls"> 
    91101      <oneOrMore> 
     
    112122     <element name="input"> 
    113123       <ref name="UI.Common" /> 
     124       <ref name="UI.Common.data" /> 
    114125     </element> 
    115126  </define> 
     
    131142     <element name="upload"> 
    132143       <ref name="UI.Common" /> 
    133        <attribute name="mediatype" /> <!-- indique les types autorisés --> 
     144       <!--<attribute name="mediatype" /> --> 
    134145     </element> 
    135146  </define> 
     
    138149     <element name="select1"> 
    139150       <ref name="UI.Common" /> 
    140        <ref name="UI.Common.Attrs" /> 
    141151       <ref name="List.UI.Common" /> 
    142        <!-- saisie libre autorisée --> 
    143        <!--<optional> 
     152        <optional> 
    144153           <attribute name="selection"> 
    145154             <choice> 
     
    148157              </choice> 
    149158           </attribute> 
    150        </optional>--> 
     159       </optional> 
    151160     </element> 
    152161  </define> 
     
    156165     <element name="select"> 
    157166       <ref name="UI.Common" /> 
    158        <ref name="UI.Common.Attrs" /> 
    159167       <ref name="List.UI.Common" /> 
    160        <!--<optional> 
     168       <optional> 
    161169           <attribute name="selection"> 
    162170             <choice> 
     
    165173              </choice> 
    166174           </attribute> 
    167        </optional>--> 
     175       </optional> 
    168176     </element> 
    169177  </define> 
     
    172180     <element name="output"> 
    173181       <ref name="UI.Common" /> 
    174        <ref name="UI.Common.Attrs" /> 
    175        <optional><attribute name="value"/></optional> 
     182       <!--<optional><attribute name="value"/></optional>--> 
    176183     </element> 
    177184  </define> 
     
    180187     <element name="submit"> 
    181188       <ref name="UI.Common" /> 
    182        <ref name="UI.Common.Attrs" /> 
    183        <optional><attribute name="value"/></optional> 
    184      </element> 
    185   </define> 
    186  
     189       <!--<optional><attribute name="value"/></optional>--> 
     190     </element> 
     191  </define> 
     192--> 
    187193   
    188194  <!-- 
     
    217223  <define name="label"> 
    218224     <element name="label"> 
    219         <choice> 
    220             <ref name="message.content" /> 
    221             <attribute name="locale" /> 
    222         </choice> 
    223      </element> 
    224   </define> 
    225  
     225        <ref name="message.content" /> 
     226     </element> 
     227  </define> 
     228 
     229  <!-- 
    226230  <define name="hint"> 
    227231     <element name="hint"> 
     
    241245     </element> 
    242246  </define> 
    243  
    244   <define name="item"> 
    245      <element name="item"> 
    246          <choice> 
    247              <ref name="label" /> 
    248              <attribute name="label" /> 
    249              <attribute name="labellocale" /> 
    250          </choice> 
    251          <attribute name="value" /> 
    252      </element> 
    253   </define> 
     247--> 
     248 
    254249 
    255250</grammar> 
  • branches/experimental/jforms/lib/jelix/docs/ns/jforms.rng

    r436 r437  
    1212      </element> 
    1313   </start> 
    14  
     14<!-- 
    1515  <define name="Common.Attributes"> 
    1616 
    1717  </define> 
    18  
    19  
    20  
     18--> 
     19<!-- 
    2120   <define name="any"> 
    2221      <element> 
     
    3332      </element> 
    3433   </define> 
    35  
     34--> 
    3635</grammar> 
  • branches/experimental/jforms/lib/jelix/forms/jFormsBase.class.php

    r432 r437  
    4444     */ 
    4545    protected $_readOnly = false; 
     46 
     47    /** 
     48     * content list of available form builder 
     49     * @var boolean 
     50     */ 
     51    protected $_builders = array(); 
     52 
     53 
    4654     
    4755    /** 
     
    193201 
    194202 
     203    /** 
     204     * @param string $buildertype  the type name of a form builder 
     205     * @return jFormsBuilderBase 
     206     */ 
     207    public function getBuilder($buildertype){ 
     208        if(isset($this->_builders[$buildertype])){ 
     209            include_once ($this->_builders[$buildertype][0]); 
     210            $c =  $this->_builders[$buildertype][1]; 
     211            return new $c($this); 
     212        }else{ 
     213            throw new Exception('invalid form builder type'); 
     214        } 
     215    } 
    195216 
    196217} 
  • branches/experimental/jforms/lib/jelix/forms/jFormsBuilderBase.class.php

    r374 r437  
    4141     * @param jFormsBase $form a form object 
    4242     * @param string $action action selector where form will be submit 
     43     * @param array $actionParams  parameters for the action 
    4344     */ 
    44     public function __construct($form, $action){ 
     45    public function __construct($form, $action, $actionParams){ 
    4546        $this->_form = $form; 
    4647        $this->_action = $action; 
     48        $this->_actionParams = $actionParams; 
    4749    } 
    4850 
    4951 
    50     protected function getFormName(){ 
     52    abstract public function outputHeader(); 
     53 
     54    abstract public function outputFooter(); 
     55 
     56    public static function getFormName(){ 
    5157        static $number = 0; 
    5258        $number++; 
     
    5561} 
    5662 
    57 abstract class jFormsBuilderBase extends jFormsHtmlBuilderBase { 
     63abstract class jFormsHtmlBuilderBase extends  jFormsBuilderBase { 
    5864 
    59     
     65    public function outputHeader(){ 
     66        $url = jUrl::get($this->_action, $this->_actionParams, 2); // retourne le jurl correspondant 
     67 
     68        echo '<form action="'.$url->scriptName . $url->pathInfo.'" method="POST" name="'. jFormsBuilderBase::getFormName().'">'; 
     69        if(count($url->params)){ 
     70            echo '<div>'; 
     71            foreach ($url->params as $p_name => $p_value) { 
     72                echo '<input type="hidden" name="'. $p_name .'" value="'. htmlspecialchars($p_value) .'"/>', "\n"; 
     73            } 
     74            echo '</div>'; 
     75        } 
     76 
     77    } 
     78 
     79    public function outputFooter(){ 
     80        echo '</form>'; 
     81    } 
    6082 
    6183    abstract public function getJavascriptCheck(); 
  • branches/experimental/jforms/lib/jelix/forms/jFormsCompiler.class.php

    r436 r437  
    3232      $this->sourceFile = $selector->getPath(); 
    3333      $cachefile = $selector->getCompiledFilePath(); 
    34       $cachebuildfile = $selector->getCompiledBuildFilePath (); 
     34      $cacheHtmlBuilderFile = $selector->getCompiledBuildFilePath ('html'); 
    3535 
    3636      // compilation du fichier xml 
     
    4747 
    4848      $source=array(); 
    49       $source[]='<?php class '.$selector->getClass().' extends jFormsBase {'; 
     49      $source[]='<?php '; 
     50      $source[]='class '.$selector->getClass().' extends jFormsBase {'; 
     51      $source[]='    protected $_builders = array( '; 
     52      $source[]='    \'html\'=>array(\''.$cacheHtmlBuilderFile.'\',\''.$selector->getClass().'_builder_html\'), '; 
     53      $source[]='    );'; 
    5054      $source[]=' public function __construct(&$container, $reset = false){'; 
    5155      $source[]='          parent::__construct($container, $reset); '; 
    5256 
    5357 
    54       $srcbuild=array(); 
    55       $srcbuild[]='<?php class '.$selector->getClass().'_HtmlBuilder extends jFormsHtmlBuilderBase {'; 
    56       $srcbuild[]=' public function __construct($form, $action){'; 
    57       $srcbuild[]='          parent::__construct($form, $action); '; 
    58       $srcbuild[]='  }'; 
     58      $srcHtmlBuilder=array(); 
     59      $srcHtmlBuilder[]='<?php class '.$selector->getClass().'_builder_html extends jFormsHtmlBuilderBase {'; 
     60      $srcHtmlBuilder[]=' public function __construct($form, $action){'; 
     61      $srcHtmlBuilder[]='          parent::__construct($form, $action); '; 
     62      $srcHtmlBuilder[]='  }'; 
    5963 
    6064      $srcjs=array(); 
     
    6367      foreach($xml->children() as $controltype=>$control){ 
    6468            $source[] = $this->generatePHPControl($controltype, $control); 
     69            $srcjs[] =  $this->generateJsControl($control); 
    6570      } 
    6671 
     
    7479 
    7580 
    76       $srcbuild[]=' public function getJavascriptCheck(){'; 
    77       $srcbuild[]= implode("\n", $srcjs); 
    78       $srcbuild[]=' return $verif; }'; 
    79       $srcbuild[]='} ?>'; 
    80  
    81       jFile::write($cachebuildfile, implode("\n", $srcbuild)); 
     81      $srcHtmlBuilder[]=' public function getJavascriptCheck(){'; 
     82      $srcHtmlBuilder[]= implode("\n", $srcjs); 
     83      $srcHtmlBuilder[]=' return $verif; }'; 
     84      $srcHtmlBuilder[]='} ?>'; 
     85 
     86      jFile::write($cacheHtmlBuilderFile, implode("\n", $srcHtmlBuilder)); 
    8287 
    8388      return true; 
    84    
     89   
    8590 
    8691 
    8792    protected function generatePHPControl($controltype, $control){ 
    8893 
    89         $source[] = array(); 
     94        $source = array(); 
    9095        $class = 'jFormsControl'.$controltype; 
    9196 
     
    133138         } 
    134139 
    135          if(!isset($control->label)){ 
    136             throw new jException('jelix~formserr.tag.missing',array('label',$controltype,$this->sourceFile)); 
    137          } 
    138  
    139          if(isset($control->label['locale'])){ 
    140              $label=''; 
    141              $labellocale=(string)$control->label['locale']; 
    142              $source[]='$ctrl->labellocale=\''.$labellocale.'\';'; 
    143          }else{ 
    144              $label=(string)$control->label; 
    145              $labellocale=''; 
    146              $source[]='$ctrl->label=\''.str_replace("'","\\'",$label).'\';'; 
    147          } 
    148  
    149140         switch($controltype){ 
    150141            case 'input': 
     
    169160                    $daovalue = (string)$control['daovalueproperty']; 
    170161                    $source[]='$ctrl->datasource = new jFormDaoDatasource(\''.$daoselector.'\',\''. 
    171                         $daomethod.'\',\''.$daolabel.'\',\''.$daovalue.'\',);'; 
     162                        $daomethod.'\',\''.$daolabel.'\',\''.$daovalue.'\');'; 
    172163 
    173164                }else{ 
    174165                    $source[]='$ctrl->datasource= new jFormStaticDatasource();'; 
    175                     $source[]='$ctrl->datasource->array('; 
     166                    $source[]='$ctrl->datasource->datas = array('; 
    176167 
    177168                    foreach($control->item as $item){ 
     
    198189 
    199190    protected function generateJsControl($control){ 
    200  
     191        $source = array(); 
     192 
     193        if(isset($control['type'])){ 
     194            $dt = (string)$control['type']; 
     195        }else{ 
     196            $dt = 'string'; 
     197        } 
     198 
     199        if(isset($control->label['locale'])){ 
     200            $label='\'".str_replace("\'","\\\'",jLocale::get(\''.(string)$control->label['locale'].'\'))."\''; 
     201        }else{ 
     202            $label="'".str_replace("'","\\'",(string)$control->label)."'"; 
     203            $label=str_replace('"','\"',$label); 
     204        } 
     205        $source[]='$js.="gControl = new jFormsControl(\''.(string)$control['ref'].'\', '.$label.', \''.$dt.'\');\n";'; 
     206 
     207 
     208        if(isset($control['readonly'])){ 
     209            $readonly=(string)$control['readonly']; 
     210            $source[]='$js.="gControl.readonly = '.($readonly=='true'?'true':'false').';\n";'; 
     211        } 
     212        if(isset($control['required'])){ 
     213            $required=(string)$control['required']; 
     214            $source[]='$js.="gControl.required = '.($required=='true'?'true':'false').';\n";'; 
     215        } 
     216 
     217/*          
     218 
     219         switch($controltype){ 
     220            case 'input': 
     221               break; 
     222            case 'textarea': 
     223               break; 
     224            case 'secret': 
     225               break; 
     226            case 'output': 
     227                //attr value 
     228               break; 
     229            case 'upload': 
     230                // attr mediatype 
     231               break; 
     232            case 'select1': 
     233            case 'select': 
     234                // recuperer les <items> attr label|labellocale value 
     235                if(isset($control['dao'])){ 
     236                    $daoselector = (string)$control['dao']; 
     237                    $daomethod = (string)$control['daomethod']; 
     238                    $daolabel = (string)$control['daolabelproperty']; 
     239                    $daovalue = (string)$control['daovalueproperty']; 
     240                    $source[]='$ctrl->datasource = new jFormDaoDatasource(\''.$daoselector.'\',\''. 
     241                        $daomethod.'\',\''.$daolabel.'\',\''.$daovalue.'\',);'; 
     242 
     243                }else{ 
     244                    $source[]='$ctrl->datasource= new jFormStaticDatasource();'; 
     245                    $source[]='$ctrl->datasource->array('; 
     246 
     247                    foreach($control->item as $item){ 
     248                        $value ="'".str_replace("'","\\'",(string)$item['value'])."'=>"; 
     249                        if(isset($item['label'])){ 
     250                            $source[] = $value."'".str_replace("'","\\'",(string)$item['label'])."',"; 
     251                        }elseif(isset($item['labellocale'])){ 
     252                            $source[] = $value."jLocale::get('".(string)$item['labellocale']."'),"; 
     253                        }else{ 
     254                            $source[] = $value."'".str_replace("'","\\'",(string)$item['value'])."',"; 
     255                        } 
     256                    } 
     257                    $source[]=");"; 
     258                } 
     259               break; 
     260            case 'submit': 
     261                // attr value 
     262               break; 
     263         }*/ 
     264         $source[]='$js.="gForm.addControl( gControl);\n";'; 
     265 
     266         return implode("\n", $source); 
    201267 
    202268 
  • branches/experimental/jforms/lib/jelix/tpl/plugins/html/block.form.php

    r374 r437  
    1212 * a block to display an html form, with datas from a jforms 
    1313 * 
    14  * usage : {form $theformobject,'submit_action'} here form content {/form} 
     14 * usage : {form $theformobject,'submit_action', $submit_action_params} here form content {/form} 
    1515 *     
    1616 * @param jTplCompiler $compiler the template compiler 
     
    2222function jtpl_block_form($compiler, $begin, $param=array()) 
    2323{ 
    24     static $formNumber =0; 
    2524 
    2625    if(!$begin){ 
    27         return '</form>'; 
     26        return '$builder->outputFooter();'; 
    2827    } 
    2928 
    30     if(count($param) != 2){ 
     29    if(count($param) != 3){ 
    3130        $compiler->doError2('errors.tplplugin.block.bad.argument.number','form',2); 
    3231        return ''; 
    3332    } 
    3433 
    35     $formNumber ++; 
    36  
    37     $formName = 'jform_'.$formNumber; 
    38  
    39  
    40     $content = '<form action="" method="POST" name="'.$formName.'">'; 
    41  
    42  
    43  
     34    $content = ' $form = '.$param[0].'; 
     35$builder = $form->getBuilder(\'html\', '.$param[1].','.$param[2].'); 
     36$builder->outputHeader(); 
     37'; 
    4438    return $content; 
    4539} 
  • branches/experimental/jforms/testapp/modules/unittest/classes/unittestservice.class.php

    r436 r437  
    102102   } 
    103103 
     104   function jformsTest(){ 
     105      $test = jClasses::create("utjforms"); 
     106      $test->run(new jHtmlRespReporter($this->_rep)); 
     107   } 
     108 
    104109} 
    105110?> 
  • branches/experimental/jforms/testapp/modules/unittest/templates/menu.tpl

    r436 r437  
    5050</ul> 
    5151 
     52<h3>jForms</h3> 
     53<ul> 
     54    <li><a href="?module=unittest&amp;action=jforms_parseControls">compilateur</a></li> 
     55</ul> 
     56 
     57 
    5258<h3>Utilitaires</h3> 
    5359<ul> 
Download in other formats: Unified Diff Zip Archive