LSformElement :: select_list / select_box : permit to put possible values in labelled group

This commit is contained in:
Benjamin Renard 2015-07-25 18:23:06 +02:00
parent a2218dc59a
commit 772cd8b0da
13 changed files with 2655 additions and 2027 deletions

View file

@ -19,6 +19,16 @@
'filter' => '[Filtre de recherche des LSobject]', 'filter' => '[Filtre de recherche des LSobject]',
'scope' => '[Scope de la recherche]', 'scope' => '[Scope de la recherche]',
'basedn' => '[Basedn de la recherche]' 'basedn' => '[Basedn de la recherche]'
),
array (
'label' => '[LSformat du nom du groupe de valeurs]',
'possible_values' => array (
'[LSformat de la valeur clé]' => '[LSformat du nom d'affichage]',
...
'OTHER_OBJECT' => array (
...
)
)
) )
), ),
'sort' => [Booléen], 'sort' => [Booléen],
@ -43,7 +53,14 @@
associée est alors un tableau associatif dont les valeurs clés sont les noms associée est alors un tableau associatif dont les valeurs clés sont les noms
des paramètres de configuration de la recherche de ces &LSobjects; et les des paramètres de configuration de la recherche de ces &LSobjects; et les
valeurs associées, les valeurs des paramètres.</para> valeurs associées, les valeurs des paramètres.</para>
<para>Il est possible de regrouper des valeurs de l'attribut en plaçant leur
déclaration dans un sous-tableau. Ce sous-tableau devra contenir la clé
<literal>label</literal> dont la valeur associé sera le &LSformat; du nom du groupe
de valeurs. Ce &LSformat; est composé à partir des valeurs de l'objet courant
(attributs, dn, ...). Une seconde clé <literal>possible_values</literal> regroupera
les valeurs possibles du groupe. Comme pour le tableau principal, la clé <literal>
OTHER_OBJECT</literal> permet d'imcorporer une liste d'&LSobject;.</para>
<variablelist> <variablelist>
<varlistentry> <varlistentry>
<term>object_type</term> <term>object_type</term>

View file

@ -306,21 +306,27 @@ $GLOBALS['LSobjects']['LSpeople'] = array (
'html_type' => 'select_list', 'html_type' => 'select_list',
'html_options' => array ( 'html_options' => array (
'possible_values' => array( 'possible_values' => array(
'OTHER_OBJECT' => array( '0' => 'No group',
'object_type' => 'LSgroup', // Nom de l'objet à lister array (
'display_name_format' => '%{cn} (%{gidNumber})', // Spécifie le attributs à lister pour le choix, 'label' => 'LDAP Groups',
// si non définie => utilisation du 'display_name_format' 'possible_values' => array (
// de la définition de l'objet 'OTHER_OBJECT' => array (
'object_type' => 'LSgroup', // Nom de l'objet à lister
'value_attribute' => 'gidNumber', // Spécifie le attributs dont la valeur sera retournée par 'display_name_format' => '%{cn} (%{gidNumber})', // Spécifie le attributs à lister pour le choix,
/*'filter' => // le formulaire spécifie les filtres de recherche pour // si non définie => utilisation du 'display_name_format'
array ( // l'établissement de la liste d'objets : // de la définition de l'objet
array( // Premier filtre
'filter' => 'cn=*a*', 'value_attribute' => 'gidNumber', // Spécifie le attributs dont la valeur sera retournée par
//'basedn' => 'o=company', /*'filter' => // le formulaire spécifie les filtres de recherche pour
'scope' => 'sub', array ( // l'établissement de la liste d'objets :
) array( // Premier filtre
)*/ 'filter' => 'cn=*a*',
//'basedn' => 'o=company',
'scope' => 'sub',
)
)*/
)
)
) )
) )
), ),

View file

@ -0,0 +1,10 @@
span.LSformElement_selectbox_sub_values_label {
font-style: Italic;
font-weight: bold;
font-size: 0.9em;
}
ul.LSformElement_selectbox_sub_values {
list-style-type: none;
padding-left: 1em;
}

View file

