From e83e318fc92bef80fa98d919ed694ec627e850d7 Mon Sep 17 00:00:00 2001 From: "Laurent Destailleur (aka Eldy)" Date: Thu, 30 Jan 2025 16:04:25 +0100 Subject: [PATCH] Enhance selectForForms --- htdocs/core/class/commonobject.class.php | 31 +++++++++++------- htdocs/core/class/html.form.class.php | 41 +++++++++++++----------- htdocs/hrm/class/position.class.php | 6 ++-- htdocs/hrm/position_list.php | 14 +++++--- 4 files changed, 55 insertions(+), 37 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 3aa18de36e9..6ba5e3e730f 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7450,16 +7450,15 @@ abstract class CommonObject * Return HTML string to put an input field into a page * Code very similar with showInputField of extra fields * - * @param ?array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array,comment?:string} $val Array of properties for field to show (used only if ->fields not defined) - * Array of properties of field to show - * @param string $key Key of attribute - * @param string|string[] $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value, for array type must be array) - * @param string $moreparam To add more parameters on html input tag - * @param string $keysuffix Suffix string to add into name and id of field (can be used to avoid duplicate names) - * @param string $keyprefix Prefix string to add into name and id of field (can be used to avoid duplicate names) - * @param string|int $morecss Value for css to define style/length of field. May also be a numeric. - * @param int<0,1> $nonewbutton Force to not show the new button on field that are links to object - * @return string + * @param ?array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array,comment?:string} $val Array of properties for field to show (used only if ->fields not defined, so try to keep this null) + * @param string $key Key of attribute + * @param string|string[] $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value, for array type must be array) + * @param string $moreparam To add more parameters on html input tag + * @param string $keysuffix Suffix string to add into name and id of field (can be used to avoid duplicate names) + * @param string $keyprefix Prefix string to add into name and id of field (can be used to avoid duplicate names) + * @param string|int $morecss Value for css to define style/length of field. May also be a numeric. + * @param int<0,1> $nonewbutton Force to not show the new button on field that are links to object + * @return string */ public function showInputField($val, $key, $value, $moreparam = '', $keysuffix = '', $keyprefix = '', $morecss = 0, $nonewbutton = 0) { @@ -8269,7 +8268,17 @@ abstract class CommonObject } } } - $objectfield = $this->element.($this->module ? '@'.$this->module : '').':'.$key.$keysuffix; + + // $param_list_array[0] can be the name of object (Example 'User' the field is linked to). Not as taking the information from the record in ->fields found from $objectfield. + + // $valparent is a string 'dataobject@module:keyoffieldinfieldsarray' to find the record field to link to. + // $valparent = $this->element.($this->module ? '@'.$this->module : '').':'.$key.$keysuffix; + + // $val is already the record field found at same place than found by $valparent but already loaded and may have been modified by parent caller. + + //$objectfield = $valparent; + $objectfield = $val; // Is better than using old method $valparent + $out = $form->selectForForms($param_list_array[0], $keyprefix.$key.$keysuffix, $value, $showempty, '', '', $morecss, $moreparam, 0, (empty($val['disabled']) ? 0 : 1), '', $objectfield); if (!empty($param_list_array[2])) { // If the entry into $fields is set, we must add a create button diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index e611d3ec66f..af533b9474b 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -8486,22 +8486,22 @@ class Form /** * Generic method to select a component from a combo list. - * Can use autocomplete with ajax after x key pressed or a full combo, depending on setup. + * Can use autocomplete with ajax after x key pressed (if $objecttmp->element.'_USE_SEARCH_TO_SELECT' is set) or a full combo, depending on setup. * This is the generic method that will replace all specific existing methods. * - * @param string $objectdesc 'ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]'. For hard coded custom needs. Try to prefer method using $objectfield instead of $objectdesc. - * @param string $htmlname Name of HTML select component - * @param int $preSelectedValue Preselected value (ID of element) + * @param string $objectdesc 'ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]'. For hard coded custom needs. Try to prefer method using $objectfield array instead of $objectdesc. + * @param string $htmlname Name of HTML select component + * @param int $preSelectedValue Preselected value (ID of element) * @param string|int<0,1> $showempty ''=empty values not allowed, 'string'=value show if we allow empty values (for example 'All', ...) - * @param string $searchkey Search criteria - * @param string $placeholder Place holder - * @param string $morecss More CSS - * @param string $moreparams More params provided to ajax call - * @param int $forcecombo Force to load all values and output a standard combobox (with no beautification) - * @param int<0,1> $disabled 1=Html component is disabled - * @param string $selected_input_value Value of preselected input text (for use with ajax) - * @param string $objectfield Object:Field that contains the definition of parent (in table $fields or $extrafields). Example: 'Object:xxx' or 'Object@module:xxx' (old syntax 'Module_Object:xxx') or 'Object:options_xxx' or 'Object@module:options_xxx' (old syntax 'Module_Object:options_xxx') - * @return string Return HTML string + * @param string $searchkey Search criteria + * @param string $placeholder Place holder + * @param string $morecss More CSS + * @param string $moreparams More params provided to ajax call + * @param int $forcecombo Force to load all values and output a standard combobox (with no beautification) + * @param int<0,1> $disabled 1=Html component is disabled + * @param string $selected_input_value Value of preselected input text (for use with ajax) + * @param string|array $objectfield 'Object:Field' that contains the definition of parent (in table $fields or $extrafields). Example: 'Object:xxx' or 'Object@module:xxx' or 'Object:options_xxx' or 'Object@module:options_xxx' or, better, the full entry array in ->fields + * @return string Return HTML string * @see selectForFormsList(), select_thirdparty_list() */ public function selectForForms($objectdesc, $htmlname, $preSelectedValue, $showempty = '', $searchkey = '', $placeholder = '', $morecss = '', $moreparams = '', $forcecombo = 0, $disabled = 0, $selected_input_value = '', $objectfield = '') @@ -8511,14 +8511,16 @@ class Form // Example of common usage for a link to a thirdparty // We got this in a modulebuilder form of "MyObject" of module "mymodule". - // ->fields is array( ... "fk_soc" => array("type"=>"integer:Societe:societe/class/societe.class.php:1:((status:=:1) AND (entity:IN:__SHARED_ENTITIES__))" ...) + // When ->fields is array( ... "fk_soc" => array("type"=>"integer:Societe:societe/class/societe.class.php:1:((status:=:1) AND (entity:IN:__SHARED_ENTITIES__))" ...), we have // $objectdesc = 'Societe' - // $objectfield = 'myobject@mymodule:fk_soc' ('fk_soc' is code to retrieve myobject->fields['fk_soc']) + // $objectfield = 'myobject@mymodule:fk_soc' ('fk_soc' is code to retrieve myobject->fields['fk_soc']) or it can be an array that is directly + // array("type"=>"integer:Societe:societe/class/societe.class.php:1:((status:=:1) AND (entity:IN:__SHARED_ENTITIES__))" ...) // We got this when showing an extrafields on resource that is a link to societe - // extrafields 'link_to_societe' of Resource is 'link' to 'Societe:societe/class/societe.class.php:1:((status:=:1) AND (entity:IN:__SHARED_ENTITIES__))" ...)' + // When extrafields 'link_to_societe' of Resource is 'link' to 'Societe:societe/class/societe.class.php:1:((status:=:1) AND (entity:IN:__SHARED_ENTITIES__))" ...)', we have // $objectdesc = 'Societe' - // $objectfield = 'resource:options_link_to_societe' + // $objectfield = 'resource:options_link_to_societe' or it can be an array that is directly + // array("type"=>'Societe:societe/class/societe.class.php:1:((status:=:1) AND (entity:IN:__SHARED_ENTITIES__))" ...) // With old usage: // $objectdesc = 'Societe:societe/class/societe.class.php:1:((status:=:1) AND (entity:IN:__SHARED_ENTITIES__))' @@ -8534,7 +8536,10 @@ class Form $filter = ''; // Ensure filter has value (for static analysis) $sortfield = ''; // Ensure filter has value (for static analysis) - if ($objectfield) { // We must retrieve the objectdesc from the field or extrafield + if (is_array($objectfield)) { + $objectdesc = $objectfield['type']; + $objectdesc = preg_replace('/^integer[^:]*:/', '', $objectdesc); + } elseif ($objectfield) { // We must retrieve the objectdesc from the field or extrafield. Deprecated, it is better to provide the array record directly. // Example: $objectfield = 'product:options_package' or 'myobject@mymodule:options_myfield' $tmparray = explode(':', $objectfield); diff --git a/htdocs/hrm/class/position.class.php b/htdocs/hrm/class/position.class.php index 50705c1ece5..2059a1d4ff5 100644 --- a/htdocs/hrm/class/position.class.php +++ b/htdocs/hrm/class/position.class.php @@ -207,7 +207,7 @@ class Position extends CommonObject */ public function __construct(DoliDB $db) { - global $conf, $langs; + global $langs; $this->db = $db; @@ -873,7 +873,7 @@ class Position extends CommonObject * Return HTML string to put an input field into a page * Code very similar with showInputField of extra fields * - * @param ?array{type:string,label:string,enabled:int|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array,comment?:string} $val Array of properties for field to show + * @param ?array{type:string,label:string,enabled:int|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array,comment?:string} $val Array of properties for field to show. If ->fields is defined, keep this null. * @param string $key Key of attribute * @param string $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value) * @param string $moreparam To add more parameters on html input tag @@ -890,7 +890,7 @@ class Position extends CommonObject if ($key == 'fk_user') { $vacantId = $keyprefix.$key.'vacant'.$keysuffix; - $out = parent::showInputField($val, $key, $value, $moreparam, $keysuffix, $keyprefix, $morecss); + $out = parent::showInputField(null, $key, $value, $moreparam, $keysuffix, $keyprefix, $morecss); $out .= ''; ?>