From 19dd3431067d9bd936540434eeb026b871e37d81 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Fri, 5 Feb 2021 12:22:32 +0100 Subject: [PATCH] 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. --- doc/api/api.docbook | 16 +++-- src/includes/class/class.LSformElement.php | 23 +++++-- .../class/class.LSformElement_image.php | 5 +- ...s.LSformElement_jsonCompositeAttribute.php | 39 ++++++----- .../class.LSformElement_labeledValue.php | 19 +++--- .../class/class.LSformElement_mailQuota.php | 28 ++------ .../class/class.LSformElement_quota.php | 27 ++------ .../class/class.LSformElement_select.php | 31 +++++++++ .../class.LSformElement_select_object.php | 19 ++++++ .../class/class.LSformElement_ssh_key.php | 43 ++++++++---- .../class.LSformElement_valueWithUnit.php | 68 ++++++++++--------- src/includes/routes.php | 3 +- 12 files changed, 189 insertions(+), 132 deletions(-) diff --git a/doc/api/api.docbook b/doc/api/api.docbook index 5cb97846..b0330e75 100644 --- a/doc/api/api.docbook +++ b/doc/api/api.docbook @@ -248,14 +248,19 @@ 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 + details afin d'obtenir des informations complémentaires sur les valeurs + des attributs. Exemple 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]; } } diff --git a/src/includes/class/class.LSformElement_image.php b/src/includes/class/class.LSformElement_image.php index 3470d3fb..a4b6ec9d 100644 --- a/src/includes/class/class.LSformElement_image.php +++ b/src/includes/class/class.LSformElement_image.php @@ -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++) diff --git a/src/includes/class/class.LSformElement_jsonCompositeAttribute.php b/src/includes/class/class.LSformElement_jsonCompositeAttribute.php index 86cba193..aff21804 100644 --- a/src/includes/class/class.LSformElement_jsonCompositeAttribute.php +++ b/src/includes/class/class.LSformElement_jsonCompositeAttribute.php @@ -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); diff --git a/src/includes/class/class.LSformElement_labeledValue.php b/src/includes/class/class.LSformElement_labeledValue.php index 4bb29b94..56311220 100644 --- a/src/includes/class/class.LSformElement_labeledValue.php +++ b/src/includes/class/class.LSformElement_labeledValue.php @@ -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]; diff --git a/src/includes/class/class.LSformElement_mailQuota.php b/src/includes/class/class.LSformElement_mailQuota.php index cad40a60..9f0a1760 100644 --- a/src/includes/class/class.LSformElement_mailQuota.php +++ b/src/includes/class/class.LSformElement_mailQuota.php @@ -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]; - } - } diff --git a/src/includes/class/class.LSformElement_quota.php b/src/includes/class/class.LSformElement_quota.php index 5034f21e..9d1542ca 100644 --- a/src/includes/class/class.LSformElement_quota.php +++ b/src/includes/class/class.LSformElement_quota.php @@ -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]; - } - } diff --git a/src/includes/class/class.LSformElement_select.php b/src/includes/class/class.LSformElement_select.php index e252ffdb..62da6ed9 100644 --- a/src/includes/class/class.LSformElement_select.php +++ b/src/includes/class/class.LSformElement_select.php @@ -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]; + } + } /** diff --git a/src/includes/class/class.LSformElement_select_object.php b/src/includes/class/class.LSformElement_select_object.php index eee521e6..a639ca9e 100644 --- a/src/includes/class/class.LSformElement_select_object.php +++ b/src/includes/class/class.LSformElement_select_object.php @@ -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]; + } + } diff --git a/src/includes/class/class.LSformElement_ssh_key.php b/src/includes/class/class.LSformElement_ssh_key.php index f22478c4..5d9e4061 100644 --- a/src/includes/class/class.LSformElement_ssh_key.php +++ b/src/includes/class/class.LSformElement_ssh_key.php @@ -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'); diff --git a/src/includes/class/class.LSformElement_valueWithUnit.php b/src/includes/class/class.LSformElement_valueWithUnit.php index 66d53d91..9f3aa681 100644 --- a/src/includes/class/class.LSformElement_valueWithUnit.php +++ b/src/includes/class/class.LSformElement_valueWithUnit.php @@ -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]; - } } /* diff --git a/src/includes/routes.php b/src/includes/routes.php index 995ab3de..d84e5023 100644 --- a/src/includes/routes.php +++ b/src/includes/routes.php @@ -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')) {