@ -60,7 +60,7 @@ class LSattr_html_select_list extends LSattr_html{
if ($element) { if ($element) {
// Mise en place de la regle de verification des donnees // Mise en place de la regle de verification des donnees
$form -> addRule($this -> name, 'inarray', array('msg'=> 'Valeur incorrect','params' => array('possible_values' => array_keys($possible_values))) ); $form -> addRule($this -> name, 'LSformElement_select_validValue', array('msg'=> _('Invalid value'),'params' => array('possible_values' => $possible_values)) );
} }
return $element; return $element;
} }
@ -76,61 +76,128 @@ class LSattr_html_select_list extends LSattr_html{
function getPossibleValues() { function getPossibleValues() {
$retInfos = array(); $retInfos = array();
if (is_array($this -> config['html_options']['possible_values'])) { if (is_array($this -> config['html_options']['possible_values'])) {
foreach($this -> config['html_options']['possible_values'] as $val_name => $val) { foreach($this -> config['html_options']['possible_values'] as $val_key => $val_label) {
if($val_name==='OTHER_OBJECT') { if($val_key==='OTHER_OBJECT') {
if ((!isset($val['object_type'])) || (!isset($val['value_attribute']))) { $objInfos=$this -> getLSobjectPossibleValues($val_label);
LSerror :: addErrorCode('LSattr_html_select_list_01',$this -> name); $retInfos=self :: _array_merge($retInfos,$objInfos);
break;
}
if (!LSsession :: loadLSclass('LSsearch')) {
return;
}
$param=array(
'filter' => (isset($val['filter'])?$val['filter']:null),
'basedn' => (isset($val['basedn'])?$val['basedn']:null),
'scope' => (isset($val['scope'])?$val['scope']:null),
'displayFormat' => (isset($val['display_name_format'])?$val['display_name_format']:null),
);
if ($val['value_attribute']!='dn') {
$param['attributes'][] = $val['value_attribute'];
}
$LSsearch = new LSsearch($val['object_type'],'LSattr_html_select_list',$param,true);
$LSsearch -> run();
if(($val['value_attribute']=='dn')||($val['value_attribute']=='%{dn}')) {
$retInfos = $LSsearch -> listObjectsName();
}
else {
$list = $LSsearch -> getSearchEntries();
foreach($list as $entry) {
$key = $entry -> get($val['value_attribute']);
if(is_array($key)) {
$key = $key[0];
}
$retInfos[$key]=$entry -> displayName;
}
}
} }
elseif (is_array($val_label)) {
if (!isset($val_label['possible_values']) || !is_array($val_label['possible_values']) || !isset($val_label['label']))
continue;
$subRetInfos=array();
foreach($val_label['possible_values'] as $vk => $vl) {
if ($vk==='OTHER_OBJECT') {
$objInfos=$this -> getLSobjectPossibleValues($vl);
$subRetInfos=self :: _array_merge($subRetInfos,$objInfos);
}
else {
$vk=$this->attribute->ldapObject->getFData($vk);
$vl=$this->attribute->ldapObject->getFData(__($vl));
$subRetInfos[$vk]=$vl;
}
}
$this -> _sort($subRetInfos);
$retInfos[] = array (
'label' => $this->attribute->ldapObject->getFData(__($val_label['label'])),
'possible_values' => $subRetInfos
);
}
else { else {
$val_name=$this->attribute->ldapObject->getFData($val_name); $val_key=$this->attribute->ldapObject->getFData($val_key);
$val=$this->attribute->ldapObject->getFData(__($val)); $val_label=$this->attribute->ldapObject->getFData(__($val_label));
$retInfos[$val_name]=$val; $retInfos[$val_key]=$val_label;
} }
} }
} }
if (!isset($this -> config['html_options']['sort']) || $this -> config['html_options']['sort']) { $this -> _sort($retInfos);
uasort($retInfos,array($this,'_sortTwoValues'));
}
return $retInfos; return $retInfos;
} }
/** /**
* Merge arrays preserving keys (string or numeric)
*
* As array_merge PHP function, this function merge arrays but
* this method permit to preverve key even if it's numeric key.
*
* @retval array Merged array
**/
private function _array_merge() {
$ret=array();
foreach(func_get_args() as $a) {
foreach($a as $k => $v) {
$ret[$k]=$v;
}
}
return $ret;
}
/**
* Apply sort feature on possible values if this feature is enabled
*
* @param[in] &$retInfos array Possible values array reference to sort
*
* @retval void
**/
private function _sort(&$retInfos) {
if (!isset($this -> config['html_options']['sort']) || $this -> config['html_options']['sort']) {
uasort($retInfos,array($this,'_sortTwoValues'));
}
}
/**
* Retourne un tableau des valeurs possibles d'un type d'objet
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @retval array Tableau associatif des valeurs possible de la liste avec en clé
* la valeur des balises option et en valeur ce qui sera affiché.
*/
private function getLSobjectPossibleValues($conf) {
$retInfos = array();
if ((!isset($conf['object_type'])) || (!isset($conf['value_attribute']))) {
LSerror :: addErrorCode('LSattr_html_select_list_01',$this -> name);
break;
}
if (!LSsession :: loadLSclass('LSsearch')) {
return;
}
$param=array(
'filter' => (isset($conf['filter'])?$conf['filter']:null),
'basedn' => (isset($conf['basedn'])?$conf['basedn']:null),
'scope' => (isset($conf['scope'])?$conf['scope']:null),
'displayFormat' => (isset($conf['display_name_format'])?$conf['display_name_format']:null),
);
if ($conf['value_attribute']!='dn') {
$param['attributes'][] = $conf['value_attribute'];
}
$LSsearch = new LSsearch($conf['object_type'],'LSattr_html_select_list',$param,true);
$LSsearch -> run();
if(($conf['value_attribute']=='dn')||($conf['value_attribute']=='%{dn}')) {
$retInfos = $LSsearch -> listObjectsName();
}
else {
$list = $LSsearch -> getSearchEntries();
foreach($list as $entry) {
$key = $entry -> get($conf['value_attribute']);
if(is_array($key)) {
$key = $key[0];
}
$retInfos[$key]=$entry -> displayName;
}
}
$this -> _sort($retInfos);
return $retInfos;
}
/**
* Function use with uasort to sort two values * Function use with uasort to sort two values
* *
* @param[in] $va string One value * @param[in] $va string One value
@ -145,8 +212,25 @@ class LSattr_html_select_list extends LSattr_html{
else { else {
$dir=1; $dir=1;
} }
if ($va == $vb) return 0;
$val = strcoll(strtolower($va), strtolower($vb)); if (is_array($va)) {
$nva=$va['label'];
}
else {
$nva=$va;
}
if (is_array($vb)) {
$nvb=$vb['label'];
}
else {
$nvb=$vb;
}
if ($nva == $nvb) return 0;
$val = strcoll(strtolower($nva), strtolower($nvb));
return $val*$dir; return $val*$dir;
} }
@ -158,4 +242,3 @@ class LSattr_html_select_list extends LSattr_html{
LSerror :: defineError('LSattr_html_select_list_01', LSerror :: defineError('LSattr_html_select_list_01',
_("LSattr_html_select_list : Configuration data are missing to generate the select list of the attribute %{attr}.") _("LSattr_html_select_list : Configuration data are missing to generate the select list of the attribute %{attr}.")
); );
?>

View file

@ -36,9 +36,9 @@ class LSformElement_select extends LSformElement {
var $fieldTemplate = 'LSformElement_select.tpl'; var $fieldTemplate = 'LSformElement_select.tpl';
/** /**
* Retourn les infos d'affichage de l'élément * Return display data of this element
* *
* Cette méthode retourne les informations d'affichage de l'élement * This method return display data of this element
* *
* @retval array * @retval array
*/ */
@ -60,6 +60,80 @@ class LSformElement_select extends LSformElement {
return $return; return $return;
} }
/**
* Check if a value is valid
*
* This method check if a value is correct, that mean if it's one
* of the possible values.
*
* @param[in] $value The value to check
* @param[in] $possible_values (Optional) The possible values
*
* @retval string or False The value's label or False if this value is incorrect
*/
public function isValidValue($value,$possible_values=False) {
if (!$possible_values)
$possible_values=$this -> params['text_possible_values'];
$ret=False;
if (is_array($possible_values) && isset($value)) {
foreach($possible_values as $key => $name) {
if (is_array($name)) {
if (!is_array($name['possible_values'])) continue;
foreach($name['possible_values'] as $k => $v) {
if ($k==$value) {
$ret=$v;
break;
}
}
if ($ret) break;
}
elseif ($key==$value) {
$ret=$name;
break;
}
if ($ret) break;
}
}
return $ret;
}
} }
?> /**
* LSformElement_select_checkIsValidValue template function
*
* This function permit to check during template processing
* if a value is valid. This function get as parameters
* (in $params) :
* - $value : the value to check
* - $possible_values : the possible values of the element
* As return, this function assign two template variables :
* - LSformElement_select_isValidValue :
* Boolean defining if the value is valid
* - LSformElement_select_isValidValue_label :
* The value's label
*
* @param[in] $params The template function parameters
* @param[in] $template Smarty object
*
* @retval void
**/
function LSformElement_select_checkIsValidValue($params,$template) {
extract($params);
$ret = LSformElement_select :: isValidValue($value,$possible_values);
if ($ret===False) {
$label='';
$ret=false;
}
else {
$label=$ret;
$ret=true;
}
$template -> assign('LSformElement_select_isValidValue',$ret);
$template -> assign('LSformElement_select_isValidValue_label',$label);
}
LStemplate :: registerFunction('LSformElement_select_checkIsValidValue','LSformElement_select_checkIsValidValue');

