Added LSformElement_valueWithUnit

This commit is contained in:
Benjamin Renard 2011-03-24 15:46:51 +01:00
parent c8e3daf5fd
commit a3fdebd362
4 changed files with 243 additions and 0 deletions

View file

@ -0,0 +1,14 @@
input.LSformElement_valueWithUnit {
width: 3em !important;
text-align: right;
}
select.LSformElement_valueWithUnit {
width: 6em;
}
span.LSformElement_valueWithUnit_unknown {
color: #f59a67;
}

View file

@ -0,0 +1,34 @@
<?php
/*******************************************************************************
* Copyright (C) 2007 Easter-eggs
* http://ldapsaisie.labs.libre-entreprise.org
*
* Author: See AUTHORS file in top-level directory.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
******************************************************************************/
/**
* Type d'attribut HTML valueWithUnit
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSattr_html_valueWithUnit extends LSattr_html {
var $LSformElement_type = 'valueWithUnit';
}
?>

View file

@ -0,0 +1,168 @@
<?php
/*******************************************************************************
* Copyright (C) 2007 Easter-eggs
* http://ldapsaisie.labs.libre-entreprise.org
*
* Author: See AUTHORS file in top-level directory.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
******************************************************************************/
/**
* Element valueWithUnit d'un formulaire pour LdapSaisie
*
* Cette classe définis les éléments valueWithUnit des formulaires.
* Elle étant la classe basic LSformElement.
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
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}.")
);
?>

View file

@ -0,0 +1,27 @@
{if $freeze}
{if $value}
{if $values_and_units[$value].unknown}
<span class='LSformElement_valueWithUnit_unknown'>{$values_and_units[$value].unknown}</span>
{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}
<input name='{$attr_name}_valueWithUnit[]' type=text class='LSformElement_valueWithUnit' value="{$values_and_units[$value].valueWithUnit}"/>
<select name='{$attr_name}_unitFact[]' class='LSform LSformElement_valueWithUnit'>
{html_options options=$units selected=$values_and_units[$value].unitSill}
</select>
{else}
<input name='{$attr_name}_value[]' type=text class='LSformElement_valueWithUnit' value="{$values_and_units[$value].value}"/>
{/if}
{if $values_and_units[$value].unknown}
<span class='LSformElement_valueWithUnit_unknown'>{$values_and_units[$value].unknown}</span>
{/if}
{/if}