developer.jelix.org is not used any more and exists only for history. Post new tickets on the Github account.
developer.jelix.org n'est plus utilisée, et existe uniquement pour son historique. Postez les nouveaux tickets sur le compte github.

Ticket #566: repeat.5.0.2.diff

File repeat.5.0.2.diff, 24.0 KB (added by bastnic, 12 years ago)
  • lib/jelix-www/js/jforms_light.js

     
    222222        ctrl.formName = this.name;
    223223    },
    224224
     225    removeControl :function (aControlName) {
     226        var ctrls = this.controls;
     227        for(var i=0; i < ctrls.length; i++){
     228            if (ctrls[i].name == aControlName) {
     229                ctrls.splice(i,1);
     230                return;
     231            }
     232        }
     233    },
     234
    225235    setErrorDecorator : function (decorator){
    226236        this.errorDecorator = decorator;
    227237    },
     
    685695    }
    686696};
    687697
     698function jFormsRepeatGroup(name) {
     699    this.name = name;
     700    this.maxControl = 0;
     701    this.minControl = 0;
     702    this.controlNumber = 0;
     703    this.addButtonId = '';
     704    this.removeButtonId = '';
     705    this.formName = ''; // the id of the form
     706};
     707
     708jFormsRepeatGroup.prototype = {
     709
     710    /**
     711     * _addElements should be a function which will
     712     *   - insert new html elements in the document
     713     *   - create new javascript objects jForms* corresponding
     714     *     to the added control
     715     *   - call the addControl method of the jform object with
     716     *    each new control
     717     *  @param jFormsForm   the javascript object of the form
     718     *  @param integer        the number of the new item
     719     */
     720    _addElements : null,
     721
     722    /**
     723     * _removeElements should be a function which will
     724     *  - remove html elements corresponding to the item of the repeat group
     725     *  - call removeControl metho of the jform objet for each
     726     *    removed control
     727     *  @param jFormsForm   the javascript object of the form
     728     *  @param integer        the number of the item to delete
     729     */
     730    _removeElements : null,
     731
     732    addGroup : function () {
     733        if (this.controlNumber >= this.maxControl)
     734            return;
     735        this.controlNumber ++;
     736        this._addElements(jForms.getForm(this.formName), this.controlNumber);
     737        if (this.controlNumber == this.maxControl) {
     738            document.getElementById(this.addButtonId).setAttribute('disabled','disabled');
     739        }
     740        if (this.controlNumber > this.minControl) {
     741            document.getElementById(this.removeButtonId).removeAttribute('disabled');
     742        }
     743    },
     744
     745    removeGroup : function (number) {
     746        if (this.controlNumber <= this.minControl)
     747            return;
     748
     749        this._removeElements(jForms.getForm(this.formName), this.controlNumber);
     750
     751        this.controlNumber --;
     752        if (this.controlNumber == this.minControl) {
     753            document.getElementById(this.removeButtonId).setAttribute('disabled','disabled');
     754        }
     755        if (this.controlNumber < this.maxControl) {
     756            document.getElementById(this.addButtonId).removeAttribute('disabled');
     757        }
     758    }
     759
     760}
     761
     762
     763
    688764/**
    689765 * Decorator to display errors in an alert dialog box
    690766 */
  • lib/jelix-www/js/jforms_jquery.js

     
    209209function jFormsJQForm(name){
    210210    this.name = name;
    211211    this.controls = [];
     212    this.repeatGroup = [];
    212213    this.errorDecorator =  new jFormsJQErrorDecoratorAlert();
    213214    this.helpDecorator =  new jFormsJQHelpDecoratorAlert();
    214215    this.element = jQuery('#'+name).get(0);
    215216};
    216217
    217218jFormsJQForm.prototype={
     219
    218220    addControl : function(ctrl){
    219221        this.controls.push(ctrl);
    220222        ctrl.formName = this.name;
    221223    },
    222224
     225    removeControl :function (aControlName) {
     226        var ctrls = this.controls;
     227        for(var i=0; i < ctrls.length; i++){
     228            if (ctrls[i].name == aControlName) {
     229                ctrls.splice(i,1);
     230                return;
     231            }
     232        }
     233    },
     234
    223235    setErrorDecorator : function (decorator){
    224236        this.errorDecorator = decorator;
    225237    },
     
    683695    }
    684696};
    685697
     698function jFormsJQRepeatGroup(name) {
     699    this.name = name;
     700    this.maxControl = 0;
     701    this.minControl = 0;
     702    this.controlNumber = 0;
     703    this.addButtonId = '';
     704    this.removeButtonId = '';
     705    this.formName = ''; // the id of the form
     706};
     707
     708jFormsJQRepeatGroup.prototype = {
     709
     710    /**
     711     * _addElements should be a function which will
     712     *   - insert new html elements in the document
     713     *   - create new javascript objects jFormsJQ* corresponding
     714     *     to the added control
     715     *   - call the addControl method of the jform object with
     716     *    each new control
     717     *  @param jFormsJQForm   the javascript object of the form
     718     *  @param integer        the number of the new item
     719     */
     720    _addElements : null,
     721
     722    /**
     723     * _removeElements should be a function which will
     724     *  - remove html elements corresponding to the item of the repeat group
     725     *  - call removeControl metho of the jform objet for each
     726     *    removed control
     727     *  @param jFormsJQForm   the javascript object of the form
     728     *  @param integer        the number of the item to delete
     729     */
     730    _removeElements : null,
     731
     732    addGroup : function () {
     733        if (this.controlNumber >= this.maxControl)
     734            return;
     735        this.controlNumber ++;
     736        this._addElements(jFormsJQ.getForm(this.formName), this.controlNumber);
     737        if (this.controlNumber == this.maxControl) {
     738            document.getElementById(this.addButtonId).setAttribute('disabled','disabled');
     739        }
     740        if (this.controlNumber > this.minControl) {
     741            document.getElementById(this.removeButtonId).removeAttribute('disabled');
     742        }
     743    },
     744
     745    removeGroup : function (number) {
     746        if (this.controlNumber <= this.minControl)
     747            return;
     748
     749        this._removeElements(jFormsJQ.getForm(this.formName), this.controlNumber);
     750
     751        this.controlNumber --;
     752        if (this.controlNumber == this.minControl) {
     753            document.getElementById(this.removeButtonId).setAttribute('disabled','disabled');
     754        }
     755        if (this.controlNumber < this.maxControl) {
     756            document.getElementById(this.addButtonId).removeAttribute('disabled');
     757        }
     758    }
     759
     760}
     761
     762
     763
     764
     765
     766
    686767/**
    687768 * Decorator to display errors in an alert dialog box
    688769 */
  • lib/jelix/plugins/tpl/html/function.formdatafull.php

     
    2828        echo htmlspecialchars($ctrl->label);
    2929        echo '</th><td>';
    3030        $value = $ctrl->getDisplayValue($form->getData($ctrlref));
    31         if(is_array($value)){
     31        if ($ctrl->type == 'repeat') {
     32            foreach($value as $v){
     33                    echo "<pre>";
     34                    print_r($v);
     35                    echo "</pre>";
     36            }       
     37        }elseif(is_array($value)){
    3238            $s ='';
    3339            foreach($value as $v){
    3440                $s.=','.htmlspecialchars($v);
  • lib/jelix/plugins/tpl/html/function.ctrl_value.php

     
    5252
    5353    $value = $tpl->_privateVars['__form']->getData($ctrlname);
    5454    $value = $ctrl->getDisplayValue($value);
    55     if(is_array($value)){
     55    if ($ctrl->type == 'repeat') {
     56        foreach($value as $v){
     57                echo "<pre>";
     58                print_r($v);
     59                echo "</pre>";
     60        }       
     61    }elseif(is_array($value)){
    5662        $s ='';
    5763        foreach($value as $v){
    5864            $s.=$sep.htmlspecialchars($v);
  • lib/jelix/plugins/jforms/html/html.jformsbuilder.php

     
    66* @contributor Julien Issler, Dominique Papin
    77* @copyright   2006-2008 Laurent Jouanneau
    88* @copyright   2008 Julien Issler, 2008 Dominique Papin
     9* @copyright   2009 Bastien Jaillot
    910* @link        http://www.jelix.org
    1011* @licence     http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
    1112*/
     
    2728        foreach( $this->_form->getRootControls() as $ctrlref=>$ctrl){
    2829            if($ctrl->type == 'submit' || $ctrl->type == 'reset' || $ctrl->type == 'hidden') continue;
    2930            if(!$this->_form->isActivated($ctrlref)) continue;
    30             if($ctrl->type == 'group') {
     31            if($ctrl->type == 'group' || $ctrl->type == 'repeat') {
    3132                echo '<tr><td colspan="2">';
    3233                $this->outputControl($ctrl);
    3334                echo '</td></tr>';
     
    189190    }
    190191
    191192    public function outputControlLabel($ctrl){
    192         if($ctrl->type == 'hidden' || $ctrl->type == 'group') return;
     193        if($ctrl->type == 'hidden' || $ctrl->type == 'group' || $ctrl->type == 'repeat') return;
    193194        $required = ($ctrl->required == false || $ctrl->isReadOnly()?'':' jforms-required');
    194195        $inError = (isset($this->_form->getContainer()->errors[$ctrl->ref]) ?' jforms-error':'');
    195196        $hint = ($ctrl->hint == ''?'':' title="'.htmlspecialchars($ctrl->hint).'"');
     
    201202        }
    202203    }
    203204
    204     public function outputControl($ctrl){
     205    public function outputControl($ctrl,$output_switch=0){
    205206        if($ctrl->type == 'hidden') return;
    206207        $ro = $ctrl->isReadOnly();
    207208        $id = ' name="'.$ctrl->ref.'" id="'.$this->_name.'_'.$ctrl->ref.'"';
     
    211212        $readonly = ($ro?' readonly="readonly"':'');
    212213        if($class !='') $class = ' class="'.$class.'"';
    213214        $hint = ($ctrl->hint == ''?'':' title="'.htmlspecialchars($ctrl->hint).'"');
    214         $this->{'output'.$ctrl->type}($ctrl, $id, $class, $readonly, $hint);
    215         $this->{'js'.$ctrl->type}($ctrl);
     215        switch($output_switch){
     216            case 1:
     217                // add the "[]"
     218                $id = ' name="'.$ctrl->ref.'[]" id="'.$this->_name.'_'.$ctrl->ref.'"';
     219                $this->{'output'.$ctrl->type}($ctrl, $id, $class, $readonly, $hint);
     220                break;
     221            case 2:
     222                $this->{'js'.$ctrl->type}($ctrl);
     223                break;
     224            default:
     225                $this->{'output'.$ctrl->type}($ctrl, $id, $class, $readonly, $hint);
     226                $this->{'js'.$ctrl->type}($ctrl);
     227                break;
     228        }       
    216229        $this->outputHelp($ctrl);
    217230    }
    218231
     
    843856            }else{
    844857                $name=$ctrl->ref;
    845858            }
    846             echo '<span class="jforms-help"><a href="javascript:jFormsJQ.showHelp(\''. $this->_name.'\',\''.$name.'\')">?</a></span>';
     859            echo '<span class="jforms-help"><a href="javascript:jFormsJQ.showHelp(\''.$this->_name.'\',\''.$name.'\')">?</a></span>';
    847860        }
    848861    }
     862   
     863    protected function jsRepeat($ctrl) {
     864        $htmlAppend = '<table>';
     865        $jsObject = '';
     866        $this->jsContent .= "var i=".$ctrl->itemsCount.";\n";
     867        $this->jsContent .="c = new jFormsJQRepeatGroup('".$ctrl->ref."');\n";
     868        foreach( $ctrl->getChildControls() as $ctrlref=>$c){
     869            $jsObject .= "c = new jFormsJQ".$c->type."('".$c->ref."','".$c->label."');\n";
     870            $jsObject .= "jFormsJQ.tForm.addControl(c);\n";
     871            if($c->type == 'submit' || $c->type == 'reset' || $c->type == 'hidden') continue;
     872            if(!$this->_form->isActivated($ctrlref)) continue;
     873            $htmlAppend .= "<tr><th>";
     874            ob_start();
     875            $this->outputControlLabel($c);
     876            $out = ob_get_contents();
     877            ob_end_clean();
     878            $htmlAppend .= $out;
     879            $htmlAppend .= "</th><td>";
     880            ob_start();
     881            $this->outputControl($c,1);
     882            $out = ob_get_contents();
     883            ob_end_clean();
     884            $htmlAppend .= $out;
     885            $htmlAppend .= "</td></tr>";
     886        }
     887        $htmlAppend .= "<tr><td>";
     888        $htmlAppend .= "<input type=\"button\" class=\"jforms-submit\" value=\"".htmlspecialchars($ctrl->removeButtonLabel)."\" />";
     889        $htmlAppend .= "</td><td></td></tr></table>";
     890        $this->jsContent .= "$('#add".$ctrl->ref."').click(function(){\n";
     891        $this->jsContent .= "var re1 = /\[[0-9]*\]/gi;\n";
     892        $this->jsContent .= "var htmlstr = '".$htmlAppend."';\n";
     893        //$this->jsContent .= "var jsstr = '".$jsObject."';\n";
     894        $this->jsContent .= "var newstr1=htmlstr.replace(re1, '['+i+']');\n";
     895        $this->jsContent .= "i++;\n";
     896        $this->jsContent .= "var appended_content=$(newstr1).appendTo('#td".$ctrl->ref."');\n";
     897        $this->jsContent .= "appended_content.find(\"input[type='button']\").click( function () {\n";
     898        $this->jsContent .= "$(this).parents('table:first').remove();\n";
     899        $this->jsContent .= "})\n";
     900        $this->jsContent .= "})\n";
     901    }
     902   
     903    protected function outputRepeat($ctrl, $id, $class, $readonly, $hint) {
     904        echo "<fieldset><legend>".htmlspecialchars($ctrl->label)."</legend>\n";
     905        echo "<table class=\"jforms-table-group\" border=\"0\">\n";
     906        echo "<tr><td>\n";
     907        echo "<input id=\"add".$ctrl->ref."\" type=\"button\" class=\"jforms-submit\" value=\"".htmlspecialchars($ctrl->addButtonLabel)."\" />\n";
     908        echo "</td></tr>\n";
     909        echo "<tr><td id=\"td".$ctrl->ref."\">\n";
     910        for ($i=0;$i<$ctrl->itemsCount;$i++){
     911            echo "<table>\n";
     912            foreach( $ctrl->getChildControls() as $ctrlref=>$c){
     913                if($c->type == 'submit' || $c->type == 'reset' || $c->type == 'hidden') continue;
     914                if(!$this->_form->isActivated($ctrlref)) continue;
     915                $c->ref = preg_replace('/\[\d*\]/','['.$i.']',$c->ref);
     916                echo "<tr><th>\n";
     917                $this->outputControlLabel($c);
     918                echo "</th>\n<td>";
     919                $this->outputControl($c,1);
     920                echo "</td></tr>\n";
     921            }
     922            echo "<tr><td>\n";
     923            echo "<input type=\"button\" class=\"jforms-submit\" value=\"".htmlspecialchars($ctrl->removeButtonLabel)."\" />\n";
     924            echo "</td></tr>\n";
     925            echo "</table>\n";
     926        }   
     927        echo "</td></tr>\n";
     928        echo "<tr><td id=\"td".$ctrl->ref."\">\n";
     929        echo "</td></tr>\n";
     930        echo "</table></fieldset>\n";
     931    }
     932
    849933}
  • lib/jelix/forms/controls/jFormsControlRepeat.class.php

     
    33* @package     jelix
    44* @subpackage  forms
    55* @author      Laurent Jouanneau
    6 * @contributor
    7 * @copyright   2008 Laurent Jouanneau
     6* @contributor Bastien Jaillot
     7* @copyright   2009 Bastien Jaillot
    88* @link        http://www.jelix.org
    99* @licence     http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
    1010*/
     
    1717 */
    1818class jFormsControlRepeat extends jFormsControlGroups {
    1919    public $type="repeat";
     20    public $minItems=0;
     21    public $maxItems=0;
     22    public $itemsCount=0;
     23    public $addButtonLabel = 'Add';
     24    public $removeButtonLabel = 'Remove';
     25
     26
     27    /**
     28     * all child controls of the group BY number
     29     * $childControls[0] = array(new jFormsControlInput(), new jFormsControlTextarea())
     30     * $childControls[1] = array(new jFormsControlInput(), new jFormsControlTextarea())
     31     * $childControls[2] = array(new jFormsControlInput(), new jFormsControlTextarea())
     32     */
     33    protected $childControls = array();
     34   
     35
     36    function check(){
     37        $rv = null;
     38        foreach($this->childControls as $group) {
     39            foreach($group as $ctrl) {
     40                if(($rv2 = $ctrl->check())!==null) {
     41                    $rv = $rv2;
     42                }
     43            }
     44        }
     45        return $rv;
     46    }
     47
     48    function getDisplayValue($value){
     49        return $value;
     50    }
     51
     52    function setValueFromRequest($request) {
     53       
     54        // get the first control of our group
     55        foreach($this->childControls[0] as $name => $ctrl) {
     56            break;
     57        }
     58        if (!$name) return;
     59       
     60        // first : get size of the object
     61        $this->itemsCount = count($request->getParam($name,''));
     62
     63       
     64       
     65        // verify if not too big !
     66       
     67        $values = array();
     68        foreach($this->childControls as $group) {
     69           
     70            foreach($group as $name => $ctrl) {
     71                if(!$this->form->isActivated($name) || $this->form->isReadOnly($name))
     72                    continue;
     73                $value = $request->getParam($ctrl->ref,'');
     74               
     75                for ($i=0;$i<$this->itemsCount;$i++) {
     76                    if ($i > 0) $this->childControls[$i][$name] = $this->childControls[0][$name];
     77                   
     78                    $this->childControls[$i][$name]->setData($value[$i]); // viole l'encapsulation :s, should use setValueFromRequest
     79                    $values[$i][$name] = $value[$i];
     80                   
     81                }
     82               
     83            }
     84        }
     85       
     86        $this->setData($values);
     87    }
     88
     89    function addChildControl($control, $itemName = '') {
     90        $this->childControls[0][$control->ref]=$control;
     91    }
     92
     93    function getChildControls() { return $this->childControls[0];}
     94
     95    function setReadOnly($r = true){
     96        $this->container->setReadOnly($this->ref, $r);
     97        foreach($this->childControls as $group) {       
     98            foreach($group as $ctrl) {
     99               $ctrl->setReadOnly($r);
     100            }
     101        }
     102    }
     103
     104    public function deactivate($deactivation=true) {
     105        $this->container->deactivate($this->ref, $deactivation);
     106        foreach($this->childControls as $group) {       
     107            foreach($group as $ctrl) {
     108                $ctrl->deactivate($deactivation);
     109            }
     110        }
     111    }
     112   
     113
    20114}
  • lib/jelix/forms/jFormsCompiler_jf_1_1.class.php

     
    356356            $source[]='$ctrl->datasource= new jFormsStaticDatasource();';
    357357        }
    358358    }
     359
     360    protected function generateRepeat(&$source, $control, &$attributes) {
     361
     362        if (!is_numeric($attributes['min']) || !is_numeric($attributes['max']) || !is_numeric(intval($attributes['count']))) {
     363            throw new jException('jelix~formserr.repeat.not.int',array('min','max','count',$this->sourceFile));
     364        }
     365
     366        if ($attributes['min'] > $attributes['max']) {
     367            throw new jException('jelix~formserr.repeat.min.superior.to.max',array('min','max',$this->sourceFile));
     368        }
     369
     370        $source[]='$ctrl->minItems='.$attributes['min'].';';
     371        unset($attributes['min']);
     372
     373        $source[]='$ctrl->maxItems='.$attributes['max'].';';
     374        unset($attributes['max']);
     375
     376        $source[]='$ctrl->itemsCount='.$attributes['count'].';';
     377        unset($attributes['count']);
     378
     379        if (!isset($attributes['addlabel'])){
     380            $addlabel='Add';  //don't forget to use jLocale here
     381        } else {
     382            $addlabel=$attributes['addlabel'];
     383        }
     384        $source[]='$ctrl->addButtonLabel=\''.str_replace("'","\\'",$addlabel).'\';';
     385        unset($attributes['addlabel']);
     386
     387        if (!isset($attributes['removelabel'])){
     388            $removelabel='Remove'; //don't forget to use jLocale here
     389        } else {
     390            $removelabel=$attributes['removelabel'];
     391        }
     392
     393        $source[]='$ctrl->removeButtonLabel=\''.str_replace("'","\\'",$removelabel).'\';';
     394        unset($attributes['removelabel']);
     395
     396        $this->readLabel($source, $control, 'repeat');
     397        $this->readHelpHintAlert($source, $control);
     398        $this->attrReadOnly($source, $attributes);
     399        $source[]='$topctrl = $ctrl;';
     400        $ctrlcount = $this->readChildControls($source, 'repeat', $control, array('label', 'help', 'hint', 'alert'));
     401        if ($ctrlcount == 0) {
     402             throw new jException('jelix~formserr.no.child.control',array('repeat',$this->sourceFile));
     403        }
     404        $source[]='$ctrl = $topctrl;';
     405
     406        return false;
     407    }
     408
    359409}
     410
  • lib/jelix/docs/ns/jforms_1.1.rng

     
    2424            <ref name="submit" />
    2525            <ref name="choice" />
    2626            <ref name="captcha" />
     27            <ref name="repeat" />
    2728            <!--
    2829            <ref name="range" />
    2930            -->
     
    418419     </element>
    419420  </define>
    420421
     422  <define name="repeat">
     423     <element name="repeat">
     424        <attribute name="ref" />
     425        <attribute name="min" />
     426        <attribute name="max" />
     427        <attribute name="count" />
     428        <attribute name="addlabel" />
     429        <attribute name="removelabel" />
     430        <ref name="label" />
     431        <ref name="UI.attr.readonly" />
     432        <oneOrMore>
     433            <choice>
     434                <ref name="primary_controls" />
     435            </choice>
     436        </oneOrMore>
     437     </element>
     438   </define>
     439
    421440   <!--
    422441   <define name="any">
    423442      <element>
     
    449468  -->
    450469 
    451470
    452 </grammar>
    453  No newline at end of file
     471</grammar>
  • lib/jelix/core-modules/jelix/locales/en_EN/formserr.UTF-8.properties

     
    1919control.not.allowed= (818) The tag %1$s is not allowed in %2$s (file %3$s)
    2020no.child.control= (819) The tag %1$s must contain some controls (file %2$s)
    2121bad.root.tag=(820) the root element %1$s is incorrect, it should have the name "form" (fichier %2$s)
     22repeat.not.int=(821) %1$s or %2$s or %3$s are not integer (file %4$s)
     23repeat.min.superior.to.max=(822) %1$s is not inferior to %2$s (file %3$s)
    2224
    2325unknow.control=(830) The control "%1$s" doesn't exist in the form "%2$s" (template %3$s)
    2426
  • lib/jelix/core-modules/jelix/locales/en_US/formserr.UTF-8.properties

     
    1919control.not.allowed= (818) The tag %1$s is not allowed in %2$s (file %3$s)
    2020no.child.control= (819) The tag %1$s must contain some controls (file %2$s)
    2121bad.root.tag=(820) the root element %1$s is incorrect, it should have the name "form" (fichier %2$s)
     22repeat.not.int=(821) %1$s or %2$s or %3$s are not integer (file %4$s)
     23repeat.min.superior.to.max=(822) %1$s is not inferior to %2$s (file %3$s)
    2224
    2325unknow.control=(830) The control "%1$s" doesn't exist in the form "%2$s" (template %3$s)
    2426
  • lib/jelix/core-modules/jelix/locales/fr_FR/formserr.UTF-8.properties

     
    1919control.not.allowed= (818) La balise %1$s n'est pas autorisé dans %2$s (fichier %3$s)
    2020no.child.control= (819) La balise %1$s doit contenir des balises de contrôles (fichier %2$s)
    2121bad.root.tag=(820) la balise racine %1$s est incorrecte, elle devrait avoir pour nom "form" (fichier %2$s)
     22repeat.not.int=(821) %1$s ou %2$s ou %3$s ne sont pas des entiers (file %4$s)
     23repeat.min.superior.to.max=(822) %1$s n'est pas inferieur à %2$s (file %3$s)
    2224
    2325unknow.control=(830) Le contrôle "%1$s" n'existe pas dans le formulaire "%2$s" (template "%3$s")
    2426