View file

@ -36,4 +36,18 @@ class LSformElement_select_box extends LSformElement_select {
var $template = 'LSformElement_select_box.tpl'; var $template = 'LSformElement_select_box.tpl';
var $fieldTemplate = 'LSformElement_select_box.tpl'; var $fieldTemplate = 'LSformElement_select_box.tpl';
/**
* Return display data of this element
*
* This method return display data of this element
*
* @retval array
*/
function getDisplay(){
if (!$this -> isFreeze()) {
LSsession :: addCssFile('LSformElement_select_box.css');
}
return parent :: getDisplay();
}
} }

View file

@ -0,0 +1,45 @@
<?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.
******************************************************************************/
/**
* Rule to validate LSformRule_LSformElement_select valid values
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_LSformElement_select_validValue extends LSformRule {
/**
* Validate value
*
* @param string $values The value to validate
* @param array $options Validation options
* @param object $formElement The related formElement object
*
* @return boolean true if the value is valide, false if not
*/
function validate($value,$option,$formElement) {
$ret = $formElement -> isValidValue($value);
if ($ret===False) return False;
return True;
}
}

File diff suppressed because it is too large Load diff

View file

@ -146,9 +146,18 @@ if (loadDir('../'.LS_OBJECTS_DIR) && loadDir('../'.LS_LOCAL_DIR.LS_OBJECTS_DIR))
add($attr['html_options']['mail']['msg']); add($attr['html_options']['mail']['msg']);
// LSattr_html_select_list // LSattr_html_select_list
if ($attr['html_type']=='select_list' && is_array($attr['html_options']['possible_values']) && !$withoutselectlist) { if (($attr['html_type']=='select_list' || $attr['html_type']=='select_box') && is_array($attr['html_options']['possible_values']) && !$withoutselectlist) {
foreach($attr['html_options']['possible_values'] as $pkey => $pname) { foreach($attr['html_options']['possible_values'] as $pkey => $pname) {
if ($pkey != 'OTHER_OBJECT') { if (is_array($pname)) {
add($pname['label']);
if (is_array($pname['possible_values'])) {
foreach($pname['possible_values'] as $pk => $pn) {
if ($pk == 'OTHER_OBJECT') continue;
add($pn);
}
}
}
elseif ($pkey != 'OTHER_OBJECT') {
add($pname); add($pname);
} }
} }

File diff suppressed because it is too large Load diff

View file

@ -1,8 +1,9 @@
<ul class='LSform' id='{$attr_name}'> <ul class='LSform' id='{$attr_name}'>
{if $freeze} {if $freeze}
{foreach from=$values item=value} {foreach from=$values item=value}
{if $possible_values.$value} {LSformElement_select_checkIsValidValue value=$value possible_values=$possible_values}
<li>{$possible_values.$value}</li> {if $LSformElement_select_isValidValue}
<li>{$LSformElement_select_isValidValue_label}</li>
{else} {else}
<li class='LSform-errors'>{getFData format=$unrecognized_value_label_format data=$value}</li> <li class='LSform-errors'>{getFData format=$unrecognized_value_label_format data=$value}</li>
{/if} {/if}
@ -12,7 +13,17 @@
{else} {else}
<li> <li>
<select name='{$attr_name}[]' {if $multiple}multiple{/if} class='LSformElement_select'> <select name='{$attr_name}[]' {if $multiple}multiple{/if} class='LSformElement_select'>
{html_options options=$possible_values selected=$values} {foreach from=$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=$values}
</optgroup>
{/if}
{else}
<option value="{$key}" {if in_array($key,$values)}selected{/if}>{$label}</option>
{/if}
{/foreach}
</select> </select>
</li> </li>
{/if} {/if}

View file

@ -1,8 +1,9 @@
<ul class='LSform' id='{$attr_name}'> <ul class='LSform' id='{$attr_name}'>
{if $freeze} {if $freeze}
{foreach from=$values item=value} {foreach from=$values item=value}
{if array_key_exists($value,$possible_values)} {LSformElement_select_checkIsValidValue value=$value possible_values=$possible_values}
<li>{$possible_values.$value}</li> {if $LSformElement_select_isValidValue}
<li>{$LSformElement_select_isValidValue_label}</li>
{else} {else}
<li class='LSform-errors'>{getFData format=$unrecognized_value_label_format data=$value}</li> <li class='LSform-errors'>{getFData format=$unrecognized_value_label_format data=$value}</li>
{/if} {/if}
@ -11,9 +12,24 @@
{/foreach} {/foreach}
{else} {else}
{foreach from=$possible_values item=label key=value name=LSformElement_selectbox} {foreach from=$possible_values item=label key=value name=LSformElement_selectbox}
<li> {if is_array($label)}
<input type='{if $multiple}checkbox{else}radio{/if}' name='{$attr_name}[]' class='LSformElement_selectbox' id='LSformElement_selectbox_{$attr_name}_{$smarty.foreach.LSformElement_selectbox.index}' value="{$value}" {if in_array($value,$values)}checked{/if}/> <label for='LSformElement_selectbox_{$attr_name}_{$smarty.foreach.LSformElement_selectbox.index}'>{tr msg=$label}</label> {if count($label.possible_values)>0}
</li> <li>
<span class='LSformElement_selectbox_sub_values_label'>{$label.label} :</span>
<ul class='LSformElement_selectbox_sub_values'>
{foreach from=$label.possible_values item=l key=v name=LSformElement_selectbox_sub_values}
<li>
<input type='{if $multiple}checkbox{else}radio{/if}' name='{$attr_name}[]' class='LSformElement_selectbox' id='LSformElement_selectbox_{$attr_name}_{$smarty.foreach.LSformElement_selectbox.index}_{$smarty.foreach.LSformElement_selectbox_sub_values.index}' value="{$v}" {if in_array($v,$values)}checked{/if}/> <label for='LSformElement_selectbox_{$attr_name}_{$smarty.foreach.LSformElement_selectbox.index}_{$smarty.foreach.LSformElement_selectbox_sub_values.index}'>{tr msg=$l}</label>
</li>
{/foreach}
</ul>
</li>
{/if}
{else}
<li>
<input type='{if $multiple}checkbox{else}radio{/if}' name='{$attr_name}[]' class='LSformElement_selectbox' id='LSformElement_selectbox_{$attr_name}_{$smarty.foreach.LSformElement_selectbox.index}' value="{$value}" {if in_array($value,$values)}checked{/if}/> <label for='LSformElement_selectbox_{$attr_name}_{$smarty.foreach.LSformElement_selectbox.index}'>{tr msg=$label}</label>
</li>
{/if}
{/foreach} {/foreach}
{/if} {/if}
</ul> </ul>