diff --git a/public_html/css/default/LSformElement_valueWithUnit.css b/public_html/css/default/LSformElement_valueWithUnit.css new file mode 100644 index 00000000..4ca147c4 --- /dev/null +++ b/public_html/css/default/LSformElement_valueWithUnit.css @@ -0,0 +1,14 @@ +input.LSformElement_valueWithUnit { + width: 3em !important; + text-align: right; +} + +select.LSformElement_valueWithUnit { + width: 6em; +} + +span.LSformElement_valueWithUnit_unknown { + color: #f59a67; +} + + diff --git a/public_html/includes/class/class.LSattr_html_valueWithUnit.php b/public_html/includes/class/class.LSattr_html_valueWithUnit.php new file mode 100644 index 00000000..1df695fe --- /dev/null +++ b/public_html/includes/class/class.LSattr_html_valueWithUnit.php @@ -0,0 +1,34 @@ + + */ +class LSattr_html_valueWithUnit extends LSattr_html { + + var $LSformElement_type = 'valueWithUnit'; + +} + +?> diff --git a/public_html/includes/class/class.LSformElement_valueWithUnit.php b/public_html/includes/class/class.LSformElement_valueWithUnit.php new file mode 100644 index 00000000..4503138c --- /dev/null +++ b/public_html/includes/class/class.LSformElement_valueWithUnit.php @@ -0,0 +1,168 @@ + + */ + +class LSformElement_valueWithUnit extends LSformElement { + + var $fieldTemplate = 'LSformElement_valueWithUnit_field.tpl'; + + /** + * Retourne les unites de l'attribut + * + * @retval array|False Le tableau contenant en cle les seuils et en valeur les labels des unites. + * Si le parametre units n'est pas defini, cette fonction retournera False + **/ + function getUnits() { + if (isset($this -> params['html_options']['units']) && is_array($this -> params['html_options']['units'])) { + $units=array(); + foreach($this -> params['html_options']['units'] as $sill => $label) { + $units[$sill]=__($label); + } + return $units; + } + LSerror :: addErrorCode('LSformElement_valueWithUnit_01',$this -> name); + return; + } + + /** + * Retourne les infos d'affichage de l'élément + * + * Cette méthode retourne les informations d'affichage de l'élement + * + * @retval array + */ + function getDisplay(){ + $return = $this -> getLabelInfos(); + + $values_and_units=array(); + $units=$this -> getUnits(); + + if ($units) { + foreach ($this -> values as $value) { + if (ereg('^([0-9]*)$',$value,$regs)) { + $infos = array( + 'value' => $regs[1] + ); + foreach($units as $sill => $label) { + if ($infos['value'] >= $sill) { + if ($infos['value'] % $sill == 0) { + $infos['valueWithUnit']=$infos['value']/$sill; + $infos['unitSill']=$sill; + $infos['unitLabel']=$label; + break; + } + } + } + $values_and_units[$value] = $infos; + } + else { + $values_and_units[$value] = array( + 'unknown' => _('Incorrect value') + ); + } + } + } + + LSsession :: addCssFile('LSformElement_valueWithUnit.css'); + + $return['html']=$this -> fetchTemplate( + NULL, + array( + 'values_and_units' => $values_and_units, + 'units' => $units + ) + ); + return $return; + } + + /** + * Retourne le code HTML d'un champ vide + * + * @retval string Code HTML d'un champ vide. + */ + function getEmptyField() { + return $this -> fetchTemplate( + $this -> fieldTemplate, + array( + 'units' => $this -> getUnits() + ) + ); + } + + /** + * Recupère la valeur de l'élement passée en POST + * + * Cette méthode vérifie la présence en POST de la valeur de l'élément et la récupère + * pour la mettre dans le tableau passer en paramètre avec en clef le nom de l'élément + * + * @param[] array Pointeur sur le tableau qui recupèrera la valeur. + * + * @retval boolean true si la valeur est présente en POST, false sinon + */ + function getPostData(&$return) { + if($this -> isFreeze()) { + return true; + } + $return[$this -> name]=array(); + if (isset($_POST[$this -> name.'_valueWithUnit'])) { + if(!is_array($_POST[$this -> name.'_valueWithUnit'])) { + $_POST[$this -> name.'_valueWithUnit'] = array($_POST[$this -> name.'_valueWithUnit']); + } + if(isset($_POST[$this -> name.'_unitFact']) && !is_array($_POST[$this -> name.'_unitFact'])) { + $_POST[$this -> name.'_unitFact'] = array($_POST[$this -> name.'_unitFact']); + } + foreach($_POST[$this -> name.'_valueWithUnit'] as $key => $val) { + if (!empty($val)) { + $f = 1; + if (isset($_POST[$this -> name.'_unitFact'][$key]) && ($_POST[$this -> name.'_unitFact'][$key]!=1)) { + $f = $_POST[$this -> name.'_unitFact'][$key]; + } + $return[$this -> name][$key] = ($val*$f); + } + } + } + if (isset($_POST[$this -> name.'_value'])) { + if (!is_array($_POST[$this -> name.'_value'])) { + $_POST[$this -> name.'_value']=array($_POST[$this -> name.'_value']); + } + $return[$this -> name]=array_merge($return[$this -> name],$_POST[$this -> name.'_value']); + } + return true; + } +} + +/* + * Error Codes + */ +LSerror :: defineError('LSformElement_valueWithUnit_01', +_("LSformElement_valueWithUnit : Units configuration data are missing for the attribute %{attr}.") +); +?> diff --git a/public_html/templates/default/LSformElement_valueWithUnit_field.tpl b/public_html/templates/default/LSformElement_valueWithUnit_field.tpl new file mode 100644 index 00000000..dfbcc240 --- /dev/null +++ b/public_html/templates/default/LSformElement_valueWithUnit_field.tpl @@ -0,0 +1,27 @@ +{if $freeze} + {if $value} + {if $values_and_units[$value].unknown} + {$values_and_units[$value].unknown} + {else} + {if $values_and_units[$value].valueWithUnit} + {$values_and_units[$value].valueWithUnit}{$values_and_units[$value].unitLabel} + {else} + {$values_and_units[$value].value} + {/if} + {/if} + {else} + {$noValueTxt} + {/if} +{else} + {if $values_and_units[$value].valueWithUnit} + + + {else} + + {/if} + {if $values_and_units[$value].unknown} + {$values_and_units[$value].unknown} + {/if} +{/if}