2017-05-03 01:12:35 +02:00
|
|
|
<?php
|
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (C) 2007 Easter-eggs
|
2021-04-13 18:04:19 +02:00
|
|
|
* https://ldapsaisie.org
|
2017-05-03 01:12:35 +02:00
|
|
|
*
|
|
|
|
* Author: See AUTHORS file in top-level directory.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
LSsession :: loadLSclass('LSformElement');
|
|
|
|
|
|
|
|
/**
|
2017-05-04 17:27:52 +02:00
|
|
|
* Element jsonCompositeAttribute for LSform
|
2017-05-03 01:12:35 +02:00
|
|
|
*
|
2017-05-04 17:27:52 +02:00
|
|
|
* This classe permit to handle compostie attributes encoded with JSON.
|
2017-05-03 01:12:35 +02:00
|
|
|
*
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
class LSformElement_jsonCompositeAttribute extends LSformElement {
|
|
|
|
|
|
|
|
var $template = 'LSformElement_jsonCompositeAttribute.tpl';
|
|
|
|
var $fieldTemplate = 'LSformElement_jsonCompositeAttribute_field.tpl';
|
|
|
|
|
2019-03-12 11:42:53 +01:00
|
|
|
public function __construct(&$form, $name, $label, $params, &$attr_html){
|
2019-03-12 13:10:24 +01:00
|
|
|
parent :: __construct($form, $name, $label, $params,$attr_html);
|
2019-03-11 22:21:25 +01:00
|
|
|
$this -> components = $this -> getParam('html_options.components', array());
|
2017-05-03 01:12:35 +02:00
|
|
|
}
|
2017-05-04 17:27:52 +02:00
|
|
|
|
2017-05-03 01:12:35 +02:00
|
|
|
/*
|
2017-05-04 17:27:52 +02:00
|
|
|
* Value components :
|
|
|
|
*
|
2017-05-03 01:12:35 +02:00
|
|
|
* Format :
|
|
|
|
* array (
|
2017-05-04 17:27:52 +02:00
|
|
|
* '[component1_key]' => array (
|
|
|
|
* 'label' => '[component label]',
|
|
|
|
* 'type' => '[component type]',
|
|
|
|
* 'required' => '[booléen]',
|
|
|
|
* 'check_data' => array([config LSform_rule])
|
2017-05-03 01:12:35 +02:00
|
|
|
* ),
|
2017-05-04 17:27:52 +02:00
|
|
|
* '[component2_key]' => array (
|
2017-05-03 01:12:35 +02:00
|
|
|
* 'label' => 'label2',
|
|
|
|
* 'type' => 'select_list',
|
|
|
|
* 'options' => array([config as LSattr_html_select_list html_options]),
|
|
|
|
* ),
|
|
|
|
* [...]
|
|
|
|
* )
|
|
|
|
* Types :
|
2017-05-04 17:27:52 +02:00
|
|
|
* - 'select_list' => Component feed by a list of valeur configured like an
|
|
|
|
* atribute LSattr_html :: select_list.
|
|
|
|
* - 'text' => manual entry
|
|
|
|
*
|
2017-05-03 01:12:35 +02:00
|
|
|
*/
|
|
|
|
var $components = array();
|
2017-05-04 17:27:52 +02:00
|
|
|
|
2021-02-03 14:40:28 +01:00
|
|
|
/**
|
|
|
|
* Parse values
|
|
|
|
*
|
|
|
|
* @retval array Parsed values
|
|
|
|
*/
|
2021-02-05 12:22:32 +01:00
|
|
|
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;
|
|
|
|
}
|
2021-02-03 14:40:28 +01:00
|
|
|
|
2017-05-03 01:12:35 +02:00
|
|
|
/**
|
|
|
|
* Retourne les infos d'affichage de l'élément
|
2017-05-04 17:27:52 +02:00
|
|
|
*
|
2017-05-03 01:12:35 +02:00
|
|
|
* Cette méthode retourne les informations d'affichage de l'élement
|
|
|
|
*
|
|
|
|
* @retval array
|
|
|
|
*/
|
2019-03-12 11:42:53 +01:00
|
|
|
public function getDisplay(){
|
2017-05-03 01:12:35 +02:00
|
|
|
$return = $this -> getLabelInfos();
|
|
|
|
|
|
|
|
$return['html'] = $this -> fetchTemplate(NULL,
|
|
|
|
array(
|
2021-02-03 14:40:28 +01:00
|
|
|
'parseValues' => $this -> parseValues(),
|
2020-12-02 17:43:55 +01:00
|
|
|
'fullWidth' => $this -> getParam('html_options.fullWidth', false, 'bool'),
|
2017-05-03 01:12:35 +02:00
|
|
|
)
|
|
|
|
);
|
2020-05-28 16:56:36 +02:00
|
|
|
LStemplate :: addCssFile('LSformElement_jsonCompositeAttribute.css');
|
2018-04-26 11:23:00 +02:00
|
|
|
if (!$this -> isFreeze()) {
|
2020-05-28 16:56:36 +02:00
|
|
|
LStemplate :: addJSconfigParam(
|
2018-04-26 11:23:00 +02:00
|
|
|
$this -> name,
|
|
|
|
array (
|
|
|
|
'components' => $this -> components,
|
|
|
|
)
|
|
|
|
);
|
2020-05-28 16:56:36 +02:00
|
|
|
LStemplate :: addJSscript('LSformElement_jsonCompositeAttribute_field_value_component_text_value.js');
|
|
|
|
LStemplate :: addJSscript('LSformElement_jsonCompositeAttribute_field_value_component.js');
|
|
|
|
LStemplate :: addJSscript('LSformElement_jsonCompositeAttribute_field_value.js');
|
|
|
|
LStemplate :: addJSscript('LSformElement_jsonCompositeAttribute_field.js');
|
|
|
|
LStemplate :: addJSscript('LSformElement_jsonCompositeAttribute.js');
|
2018-04-26 11:23:00 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 01:12:35 +02:00
|
|
|
return $return;
|
|
|
|
}
|
2017-05-04 17:27:52 +02:00
|
|
|
|
2017-05-03 01:12:35 +02:00
|
|
|
/**
|
2017-12-22 16:31:00 +01:00
|
|
|
* Retournne un template Smarty compilé dans le contexte d'un LSformElement
|
2017-05-03 01:12:35 +02:00
|
|
|
*
|
2017-12-22 16:31:00 +01:00
|
|
|
* @param[in] string $template Le template à retourner
|
|
|
|
* @param[in] array $variables Variables Smarty à assigner avant l'affichage
|
|
|
|
*
|
|
|
|
* @retval string Le HTML compilé du template
|
2017-05-03 01:12:35 +02:00
|
|
|
*/
|
2019-03-12 11:42:53 +01:00
|
|
|
public function fetchTemplate($template=NULL,$variables=array()) {
|
2017-12-22 16:31:00 +01:00
|
|
|
$components = $this -> components;
|
|
|
|
foreach($components as $c => $cconf) {
|
|
|
|
if ($cconf['type']=='select_list') {
|
|
|
|
$components[$c]['possible_values']=$this -> getSelectListComponentPossibleValues($c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$variables['components'] = $components;
|
|
|
|
|
|
|
|
return parent::fetchTemplate($template, $variables);
|
2017-05-03 01:12:35 +02:00
|
|
|
}
|
2017-05-04 17:27:52 +02:00
|
|
|
|
2017-05-03 01:12:35 +02:00
|
|
|
/**
|
2017-05-04 17:27:52 +02:00
|
|
|
* Translate componant value
|
|
|
|
*
|
|
|
|
* Return an array containing :
|
|
|
|
* - value : untranslated value
|
|
|
|
* - translated : translated value
|
|
|
|
*
|
|
|
|
* @param[in] $c string The component name
|
|
|
|
* @param[in] $value string The value
|
2018-04-26 11:23:00 +02:00
|
|
|
* @param[in] $inLoop boolean Internal param to control recursion
|
2017-05-04 17:27:52 +02:00
|
|
|
*
|
2017-05-03 01:12:35 +02:00
|
|
|
* @retval array
|
|
|
|
**/
|
2019-03-12 11:42:53 +01:00
|
|
|
protected function translateComponentValue($c,$value,$inLoop=false) {
|
2018-04-26 11:23:00 +02:00
|
|
|
if (!$inLoop && isset($this -> components[$c]['multiple']) && $this -> components[$c]['multiple']) {
|
|
|
|
$retval = array();
|
2020-11-30 19:43:52 +01:00
|
|
|
foreach(ensureIsArray($value) as $val)
|
2018-04-26 11:23:00 +02:00
|
|
|
$retval[] = $this -> translateComponentValue($c, $val, true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$retval = array (
|
|
|
|
'translated' => $value,
|
|
|
|
'value' => $value,
|
|
|
|
);
|
|
|
|
if (isset($this -> components[$c])) {
|
|
|
|
if ($this -> components[$c]['type']=='select_list') {
|
|
|
|
$retval['translated'] = $this -> getSelectListComponentValueLabel($c,$value);
|
|
|
|
}
|
|
|
|
//elseif type == 'text' => no transformation
|
2017-05-03 01:12:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
2017-05-04 17:27:52 +02:00
|
|
|
/**
|
|
|
|
* Retreive possible values of an select_list component
|
|
|
|
*
|
|
|
|
* @param[in] $c string The component name
|
|
|
|
*
|
|
|
|
* @retval array
|
|
|
|
**/
|
2017-05-03 01:12:35 +02:00
|
|
|
protected $_cache_getSelectListComponentPossibleValues=array();
|
|
|
|
protected function getSelectListComponentPossibleValues($c) {
|
|
|
|
if (!isset($this -> _cache_getSelectListComponentPossibleValues[$c])) {
|
|
|
|
if (!LSsession :: loadLSclass('LSattr_html_select_list')) return;
|
2021-02-03 14:40:28 +01:00
|
|
|
$this -> _cache_getSelectListComponentPossibleValues[$c] = LSattr_html_select_list :: _getPossibleValues(
|
|
|
|
$this -> components[$c]['options'],
|
|
|
|
$this -> name,
|
|
|
|
$this->attr_html->attribute->ldapObject
|
|
|
|
);
|
|
|
|
self :: log_trace(
|
|
|
|
"Component $c possible values: ".varDump($this -> _cache_getSelectListComponentPossibleValues[$c])
|
|
|
|
);
|
2017-05-03 01:12:35 +02:00
|
|
|
}
|
|
|
|
return $this -> _cache_getSelectListComponentPossibleValues[$c];
|
|
|
|
}
|
|
|
|
|
2017-05-04 17:27:52 +02:00
|
|
|
/**
|
|
|
|
* Retreive value's label of an select_list component
|
|
|
|
*
|
|
|
|
* @param[in] $c string The component name
|
|
|
|
* @param[in] $value string The value
|
|
|
|
*
|
|
|
|
* @retval array
|
|
|
|
**/
|
2021-02-03 14:40:28 +01:00
|
|
|
protected function getSelectListComponentValueLabel($c, $value) {
|
2017-05-03 01:12:35 +02:00
|
|
|
if ($this -> getSelectListComponentPossibleValues($c)) {
|
|
|
|
foreach ($this -> _cache_getSelectListComponentPossibleValues[$c] as $v => $label) {
|
|
|
|
if (is_array($label)) {
|
|
|
|
if (!isset($label['possible_values'])) continue;
|
|
|
|
foreach ($label['possible_values'] as $vk => $vl)
|
|
|
|
if ($vk == $$value) return $vl;
|
|
|
|
}
|
|
|
|
if ($v == $value) return $label;
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 14:40:28 +01:00
|
|
|
self :: log_trace("No label found for value '$value'");
|
2017-05-03 01:12:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-05-04 17:27:52 +02:00
|
|
|
|
2017-05-03 01:12:35 +02:00
|
|
|
/**
|
2017-05-04 17:27:52 +02:00
|
|
|
* Retreive LSformElement value from POST data
|
2017-05-03 01:12:35 +02:00
|
|
|
*
|
2017-05-04 17:27:52 +02:00
|
|
|
* This method check present of this element's value in POST data and retreive
|
|
|
|
* it to feed the array passed in paramater.
|
2017-05-03 01:12:35 +02:00
|
|
|
*
|
2020-05-08 20:48:13 +02:00
|
|
|
* @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.
|
2017-05-03 01:12:35 +02:00
|
|
|
*
|
2017-05-04 17:27:52 +02:00
|
|
|
* @retval boolean true if value is in POST data, false instead
|
2017-05-03 01:12:35 +02:00
|
|
|
*/
|
2020-05-08 20:48:13 +02:00
|
|
|
public function getPostData(&$return, $onlyIfPresent=false) {
|
2017-05-03 01:12:35 +02:00
|
|
|
if($this -> isFreeze()) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-04 17:27:52 +02:00
|
|
|
|
2021-02-03 14:40:28 +01:00
|
|
|
// Extract value form POST data
|
|
|
|
$parseValues = array();
|
|
|
|
// API mode
|
|
|
|
if ($this -> form -> api_mode) {
|
|
|
|
$json_values = $this -> getData($_POST, $this -> name);
|
|
|
|
if (!is_array($json_values) || empty($json_values)) {
|
|
|
|
self :: log_trace($this." -> getPostData(): not in POST data");
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-26 11:23:00 +02:00
|
|
|
|
2021-02-03 14:40:28 +01:00
|
|
|
$json_value_count = 0;
|
|
|
|
foreach($json_values as $json_value) {
|
|
|
|
$json_value_count += 1;
|
|
|
|
$input_value = json_decode($json_value, true);
|
|
|
|
if (!is_array($input_value)) {
|
|
|
|
$this -> form -> setElementError(
|
|
|
|
$this -> attr_html,
|
|
|
|
getFData(_('Fail to decode JSON value #%{idx}.'), $json_value_count)
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-11 12:11:56 +02:00
|
|
|
|
2021-02-03 14:40:28 +01:00
|
|
|
$parseValue = array();
|
|
|
|
$unemptyComponents = array();
|
|
|
|
|
|
|
|
foreach (array_keys($this -> components) as $c) {
|
|
|
|
if (!isset($input_value[$c]))
|
|
|
|
continue;
|
|
|
|
if ($this -> getComponentConfig($c, 'multiple', false, 'bool')) {
|
|
|
|
$parseValue[$c] = array();
|
|
|
|
if (is_array($input_value[$c])) {
|
|
|
|
foreach($input_value[$c] as $val) {
|
|
|
|
if (is_empty($val))
|
|
|
|
continue;
|
|
|
|
$parseValue[$c][] = $val;
|
|
|
|
}
|
2018-04-26 11:23:00 +02:00
|
|
|
}
|
2021-02-03 14:40:28 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$parseValue[$c] = $input_value[$c];
|
|
|
|
}
|
2018-04-26 11:23:00 +02:00
|
|
|
|
2021-02-03 14:40:28 +01:00
|
|
|
if (is_empty($parseValue[$c])) {
|
|
|
|
unset($parseValue[$c]);
|
|
|
|
continue;
|
2017-05-03 01:12:35 +02:00
|
|
|
}
|
2021-02-03 14:40:28 +01:00
|
|
|
$unemptyComponents[] = $c;
|
2017-05-03 01:12:35 +02:00
|
|
|
}
|
2017-05-04 17:27:52 +02:00
|
|
|
|
2021-02-03 14:40:28 +01:00
|
|
|
// Ignore empty value from form
|
|
|
|
if (empty($unemptyComponents))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$parseValues[] = $parseValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif (is_array($_POST[$this -> name.'__values_uuid'])) {
|
|
|
|
// HTML form mode
|
|
|
|
foreach ($_POST[$this -> name.'__values_uuid'] as $uuid) {
|
|
|
|
$parseValue = array();
|
|
|
|
$unemptyComponents = array();
|
|
|
|
|
|
|
|
foreach (array_keys($this -> components) as $c) {
|
|
|
|
if (!isset($_POST[$this -> name.'__'.$c.'__'.$uuid]))
|
|
|
|
continue;
|
|
|
|
$parseValue[$c] = array();
|
|
|
|
foreach($_POST[$this -> name.'__'.$c.'__'.$uuid] as $val) {
|
|
|
|
if (empty($val))
|
2018-06-11 12:11:56 +02:00
|
|
|
continue;
|
2021-02-03 14:40:28 +01:00
|
|
|
$parseValue[$c][] = $val;
|
2018-06-11 12:11:56 +02:00
|
|
|
}
|
2021-02-03 14:40:28 +01:00
|
|
|
|
|
|
|
if (empty($parseValue[$c])) {
|
|
|
|
unset($parseValue[$c]);
|
|
|
|
continue;
|
2017-05-03 01:12:35 +02:00
|
|
|
}
|
2021-02-03 14:40:28 +01:00
|
|
|
if (!$this -> getComponentConfig($c, 'multiple', false, 'bool')) {
|
|
|
|
$parseValue[$c] = $parseValue[$c][0];
|
|
|
|
}
|
|
|
|
$unemptyComponents[] = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore empty value from form
|
|
|
|
if (empty($unemptyComponents))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$parseValues[] = $parseValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check extracted values
|
|
|
|
foreach ($parseValues as $parseValue) {
|
|
|
|
// Check component value
|
|
|
|
foreach ($parseValue as $c => $value)
|
|
|
|
$this -> checkComponentValues($c, $value);
|
|
|
|
|
|
|
|
// Check required components
|
|
|
|
foreach (array_keys($this -> components) as $c) {
|
|
|
|
if ($this -> getComponentConfig($c, 'required', false, 'bool') && !isset($parseValue[$c])) {
|
|
|
|
$this -> form -> setElementError(
|
|
|
|
$this -> attr_html,
|
|
|
|
getFData(
|
|
|
|
_('Component %{c} must be defined'),
|
|
|
|
__($this -> getComponentConfig($c, 'label'))
|
|
|
|
)
|
|
|
|
);
|
2017-05-03 01:12:35 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-03 14:40:28 +01:00
|
|
|
$return[$this -> name][] = json_encode($parseValue);
|
2017-05-03 01:12:35 +02:00
|
|
|
}
|
2021-02-03 14:40:28 +01:00
|
|
|
|
2017-05-03 01:12:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-05-04 17:27:52 +02:00
|
|
|
|
2021-02-03 14:40:28 +01:00
|
|
|
/**
|
|
|
|
* Check one component's values
|
|
|
|
*
|
|
|
|
* @param[] $c The component name
|
|
|
|
* @param[] $value The values of the component
|
|
|
|
*
|
|
|
|
* @retval void
|
|
|
|
**/
|
|
|
|
private function checkComponentValues($c, $value) {
|
|
|
|
if ($this -> getComponentConfig($c, 'multiple', false, 'bool')) {
|
|
|
|
foreach ($value as $val) {
|
|
|
|
$this -> checkComponentValue($c, $val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$this -> checkComponentValue($c, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check one component's value
|
|
|
|
*
|
|
|
|
* @param[] $c The component name
|
|
|
|
* @param[] $value The value to check
|
|
|
|
*
|
|
|
|
* @retval void
|
|
|
|
**/
|
|
|
|
private function checkComponentValue($c, $value) {
|
|
|
|
$label = __($this -> getComponentConfig($c, 'label'));
|
|
|
|
|
|
|
|
// select_list components : check values
|
|
|
|
if ($this -> getComponentConfig($c, 'type') == 'select_list') {
|
|
|
|
if (!$this -> getSelectListComponentValueLabel($c, $value)) {
|
|
|
|
$this -> form -> setElementError(
|
|
|
|
$this -> attr_html,
|
|
|
|
getFData(
|
|
|
|
_('Invalid value "%{value}" for component %{component}.'),
|
|
|
|
array('value' => $value, 'component' => $label)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply check data rules
|
|
|
|
foreach($this -> getComponentConfig($c, 'check_data', array(), 'array') as $ruleType => $rconf) {
|
|
|
|
$className = 'LSformRule_'.$ruleType;
|
|
|
|
if (LSsession::loadLSclass($className)) {
|
|
|
|
$r = new $className();
|
|
|
|
if (!$r -> validate($value, $rconf, $this)) {
|
|
|
|
if (isset($rconf['msg'])) {
|
|
|
|
$this -> form -> setElementError(
|
|
|
|
$this -> attr_html,
|
|
|
|
getFData(__($rconf['msg']), $label)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this -> form -> setElementError(
|
|
|
|
$this -> attr_html,
|
|
|
|
getFData(
|
|
|
|
_('Invalid value "%{value}" for component %{component}.'),
|
|
|
|
array('value' => $value, 'component' => $label)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this -> form -> setElementError(
|
|
|
|
$this -> attr_html,
|
|
|
|
getFData(_("Can't validate value of component %{c}."), $label)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a configuration parameter for a specific component (or default value)
|
|
|
|
*
|
|
|
|
* @param[] $component The component name
|
|
|
|
* @param[] $param The configuration parameter
|
|
|
|
* @param[] $default The default value (default : null)
|
|
|
|
* @param[] $cast Cast resulting value in specific type (default : disabled)
|
|
|
|
*
|
|
|
|
* @retval mixed The configuration parameter value or default value if not set
|
|
|
|
**/
|
|
|
|
public function getComponentConfig($component, $param, $default=null, $cast=null) {
|
|
|
|
return LSconfig :: get(
|
|
|
|
$param, $default, $cast,
|
|
|
|
(array_key_exists($component, $this -> components)?$this -> components[$component]:array())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retreive value as return in API response
|
|
|
|
*
|
2021-02-05 12:22:32 +01:00
|
|
|
* @param[in] $details boolean If true, returned values will contain details if this field type
|
|
|
|
* support it (optional, default: false)
|
|
|
|
*
|
2021-02-03 14:40:28 +01:00
|
|
|
* @retval mixed API value(s) or null/empty array if no value
|
|
|
|
*/
|
2021-02-05 12:22:32 +01:00
|
|
|
public function getApiValue($details=false) {
|
2021-02-03 14:40:28 +01:00
|
|
|
$values = array();
|
|
|
|
foreach(ensureIsArray($this -> values) as $value) {
|
|
|
|
$decodedValue = json_decode($value, true);
|
|
|
|
if (is_array($decodedValue)) {
|
|
|
|
$parsedValue = array();
|
|
|
|
foreach(array_keys($this -> components) as $c) {
|
|
|
|
if (!isset($decodedValue[$c]))
|
|
|
|
continue;
|
|
|
|
if ($this -> getComponentConfig($c, 'multiple', false, 'bool')) {
|
|
|
|
$parsedValue[$c] = ensureIsArray($decodedValue[$c]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$parsedValue[$c] = $decodedValue[$c];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$values[] = $parsedValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($this -> isMultiple()) {
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
if (!$values)
|
|
|
|
return null;
|
|
|
|
return $values[0];
|
|
|
|
}
|
|
|
|
|
2017-05-03 01:12:35 +02:00
|
|
|
}
|