LSattr_html::date & LSformRule:📅 add special_values parameter

This commit is contained in:
Benjamin Renard 2020-09-09 18:40:44 +02:00
parent cccce6edb5
commit 7e74853021
12 changed files with 428 additions and 64 deletions

View file

@ -14,7 +14,11 @@
'manual' => '[Booleen pour l edition manuelle ou non]',
'showNowButton' => '[Booleen]',
'showTodayButton' => '[Booleen]',
'style' => '[Nom du style utilise]'
'style' => '[Nom du style utilise]',
'special_values' => array (
'[value]' => '[label]',
[...]
),
),]]>
...
</programlisting>
@ -212,6 +216,19 @@
</listitem>
</varlistentry>
<varlistentry>
<term>special_values</term>
<listitem>
<para>Tableau listant les valeurs spéciales que peut prendre l'attribut. Dans ce tableau
associatif, la clé doit correspondre à la valeur de l'attribut (telle que fournie par
<link linkend='config-LSattr_ldap_date'>l'attribut LDAP</link>) et la valeur associée au
label associé.</para>
<para>Ces valeurs spéciales seront proposées à l'utilisateur sous la forme de cases à cocher
dans le formulaire. Elles peuvent permettre par exemple de données une signification
particulière au zéro pour un attribut LDAP stockant un <emphasis>timestamp</emphasis>.</para>
</listitem>
</varlistentry>
</variablelist>

View file

