LSformElement::getApiValue(): add $details parameter

This parameter permit to control if the returned value contain or not 
details. By default (no details), the value used the format as accept by 
create/modify method. The details mode is more efficient to display the 
value.
This commit is contained in:
Benjamin Renard 2021-02-05 12:22:32 +01:00
parent 36a1719b26
commit 19dd343106
12 changed files with 189 additions and 132 deletions

View file

@ -248,14 +248,19 @@
<listitem>
<para>Cette méthode permet de récupérer les informations d'un objet de l'annuaire au format
JSON. Le type de l'objet et son DN sont précisés dans l'URL et doivent être encodés en
conséquence.
conséquence. Par défaut, les valeurs des attributs retournées sont au format tel qu'attendu
en cas de création/modification de l'objet. Il est cependant possible d'ajouter le paramètre
<literal>details</literal> afin d'obtenir des informations complémentaires sur les valeurs
des attributs.
<programlisting linenumbering="unnumbered">
<citetitle>Exemple</citetitle>
<![CDATA[# curl -u username:secret 'https://ldapsaisie/api/1.0/object/LSpeople/uid=hmartin,ou=people,o=ls?pretty'
{
"success": true,
"dn": "uid=hmartin,ou=people,o=ls",
"type": "LSpeople",
"name": "Henri MARTIN",
"details": false,
"attributes": {
"uid": "hmartin",
"givenName": "Henri",
@ -265,12 +270,9 @@
"personalTitle": "M.",
"description": [],
"jpegPhoto": null,
"lsGodfatherDn": {
"uid=eeggs,ou=people,o=ls": {
"name": "Easter Eggs",
"object_type": "LSpeople"
}
},
"lsGodfatherDn": [
"uid=eeggs,ou=people,o=ls"
],
"uidNumber": "101022",
"gidNumber": "102001",
"loginShell": "no",

View file

@ -406,15 +406,28 @@ class LSformElement extends LSlog_staticLoggerClass {
/**
* 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() {
if ($this -> isMultiple()) {
return ensureIsArray($this -> values);
public function getApiValue($details=false) {
if (method_exists($this, 'parseValue')) {
$values = array();
foreach(ensureIsArray($this -> values) as $value) {
$parsed_value = $this -> parseValue($value, $details);
if ($parsed_value != false)
$values[] = $parsed_value;
}
}
if (!$this -> values)
else {
$values = ensureIsArray($this -> values);
}
if ($this -> isMultiple())
return $values;
if (!$values)
return null;
return $this -> values[0];
return $values[0];
}
}

View file

@ -167,9 +167,12 @@ class LSformElement_image extends LSformElement {
/**
* 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() {
public function getApiValue($details=false) {
if ($this -> isMultiple()) {
$values = array();
for ($i=0; $i < count($this -> values); $i++)

View file

@ -71,23 +71,23 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
*
* @retval array Parsed values
*/
private function parseValues() {
self :: log_trace('values: '.varDump($this -> values));
$parseValues=array();
foreach($this -> values as $val) {
$decodedValue = json_decode($val, true);
self :: log_trace('decoded value: '.varDump($decodedValue));
if (is_array($decodedValue)) {
$parseValue = array('value' => $val);
foreach($decodedValue as $c => $cvalue) {
$parseValue[$c] = $this -> translateComponentValue($c,$cvalue);
}
$parseValues[] = $parseValue;
}
}
self :: log_trace('parsed values: '.varDump($parseValues));
return $parseValues;
}
private function parseValues() {
self :: log_trace('values: '.varDump($this -> values));
$parseValues=array();
foreach($this -> values as $val) {
$decodedValue = json_decode($val, true);
self :: log_trace('decoded value: '.varDump($decodedValue));
if (is_array($decodedValue)) {
$parseValue = array('value' => $val);
foreach($decodedValue as $c => $cvalue) {
$parseValue[$c] = $this -> translateComponentValue($c,$cvalue);
}
$parseValues[] = $parseValue;
}
}
self :: log_trace('parsed values: '.varDump($parseValues));
return $parseValues;
}
/**
* Retourne les infos d'affichage de l'élément
@ -448,9 +448,12 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
/**
* 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() {
public function getApiValue($details=false) {
$values = array();
foreach(ensureIsArray($this -> values) as $value) {
$decodedValue = json_decode($value, true);

View file

@ -85,14 +85,17 @@ class LSformElement_labeledValue extends LSformElement {
);
}
/**
* Parse une valeur
*
* @param[in] $value La valeur
*
* @retval array Un tableau cle->valeur contenant value et label
**/
public function parseValue($value) {
/**
* Parse one value
*
* @param[in] $value string The value to parse
* @param[in] $details boolean Enable/disable details return (optional, default: true)
*
* @retval array Parsed value
*/
public function parseValue($value, $details=true) {
if (!$details)
return $value;
$ret=array('raw_value' => $value);
if (preg_match('/^\[([^\]]*)\](.*)$/',$value,$m)) {
$ret['label'] = $m[1];

View file

@ -45,14 +45,17 @@ class LSformElement_mailQuota extends LSformElement {
* Parse one value
*
* @param[in] $value string The value to parse
* @param[in] $details boolean Enable/disable details return (optional, default: true)
*
* @retval array Parsed value
*/
public function parseValue($value) {
if (preg_match('/^([0-9]+)'.$this -> getSuffix().'$/',$value,$regs)) {
public function parseValue($value, $details=true) {
if (preg_match('/^([0-9]+)'.$this -> getSuffix().'$/', $value, $regs)) {
$infos = array(
'size' => $regs[1],
);
if (!$details)
return $infos['size'];
if ($infos['size'] == 0) {
return array(
'size' => 0,
@ -199,25 +202,4 @@ class LSformElement_mailQuota extends LSformElement {
return true;
}
/**
* Retreive value as return in API response
*
* @retval mixed API value(s) or null/empty array if no value
*/
public function getApiValue() {
$values = array();
foreach(ensureIsArray($this -> values) as $value) {
$parsed_value = $this -> parseValue($value);
if (is_array($parsed_value)) {
$values[] = $parsed_value['size'];
}
}
if ($this -> isMultiple()) {
return $values;
}
if (!$values)
return null;
return $values[0];
}
}

View file

@ -42,19 +42,21 @@ class LSformElement_quota extends LSformElement {
1099511627776 => 'To',
);
/**
* Parse one value
*
* @param[in] $value string The value to parse
* @param[in] $details boolean Enable/disable details return (optional, default: true)
*
* @retval array Parsed value
*/
public function parseValue($value) {
public function parseValue($value, $details=true) {
if (preg_match('/^([0-9]+)$/', $value, $regs)) {
$infos = array(
'size' => ceil($regs[1]/$this -> getFactor()),
);
if (!$details)
return $infos['size'];
if ($infos['size'] == 0) {
return array(
'size' => 0,
@ -194,25 +196,4 @@ class LSformElement_quota extends LSformElement {
return $this -> getParam('html_options.factor', 1, 'int');
}
/**
* Retreive value as return in API response
*
* @retval mixed API value(s) or null/empty array if no value
*/
public function getApiValue() {
$values = array();
foreach(ensureIsArray($this -> values) as $value) {
$parsed_value = $this -> parseValue($value);
if (is_array($parsed_value)) {
$values[] = $parsed_value['size'];
}
}
if ($this -> isMultiple()) {
return $values;
}
if (!$values)
return null;
return $values[0];
}
}

View file

@ -141,6 +141,37 @@ class LSformElement_select extends LSformElement {
}
}
/**
* 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) {
$label = $this -> _isValidValue($value, $this -> params['text_possible_values']);
if ($label === false) continue;
if ($details)
$values[] = array(
'label' => $label,
'value' => $value,
);
else
$values[] = $value;
}
if ($this -> isMultiple()) {
return ensureIsArray($values);
}
if (!$values)
return null;
if ($details)
return $values[0];
}
}
/**

View file

@ -322,4 +322,23 @@ class LSformElement_select_object extends LSformElement {
}
}
/**
* 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 = ($details?$this -> values:array_keys(ensureIsArray($this -> values)));
self :: log_debug("getApiValue(): ".varDump($values));
if ($this -> isMultiple()) {
return ensureIsArray($values);
}
if (!$values)
return null;
return $values[0];
}
}

View file

@ -35,6 +35,32 @@ class LSformElement_ssh_key extends LSformElement {
var $template = 'LSformElement_ssh_key.tpl';
var $fieldTemplate = 'LSformElement_ssh_key_field.tpl';
/**
* Parse one value
*
* @param[in] $value string The value to parse
* @param[in] $details boolean Enable/disable details return (optional, default: true)
*
* @retval array Parsed value
*/
public function parseValue($value, $details=true) {
if (!$details)
return $value;
if (preg_match('/^ssh-([a-z0-9]+) +([^ ]+) +(.*)$/', $value, $regs)) {
return array(
'type' => $regs[1],
'mail' => $regs[3],
'value' => $value
);
}
return array(
'type' => null,
'mail' => null,
'value' => $value
);
}
/**
* Retourne les infos d'affichage de l'élément
*
@ -60,20 +86,9 @@ class LSformElement_ssh_key extends LSformElement {
$values_txt = array();
foreach ($this -> values as $value) {
if (preg_match('/^ssh-([a-z0-9]+) +([^ ]+) +(.*)$/',$value,$regs)) {
$values_txt[] = array(
'type' => $regs[1],
'shortTxt' => substr($regs[2],0,10),
'mail' => $regs[3],
'value' => $value
);
}
else {
$values_txt[] = array(
'shortTxt' => substr($value,0,15),
'value' => $value
);
}
$parsedValue = $this -> parseValue($value);
$parsedValue['shortTxt'] = substr($value, 0, 15);
$values_txt[] = $parsedValue;
}
$params['values_txt'] = $values_txt;
$params['unknowTypeTxt'] = _('Unknown type');

View file

@ -74,6 +74,37 @@ class LSformElement_valueWithUnit extends LSformElement {
);
}
/**
* Parse one value
*
* @param[in] $value string The value to parse
* @param[in] $details boolean Enable/disable details return (optional, default: true)
*
* @retval array Parsed value
*/
public function parseValue($value, $details=true) {
if (preg_match('/^([0-9]*)$/' ,$value, $regs)) {
$infos = array(
'value' => intval($regs[1]),
);
if (!$details)
return $infos['value'];
$units = $this -> getUnits();
if ($units) {
foreach($units as $sill => $label) {
if ($infos['value'] >= $sill) {
$infos['valueWithUnit'] = $this -> formatNumber($infos['value']/$sill);
$infos['unitSill'] = $sill;
$infos['unitLabel'] = $label;
break;
}
}
}
return $infos;
}
return false;
}
/**
* Retourne les infos d'affichage de l'élément
*
@ -89,25 +120,15 @@ class LSformElement_valueWithUnit extends LSformElement {
if ($units) {
foreach ($this -> values as $value) {
if (preg_match('/^([0-9]*)$/',$value,$regs)) {
$infos = array(
'value' => $regs[1]
);
foreach($units as $sill => $label) {
if ($infos['value'] >= $sill) {
$infos['valueWithUnit']=$this -> formatNumber($infos['value']/$sill);
$infos['unitSill']=$sill;
$infos['unitLabel']=$label;
break;
}
}
$values_and_units[$value] = $infos;
}
else {
$parsedValue = $this -> parseValue($value);
if ($parsedValue === false) {
$values_and_units[$value] = array(
'unknown' => _('Incorrect value')
);
}
else {
$values_and_units[$value] = $parsedValue;
}
}
}
@ -204,23 +225,6 @@ class LSformElement_valueWithUnit extends LSformElement {
return true;
}
/**
* Retreive value as return in API response
*
* @retval mixed API value(s) or null/empty array if no value
*/
public function getApiValue() {
$values = array();
foreach (ensureIsArray($this -> values) as $value)
if (preg_match('/^([0-9]*)$/', $value, $regs))
$values[] = intval($regs[1]);
if ($this -> isMultiple()) {
return $values;
}
if (!$values)
return null;
return $values[0];
}
}
/*

View file

@ -1693,13 +1693,14 @@ function handle_api_LSobject_show($request) {
'dn' => $object -> getDn(),
'type' => $object -> getType(),
'name' => $object -> getDisplayName(),
'details' => isset($_REQUEST['details']),
'attributes' => array(),
'relations' => array(),
);
$view = $object -> getView();
foreach($view -> elements as $element) {
$data['attributes'][$element -> name] = $element -> getApiValue();
$data['attributes'][$element -> name] = $element -> getApiValue($data['details']);
}
if (LSsession :: loadLSclass('LSrelation')) {