Enhance selectForForms

This commit is contained in:
Laurent Destailleur (aka Eldy) 2025-01-30 16:04:25 +01:00
parent 58134e840d
commit e83e318fc9
4 changed files with 55 additions and 37 deletions

View file

@ -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<int,string>,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<int,string>,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

View file

@ -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);

View file

@ -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<int,string>,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<int,string>,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 .= '<label class="nowrap position-fk-user classfortooltip" title="'.dol_escape_js($langs->trans('VacantCheckboxHelper')).'"><input type="checkbox" id="'.$vacantId.'" name="'.$vacantId.'">&nbsp;'.$langs->trans("Vacant").'</label>'; ?>
<script type="text/javascript">
$(document).ready(function () {

View file

@ -1,10 +1,10 @@
<?php
/* Copyright (C) 2007-2017 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
* Copyright (C) 2021 Greg Rastklan <greg.rastklan@atm-consulting.fr>
* Copyright (C) 2021 Jean-Pascal BOUDET <jean-pascal.boudet@atm-consulting.fr>
* Copyright (C) 2021 Grégory BLEMAND <gregory.blemand@atm-consulting.fr>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
* Copyright (C) 2021 Greg Rastklan <greg.rastklan@atm-consulting.fr>
* Copyright (C) 2021 Jean-Pascal BOUDET <jean-pascal.boudet@atm-consulting.fr>
* Copyright (C) 2021 Grégory BLEMAND <gregory.blemand@atm-consulting.fr>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -159,6 +159,10 @@ if (!$permissiontoread) {
accessforbidden();
}
// Force the type of field fk_user to remove the filter on active user.
// Note: Another fix would be to be able to filter on a freetet on firstname/lastname.
//var_dump($object->fields['fk_user']);
$object->fields['fk_user']['type'] = 'integer:User:user/class/user.class.php:0';
/*