LSattr_html::select_list: add get_possible_values parameter

This commit is contained in:
Benjamin Renard 2020-09-22 15:04:31 +02:00
parent 8f407e9345
commit de62999fea
5 changed files with 178 additions and 36 deletions

View file

@ -46,6 +46,7 @@
)
)
),
'get_possible_values' => [callable],
'translate_labels' => [booléen],
'sort' => [Booléen],
'sortDirection' => '[ASC|DESC]'
@ -178,6 +179,56 @@
</listitem>
</varlistentry>
<varlistentry>
<term>get_possible_values</term>
<listitem>
<para>Paramètre permettant de spécifier un <emphasis>callable</emphasis> qui sera utilisé
pour lister les valeurs possibles de l'attribut. Il recevra en paramètres les informations
suivantes:
<variablelist>
<varlistentry>
<term>$options</term>
<listitem>
<simpara>Les options HTML de l'attribut.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>$name</term>
<listitem>
<simpara>Le nom de l'attribut.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>&amp;$ldapObject</term>
<listitem>
<simpara>Une référence à l'objet <literal>LSldapObject</literal>.</simpara>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>La valeur de retour attendue est un tableau associatif des valeurs possibles
de l'attribut avec la valeur que prendra l'attribut en tant que clé et le label
correspondant en tant que valeur. Tout autre retour sera considéré comme un échec
et déclenchera une erreur.</para>
<para>Il est également possible de regrouper des valeurs possibles de l'attribut: pour
cela, le tableau retourné devra lui-même contenir un tableau associatif contenant la
label traduit du groupe sous la clé <literal>label</literal> et les valeurs possibles
du groupe sous la clé <literal>possible_values</literal>.</para>
<para>Les valeurs retournées pourront être combinées avec les autres valeurs possibles
configurées de l'attribut. La prise en charge du tri des valeurs possibles est assurée
par la fonction appelante sauf dans le cas des sous-groupes de valeurs possibles. Dans
ce cas, la méthode <literal>LSattr_html_select_list :: _sort()</literal> pourra être
utilisée pour trier les valeurs du sous-groupe: cette méthode accepte en paramètre une
référence du tableau des valeurs possibles ainsi que les options HTML de l'attribut.
</para>
<para>Si la traduction des labels des valeurs possibles de l'attribut est activées
(voir ci-dessous), celle-ci doit être prise en charge par la fonction configurée.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>translate_labels</term>
<listitem>

View file

