LSattr_html :: jsonCompositeAttribute : Add help_info and multiple parameters on components

This commit is contained in:
Benjamin Renard 2018-04-26 11:23:00 +02:00
parent 5335ca477c
commit 9cb3bb8e18
14 changed files with 406 additions and 76 deletions

View file

@ -19,8 +19,10 @@
'components' => array (
'[clé composant 1]' => array (
'label' => '[Label du composant]',
'help_info' => '[Message d'aide sur le composant]',
'type' => '[Type de la valeur stocké]',
'required' => [Booléen],
'multiple' => [Booléen],
'check_data' => => array (
// Régle de vérification syntaxique des données saisies
),
@ -57,6 +59,14 @@
</listitem>
</varlistentry>
<varlistentry>
<term>help_info</term>
<listitem>
<simpara>Message d'aide sur le composant (affiché uniquement en mode édition).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>type</term>
<listitem>
@ -77,6 +87,22 @@
</listitem>
</varlistentry>
<varlistentry>
<term>multiple</term>
<listitem>
<simpara>Booléen définissant si ce composant peut stocker plusieurs valeurs (Défaut :
<emphasis>Faux</emphasis>).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>required</term>
<listitem>
<simpara>Booléen définissant si ce composant doit obligatoirement être défini (Défaut :
<emphasis>Faux</emphasis>).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>check_data</term>
<listitem>

View file

@ -7,12 +7,29 @@ ul.LSformElement_jsonCompositeAttribute li.noValue {
border: none;
}
ul.LSformElement_jsonCompositeAttribute p {
ul.LSformElement_jsonCompositeAttribute div {
margin: 0;
margin-bottom: 2px;
}
ul.LSformElement_jsonCompositeAttribute div label {
display: inline-block;
height: 100%;
vertical-align: top;
width: 10em;
font-weight: bold;
}
ul.LSformElement_jsonCompositeAttribute div ul {
display: inline-block;
list-style-type: none;
padding: 0;
margin: 0;
}
ul.LSformElement_jsonCompositeAttribute p label {
font-weight: bold;
ul.LSformElement_jsonCompositeAttribute div ul li {
margin-bottom: 2px;
border: none;
}
div.jsonCompositeAttribute_possibleValues ul {

View file

@ -100,6 +100,20 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
)
);
LSsession :: addCssFile('LSformElement_jsonCompositeAttribute.css');
if (!$this -> isFreeze()) {
LSsession :: addJSconfigParam(
$this -> name,
array (
'components' => $this -> components,
)
);
LSsession :: addJSscript('LSformElement_jsonCompositeAttribute_field_value_component_text_value.js');
LSsession :: addJSscript('LSformElement_jsonCompositeAttribute_field_value_component.js');
LSsession :: addJSscript('LSformElement_jsonCompositeAttribute_field_value.js');
LSsession :: addJSscript('LSformElement_jsonCompositeAttribute_field.js');
LSsession :: addJSscript('LSformElement_jsonCompositeAttribute.js');
}
return $return;
}
@ -132,19 +146,29 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
*
* @param[in] $c string The component name
* @param[in] $value string The value
* @param[in] $inLoop boolean Internal param to control recursion
*
* @retval array
**/
function translateComponentValue($c,$value) {
$retval = array (
'translated' => $value,
'value' => $value,
);
if (isset($this -> components[$c])) {
if ($this -> components[$c]['type']=='select_list') {
$retval['translated'] = $this -> getSelectListComponentValueLabel($c,$value);
function translateComponentValue($c,$value,$inLoop=false) {
if (!$inLoop && isset($this -> components[$c]['multiple']) && $this -> components[$c]['multiple']) {
$retval = array();
if (!is_array($value))
$value = array($value);
foreach($value as $val)
$retval[] = $this -> translateComponentValue($c, $val, true);
}
else {
$retval = array (
'translated' => $value,
'value' => $value,
);
if (isset($this -> components[$c])) {
if ($this -> components[$c]['type']=='select_list') {
$retval['translated'] = $this -> getSelectListComponentValueLabel($c,$value);
}
//elseif type == 'text' => no transformation
}
//elseif type == 'text' => no transformation
}
return $retval;
}
@ -202,66 +226,73 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
return true;
}
$count=0;
$end=false;
$return[$this -> name]=array();
while ($end==false) {
$value=array();
$parseValue=array();
$errors=array();
$unemptyComponents=array();
foreach ($this -> components as $c => $cconf) {
if (isset($_POST[$this -> name.'__'.$c][$count])) {
$parseValue[$c]=$_POST[$this -> name.'__'.$c][$count];
if ($cconf['required'] && empty($parseValue[$c])) {
$errors[]=getFData(__('Component %{c} must be defined'),__($cconf['label']));
continue;
}
if (empty($parseValue[$c])) {
continue;
}
$unemptyComponents[]=$c;
if ($cconf['type']=='select_list') {
if (!$this -> getSelectListComponentValueLabel($c, $parseValue[$c])) {
$errors[]=getFData(__('Invalid value for component %{c}.'),__($cconf['label']));
}
}
if (is_array($cconf['check_data'])) {
foreach($cconf['check_data'] as $ruleType => $rconf) {
$className='LSformRule_'.$ruleType;
if (LSsession::loadLSclass($className)) {
$r=new $className();
if (!$r -> validate($parseValue[$c],$rconf,$this)) {
if (isset($rconf['msg'])) {
$errors[]=getFData(__($rconf['msg']),__($cconf['label']));
if (is_array($_POST[$this -> name.'__values_uuid'])) {
foreach ($_POST[$this -> name.'__values_uuid'] as $uuid) {
$value=array();
$parseValue=array();
$errors=array();
$unemptyComponents=array();
foreach ($this -> components as $c => $cconf) {
if (isset($_POST[$this -> name.'__'.$c.'__'.$uuid])) {
if (!is_array($_POST[$this -> name.'__'.$c.'__'.$uuid]))
$_POST[$this -> name.'__'.$c.'__'.$uuid] = array($_POST[$this -> name.'__'.$c.'__'.$uuid]);
$parseValue[$c]=array();
foreach($_POST[$this -> name.'__'.$c.'__'.$uuid] as $val) {
if (empty($val))
continue;
$parseValue[$c][] = $val;
if ($cconf['type']=='select_list') {
if (!$this -> getSelectListComponentValueLabel($c, $val)) {
$errors[]=getFData(_('Invalid value "%{value}" for component %{component}.'),array('value' => $val, 'component' => __($cconf['label'])));
}
}
if (is_array($cconf['check_data'])) {
foreach($cconf['check_data'] as $ruleType => $rconf) {
$className='LSformRule_'.$ruleType;
if (LSsession::loadLSclass($className)) {
$r=new $className();
if (!$r -> validate($val,$rconf,$this)) {
if (isset($rconf['msg'])) {
$errors[]=getFData(__($rconf['msg']),__($cconf['label']));
}
else {
$errors[]=getFData(_('Invalid value "%{value}" for component %{component}.'),array('value' => $val, 'component' => __($cconf['label'])));
}
}
}
else {
$errors[]=getFData(__('Invalid value for component %{c}.'),__($cconf['label']));
$errors[]=getFData(_("Can't validate value of component %{c}."),__($cconf['label']));
}
}
}
else {
$errors[]=getFData(__("Can't validate value of component %{c}."),__($cconf['label']));
}
}
if (!isset($cconf['multiple']) || !$cconf['multiple']) {
if (count($parseValue[$c])>=1)
$parseValue[$c] = $parseValue[$c][0];
else
$parseValue[$c] = '';
}
if ($cconf['required'] && empty($parseValue[$c])) {
$errors[]=getFData(_('Component %{c} must be defined'),__($cconf['label']));
continue;
}
$unemptyComponents[]=$c;
$value[$c]=$parseValue[$c];
}
$value[$c]=$parseValue[$c];
}
else {
// end of value break
$end=true;
break;
}
}
if (!$end) {
if (!empty($unemptyComponents)) {
foreach($errors as $e) {
$this -> form -> setElementError($this -> attr_html,$e);
}
$return[$this -> name][]=json_encode($value);
}
$count++;
}
}
return true;

View file

@ -122,6 +122,7 @@ class LStemplate {
self :: registerFunction("tr", "LStemplate_smarty_tr");
self :: registerFunction("img", "LStemplate_smarty_img");
self :: registerFunction("css", "LStemplate_smarty_css");
self :: registerFunction("uniqid", "LStemplate_smarty_uniqid");
return True;
}
@ -297,6 +298,12 @@ function LStemplate_smarty_css($params) {
echo LStemplate :: getCSSPath($name);
}
function LStemplate_smarty_uniqid($params, &$smarty) {
if (!isset($params['var']))
$params['var'] = 'uniqid';
$smarty -> assign($params['var'], uniqid());
}
// Errors
LSerror :: defineError('LStemplate_01',
_("LStemplate : Template %{file} not found.")

View file

@ -0,0 +1,18 @@
var LSformElement_jsonCompositeAttribute = new Class({
initialize: function(){
this.fields = [];
this.initialiseLSformElement_jsonCompositeAttribute();
},
initialiseLSformElement_jsonCompositeAttribute: function(el) {
if (!$type(el)) {
el = document;
}
el.getElements('ul.LSformElement_jsonCompositeAttribute').each(function(ul) {
this.fields[ul.id] = new LSformElement_jsonCompositeAttribute_field(ul);
}, this);
}
});
window.addEvent(window.ie ? 'load' : 'domready', function() {
varLSformElement_jsonCompositeAttribute = new LSformElement_jsonCompositeAttribute();
});

View file

@ -0,0 +1,32 @@
var LSformElement_jsonCompositeAttribute_field = new Class({
initialize: function(ul){
this.ul=ul;
this.dd=ul.getParent();
this.name = ul.id;
this.values = [];
this.field_type = ul.get('data-fieldType');
this.initializeLSformElement_jsonCompositeAttribute_field();
varLSform.addField(this.name,this);
},
initializeLSformElement_jsonCompositeAttribute_field: function(el) {
if (!$type(el)) {
el = this.ul;
}
el.getChildren('li').each(function(li) {
this.values.push(new LSformElement_jsonCompositeAttribute_field_value(li,this.name,this.field_type));
}, this);
},
clearValue: function() {
if (this.values.length>1) {
for(var i=1; i<=this.values.length; i++) {
$(this.values[i].li).dispose();
}
this.values[0].clear();
}
else if (this.values.length==1) {
this.values[0].clear();
}
}
});

View file

@ -0,0 +1,31 @@
var LSformElement_jsonCompositeAttribute_field_value = new Class({
initialize: function(li,name,field_type){
this.li=li;
this.name = name;
this.input_uuid = li.getElement('input[name='+name+'__values_uuid[]]');
this.uuid = this.input_uuid.get('value');
this.components = {};
this.field_type = field_type;
this.initializeLSformElement_jsonCompositeAttribute_field_value();
varLSform.addModule(field_type,this);
},
initializeLSformElement_jsonCompositeAttribute_field_value: function(el) {
if (!$type(el)) {
el = this.li;
}
el.getElements('div').each(function(div) {
this.components[div.get('data-component')]=new LSformElement_jsonCompositeAttribute_field_value_component(div,div.get('data-component'),this.name,this.uuid);
}, this);
},
reinitialize: function(el) {
this.initializeLSformElement_jsonCompositeAttribute_field_value(el);
},
clear: function() {
for (c in this.components) {
this.components[c].clear();
}
}
});

View file

@ -0,0 +1,62 @@
var LSformElement_jsonCompositeAttribute_field_value_component = new Class({
initialize: function(div,name,field_name,field_uuid){
this.div = div;
this.field_name = field_name;
this.field_uuid = field_uuid;
this.field_params = varLSdefault.LSjsConfig[this.field_name];
this.name = name;
this.params = this.field_params['components'][this.name];
this.label = div.getElement('label');
if (this.params.type == 'select_list') {
this.select = div.getElement('select');
}
else {
// Type text
this.ul = div.getElement('ul');
this.lis = {};
this.values = {};
this.ul.getElements('li').each(function(li) {
this.initTextComponentValue(li);
}, this);
}
},
initTextComponentValue: function(li) {
var uuid = generate_uuid();
this.lis[uuid] = li;
this.values[uuid] = new LSformElement_jsonCompositeAttribute_field_value_component_text_value(this,li);
},
onAddTextValueBtnClick: function(after) {
var li = new Element('li');
var input = new Element('input');
input.type='text';
input.name=this.field_name+'__'+this.name+'__'+this.field_uuid+'[]';
input.injectInside(li);
li.injectAfter(after.li);
this.initTextComponentValue(li);
},
onRemoveTextValueBtnClick: function(value) {
if (this.ul.getElements('li').length == 1) {
value.clear.bind(value)();
}
else {
value.remove.bind(value)();
}
},
clear: function() {
if (this.params.type == 'select_list') {
this.select.selectedIndex=-1;
}
else {
this.values.each(function(value) {
value.clear();
}, this);
}
}
});

View file

@ -0,0 +1,30 @@
var LSformElement_jsonCompositeAttribute_field_value_component_text_value = new Class({
initialize: function(component,li) {
this.component = component;
this.li = li;
this.input = li.getElement('input');
if (this.component.params.multiple) {
this.addValueBtn = new Element('img');
this.addValueBtn.src = varLSdefault.imagePath('add');
this.addValueBtn.addClass('btn');
this.addValueBtn.addEvent('click',this.component.onAddTextValueBtnClick.bind(this.component,this));
this.addValueBtn.injectInside(this.li);
this.removeValueBtn = new Element('img');
this.removeValueBtn.src = varLSdefault.imagePath('remove');
this.removeValueBtn.addClass('btn');
this.removeValueBtn.addEvent('click',this.component.onRemoveTextValueBtnClick.bind(this.component,this));
this.removeValueBtn.injectInside(this.li);
}
},
clear: function() {
this.input.value = '';
},
remove: function() {
this.li.destroy();
}
});

View file

@ -245,6 +245,18 @@ function urlAddVar(url,name,value) {
return url;
}
/*
* Generate UUID
*/
function generate_uuid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
/*
* Base64 compatibility
*

View file

@ -7,8 +7,8 @@ msgid ""
msgstr ""
"Project-Id-Version: LdapSaisie\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-02-08 18:12+0100\n"
"PO-Revision-Date: 2018-02-08 18:13+0100\n"
"POT-Creation-Date: 2018-04-26 11:34+0200\n"
"PO-Revision-Date: 2018-04-26 11:35+0200\n"
"Last-Translator: Benjamin Renard <brenard@zionetrix.net>\n"
"Language-Team: LdapSaisie <ldapsaisie-users@lists.labs.libre-entreprise."
"org>\n"
@ -435,7 +435,7 @@ msgstr "Affichier la clé en entier."
msgid "Unknown type"
msgstr "Type inconnu"
#: includes/class/class.LSformElement_valueWithUnit.php:190
#: includes/class/class.LSformElement_valueWithUnit.php:200
msgid ""
"LSformElement_valueWithUnit : Units configuration data are missing for the "
"attribute %{attr}."
@ -490,12 +490,12 @@ msgstr "LStemplate : Impossible de charger le fichier de support de Smarty 3."
msgid "LStemplate : Smarty version not recognized."
msgstr "LStemplate : Version de Smarty non reconnue."
#: includes/class/class.LStemplate.php:129
#: includes/class/class.LStemplate.php:130
msgid "LStemplate : Can't load Smarty."
msgstr ""
"LStemplate : Impossible de charger le moteur de gestion de template Smarty."
#: includes/class/class.LStemplate.php:302
#: includes/class/class.LStemplate.php:309
msgid "LStemplate : Template %{file} not found."
msgstr "LStemplate : le template %{file} est introuvable."
@ -1625,6 +1625,19 @@ msgstr ""
"LSformRule_inarray : Les valeurs possibles n'ont pas été configurées pour "
"valider les données."
#: includes/class/class.LSformElement_jsonCompositeAttribute.php:249
#: includes/class/class.LSformElement_jsonCompositeAttribute.php:262
msgid "Invalid value \"%{value}\" for component %{component}."
msgstr "Valeur invalide pour le composant %{component} : \"%{value}\""
#: includes/class/class.LSformElement_jsonCompositeAttribute.php:267
msgid "Can't validate value of component %{c}."
msgstr "Impossible de valider la valeur du composant %{c}."
#: includes/class/class.LSformElement_jsonCompositeAttribute.php:281
msgid "Component %{c} must be defined"
msgstr "Le composant %{c} est obligatoire."
#: includes/class/class.LSformRule_date.php:59
msgid "LSformRule_date : No date format specify."
msgstr "LSformRule_date : Aucun format de date spécifié."

View file

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-02-08 18:12+0100\n"
"POT-Creation-Date: 2018-04-26 11:33+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -388,7 +388,7 @@ msgstr ""
msgid "Unknown type"
msgstr ""
#: includes/class/class.LSformElement_valueWithUnit.php:190
#: includes/class/class.LSformElement_valueWithUnit.php:200
msgid ""
"LSformElement_valueWithUnit : Units configuration data are missing for the "
"attribute %{attr}."
@ -435,11 +435,11 @@ msgstr ""
msgid "LStemplate : Smarty version not recognized."
msgstr ""
#: includes/class/class.LStemplate.php:129
#: includes/class/class.LStemplate.php:130
msgid "LStemplate : Can't load Smarty."
msgstr ""
#: includes/class/class.LStemplate.php:302
#: includes/class/class.LStemplate.php:309
msgid "LStemplate : Template %{file} not found."
msgstr ""
@ -1397,6 +1397,19 @@ msgid ""
"data."
msgstr ""
#: includes/class/class.LSformElement_jsonCompositeAttribute.php:249
#: includes/class/class.LSformElement_jsonCompositeAttribute.php:262
msgid "Invalid value \"%{value}\" for component %{component}."
msgstr ""
#: includes/class/class.LSformElement_jsonCompositeAttribute.php:267
msgid "Can't validate value of component %{c}."
msgstr ""
#: includes/class/class.LSformElement_jsonCompositeAttribute.php:281
msgid "Component %{c} must be defined"
msgstr ""
#: includes/class/class.LSformRule_date.php:59
msgid "LSformRule_date : No date format specify."
msgstr ""

View file

@ -2,35 +2,73 @@
{if isset($parseValue)}
{foreach $components as $c => $cconf}
{if !isset($parseValue[$c])}{continue}{/if}
<p>
<div>
<label>{tr msg=$cconf.label} : </label>
<span title="{$parseValue[$c].value}">{$parseValue[$c].translated}</span>
</p>
<ul>
{if $cconf.multiple && is_array($parseValue[$c])}
{foreach from=$parseValue[$c] item=$cval}
<li><span title="{$cval.value}">{$cval.translated}</span></li>
{/foreach}
{else}
<li><span title="{$parseValue[$c].value}">{$parseValue[$c].translated}</span></li>
{/if}
</ul>
</div>
{/foreach}
{else}
{$noValueTxt}
{/if}
{else}
{foreach $components as $c => $cconf}
<p data-component="{$c}">
<label>{tr msg=$cconf.label}{if $cconf.required}*{/if} :</label>
{uniqid var="uuid"}
<input type='hidden' name="{$attr_name}__values_uuid[]" value="{$uuid}" />
{foreach from=$components key=$c item=$cconf name=components}
<div data-component="{$c}" data-uuid="{$uuid}">
<label>
{tr msg=$cconf.label}{if $cconf.required}*{/if}
{if $cconf.help_info}<img class='LStips' src="{img name='help'}" alt='?' title="{$cconf.help_info}"/>{/if}
:
</label>
{if $cconf.type=='select_list'}
<select name='{$attr_name}__{$c}[]'>
<select name='{$attr_name}__{$c}__{$uuid}[]' {if $cconf.multiple}multiple{/if}>
{foreach from=$cconf.possible_values key=key item=label}
{if is_array($label)}
{if count($label.possible_values)>0}
<optgroup label="{$label.label}">
{html_options options=$label.possible_values selected=$parseValue[$c].value}
{if $cconf.multiple && is_array($parseValue[$c])}
{html_options options=$label.possible_values selected=$parseValue[$c]}
{else}
{html_options options=$label.possible_values selected=$parseValue[$c].value}
{/if}
</optgroup>
{/if}
{else}
<option value="{$key}" {if $key == $parseValue[$c].value}selected{/if}>{$label}</option>
{assign var="selected" value=0}
{if $cconf.multiple && is_array($parseValue[$c])}
{foreach from=$parseValue[$c] item=cval}
{if $key==$cval.value}{assign var="selected" value=1}{/if}
{/foreach}
{else}
{if $key == $parseValue[$c].value}
{assign var="selected" value=1}
{/if}
{/if}
<option value="{$key}" {if $selected == 1}selected{/if}>{$label}</option>
{/if}
{/foreach}
</select>
{else}
<input type='text' name='{$attr_name}__{$c}[]' value="{if $parseValue and $parseValue[$c]}{$parseValue[$c].value}{/if}"/>
<ul>
{if $cconf.multiple && is_array($parseValue[$c])}
{foreach from=$parseValue[$c] item=$cval}
<li><input type='text' name='{$attr_name}__{$c}__{$uuid}[]' value='{$cval.value|escape:"quotes"}'/></li>
{foreachelse}
<li><input type='text' name='{$attr_name}__{$c}__{$uuid}[]' value=''/></li>
{/foreach}
{else}
<li><input type='text' name='{$attr_name}__{$c}__{$uuid}[]' value='{if $parseValue and $parseValue[$c]}{$parseValue[$c].value|escape:"quotes"}{/if}'/></li>
{/if}
</ul>
{/if}
</p>
</div>
{/foreach}
{/if}