@ -3,6 +3,17 @@
<para>Ce type est utilisé pour la gestion des attributs dont la valeur est
une date.</para>
<note>
<para>Au sein d'LdapSaisie, les dates manipulées au travers ce type
d'attribut LDAP, sont au format <emphasis>timestamp</emphasis>. Il s'agit
donc de nombres entiers correpondants au nombre de secondes depuis le 1 janvier
1970.</para>
<para>Le type d'attribut HTML utilisé conjointement avec ce type d'attribut LDAP
devra être prévu pour recevoir et fournir des dates au format <emphasis>timestamp
</emphasis>, comme c'est le cas pour le <link linkend='config-LSattr_html_date'>
type d'attribut HTML <emphasis>date</emphasis></link>.</para>
</note>
<programlisting linenumbering="unnumbered">
<citetitle>Structure</citetitle>...
<![CDATA['ldap_options' => array (

View file

@ -16,6 +16,15 @@
</listitem>
</varlistentry>
<varlistentry>
<term>special_values</term>
<listitem>
<para>Tableau listant les valeurs spéciales que peut prendre l'attribut. Dans ce tableau,
seules les valeurs sont utilisées et les clés n'ont pas d'importance. Ces valeurs spéciales
n'auront pas forcément besoin de respecter le format attendu.</para>
</listitem>
</varlistentry>
</variablelist>
</sect4>

View file

@ -144,8 +144,13 @@ $GLOBALS['LSobjects']['LSpeople'] = array (
'label' => 'Samba',
'args' => array (
'sambaSID',
'sambaLogonTime',
'sambaLogoffTime',
'sambaKickoffTime',
'sambaLMPassword',
'sambaNTPassword'
'sambaNTPassword',
'sambaPwdMustChange',
'sambaPwdCanChange',
)
)
), // fin Layout
@ -433,22 +438,6 @@ $GLOBALS['LSobjects']['LSpeople'] = array (
),
/* ----------- end -----------*/
/* ----------- start -----------*/
'sambaSID' => array (
'label' => 'Samba Identifier',
'ldap_type' => 'ascii',
'html_type' => 'text',
'required' => 1,
'generate_function' => 'generate_sambaUserSID',
'rights' => array(
'admin' => 'r'
),
'form' => array (
'modify' => 0
)
),
/* ----------- end -----------*/
/* ----------- start -----------*/
'homeDirectory' => array (
'label' => 'Home Directory',
@ -625,32 +614,6 @@ $GLOBALS['LSobjects']['LSpeople'] = array (
),
/* ----------- end -----------*/
/* ----------- start -----------*/
'sambaLMPassword' => array (
'label' => 'Samba Password (LM)',
'ldap_type' => 'ascii',
'html_type' => 'text',
'required' => 1,
'generate_function' => 'generate_sambaLMPassword',
'form' => array (
'modify' => 0
)
),
/* ----------- end -----------*/
/* ----------- start -----------*/
'sambaNTPassword' => array (
'label' => 'Samba Password (NT)',
'ldap_type' => 'ascii',
'html_type' => 'text',
'required' => 1,
'generate_function' => 'generate_sambaNTPassword',
'form' => array (
'modify' => 0
)
),
/* ----------- end -----------*/
/* ----------- start -----------*/
'jpegPhoto' => array (
'label' => 'Picture',
@ -717,5 +680,185 @@ $GLOBALS['LSobjects']['LSpeople'] = array (
),
/* ----------- end -----------*/
/************************************************
* Samba Attributres
************************************************/
/* ----------- start -----------*/
'sambaSID' => array (
'label' => 'Samba Identifier',
'ldap_type' => 'ascii',
'html_type' => 'text',
'required' => 1,
'generate_function' => 'generate_user_sambaSID',
'rights' => array(
'admin' => 'r'
),
'view' => 1,
'form' => array (
'modify' => 0
)
),
/* ----------- end -----------*/
/* ----------- start -----------*/
'sambaLogonTime' => array (
'label' => 'Samba last logon time',
'ldap_type' => 'date',
'ldap_options' => array(
'timestamp' => True,
),
'html_type' => 'date',
'html_options' => array(
'time' => True,
'showTodayButton' => False,
'special_values' => array(
'0' => 'Never',
),
),
'rights' => array(
'admin' => 'w'
),
'view' => 1,
'form' => array (
'modify' => 1
)
),
/* ----------- end -----------*/
/* ----------- start -----------*/
'sambaLogoffTime' => array (
'label' => 'Samba last logoff time',
'ldap_type' => 'date',
'ldap_options' => array(
'timestamp' => True,
),
'html_type' => 'date',
'html_options' => array(
'time' => True,
'showTodayButton' => False,
'special_values' => array(
'0' => 'Never',
),
),
'rights' => array(
'admin' => 'w'
),
'view' => 1,
'form' => array (
'modify' => 1
)
),
/* ----------- end -----------*/
/* ----------- start -----------*/
'sambaKickoffTime' => array (
'label' => 'Samba expiration time',
'help_info' => 'Specifies the time when the user will be locked down and cannot login any longer.',
'ldap_type' => 'date',
'ldap_options' => array(
'timestamp' => True,
),
'html_type' => 'date',
'html_options' => array(
'time' => True,
'showTodayButton' => False,
'special_values' => array(
LS_SAMBA_INFINITY_TIME => 'Never',
),
),
'no_value_label' => 'Never',
'rights' => array(
'admin' => 'w'
),
'view' => 1,
'form' => array (
'modify' => 1
)
),
/* ----------- end -----------*/
/* ----------- start -----------*/
'sambaLMPassword' => array (
'label' => 'Samba Password (LM)',
'ldap_type' => 'ascii',
'html_type' => 'text',
'required' => 1,
'generate_function' => 'generate_sambaLMPassword',
'form' => array (
'modify' => 0
)
),
/* ----------- end -----------*/
/* ----------- start -----------*/
'sambaNTPassword' => array (
'label' => 'Samba Password (NT)',
'ldap_type' => 'ascii',
'html_type' => 'text',
'required' => 1,
'generate_function' => 'generate_sambaNTPassword',
'form' => array (
'modify' => 0
)
),
/* ----------- end -----------*/
/* ----------- start -----------*/
'sambaPwdMustChange' => array (
'label' => 'Samba password must change',
'ldap_type' => 'date',
'ldap_options' => array(
'timestamp' => True,
),
'html_type' => 'date',
'html_options' => array(
'time' => True,
'showTodayButton' => False,
'special_values' => array(
'0' => 'At first login',
LS_SAMBA_INFINITY_TIME => 'Never',
),
),
'no_value_label' => 'Never',
'rights' => array(
'admin' => 'w'
),
'view' => 1,
'form' => array (
'modify' => 0
)
),
/* ----------- end -----------*/
/* ----------- start -----------*/
'sambaPwdCanChange' => array (
'label' => 'Samba password can change',
'help_info' => 'If not set, the user will be free to change his password whenever he wants.',
'ldap_type' => 'date',
'ldap_options' => array(
'timestamp' => True,
),
'html_type' => 'date',
'html_options' => array(
'time' => True,
'showTodayButton' => False,
'special_values' => array(
LS_SAMBA_INFINITY_TIME => 'Never',
0 => 'Whenever',
),
),
'no_value_label' => 'Whenever',
'multiple' => true,
'rights' => array(
'admin' => 'w'
),
'view' => 1,
'form' => array (
'modify' => 1
)
),
/* ----------- end -----------*/
) // Fin args
);

View file

@ -40,7 +40,17 @@ class LSattr_html_date extends LSattr_html {
*/
function addToForm (&$form,$idForm,$data=NULL) {
$element = parent::addToForm($form,$idForm,$data);
$form -> addRule($this -> name, 'date', array('msg' => _('Invalid value'), 'params' => array('format' => $element -> getFormat())) );
$form -> addRule(
$this -> name,
'date',
array(
'msg' => _('Invalid value'),
'params' => array(
'format' => $element -> getFormat(),
'special_values' => array_keys($element -> getSpecialValues()),
)
)
);
return $element;
}

View file

@ -77,17 +77,23 @@ class LSformElement_date extends LSformElement {
if (!is_array($data)) {
$data=array($data);
}
for($i=0;$i<count($data);$i++) {
if(is_numeric($data[$i])) {
$data[$i]=strftime($this -> getFormat(),$data[$i]);
$special_values = $this -> getSpecialValues();
$values = array();
foreach ($data as $value) {
if(is_numeric($value)) {
if (array_key_exists($value, $special_values)) {
$values[] = $special_values[$value];
continue;
}
$values[] = strftime($this -> getFormat(), $value);
}
else {
$this -> form -> setElementError($this -> attr_html);
return false;
}
}
$this -> values = $data;
self :: log_trace("$this -> setValue():".varDump($data)." => ".varDump($values));
$this -> values = $values;
return true;
}
@ -99,13 +105,22 @@ class LSformElement_date extends LSformElement {
public function exportValues(){
$retval=array();
if (is_array($this -> values)) {
$special_values = $this -> getSpecialValues();
foreach($this -> values as $val) {
if (array_key_exists($val, $special_values)) {
$retval[] = $val;
continue;
}
$date = strptime($val, $this -> getFormat());
if (is_array($date)) {
$retval[] = mktime($date['tm_hour'],$date['tm_min'],$date['tm_sec'],$date['tm_mon']+1,$date['tm_mday'],$date['tm_year']+1900);
}
else {
self :: log_warning("exportValues($val): Fail to parse value from form.");
}
}
}
self :: log_trace("$this -> exportValues():".varDump($this -> values)." => ".varDump($retval));
return $retval;
}
@ -175,10 +190,77 @@ class LSformElement_date extends LSformElement {
LStemplate :: addJSscript('LSformElement_date_field.js');
LStemplate :: addJSscript('LSformElement_date.js');
}
$return['html'] = $this -> fetchTemplate();
return $return;
}
/**
* Retournne un template Smarty compilé dans le contexte d'un LSformElement
*
* @param[in] string $template Le template à retourner
* @param[in] array $variables Variables Smarty à assigner avant l'affichage
*
* @retval string Le HTML compilé du template
*/
public function fetchTemplate($template=NULL, $variables=array()) {
$variables['special_values'] = $this -> getSpecialValues();
return parent :: fetchTemplate($template, $variables);
}
/**
* 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[in] &$return array Reference of the array for retreived values
* @param[in] $onlyIfPresent boolean If true and data of this element is not present in POST data,
* just ignore it.
*
* @retval boolean true si la valeur est présente en POST, false sinon
*/
public function getPostData(&$return, $onlyIfPresent=false) {
if($this -> isFreeze()) {
return true;
}
$values = self :: getData($_POST, $this -> name);
$special_values = self :: getData($_POST, $this -> name.'__special_value');
self :: log_trace($this." -> getPostData(): values=".varDump($values));
self :: log_trace($this." -> getPostData(): special_values=".varDump($special_values));
if (!is_array($values) && !is_array($special_values)) {
self :: log_trace($this." -> getPostData(): not in POST data");
if ($onlyIfPresent) {
self :: log_debug($this -> name.": not in POST data => ignore it");
}
else {
$return[$this -> name] = array();
}
}
else {
if(!is_array($values))
$values = array();
if(!is_array($special_values))
$special_values = array();
$return[$this -> name] = $special_values + $values;
self :: log_trace($this." -> merged values=".varDump($return[$this -> name]));
}
return true;
}
/**
* Retrieve list of special values with translated labels
*
* @return array Associative array with special values as keys and corresponding translated labels as values
*/
public function getSpecialValues() {
$special_values = array();
foreach ($this -> getParam('html_options.special_values', array()) as $value => $label)
$special_values[strval($value)] = __($label);
self :: log_trace("getSpecialValues(): ".varDump($special_values));
return $special_values;
}
/**
* Convertis un format de date Php (strftime) en JS (jscalendar)
*

View file

@ -33,11 +33,15 @@ class LSformRule_date extends LSformRule {
* @param mixed $value Données à valider
* @param array $options Options de validation
* $options['params']['format']: le format de la date
* $options['params']['special_values']: array of special allowed values
* @param object $formElement L'objet formElement attaché
*
* @return boolean True si les données sont valide, False sinon.
*/
public static function validate($value, $options=array(), &$formElement) {
$special_values = LSconfig :: get('params.special_values', array(), null, $options);
if (in_array($value, $special_values))
return true;
$format = LSconfig :: get('params.format', null, 'string', $options);
if (is_null($format)) {
LSerror :: addErrorCode('LSformRule_date_01');

View file

@ -636,6 +636,9 @@ function cli_generate_lang_file($command_args) {
case 'valueWithUnit':
addFromLSconfig("LSobjects.$obj.attrs.$attr.html_options.units.*");
break;
case 'date':
addFromLSconfig("LSobjects.$obj.attrs.$attr.html_options.special_values.*");
break;
}
}
}

View file

@ -11,15 +11,22 @@ var LSformElement_date = new Class({
if (!$type(el)) {
el = document;
}
var getName = /^(.*)\[\]$/
el.getElements('input.LSformElement_date').each(function(input) {
var getName = /^(.*)\[[0-9]*\]$/;
el.getElements('input.LSformElement_date[type=text]').each(function(input) {
var name = getName.exec(input.name)[1];
this.fields[name] = new LSformElement_date_field(name,input);
varLSform.addField(name, this.fields[name]);
}, this);
},
reinitialize: function(el) {
this.initialiseLSformElement_date(el);
},
clearValue: function() {
this.fields.each(function(field) {
field.clearValue();
}, this);
}
});
window.addEvent(window.ie ? 'load' : 'domready', function() {

View file

@ -2,6 +2,12 @@ var LSformElement_date_field = new Class({
initialize: function(name,input){
this.name = name;
this.input = input;
this.specialValueInputs = $$(input.getAllNext('input.LSformElement_date[type=radio]'));
this.input.addEvent('change', this.onInputChange.bind(this));
this.specialValueInputs.each(function(input) {
input.addEvent('click', this.onSpecialValueInputClick.bind(this));
}, this);
this.params = varLSdefault.LSjsConfig[this.name];
if (!$type(this.params)) {
@ -41,6 +47,7 @@ var LSformElement_date_field = new Class({
useFadeInOut: !Browser.ie
}
);
this.calendar.addEvent('onSelect', this.onInputChange.bind(this));
if (this.params.showNowButton) {
this.nowBtn = new Element('img');
@ -66,8 +73,21 @@ var LSformElement_date_field = new Class({
}
},
onInputChange: function() {
if (!this.input.value)
return true;
this.specialValueInputs.each(function(input) {
input.removeProperty('checked');
}, this);
},
onSpecialValueInputClick: function() {
this.input.value="";
},
onNowBtnClick: function() {
this.input.value = new Date().format(this.params.format);
this.input.fireEvent('change');
},
onTodayBtnClick: function() {
@ -85,7 +105,15 @@ var LSformElement_date_field = new Class({
date: now.get('date')
});
this.input.value = today.format(this.params.format);
this.input.fireEvent('change');
}
}
},
clearValue: function() {
this.input.value="";
this.specialValueInputs.each(function(input) {
input.removeProperty('checked');
}, this);
}
});

View file

@ -16,6 +16,10 @@ $GLOBALS['LSlang'] = array (
"Allow user to connect a POSIX system." =>
"Autorisie l'utilisateur à se connecter aux systèmes POSIX.",
# LSobjects.LSpeople.attrs.sambaPwdMustChange.html_options.special_values.0
"At first login" =>
"À la première connexion",
# LSobjects.LSpeople.LSrelation.groups.label
# LSobjects.LSsysaccount.LSrelation.groups.label
"Belongs to groups ..." =>
@ -111,6 +115,10 @@ $GLOBALS['LSlang'] = array (
"Identifier must contain alphanumeric values, dots (.) and dashes (-) only." =>
"L'identifiant doit contenir uniquement des valeurs alpha-numériques, avec eventuellement des points (.) et des tirets (-).",
# LSobjects.LSpeople.attrs.sambaPwdCanChange.help_info
"If not set, the user will be free to change his password whenever he wants." =>
"Si non défini, l'utilisateur pourra changer son mot de passe quand il le veut.",
# LSobjects.LSpeople.attrs.gidNumber.html_options.possible_values.1.label
"LDAP Groups" =>
"Groupes LDAP",
@ -175,6 +183,16 @@ $GLOBALS['LSlang'] = array (
"Nb / page :" =>
"Nb / page :",
# LSobjects.LSpeople.attrs.sambaLogonTime.html_options.special_values.0
# LSobjects.LSpeople.attrs.sambaLogoffTime.html_options.special_values.0
# LSobjects.LSpeople.attrs.sambaKickoffTime.no_value_label
# LSobjects.LSpeople.attrs.sambaKickoffTime.html_options.special_values.2147483647
# LSobjects.LSpeople.attrs.sambaPwdMustChange.no_value_label
# LSobjects.LSpeople.attrs.sambaPwdMustChange.html_options.special_values.2147483647
# LSobjects.LSpeople.attrs.sambaPwdCanChange.html_options.special_values.2147483647
"Never" =>
"Jamais",
# LSobjects.LSpeople.attrs.uidNumber.label
"Numeric identifier" =>
"Identifiant numérique",
@ -220,10 +238,32 @@ $GLOBALS['LSlang'] = array (
"Samba Password (NT)" =>
"Mot de passe Samba (NT)",
# LSobjects.LSpeople.attrs.sambaKickoffTime.label
"Samba expiration time" =>
"Date d'expiration Samba",
# LSobjects.LSpeople.attrs.sambaLogoffTime.label
"Samba last logoff time" =>
"Date de dernière déconnexion Samba",
# LSobjects.LSpeople.attrs.sambaLogonTime.label
"Samba last logon time" =>
"Date de dernière connexion Samba",
# LSobjects.LSpeople.attrs.sambaPwdCanChange.label
"Samba password can change" =>
"Date de prochaine modification possible du mot de passe Samba",
# LSobjects.LSpeople.attrs.sambaPwdMustChange.label
"Samba password must change" =>
"Date de prochaine modification obligatoire du mot de passe Samba",
# LSobjects.LSpeople.LSform.dataEntryForm.simple.label
"Simple" =>
"Simple",
# LSobjects.LSpeople.attrs.sambaKickoffTime.help_info
"Specifies the time when the user will be locked down and cannot login any longer." =>
"Détermine la date à partir de laquelle l'utilisateur sera bloqué et ne pourra plus se connecter.",
# LSobjects.LSsysaccount.label
"System accounts" =>
"Comptes systèmes",
@ -264,6 +304,11 @@ $GLOBALS['LSlang'] = array (
"Users" =>
"Utilisateurs",
# LSobjects.LSpeople.attrs.sambaPwdCanChange.no_value_label
# LSobjects.LSpeople.attrs.sambaPwdCanChange.html_options.special_values.0
"Whenever" =>
"N'importe quand",
# LSobjects.LSpeople.LSsearch.predefinedFilters.(jpegPhoto=*)
"With photo" =>
"Avec photo",

View file

@ -1,5 +1,10 @@
{if $freeze}
{if $value}{$value|escape:"htmlall"}{else}{$noValueTxt|escape:"htmlall"}{/if}
{else}
<input type='text' name='{$attr_name|escape:"htmlall"}[]' value='{$value|escape:'htmlall'}' class='LSformElement_date' autocomplete="off">
<input type='text' name='{$attr_name|escape:"htmlall"}[{$value_idx}]' value='{if !in_array($value, $special_values)}{$value|escape:'htmlall'}{/if}' class='LSformElement_date' autocomplete="off">
{foreach from=$special_values item=special_value_label key=special_value name=LSformElement_date_special_value}
<input type='radio' name='{$attr_name|escape:"htmlall"}__special_value[{$value_idx}]' class='LSformElement_date' value='{$special_value|escape:"htmlall"}'
id='LSformElement_date_special_value_{$attr_name|escape:"htmlall"}_{$value_idx}_{$smarty.foreach.LSformElement_date_special_value.index}' {if $value == $special_value_label}checked{/if}/>
<label for='LSformElement_date_special_value_{$attr_name|escape:"htmlall"}_{$value_idx}_{$smarty.foreach.LSformElement_date_special_value.index}'>{$special_value_label|escape:"htmlall"}</label>
{/foreach}
{/if}