LSformElement_supannCompositeAttribute: handle API/CLI mode

This commit is contained in:
Benjamin Renard 2021-06-11 16:24:13 +02:00
parent 7c45c4bb8c
commit c533aecc34

View file

@ -329,42 +329,89 @@ class LSformElement_supannCompositeAttribute extends LSformElement {
* @retval boolean true si la valeur est présente en POST, false sinon * @retval boolean true si la valeur est présente en POST, false sinon
*/ */
public function getPostData(&$return, $onlyIfPresent=false) { public function getPostData(&$return, $onlyIfPresent=false) {
if ($onlyIfPresent || $this -> form -> api_mode) {
self :: log_warning("getPostData : does not support \$onlyIfPresent / API mode => Post data ignored");
return true;
}
if($this -> isFreeze()) { if($this -> isFreeze()) {
return true; return true;
} }
$count = 0; // Extract value form POST data and store it in $parseValues
$end = false;
$parseValues = array(); $parseValues = array();
$return[$this -> name] = array(); if ($this -> form -> api_mode) {
while ($end==false) { // API mode
$value = array(); $form_values = $this -> getData($_POST, $this -> name);
if (!is_array($form_values) || empty($form_values)) {
self :: log_trace($this." -> getPostData(): not in POST data");
return true;
}
foreach($form_values as $idx => $form_value) {
// Handle string value (for value provided by CLI for instance) and already decomposed value
$input_value = (is_string($form_value)?$this -> parseCompositeValue($form_value):$form_value);
if (!is_array($input_value)) {
$this -> form -> setElementError(
$this -> attr_html,
getFData(_('Fail to decode composite value #%{idx}.'), $idx)
);
continue;
}
$parseValue = array();
$unemptyComponents = array();
foreach (array_keys($this -> components) as $c) {
if (!isset($input_value[$c]) || is_empty($input_value))
continue;
$parseValue[$c] = $input_value[$c];
$unemptyComponents[] = $c;
}
// Ignore empty value from form
if (empty($unemptyComponents))
continue;
$parseValues[] = $parseValue;
}
}
else {
// HTML Form
$end = false;
$count = 0;
while (!$end) {
$parseValue = array(); $parseValue = array();
$errors = array(); $errors = array();
$unemptyComponents = array();
foreach ($this -> components as $c => $cconf) { foreach ($this -> components as $c => $cconf) {
if (isset($_POST[$this -> name.'__'.$c][$count])) { if (!isset($_POST[$this -> name.'__'.$c][$count])) {
// end of value break
$end = true;
break;
}
if (is_empty($_POST[$this -> name.'__'.$c][$count])) {
continue;
}
$parseValue[$c] = $_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])) { $count++;
// Ignore empty value from form
if (empty($parseValue))
continue; continue;
$parseValues[] = $parseValue;
} }
$unemptyComponents[] = $c; }
// Handle POST component value (by type) self :: log_debug($this." -> getPostData(): POST data = ".varDump($parseValues));
// Check extracted values
$errors = array();
foreach ($parseValues as $parseValue) {
// Check component value
foreach ($parseValue as $c => $value) {
$cconf = $this -> components[$c];
switch ($cconf['type']) { switch ($cconf['type']) {
case 'table': case 'table':
$pv = supannParseLabeledValue($parseValue[$c]); $pv = supannParseLabeledValue($value);
self :: log_debug("supannParseLabeledValue(".$parseValue[$c].") == ".varDump($pv)); self :: log_debug("supannParseLabeledValue($value) == ".varDump($pv));
if ($pv) { if ($pv) {
if (!supannValidateNomenclatureValue($cconf['table'],$pv['label'],$pv['value'])) { if (!supannValidateNomenclatureValue($cconf['table'], $pv['label'], $pv['value'])) {
$errors[] = getFData(__('Invalid value for component %{c}.'), __($cconf['label'])); $errors[] = getFData(__('Invalid value for component %{c}.'), __($cconf['label']));
} }
} }
@ -374,16 +421,25 @@ class LSformElement_supannCompositeAttribute extends LSformElement {
break; break;
case 'select': case 'select':
if (!array_key_exists($parseValue[$c], $cconf['possible_values'])) { if (!array_key_exists($value, $cconf['possible_values'])) {
$errors[] = getFData(__('Invalid value for component %{c}.'), __($cconf['label'])); $errors[] = getFData(__('Invalid value for component %{c}.'), __($cconf['label']));
} }
break; break;
case 'date': case 'date':
case 'datetime': case 'datetime':
$datetime = date_create_from_format($cconf['php_format'], $parseValue[$c]); if ($this -> form -> api_mode) {
$datetime = ldapDate2DateTime(
$value,
$this -> components[$c]['naive'],
$this -> components[$c]['ldap_format']
);
}
else {
$datetime = date_create_from_format($cconf['php_format'], $value);
}
if ($datetime) { if ($datetime) {
$parseValue[$c] = dateTime2LdapDate( $parseValue[$c] = $value = dateTime2LdapDate(
$datetime, $datetime,
$this -> components[$c]['timezone'], $this -> components[$c]['timezone'],
$this -> components[$c]['ldap_format'] $this -> components[$c]['ldap_format']
@ -395,13 +451,13 @@ class LSformElement_supannCompositeAttribute extends LSformElement {
break; break;
case 'codeEntite': case 'codeEntite':
if (!supannValidateEntityId($parseValue[$c])) { if (!supannValidateEntityId($value)) {
$errors[] = getFData(__('Invalid value for component %{c}.'), __($cconf['label'])); $errors[] = getFData(__('Invalid value for component %{c}.'), __($cconf['label']));
} }
break; break;
case 'parrainDN': case 'parrainDN':
if (!supannValidateParrainDN($parseValue[$c])) { if (!supannValidateParrainDN($value)) {
$errors[] = getFData(__('Invalid value for component %{c}.'), __($cconf['label'])); $errors[] = getFData(__('Invalid value for component %{c}.'), __($cconf['label']));
} }
break; break;
@ -410,47 +466,37 @@ class LSformElement_supannCompositeAttribute extends LSformElement {
// Check component value (if configured) // Check component value (if configured)
if (isset($cconf['check_data']) && is_array($cconf['check_data'])) { if (isset($cconf['check_data']) && is_array($cconf['check_data'])) {
foreach($cconf['check_data'] as $ruleType => $rconf) { foreach($cconf['check_data'] as $ruleType => $rconf) {
$className='LSformRule_'.$ruleType; $className = 'LSformRule_'.$ruleType;
if (LSsession::loadLSclass($className)) { if (!LSsession::loadLSclass($className)) {
$r=new $className(); $errors[] = getFData(__("Can't validate value of component %{c}."),__($cconf['label']));
if (!$r -> validate($parseValue[$c],$rconf,$this)) { continue;
if (isset($rconf['msg'])) {
$errors[]=getFData(__($rconf['msg']),__($cconf['label']));
} }
else { $r = new $className();
$errors[]=getFData(__('Invalid value for component %{c}.'),__($cconf['label'])); if (!$r -> validate($value, $rconf, $this)) {
$errors[] = getFData(
__(LSconfig :: get('msg', 'Invalid value for component %{c}.', 'string', $rconf)),
__($cconf['label'])
);
} }
} }
} }
else {
$errors[]=getFData(__("Can't validate value of component %{c}."),__($cconf['label']));
}
}
} }
$value[$c] = $parseValue[$c]; // Check required component is defined
} foreach($this -> components as $c => $cconf) {
else { if (!LSconfig :: get('required', false, 'bool', $cconf))
// end of value break continue;
$end=true; if (isset($parseValue[$c]) && !is_empty($parseValue[$c]))
break; continue;
} $errors[] = getFData(__('Component %{c} must be defined'), __($cconf['label']));
} }
// Format value // Format value and add to return
$value = $this -> formatCompositeValue($value); $return[$this -> name][] = $this -> formatCompositeValue($parseValue);
}
if (!$end) { foreach($errors as $e)
if (!empty($unemptyComponents)) { $this -> form -> setElementError($this -> attr_html, $e);
foreach($errors as $e) {
$this -> form -> setElementError($this -> attr_html,$e);
}
$return[$this -> name][] = $value;
$parseValues[] = $parseValue;
}
$count++;
}
}
$this -> _postParsedData = $parseValues; $this -> _postParsedData = $parseValues;
return true; return true;
} }
@ -510,4 +556,39 @@ class LSformElement_supannCompositeAttribute extends LSformElement {
return $retval; return $retval;
} }
/**
* Retreive value as return in API response
*
* @param[in] $details boolean If true, returned values will contain details if this field type
* support it (optional, default: false)
*
* @retval mixed API value(s) or null/empty array if no value
*/
public function getApiValue($details=false) {
$values = array();
foreach(ensureIsArray($this -> values) as $value) {
$decodedValue = $this -> parseCompositeValue($value, true);
if (is_array($decodedValue)) {
$parsedValue = array();
foreach(array_keys($this -> components) as $c) {
if (!isset($decodedValue[$c]))
continue;
$parsedValue[$c] = (
$details?
$this -> translateComponentValue($c, $decodedValue[$c]):
$decodedValue[$c]
);
}
$values[] = $parsedValue;
}
}
if ($this -> isMultiple()) {
return $values;
}
if (!$values)
return null;
return $values[0];
}
} }