Source for file html.jformsbuilder.php

Documentation is available at html.jformsbuilder.php

  1. <?php
  2. /**
  3. @package     jelix
  4. @subpackage  forms
  5. @author      Laurent Jouanneau
  6. @contributor Julien Issler, Dominique Papin
  7. @copyright   2006-2009 Laurent Jouanneau
  8. @copyright   2008 Julien Issler, 2008 Dominique Papin
  9. @link        http://www.jelix.org
  10. @licence     http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  11. */
  12.  
  13. /**
  14.  * HTML form builder
  15.  * @package     jelix
  16.  * @subpackage  jelix-plugins
  17.  */
  18. class htmlJformsBuilder extends jFormsBuilderBase {
  19.  
  20.     protected $options;
  21.  
  22.     protected $isRootControl = true;
  23.  
  24.     public function outputAllControls({
  25.  
  26.         echo '<table class="jforms-table" border="0">';
  27.         foreach$this->_form->getRootControls(as $ctrlref=>$ctrl){
  28.             if($ctrl->type == 'submit' || $ctrl->type == 'reset' || $ctrl->type == 'hidden'continue;
  29.             if(!$this->_form->isActivated($ctrlref)) continue;
  30.             if($ctrl->type == 'group'{
  31.                 echo '<tr><td colspan="2">';
  32.                 $this->outputControl($ctrl);
  33.                 echo '</td></tr>';
  34.             }else{
  35.                 echo '<tr><th scope="row">';
  36.                 $this->outputControlLabel($ctrl);
  37.                 echo '</th><td>';
  38.                 $this->outputControl($ctrl);
  39.                 echo '</td></tr>';
  40.             }
  41.         }
  42.         echo '</table> <div class="jforms-submit-buttons">';
  43.         if $ctrl $this->_form->getReset() ) {
  44.             if(!$this->_form->isActivated($ctrl->ref)) continue;
  45.             $this->outputControl($ctrl);
  46.             echo ' ';
  47.         }
  48.         foreach$this->_form->getSubmits(as $ctrlref=>$ctrl){
  49.             if(!$this->_form->isActivated($ctrlref)) continue;
  50.             $this->outputControl($ctrl);
  51.             echo ' ';
  52.         }
  53.         echo '</div>';
  54.     }
  55.  
  56.     public function outputMetaContent($t{
  57.         global $gJCoord$gJConfig;
  58.         $resp$gJCoord->response;
  59.         if($resp === null || $resp->getType(!='html'){
  60.             return;
  61.         }
  62.         $www =$gJConfig->urlengine['jelixWWWPath'];
  63.         $bp =$gJConfig->urlengine['basePath'];
  64.         $resp->addJSLink($www.'jquery/jquery.js');
  65.         $resp->addJSLink($www.'jquery/include/jquery.include.js');
  66.         $resp->addJSLink($www.'js/jforms_jquery.js');
  67.         $resp->addCSSLink($www.'design/jform.css');
  68.         foreach($t->_vars as $k=>$v){
  69.             if(!$v instanceof jFormsBase)
  70.                 continue;
  71.             foreach($v->getHtmlEditors(as $ed{
  72.                 if(isset($gJConfig->htmleditors[$ed->config.'.engine.file'])){
  73.                     if(is_array($gJConfig->htmleditors[$ed->config.'.engine.file'])){
  74.                         foreach($gJConfig->htmleditors[$ed->config.'.engine.file'as $url{
  75.                             $resp->addJSLink($bp.$url);
  76.                         }
  77.                     }else
  78.                         $resp->addJSLink($bp.$gJConfig->htmleditors[$ed->config.'.engine.file']);
  79.                 }
  80.                 if(isset($gJConfig->htmleditors[$ed->config.'.config']))
  81.                     $resp->addJSLink($bp.$gJConfig->htmleditors[$ed->config.'.config']);
  82.                 if(isset($gJConfig->htmleditors[$ed->config.'.skin.'.$ed->skin]))
  83.                     $resp->addCSSLink($bp.$gJConfig->htmleditors[$ed->config.'.skin.'.$ed->skin]);
  84.             }
  85.             $datepicker_default_config $gJConfig->forms['datepicker'];
  86.             foreach($v->getControls(as $ctrl){
  87.                 if($ctrl instanceof jFormsControlDate || get_class($ctrl->datatype== 'jDatatypeDate' || get_class($ctrl->datatype== 'jDatatypeLocaleDate'){
  88.                     $config = isset($ctrl->datepickerConfig)?$ctrl->datepickerConfig:$datepicker_default_config;
  89.                     $resp->addJSLink($bp.$gJConfig->datepickers[$config]);
  90.                 }
  91.             }
  92.         }
  93.     }
  94.  
  95.     /**
  96.      * output the header content of the form
  97.      * @param array $params some parameters <ul>
  98.      *       <li>"errDecorator"=>"name of your javascript object for error listener"</li>
  99.      *       <li>"helpDecorator"=>"name of your javascript object for help listener"</li>
  100.      *       <li>"method" => "post" or "get". default is "post"</li>
  101.      *       </ul>
  102.      */
  103.     public function outputHeader($params){
  104.         $this->options = array_merge(array('errorDecorator'=>'jFormsJQErrorDecoratorAlert',
  105.                  'helpDecorator'=>'jFormsJQHelpDecoratorAlert''method'=>'post')$params);
  106.  
  107.  
  108.         if (preg_match('#^https?://#',$this->_action)) {
  109.             $urlParams $this->_actionParams;
  110.             echo '<form action="',$this->_action,'" method="'.$this->options['method'].'" id="'$this->_name,'"';
  111.         else {
  112.             $url jUrl::get($this->_action$this->_actionParams2)// retourne le jurl correspondant
  113.             $urlParams $url->params;
  114.             echo '<form action="',$url->getPath(),'" method="'.$this->options['method'].'" id="'$this->_name,'"';
  115.         }
  116.         if($this->_form->hasUpload())
  117.             echo ' enctype="multipart/form-data">';
  118.         else
  119.             echo '>';
  120.  
  121.         echo '<script type="text/javascript">
  122. //<![CDATA[
  123. jFormsJQ.tForm = new jFormsJQForm(\''.$this->_name.'\');
  124. jFormsJQ.tForm.setErrorDecorator(new '.$this->options['errorDecorator'].'());
  125. jFormsJQ.tForm.setHelpDecorator(new '.$this->options['helpDecorator'].'());
  126. jFormsJQ.declareForm(jFormsJQ.tForm);
  127. //]]>
  128. </script>';
  129.  
  130.         $hiddens '';
  131.         foreach ($urlParams as $p_name => $p_value{
  132.             $hiddens .= '<input type="hidden" name="'$p_name .'" value="'htmlspecialchars($p_value)'"'.$this->_endt"\n";
  133.         }
  134.  
  135.         foreach ($this->_form->getHiddens(as $ctrl{
  136.             if(!$this->_form->isActivated($ctrl->ref)) continue;
  137.             $hiddens .= '<input type="hidden" name="'$ctrl->ref.'" id="'.$this->_name.'_'.$ctrl->ref.'" value="'htmlspecialchars($this->_form->getData($ctrl->ref))'"'.$this->_endt"\n";
  138.         }
  139.  
  140.         if($this->_form->securityLevel){
  141.             $tok $this->_form->createNewToken();
  142.             $hiddens .= '<input type="hidden" name="__JFORMS_TOKEN__" value="'.$tok.'"'.$this->_endt"\n";
  143.         }
  144.  
  145.         if($hiddens){
  146.             echo '<div class="jforms-hiddens">',$hiddens,'</div>';
  147.         }
  148.  
  149.         $errors $this->_form->getContainer()->errors;
  150.         if(count($errors)){
  151.             $ctrls $this->_form->getControls();
  152.             echo '<ul class="jforms-error-list">';
  153.             $errRequired='';
  154.             foreach($errors as $cname => $err){
  155.                 if(!$this->_form->isActivated($ctrls[$cname]->ref)) continue;
  156.                 if($err == jForms::ERRDATA_REQUIRED{
  157.                     if($ctrls[$cname]->alertRequired){
  158.                         echo '<li>'$ctrls[$cname]->alertRequired,'</li>';
  159.                     }else{
  160.                         echo '<li>'jLocale::get('jelix~formserr.js.err.required'$ctrls[$cname]->label),'</li>';
  161.                     }
  162.                 }elseif ($err != '' && $err != jForms::ERRDATA_INVALID{
  163.                     echo '<li>'$err,'</li>';
  164.                 }else{
  165.                     if($ctrls[$cname]->alertInvalid){
  166.                         echo '<li>'$ctrls[$cname]->alertInvalid,'</li>';
  167.                     }else{
  168.                         echo '<li>'jLocale::get('jelix~formserr.js.err.invalid'$ctrls[$cname]->label),'</li>';
  169.                     }
  170.                 }
  171.             }
  172.             echo '</ul>';
  173.         }
  174.     }
  175.  
  176.     protected $jsContent = '';
  177.  
  178.     public function outputFooter(){
  179.         echo '<script type="text/javascript">
  180. //<![CDATA[
  181. (function(){var c, c2;
  182. '.$this->jsContent.'
  183. })();
  184. //]]>
  185. </script>';
  186.         echo '</form>';
  187.     }
  188.  
  189.     public function outputControlLabel($ctrl){
  190.         if($ctrl->type == 'hidden' || $ctrl->type == 'group'return;
  191.         $required ($ctrl->required == false || $ctrl->isReadOnly()?'':' jforms-required');
  192.         $inError (isset($this->_form->getContainer()->errors[$ctrl->ref]?' jforms-error':'');
  193.         $hint ($ctrl->hint == ''?'':' title="'.htmlspecialchars($ctrl->hint).'"');
  194.         if($ctrl->type == 'output' || $ctrl->type == 'checkboxes' || $ctrl->type == 'radiobuttons' || $ctrl->type == 'date' || $ctrl->type == 'datetime'){
  195.             echo '<span class="jforms-label',$required,$inError,'"',$hint,'>',htmlspecialchars($ctrl->label),'</span>';
  196.         }else if($ctrl->type != 'submit' && $ctrl->type != 'reset'){
  197.             $id $this->_name.'_'.$ctrl->ref;
  198.             echo '<label class="jforms-label',$required,$inError,'" for="',$id,'"',$hint,'>',htmlspecialchars($ctrl->label),'</label>';
  199.         }
  200.     }
  201.  
  202.     public function outputControl($ctrl){
  203.         if($ctrl->type == 'hidden'return;
  204.         $ro $ctrl->isReadOnly();
  205.         $id ' name="'.$ctrl->ref.'" id="'.$this->_name.'_'.$ctrl->ref.'"';
  206.         $class ($ctrl->required == false || $ro?'':' jforms-required');
  207.         $class.= (isset($this->_form->getContainer()->errors[$ctrl->ref]?' jforms-error':'');
  208.         $class.= ($ro && $ctrl->type != 'captcha'?' jforms-readonly':'');
  209.         $readonly ($ro?' readonly="readonly"':'');
  210.         if($class !=''$class ' class="'.$class.'"';
  211.         $hint ($ctrl->hint == ''?'':' title="'.htmlspecialchars($ctrl->hint).'"');
  212.         $this->{'output'.$ctrl->type}($ctrl$id$class$readonly$hint);
  213.         $this->{'js'.$ctrl->type}($ctrl);
  214.         $this->outputHelp($ctrl);
  215.     }
  216.  
  217.     protected function escJsStr($str{
  218.         return '\''.str_replace(array("'","\n"),array("\\'""\\n")$str).'\'';
  219.     }
  220.  
  221.     protected function commonJs($ctrl{
  222.         if($ctrl->help){
  223.             $this->jsContent .="c.help=".$this->escJsStr($ctrl->help).";\n";
  224.         }
  225.  
  226.         if($ctrl->required){
  227.             $this->jsContent .="c.required = true;\n";
  228.             if($ctrl->alertRequired){
  229.                 $this->jsContent .="c.errRequired=".$this->escJsStr($ctrl->alertRequired).";\n";
  230.             }
  231.             else {
  232.                 $this->jsContent .="c.errRequired=".$this->escJsStr(jLocale::get('jelix~formserr.js.err.required'$ctrl->label)).";\n";
  233.             }
  234.         }
  235.  
  236.         if($ctrl->alertInvalid){
  237.             $this->jsContent .="c.errInvalid=".$this->escJsStr($ctrl->alertInvalid).";\n";
  238.         }
  239.         else {
  240.             $this->jsContent .="c.errInvalid=".$this->escJsStr(jLocale::get('jelix~formserr.js.err.invalid'$ctrl->label)).";\n";
  241.         }
  242.  
  243.         if($ctrl instanceof jFormsControlDate || get_class($ctrl->datatype== 'jDatatypeDate' || get_class($ctrl->datatype== 'jDatatypeLocaleDate'){
  244.             global $gJConfig;
  245.             $config = isset($ctrl->datepickerConfig)?$ctrl->datepickerConfig:$gJConfig->forms['datepicker'];
  246.             $this->jsContent .= 'jelix_datepicker_'.$config.'(c,'.$this->escJsStr($gJConfig->locale)
  247.                             .','.$this->escJsStr($gJConfig->urlengine['basePath'])
  248.                             .','.$this->escJsStr($gJConfig->urlengine['jelixWWWPath']).");\n";
  249.         }
  250.  
  251.         if ($this->isRootControl$this->jsContent .="jFormsJQ.tForm.addControl(c);\n";
  252.     }
  253.  
  254.     protected function outputInput($ctrl$id$class$readonly$hint{
  255.         $value $this->_form->getData($ctrl->ref);
  256.         $size ($ctrl->size == 0?'' ' size="'.$ctrl->size.'"');
  257.         $maxl$ctrl->datatype->getFacet('maxLength');
  258.         if($maxl !== null)
  259.             $maxl=' maxlength="'.$maxl.'"';
  260.         else
  261.             $maxl='';
  262.         echo '<input type="text"',$id,$readonly,$hint,$class,$size,$maxl,' value="',htmlspecialchars($value),'"',$this->_endt;
  263.     }
  264.  
  265.     protected function jsInput($ctrl{
  266.  
  267.         $datatype array('jDatatypeBoolean'=>'Boolean','jDatatypeDecimal'=>'Decimal','jDatatypeInteger'=>'Integer','jDatatypeHexadecimal'=>'Hexadecimal',
  268.                         'jDatatypeDateTime'=>'Datetime','jDatatypeDate'=>'Date','jDatatypeTime'=>'Time',
  269.                         'jDatatypeUrl'=>'Url','jDatatypeEmail'=>'Email','jDatatypeIPv4'=>'Ipv4','jDatatypeIPv6'=>'Ipv6');
  270.         $isLocale false;
  271.         $data_type_class get_class($ctrl->datatype);
  272.         if(isset($datatype[$data_type_class]))
  273.             $dt $datatype[$data_type_class];
  274.         else if ($ctrl->datatype instanceof jDatatypeLocaleTime)
  275.             $dt 'Time'$isLocale true}
  276.         else if ($ctrl->datatype instanceof jDatatypeLocaleDate)
  277.             $dt 'LocaleDate'$isLocale true}
  278.         else if ($ctrl->datatype instanceof jDatatypeLocaleDateTime)
  279.             $dt 'LocaleDatetime'$isLocale true}
  280.         else
  281.             $dt 'String';
  282.  
  283.         $this->jsContent .="c = new jFormsJQControl".$dt."('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n";
  284.         if ($isLocale)
  285.             $this->jsContent .="c.lang='".$GLOBALS['gJConfig']->locale."';\n";
  286.  
  287.         $maxl$ctrl->datatype->getFacet('maxLength');
  288.         if($maxl !== null)
  289.             $this->jsContent .="c.maxLength = '$maxl';\n";
  290.  
  291.         $minl$ctrl->datatype->getFacet('minLength');
  292.         if($minl !== null)
  293.             $this->jsContent .="c.minLength = '$minl';\n";
  294.  
  295.         $this->commonJs($ctrl);
  296.     }
  297.  
  298.     protected function _outputDateControlDay($ctrl$id$value$class$readonly$hint){
  299.         if($GLOBALS['gJConfig']->forms['controls.datetime.input'== 'textboxes')
  300.             echo '<input type="text" size="2" maxlength="2" id="'.$id.'day" name="'.$ctrl->ref.'[day]"'.$readonly.$hint.$class.' value="'.$value.'"'.$this->_endt;
  301.         else{
  302.             echo '<select id="'.$id.'day" name="'.$ctrl->ref.'[day]"'.$readonly.$hint.$class.'><option value="">'.htmlspecialchars(jLocale::get('jelix~jforms.date.day.label')).'</option>';
  303.             for($i=1;$i<32;$i++){
  304.                 $k ($i<10)?'0'.$i:$i;
  305.                 echo '<option value="'.$k.'"'.($k == $value?' selected="selected"':'').'>'.$k.'</option>';
  306.             }
  307.             echo '</select>';
  308.         }
  309.     }
  310.  
  311.     protected function _outputDateControlMonth($ctrl$id$value$class$readonly$hint){
  312.         if($GLOBALS['gJConfig']->forms['controls.datetime.input'== 'textboxes')
  313.             echo '<input type="text" size="2" maxlength="2" id="'.$id.'month" name="'.$ctrl->ref.'[month]"'.$readonly.$hint.$class.' value="'.$value.'"'.$this->_endt;
  314.         else{
  315.             $monthLabels $GLOBALS['gJConfig']->forms['controls.datetime.months.labels'];
  316.             echo '<select id="'.$id.'month" name="'.$ctrl->ref.'[month]"'.$readonly.$hint.$class.'><option value="">'.htmlspecialchars(jLocale::get('jelix~jforms.date.month.label')).'</option>';
  317.             for($i=1;$i<13;$i++){
  318.                 $k ($i<10)?'0'.$i:$i;
  319.                 if($monthLabels == 'names')
  320.                     $l htmlspecialchars(jLocale::get('jelix~date_time.month.'.$k.'.label'));
  321.                 else if($monthLabels == 'shortnames')
  322.                     $l htmlspecialchars(jLocale::get('jelix~date_time.month.'.$k.'.shortlabel'));
  323.                 else
  324.                     $l $k;
  325.                 echo '<option value="'.$k.'"'.($k == $value?' selected="selected"':'').'>'.$l.'</option>';
  326.             }
  327.             echo '</select>';
  328.         }
  329.     }
  330.  
  331.     protected function _outputDateControlYear($ctrl$id$value$class$readonly$hint){
  332.         if($GLOBALS['gJConfig']->forms['controls.datetime.input'== 'textboxes')
  333.             echo '<input type="text" size="4" maxlength="4" id="'.$id.'year" name="'.$ctrl->ref.'[year]"'.$readonly.$hint.$class.' value="'.$value.'"'.$this->_endt;
  334.         else{
  335.             $minDate $ctrl->datatype->getFacet('minValue');
  336.             $maxDate $ctrl->datatype->getFacet('maxValue');
  337.             if($minDate && $maxDate){
  338.                 echo '<select id="'.$id.'year" name="'.$ctrl->ref.'[year]"'.$readonly.$hint.$class.'><option value="">'.htmlspecialchars(jLocale::get('jelix~jforms.date.year.label')).'</option>';
  339.                 for($i=$minDate->year;$i<=$maxDate->year;$i++)
  340.                     echo '<option value="'.$i.'"'.($i == $value?' selected="selected"':'').'>'.$i.'</option>';
  341.                 echo '</select>';
  342.             }
  343.             else
  344.                 echo '<input type="text" size="4" maxlength="4" id="'.$id.'year" name="'.$ctrl->ref.'[year]"'.$readonly.$hint.$class.' value="'.$value.'"'.$this->_endt;
  345.         }
  346.     }
  347.  
  348.     protected function _outputDateControlHour($ctrl$id$value$class$readonly$hint){
  349.         if($GLOBALS['gJConfig']->forms['controls.datetime.input'== 'textboxes')
  350.             echo '<input type="text" id="'.$id.'hour" name="'.$ctrl->ref.'[hour]" value="'.$value.'"'.$this->_endt;
  351.         else{
  352.             echo '<select id="'.$id.'hour" name="'.$ctrl->ref.'[hour]"'.$readonly.$hint.$class.'><option value="">'.htmlspecialchars(jLocale::get('jelix~jforms.time.hour.label')).'</option>';
  353.             for($i=0;$i<24;$i++){
  354.                 $k ($i<10)?'0'.$i:$i;
  355.                 echo '<option value="'.$k.'"'.(string) $k === $value?' selected="selected"':'').'>'.$k.'</option>';
  356.             }
  357.             echo '</select>';
  358.         }
  359.     }
  360.  
  361.     protected function _outputDateControlMinutes($ctrl$id$value$class$readonly$hint){
  362.         if($GLOBALS['gJConfig']->forms['controls.datetime.input'== 'textboxes')
  363.             echo '<input type="text" id="'.$id.'minutes" name="'.$ctrl->ref.'[minutes]" value="'.$value.'"'.$this->_endt;
  364.         else{
  365.             echo '<select id="'.$id.'minutes" name="'.$ctrl->ref.'[minutes]"'.$readonly.$hint.$class.'><option value="">'.htmlspecialchars(jLocale::get('jelix~jforms.time.minutes.label')).'</option>';
  366.             for($i=0;$i<60;$i++){
  367.                 $k ($i<10)?'0'.$i:$i;
  368.                 echo '<option value="'.$k.'"'.(string) $k === $value?' selected="selected"':'').'>'.$k.'</option>';
  369.             }
  370.             echo '</select>';
  371.         }
  372.     }
  373.  
  374.     protected function _outputDateControlSeconds($ctrl$id$value$class$readonly$hint){
  375.         if(!$ctrl->enableSeconds)
  376.             echo '<input type="hidden" id="'.$id.'seconds" name="'.$ctrl->ref.'[seconds]" value="'.$value.'"'.$this->_endt;
  377.         else if($GLOBALS['gJConfig']->forms['controls.datetime.input'== 'textboxes')
  378.             echo '<input type="text" id="'.$id.'seconds" name="'.$ctrl->ref.'[seconds]" value="'.$value.'"'.$this->_endt;
  379.         else{
  380.             echo '<select id="'.$id.'seconds" name="'.$ctrl->ref.'[seconds]"'.$readonly.$hint.$class.'><option value="">'.htmlspecialchars(jLocale::get('jelix~jforms.time.seconds.label')).'</option>';
  381.             for($i=0;$i<60;$i++){
  382.                 $k ($i<10)?'0'.$i:$i;
  383.                 echo '<option value="'.$k.'"'.(string) $k === $value?' selected="selected"':'').'>'.$k.'</option>';
  384.             }
  385.             echo '</select>';
  386.         }
  387.     }
  388.  
  389.     protected function outputDate($ctrl$id$class$readonly$hint){
  390.         $id $this->_name.'_'.$ctrl->ref.'_';
  391.         $v array('year'=>'','month'=>'','day'=>'');
  392.         if(preg_match('#^(\d{4})?-(\d{2})?-(\d{2})?$#',$this->_form->getData($ctrl->ref),$matches)){
  393.             if(isset($matches[1]))
  394.                 $v['year'$matches[1];
  395.             if(isset($matches[2]))
  396.                 $v['month'$matches[2];
  397.             if(isset($matches[3]))
  398.                 $v['day'$matches[3];
  399.         }
  400.         $f jLocale::get('jelix~format.date');
  401.         for($i=0;$i<strlen($f);$i++){
  402.             if($f[$i== 'Y')
  403.                 $this->_outputDateControlYear($ctrl$id$v['year']$class$readonly$hint);
  404.             else if($f[$i== 'm')
  405.                 $this->_outputDateControlMonth($ctrl$id$v['month']$class$readonly$hint);
  406.             else if($f[$i== 'd')
  407.                 $this->_outputDateControlDay($ctrl$id$v['day']$class$readonly$hint);
  408.             else
  409.                 echo ' ';
  410.         }
  411.     }
  412.  
  413.     protected function jsDate($ctrl){
  414.         $this->jsContent .= "c = new jFormsJQControlDate('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n";
  415.         $this->jsContent .= "c.multiFields = true;\n";
  416.         $minDate $ctrl->datatype->getFacet('minValue');
  417.         $maxDate $ctrl->datatype->getFacet('maxValue');
  418.         if($minDate)
  419.             $this->jsContent .= "c.minDate = '".$minDate->toString(jDateTime::BD_DFORMAT)."';\n";
  420.         if($maxDate)
  421.             $this->jsContent .= "c.maxDate = '".$maxDate->toString(jDateTime::BD_DFORMAT)."';\n";
  422.         $this->commonJs($ctrl);
  423.     }
  424.  
  425.     protected function outputDatetime($ctrl$id$class$readonly$hint){
  426.         $id $this->_name.'_'.$ctrl->ref.'_';
  427.         $v array('year'=>'','month'=>'','day'=>'','hour'=>'','minutes'=>'','seconds'=>'');
  428.         if(preg_match('#^(\d{4})?-(\d{2})?-(\d{2})? (\d{2})?:(\d{2})?(:(\d{2})?)?$#',$this->_form->getData($ctrl->ref),$matches)){
  429.             if(isset($matches[1]))
  430.                 $v['year'$matches[1];
  431.             if(isset($matches[2]))
  432.                 $v['month'$matches[2];
  433.             if(isset($matches[3]))
  434.                 $v['day'$matches[3];
  435.             if(isset($matches[4]))
  436.                 $v['hour'$matches[4];
  437.             if(isset($matches[5]))
  438.                 $v['minutes'$matches[5];
  439.             if(isset($matches[7]))
  440.                 $v['seconds'$matches[7];
  441.         }
  442.         $f jLocale::get('jelix~format.datetime');
  443.         for($i=0;$i<strlen($f);$i++){
  444.             if($f[$i== 'Y')
  445.                 $this->_outputDateControlYear($ctrl$id$v['year']$class$readonly$hint);
  446.             else if($f[$i== 'm')
  447.                 $this->_outputDateControlMonth($ctrl$id$v['month']$class$readonly$hint);
  448.             else if($f[$i== 'd')
  449.                 $this->_outputDateControlDay($ctrl$id$v['day']$class$readonly$hint);
  450.             else if($f[$i== 'H')
  451.                 $this->_outputDateControlHour($ctrl$id$v['hour']$class$readonly$hint);
  452.             else if($f[$i== 'i')
  453.                 $this->_outputDateControlMinutes($ctrl$id$v['minutes']$class$readonly$hint);
  454.             else if($f[$i== 's')
  455.                 $this->_outputDateControlSeconds($ctrl$id$v['seconds']$class$readonly$hint);
  456.             else
  457.                 echo ' ';
  458.         }
  459.     }
  460.  
  461.     protected function jsDatetime($ctrl){
  462.         $this->jsContent .= "c = new jFormsJQControlDatetime('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n";
  463.         $this->jsContent .= "c.multiFields = true;\n";
  464.         $minDate $ctrl->datatype->getFacet('minValue');
  465.         $maxDate $ctrl->datatype->getFacet('maxValue');
  466.         if($minDate)
  467.             $this->jsContent .= "c.minDate = '".$minDate->toString(jDateTime::BD_DTFORMAT)."';\n";
  468.         if($maxDate)
  469.             $this->jsContent .= "c.maxDate = '".$maxDate->toString(jDateTime::BD_DTFORMAT)."';\n";
  470.         $this->commonJs($ctrl);
  471.     }
  472.  
  473.     protected function outputCheckbox($ctrl$id$class$readonly$hint{
  474.         $value $this->_form->getData($ctrl->ref);
  475.  
  476.         if($ctrl->valueOnCheck == $value){
  477.             $v=' checked="checked"';
  478.         }else{
  479.             $v='';
  480.         }
  481.         echo '<input type="checkbox"',$id,$readonly,$hint,$class,$v,' value="',$ctrl->valueOnCheck,'"',$this->_endt;
  482.     }
  483.  
  484.     protected function jsCheckbox($ctrl{
  485.  
  486.         $this->jsContent .="c = new jFormsJQControlBoolean('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n";
  487.  
  488.         $this->commonJs($ctrl);
  489.     }
  490.  
  491.     protected function outputCheckboxes($ctrl$id$class$readonly$hint{
  492.         $i=0;
  493.         $id=$this->_name.'_'.$ctrl->ref.'_';
  494.         $attrs=' name="'.$ctrl->ref.'[]" id="'.$id;
  495.         $value $this->_form->getData($ctrl->ref);
  496.  
  497.         if(is_array($value&& count($value== 1)
  498.             $value $value[0];
  499.         $span ='<span class="jforms-chkbox jforms-ctl-'.$ctrl->ref.'"><input type="checkbox"';
  500.  
  501.         if(is_array($value)){
  502.             $value array_map(create_function('$v''return (string) $v;'),$value);
  503.             foreach($ctrl->datasource->getData($this->_formas $v=>$label){
  504.                 echo $span,$attrs,$i,'" value="',htmlspecialchars($v),'"';
  505.                 if(in_array((string) $v,$value,true))
  506.                     echo ' checked="checked"';
  507.                 echo $readonly,$class,$this->_endt,'<label for="',$id,$i,'">',htmlspecialchars($label),'</label></span>';
  508.                 $i++;
  509.             }
  510.         }else{
  511.             $value = (string) $value;
  512.             foreach($ctrl->datasource->getData($this->_formas $v=>$label){
  513.                 echo $span,$attrs,$i,'" value="',htmlspecialchars($v),'"';
  514.                 if((string) $v === $value)
  515.                     echo ' checked="checked"';
  516.                 echo $readonly,$class,$this->_endt,'<label for="',$id,$i,'">',htmlspecialchars($label),'</label></span>';
  517.                 $i++;
  518.             }
  519.         }
  520.     }
  521.  
  522.     protected function jsCheckboxes($ctrl{
  523.  
  524.         $this->jsContent .="c = new jFormsJQControlString('".$ctrl->ref."[]', ".$this->escJsStr($ctrl->label).");\n";
  525.  
  526.         $this->commonJs($ctrl);
  527.     }
  528.  
  529.     protected function outputRadiobuttons($ctrl$id$class$readonly$hint{
  530.         $i=0;
  531.         $id=' name="'.$ctrl->ref.'" id="'.$this->_name.'_'.$ctrl->ref.'_';
  532.         $value $this->_form->getData($ctrl->ref);
  533.         if(is_array($value)){
  534.             if(isset($value[0]))
  535.                 $value $value[0];
  536.             else
  537.                 $value='';
  538.         }
  539.         $value = (string) $value;
  540.         $span ='<span class="jforms-radio jforms-ctl-'.$ctrl->ref.'"><input type="radio"';
  541.         foreach($ctrl->datasource->getData($this->_formas $v=>$label){
  542.             echo $span,$id,$i,'" value="',htmlspecialchars($v),'"',((string) $v===$value?' checked="checked"':''),$readonly,$class,$this->_endt;
  543.             echo '<label for="',$this->_name,'_',$ctrl->ref,'_',$i,'">',htmlspecialchars($label),'</label></span>';
  544.             $i++;
  545.         }
  546.     }
  547.  
  548.     protected function jsRadiobuttons($ctrl{
  549.  
  550.         $this->jsContent .="c = new jFormsJQControlString('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n";
  551.  
  552.         $this->commonJs($ctrl);
  553.     }
  554.  
  555.     protected function outputMenulist($ctrl$id$class$readonly$hint{
  556.         echo '<select',$id,$hint,$class,' size="1">';
  557.         $value $this->_form->getData($ctrl->ref);
  558.         if(is_array($value)){
  559.             if(isset($value[0]))
  560.                 $value $value[0];
  561.             else
  562.                 $value='';
  563.         }
  564.         $value = (string) $value;
  565.         if (!$ctrl->required{
  566.             echo '<option value=""',($value===''?' selected="selected"':''),'>',htmlspecialchars($ctrl->emptyItemLabel),'</option>';
  567.         }
  568.         foreach($ctrl->datasource->getData($this->_formas $v=>$label){
  569.             echo '<option value="',htmlspecialchars($v),'"',((string) $v===$value?' selected="selected"':''),'>',htmlspecialchars($label),'</option>';
  570.         }
  571.         echo '</select>';
  572.     }
  573.  
  574.     protected function jsMenulist($ctrl{
  575.  
  576.         $this->jsContent .="c = new jFormsJQControlString('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n";
  577.  
  578.         $this->commonJs($ctrl);
  579.     }
  580.  
  581.     protected function outputListbox($ctrl$id$class$readonly$hint{
  582.         if($ctrl->multiple){
  583.             echo '<select name="',$ctrl->ref,'[]" id="',$this->_name,'_',$ctrl->ref,'"',$hint,$class,' size="',$ctrl->size,'" multiple="multiple">';
  584.             $value $this->_form->getData($ctrl->ref);
  585.  
  586.             if(is_array($value&& count($value== 1)
  587.                 $value $value[0];
  588.  
  589.             if(is_array($value)){
  590.                 $value array_map(create_function('$v''return (string) $v;'),$value);
  591.                 foreach($ctrl->datasource->getData($this->_formas $v=>$label){
  592.                     echo '<option value="',htmlspecialchars($v),'"',(in_array((string) $v,$value,true)?' selected="selected"':''),'>',htmlspecialchars($label),'</option>';
  593.                 }
  594.             }else{
  595.                 $value = (string) $value;
  596.                 foreach($ctrl->datasource->getData($this->_formas $v=>$label){
  597.                     echo '<option value="',htmlspecialchars($v),'"',((string) $v===$value?' selected="selected"':''),'>',htmlspecialchars($label),'</option>';
  598.                 }
  599.             }
  600.             echo '</select>';
  601.         }else{
  602.             $value $this->_form->getData($ctrl->ref);
  603.  
  604.             if(is_array($value)){
  605.                 if(count($value>= 1)
  606.                     $value $value[0];
  607.                 else
  608.                     $value ='';
  609.             }
  610.  
  611.             $value = (string) $value;
  612.             echo '<select',$id,$hint,$class,' size="',$ctrl->size,'">';
  613.             foreach($ctrl->datasource->getData($this->_formas $v=>$label){
  614.                 echo '<option value="',htmlspecialchars($v),'"',((string) $v===$value?' selected="selected"':''),'>',htmlspecialchars($label),'</option>';
  615.             }
  616.             echo '</select>';
  617.         }
  618.     }
  619.  
  620.     protected function jsListbox($ctrl{
  621.         if($ctrl->multiple){
  622.             $this->jsContent .= "c = new jFormsJQControlString('".$ctrl->ref."[]', ".$this->escJsStr($ctrl->label).");\n";
  623.             $this->jsContent .= "c.multiple = true;\n";
  624.         else {
  625.             $this->jsContent .="c = new jFormsJQControlString('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n";
  626.         }
  627.  
  628.         $this->commonJs($ctrl);
  629.     }
  630.  
  631.     protected function outputTextarea($ctrl$id$class$readonly$hint{
  632.         $value $this->_form->getData($ctrl->ref);
  633.         $rows ' rows="'.$ctrl->rows.'" cols="'.$ctrl->cols.'"';
  634.         echo '<textarea',$id,$readonly,$hint,$class,$rows,'>',htmlspecialchars($value),'</textarea>';
  635.     }
  636.  
  637.     protected function jsTextarea($ctrl{
  638.         $this->jsContent .="c = new jFormsJQControlString('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n";
  639.  
  640.         $maxl$ctrl->datatype->getFacet('maxLength');
  641.         if($maxl !== null)
  642.             $this->jsContent .="c.maxLength = '$maxl';\n";
  643.  
  644.         $minl$ctrl->datatype->getFacet('minLength');
  645.         if($minl !== null)
  646.             $this->jsContent .="c.minLength = '$minl';\n";
  647.  
  648.         $this->commonJs($ctrl);
  649.     }
  650.  
  651.     protected function outputHtmleditor($ctrl$id$class$readonly$hint{
  652.         $value $this->_form->getData($ctrl->ref);
  653.         $rows ' rows="'.$ctrl->rows.'" cols="'.$ctrl->cols.'"';
  654.         echo '<textarea',$id,$readonly,$hint,$class,$rows,'>',htmlspecialchars($value),'</textarea>';
  655.     }
  656.  
  657.     protected function jsHtmleditor($ctrl{
  658.         $this->jsTextarea($ctrl);
  659.         $engine $GLOBALS['gJConfig']->htmleditors[$ctrl->config.'.engine.name'];
  660.         $this->jsContent .= 'jelix_'.$engine.'_'.$ctrl->config.'("'.$this->_name.'_'.$ctrl->ref.'","'.$this->_name."\");\n";
  661.     }
  662.  
  663.     protected function outputSecret($ctrl$id$class$readonly$hint{
  664.         $size ($ctrl->size == 0?''' size="'.$ctrl->size.'"');
  665.         $maxl $ctrl->datatype->getFacet('maxLength');
  666.         if($maxl !== null)
  667.             $maxl ' maxlength="'.$maxl.'"';
  668.         else
  669.             $maxl '';
  670.         echo '<input type="password"',$id,$readonly,$hint,$class,$size,$maxl,' value="',htmlspecialchars($this->_form->getData($ctrl->ref)),'"',$this->_endt;
  671.     }
  672.  
  673.     protected function jsSecret($ctrl{
  674.         $this->jsContent .="c = new jFormsJQControlSecret('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n";
  675.  
  676.         $maxl$ctrl->datatype->getFacet('maxLength');
  677.         if($maxl !== null)
  678.             $this->jsContent .="c.maxLength = '$maxl';\n";
  679.  
  680.         $minl$ctrl->datatype->getFacet('minLength');
  681.         if($minl !== null)
  682.             $this->jsContent .="c.minLength = '$minl';\n";
  683.  
  684.         $this->commonJs($ctrl);
  685.     }
  686.  
  687.     protected function outputSecretconfirm($ctrl$id$class$readonly$hint{
  688.         $size ($ctrl->size == 0?''' size="'.$ctrl->size.'"');
  689.         echo '<input type="password"',$id,$readonly,$hint,$class,$size,' value="',htmlspecialchars($this->_form->getData($ctrl->ref)),'"',$this->_endt;
  690.     }
  691.  
  692.     protected function jsSecretconfirm($ctrl{
  693.         $this->jsContent .="c = new jFormsJQControlConfirm('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n";
  694.         $this->commonJs($ctrl);
  695.     }
  696.  
  697.     protected function outputOutput($ctrl$id$class$readonly$hint{
  698.         $value $this->_form->getData($ctrl->ref);
  699.         echo '<input type="hidden"',$id,' value="',htmlspecialchars($value),'"',$this->_endt;
  700.         echo '<span class="jforms-value"',$hint,'>',htmlspecialchars($value),'</span>';
  701.     }
  702.  
  703.     protected function jsOutput($ctrl{
  704.     }
  705.  
  706.     protected function outputUpload($ctrl$id$class$readonly$hint{
  707.         if($ctrl->maxsize){
  708.             echo '<input type="hidden" name="MAX_FILE_SIZE" value="',$ctrl->maxsize,'"',$this->_endt;
  709.         }
  710.         echo '<input type="file"',$id,$readonly,$hint,$class,' value=""',$this->_endt// ',htmlspecialchars($this->_form->getData($ctrl->ref)),'
  711.  
  712.     }
  713.  
  714.     protected function jsUpload($ctrl{
  715.         $this->jsContent .="c = new jFormsJQControlString('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n";
  716.  
  717.         $this->commonJs($ctrl);
  718.     }
  719.  
  720.     protected function outputSubmit($ctrl$id$class$readonly$hint{
  721.         if($ctrl->standalone){
  722.             echo '<input type="submit"',$id,$hint,' class="jforms-submit" value="',htmlspecialchars($ctrl->label),'"/>';
  723.         }else{
  724.             foreach($ctrl->datasource->getData($this->_formas $v=>$label){
  725.                 // because IE6 sucks with <button type=submit> (see ticket #431), we must use input :-(
  726.                 echo '<input type="submit" name="',$ctrl->ref,'" id="',$this->_name,'_',$ctrl->ref,'_',htmlspecialchars($v),'"',
  727.                     $hint,' class="jforms-submit" value="',htmlspecialchars($label),'"/> ';
  728.             }
  729.         }
  730.     }
  731.  
  732.     protected function jsSubmit($ctrl{
  733.         // no javascript
  734.     }
  735.  
  736.     protected function outputReset($ctrl$id$class$readonly$hint{
  737.         echo '<button type="reset"',$id,$hint,' class="jforms-reset">',htmlspecialchars($ctrl->label),'</button>';
  738.     }
  739.  
  740.     protected function jsReset($ctrl{
  741.         // no javascript
  742.     }
  743.  
  744.     protected function outputCaptcha($ctrl$id$class$readonly$hint{
  745.         $ctrl->initExpectedValue();
  746.         echo '<span class="jforms-captcha-question">',htmlspecialchars($ctrl->question),'</span> ';
  747.         echo '<input type="text"',$id,$hint,$class,' value=""',$this->_endt;
  748.     }
  749.  
  750.     protected function jsCaptcha($ctrl{
  751.         $this->jsTextarea($ctrl);
  752.     }
  753.  
  754.     protected function outputGroup($ctrl$id$class$readonly$hint{
  755.         echo '<fieldset><legend>',htmlspecialchars($ctrl->label),"</legend>\n";
  756.         echo '<table class="jforms-table-group" border="0">',"\n";
  757.         foreach$ctrl->getChildControls(as $ctrlref=>$c){
  758.             if($c->type == 'submit' || $c->type == 'reset' || $c->type == 'hidden'continue;
  759.             if(!$this->_form->isActivated($ctrlref)) continue;
  760.             echo '<tr><th scope="row">';
  761.             $this->outputControlLabel($c);
  762.             echo "</th>\n<td>";
  763.             $this->outputControl($c);
  764.             echo "</td></tr>\n";
  765.         }
  766.         echo "</table></fieldset>";
  767.     }
  768.  
  769.     protected function jsGroup($ctrl{
  770.         //no javacript
  771.     }
  772.  
  773.     protected function outputChoice($ctrl$id$class$readonly$hint{
  774.         echo '<ul class="jforms-choice jforms-ctl-'.$ctrl->ref.'" >',"\n";
  775.  
  776.         $value $this->_form->getData($ctrl->ref);
  777.         if(is_array($value)){
  778.             if(isset($value[0]))
  779.                 $value $value[0];
  780.             else
  781.                 $value='';
  782.         }
  783.  
  784.         $i=0;
  785.         $id=' name="'.$ctrl->ref.'" id="'.$this->_name.'_'.$ctrl->ref.'_';
  786.         $this->jsChoiceInternal($ctrl);
  787.         $this->jsContent .="c2 = c;\n";
  788.         $this->isRootControl = false;
  789.         foreach$ctrl->items as $itemName=>$listctrl){
  790.             echo '<li><label><input type="radio"',$id,$i,'" value="',htmlspecialchars($itemName),'"';
  791.             echo ($itemName==$value?' checked="checked"':''),$readonly;
  792.             echo ' onclick="jFormsJQ.getForm(\'',$this->_name,'\').getControl(\'',$ctrl->ref,'\').activate(\'',$itemName,'\')"'$this->_endt;
  793.             echo htmlspecialchars($ctrl->itemsNames[$itemName]),'</label> ';
  794.  
  795.             $displayedControls false;
  796.             foreach($listctrl as $ref=>$c{
  797.                 if(!$this->_form->isActivated($ref|| $c->type == 'hidden'continue;
  798.                 $displayedControls true;
  799.                 echo ' <span class="jforms-item-controls">';
  800.                 // we remove readonly status so when a user change the choice and
  801.                 // javascript is deactivated, it can still change the value of the control
  802.                 $ro $c->isReadOnly();
  803.                 if($ro && $readonly == ''$c->setReadOnly(false);
  804.                 $this->outputControlLabel($c);
  805.                 echo ' ';
  806.                 $this->outputControl($c);
  807.                 if($ro$c->setReadOnly(true);
  808.                 echo "</span>\n";
  809.                 $this->jsContent .="c2.addControl(c, ".$this->escJsStr($itemName).");\n";
  810.             }
  811.             if(!$displayedControls{
  812.                 $this->jsContent .="c2.items[".$this->escJsStr($itemName)."]=[];\n";
  813.             }
  814.  
  815.             echo "</li>\n";
  816.             $i++;
  817.         }
  818.         echo "</ul>\n";
  819.         $this->isRootControl = true;
  820.     }
  821.  
  822.     protected function jsChoice($ctrl{
  823.         $value $this->_form->getData($ctrl->ref);
  824.         if(is_array($value)){
  825.             if(isset($value[0]))
  826.                 $value $value[0];
  827.             else
  828.                 $value='';
  829.         }
  830.         $this->jsContent .= "c2.activate('".$value."');\n";
  831.     }
  832.  
  833.     protected function jsChoiceInternal($ctrl{
  834.  
  835.         $this->jsContent .="c = new jFormsJQControlChoice('".$ctrl->ref."', ".$this->escJsStr($ctrl->label).");\n";
  836.  
  837.         $this->commonJs($ctrl);
  838.     }
  839.  
  840.     protected function outputHelp($ctrl{
  841.         if ($ctrl->help{
  842.             if($ctrl->type == 'checkboxes' || ($ctrl->type == 'listbox' && $ctrl->multiple)){
  843.                 $name=$ctrl->ref.'[]';
  844.             }else{
  845.                 $name=$ctrl->ref;
  846.             }
  847.             echo '<span class="jforms-help"><a href="javascript:jFormsJQ.showHelp(\''$this->_name.'\',\''.$name.'\')">?</a></span>';
  848.         }
  849.     }
  850. }

Documentation generated on Thu, 22 Mar 2012 22:13:31 +0100 by phpDocumentor 1.4.3