@ -84,7 +84,7 @@ class LSattr_html_select_list extends LSattr_html{
/**
* Return array of possible values with translated labels (if enabled)
*
* @param[in] $options Attribute options (optional)
* @param[in] $options Attribute HTML options (optional)
* @param[in] $name Attribute name (optional)
* @param[in] &$ldapObject Related LSldapObject (optional)
*
@ -93,10 +93,15 @@ class LSattr_html_select_list extends LSattr_html{
* @retval array Associative array with possible values as key and corresponding
* translated label as value.
*/
public static function _getPossibleValues($options=false,$name=false,&$ldapObject=false) {
$retInfos = array();
$translate_labels = LSconfig :: get('translate_labels', true, 'bool', $options);
if (isset($options['possible_values']) && is_array($options['possible_values'])) {
public static function _getPossibleValues($options=false, $name=false, &$ldapObject=false) {
// Handle get_possible_values parameter
$retInfos = self :: getCallablePossibleValues($options, $name, $ldapObject);
if (!is_array($retInfos))
$retInfos = array();
// Handle other configured possible values
if (is_array($options) && isset($options['possible_values']) && is_array($options['possible_values'])) {
$translate_labels = LSconfig :: get('translate_labels', true, 'bool', $options);
foreach($options['possible_values'] as $val_key => $val_label) {
if($val_key==='OTHER_OBJECT') {
$objInfos=static :: getLSobjectPossibleValues($val_label,$options,$name);
@ -136,7 +141,7 @@ class LSattr_html_select_list extends LSattr_html{
}
}
static :: _sort($retInfos,$options);
static :: _sort($retInfos, $options);
return $retInfos;
}
@ -163,11 +168,11 @@ class LSattr_html_select_list extends LSattr_html{
* Apply sort feature on possible values if this feature is enabled
*
* @param[in] &$retInfos array Possible values array reference to sort
* @param[in] $options array|false Attribute options
* @param[in] $options array|false Attribute HTML options
*
* @retval void
**/
protected static function _sort(&$retInfos, $options) {
public static function _sort(&$retInfos, $options) {
if (!isset($options['sort']) || $options['sort']) {
if (isset($options['sortDirection']) && $options['sortDirection']=='DESC') {
uasort($retInfos,array('LSattr_html_select_list','_sortTwoValuesDesc'));
@ -223,7 +228,7 @@ class LSattr_html_select_list extends LSattr_html{
* Retourne un tableau des valeurs possibles d'un type d'objet
*
* @param[in] $conf OTHER_OBJECT configuration array
* @param[in] $options array|false Attribute options
* @param[in] $options array|false Attribute HTML options
* @param[in] $name Attribute name
*
* @author Benjamin Renard <brenard@easter-eggs.com>
@ -294,7 +299,7 @@ class LSattr_html_select_list extends LSattr_html{
* Retourne un tableau des valeurs possibles d'un autre attribut
*
* @param[in] $attr OTHER_ATTRIBUTE configuration value
* @param[in] $options array|false Attribute options
* @param[in] $options array|false Attribute HTML options
* @param[in] $name Attribute name
* @param[in] $LSldapObject LSldapObject reference
*
@ -379,23 +384,81 @@ class LSattr_html_select_list extends LSattr_html{
return $retInfos;
}
/**
* Return array of possible values with translated labels (if enabled)
* by using specify callable
*
* @param[in] $options Attribute HTML options
* @param[in] $name Attribute name
* @param[in] &$ldapObject Related LSldapObject
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @retval array|false Associative array with possible values as key and corresponding
* translated label as value, or false in case of error.
*/
public static function getCallablePossibleValues($options, $name, &$ldapObject) {
// Handle get_possible_values parameter
$get_possible_values = LSconfig :: get('get_possible_values', null, null, $options);
if (!$get_possible_values)
return array();
// Check callable
if (!is_callable($get_possible_values)) {
LSerror :: addErrorCode('LSattr_html_select_list_06', $name);
return false;
}
// Run callable
try {
$retInfos = call_user_func_array(
$get_possible_values,
array($options, $name, $ldapObject)
);
}
catch (Exception $er) {
self :: log_exception($er, strval($this)." -> _getPossibleValues(): exception occured running ".format_callable($get_possible_values));
$retInfos = null;
}
// Check result
if (!is_array($retInfos)) {
LSerror :: addErrorCode(
'LSattr_html_select_list_07',
array(
'attr' => $name,
'callable' => format_callable($get_possible_values)
)
);
return false;
}
return $retInfos;
}
}
/*
* Error Codes
*/
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}.")
);
LSerror :: defineError('LSattr_html_select_list_02',
___("LSattr_html_select_list : Invalid attribute %{attr} reference as OTHER_ATTRIBUTE possible values.")
___("LSattr_html_select_list: Invalid attribute %{attr} reference as OTHER_ATTRIBUTE possible values.")
);
LSerror :: defineError('LSattr_html_select_list_03',
___("LSattr_html_select_list : Attribute %{attr} referenced as OTHER_ATTRIBUTE possible values is not a jsonCompositeAttribute.")
___("LSattr_html_select_list: Attribute %{attr} referenced as OTHER_ATTRIBUTE possible values is not a jsonCompositeAttribute.")
);
LSerror :: defineError('LSattr_html_select_list_04',
___("LSattr_html_select_list : Fail to decode the following attribute %{attr} value as JSON : %{value}")
___("LSattr_html_select_list: Fail to decode the following attribute %{attr} value as JSON : %{value}")
);
LSerror :: defineError('LSattr_html_select_list_05',
___("LSattr_html_select_list : No component %{component} found in the following attribute %{attr} JSON value : %{value}")
___("LSattr_html_select_list: No component %{component} found in the following attribute %{attr} JSON value : %{value}")
);
LSerror :: defineError('LSattr_html_select_list_06',
___("LSattr_html_select_list: Invalid get_possible_values parameter found in configuration of attribute %{attr}: must be a callable.")
);
LSerror :: defineError('LSattr_html_select_list_07',
___("LSattr_html_select_list: fail to retreive possible values of attribute %{attr} using configured function %{callable}.")
);

View file

@ -8,7 +8,7 @@ msgstr ""
"Project-Id-Version: LdapSaisie\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: \n"
"PO-Revision-Date: 2020-09-21 15:46+0200\n"
"PO-Revision-Date: 2020-09-22 14:32+0200\n"
"Last-Translator: Benjamin Renard <brenard@zionetrix.net>\n"
"Language-Team: LdapSaisie <ldapsaisie-users@lists.labs.libre-entreprise."
"org>\n"
@ -2083,46 +2083,62 @@ msgstr "Discuter avec cette personne."
msgid "%s (Unparsable value)"
msgstr "%s (valeur non-analysable)"
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:388
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:445
msgid ""
"LSattr_html_select_list : Configuration data are missing to generate the "
"LSattr_html_select_list: Configuration data are missing to generate the "
"select list of the attribute %{attr}."
msgstr ""
"LSattr_html_select_list : Des données de configuration sont manquantes pour "
"générer la liste de sélection de l'attribut %{attr}."
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:391
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:448
msgid ""
"LSattr_html_select_list : Invalid attribute %{attr} reference as "
"LSattr_html_select_list: Invalid attribute %{attr} reference as "
"OTHER_ATTRIBUTE possible values."
msgstr ""
"LSattr_html_select_list : Référence invalide à l'attribut %{attr} comme "
"valeurs possibles (OTHER_ATTRIBUTE)."
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:394
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:451
msgid ""
"LSattr_html_select_list : Attribute %{attr} referenced as OTHER_ATTRIBUTE "
"LSattr_html_select_list: Attribute %{attr} referenced as OTHER_ATTRIBUTE "
"possible values is not a jsonCompositeAttribute."
msgstr ""
"LSattr_html_select_list : L'attribute %{attr} référencé comme valeurs "
"possibles (OTHER_ATTRIBUTE) n'est pas du type jsonCompositeAttribute."
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:397
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:454
msgid ""
"LSattr_html_select_list : Fail to decode the following attribute %{attr} "
"LSattr_html_select_list: Fail to decode the following attribute %{attr} "
"value as JSON : %{value}"
msgstr ""
"LSattr_html_select_list : Impossible de décodé la valeur JSON suivante de "
"l'attribut %{attr} : %{value}"
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:400
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:457
msgid ""
"LSattr_html_select_list : No component %{component} found in the following "
"LSattr_html_select_list: No component %{component} found in the following "
"attribute %{attr} JSON value : %{value}"
msgstr ""
"LSattr_html_select_list : Le composant %{component} n'a pas été trouvé dans "
"la valeur JSON de l'attribut %{attr} : %{value}"
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:460
msgid ""
"LSattr_html_select_list: Invalid get_possible_values parameter found in "
"configuration of attribute %{attr}: must be a callable."
msgstr ""
"LSattr_html_select_list : Paramètre get_possible_values invalide trouvé dans "
"la configuration de l'attribut %{attr} : cela doit être un callable."
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:463
msgid ""
"LSattr_html_select_list: fail to retreive possible values of attribute "
"%{attr} using configured function %{callable}."
msgstr ""
"LSattr_html_select_list : Impossible de récupérer les valeurs possibles de "
"l'attribut %{attr} en utilisant la fonction configurée %{callable}."
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSformRule_inarray.php:57
msgid ""
"LSformRule_inarray : Possible values has not been configured to validate "

View file

@ -1765,36 +1765,48 @@ msgstr ""
msgid "%s (Unparsable value)"
msgstr ""
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:388
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:445
msgid ""
"LSattr_html_select_list : Configuration data are missing to generate the "
"LSattr_html_select_list: Configuration data are missing to generate the "
"select list of the attribute %{attr}."
msgstr ""
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:391
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:448
msgid ""
"LSattr_html_select_list : Invalid attribute %{attr} reference as "
"LSattr_html_select_list: Invalid attribute %{attr} reference as "
"OTHER_ATTRIBUTE possible values."
msgstr ""
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:394
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:451
msgid ""
"LSattr_html_select_list : Attribute %{attr} referenced as OTHER_ATTRIBUTE "
"LSattr_html_select_list: Attribute %{attr} referenced as OTHER_ATTRIBUTE "
"possible values is not a jsonCompositeAttribute."
msgstr ""
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:397
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:454
msgid ""
"LSattr_html_select_list : Fail to decode the following attribute %{attr} "
"LSattr_html_select_list: Fail to decode the following attribute %{attr} "
"value as JSON : %{value}"
msgstr ""
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:400
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:457
msgid ""
"LSattr_html_select_list : No component %{component} found in the following "
"LSattr_html_select_list: No component %{component} found in the following "
"attribute %{attr} JSON value : %{value}"
msgstr ""
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:460
msgid ""
"LSattr_html_select_list: Invalid get_possible_values parameter found in "
"configuration of attribute %{attr}: must be a callable."
msgstr ""
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSattr_html_select_list.php:463
msgid ""
"LSattr_html_select_list: fail to retreive possible values of attribute "
"%{attr} using configured function %{callable}."
msgstr ""
#: /home/brenard/dev/ldapsaisie_clean3/src/includes/class/class.LSformRule_inarray.php:57
msgid ""
"LSformRule_inarray : Possible values has not been configured to validate "