From f71bb45bd7aea050a95bb91d55b96cbd95c8da33 Mon Sep 17 00:00:00 2001 From: Mohamed DAOUD Date: Wed, 20 Nov 2024 21:33:32 +0100 Subject: [PATCH] Fix #30766 Extra field Can Always Be Edited (#31876) * fix alwayseditable * Update extrafields.class.php * Update extrafields.class.php * Update extrafields.class.php * enhance fix alwayseditable * fix phan --- htdocs/core/class/commonobject.class.php | 4 ++-- htdocs/core/class/extrafields.class.php | 27 ++++++++++++++++-------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index d6a0ef32ba4..37a0c324dfb 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -9393,14 +9393,14 @@ abstract class CommonObject $out .= getPictoForType($extrafields->attributes[$this->table_element]['type'][$key], ($extrafields->attributes[$this->table_element]['type'][$key] == 'text' ? 'tdtop' : '')); } //$out .= ''; - $out .= $extrafields->showInputField($key, $value, '', $keysuffix, '', 0, $this->id, $this->table_element); + $out .= $extrafields->showInputField($key, $value, '', $keysuffix, '', 0, $this, $this->table_element); break; case "edit": $listoftypestoshowpicto = explode(',', getDolGlobalString('MAIN_TYPES_TO_SHOW_PICTO', 'email,phone,ip,password')); if (in_array($extrafields->attributes[$this->table_element]['type'][$key], $listoftypestoshowpicto)) { $out .= getPictoForType($extrafields->attributes[$this->table_element]['type'][$key], ($extrafields->attributes[$this->table_element]['type'][$key] == 'text' ? 'tdtop' : '')); } - $out .= $extrafields->showInputField($key, $value, '', $keysuffix, '', '', $this->id, $this->table_element); + $out .= $extrafields->showInputField($key, $value, '', $keysuffix, '', '', $this, $this->table_element); break; } diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 6138ad7c169..4a1ed96b238 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -1076,18 +1076,18 @@ class ExtraFields * Return HTML string to put an input field into a page * Code very similar with showInputField of common object * - * @param string $key Key of attribute + * @param string $key Key of attribute * @param string|array{start:int,end:int} $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 dates in filter mode, a range array('start'=>, 'end'=>) should be provided - * @param string $moreparam To add more parameters on html input tag - * @param string $keysuffix Suffix string to add after name and id of field (can be used to avoid duplicate names) - * @param string $keyprefix Prefix string to add before name and id of field (can be used to avoid duplicate names) - * @param string $morecss More css (to defined size of field. Old behaviour: may also be a numeric) - * @param int $objectid Current object id - * @param string $extrafieldsobjectkey The key to use to store retrieved data (commonly $object->table_element) - * @param int $mode 1=Used for search filters + * @param string $moreparam To add more parameters on html input tag + * @param string $keysuffix Suffix string to add after name and id of field (can be used to avoid duplicate names) + * @param string $keyprefix Prefix string to add before name and id of field (can be used to avoid duplicate names) + * @param string $morecss More css (to defined size of field. Old behaviour: may also be a numeric) + * @param int|CommonObject $object Current object or object ID. Preferably, pass the object itself. + * @param string $extrafieldsobjectkey The key to use to store retrieved data (commonly $object->table_element) + * @param int $mode 1=Used for search filters * @return string */ - public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $keyprefix = '', $morecss = '', $objectid = 0, $extrafieldsobjectkey = '', $mode = 0) + public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $keyprefix = '', $morecss = '', $object = 0, $extrafieldsobjectkey = '', $mode = 0) { global $conf, $langs, $form; @@ -1096,6 +1096,8 @@ class ExtraFields $form = new Form($this->db); } + $objectid = (is_numeric($object) ? $object : $object->id); + $out = ''; if (!preg_match('/options_$/', $keyprefix)) { // Because we work on extrafields, we add 'options_' to prefix if not already added @@ -1120,6 +1122,7 @@ class ExtraFields $list = (string) dol_eval($this->attributes[$extrafieldsobjectkey]['list'][$key], 1, 1, '2'); $totalizable = $this->attributes[$extrafieldsobjectkey]['totalizable'][$key]; $help = $this->attributes[$extrafieldsobjectkey]['help'][$key]; + $alwayseditable = $this->attributes[$extrafieldsobjectkey]['alwayseditable'][$key]; $hidden = (empty($list) ? 1 : 0); // If empty, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller) //var_dump('key='.$key.' '.$value.' '.$moreparam.' '.$keysuffix.' '.$keyprefix.' '.$objectid.' '.$extrafieldsobjectkey.' '.$mode); @@ -1935,6 +1938,12 @@ class ExtraFields if (!empty($hidden)) { $out = ''; } + + // If alwayseditable is false, and object is not in draft, then showOutputField + // @phan-suppress-next-line PhanUndeclaredConstantOfClass + if ($alwayseditable == 0 && !is_numeric($object) && isset($object->status) && $object->status != $object::STATUS_DRAFT) { + $out = $this->showOutputField($key, $value, $moreparam, $extrafieldsobjectkey, null, $object); + } /* Add comments if ($type == 'date') $out.=' (YYYY-MM-DD)'; elseif ($type == 'datetime') $out.=' (YYYY-MM-DD HH:MM:SS)';