From 461cf5616e7b0571bed62c039d0f4a35d3af7f9f Mon Sep 17 00:00:00 2001 From: ldestailleur Date: Tue, 18 Mar 2025 14:55:37 +0100 Subject: [PATCH] Clean code. WIP To manage permission on extrafields. --- htdocs/admin/mails_senderprofile_list.php | 14 ++------------ htdocs/api/class/api_setup.class.php | 1 - htdocs/core/class/extrafields.class.php | 2 +- htdocs/core/lib/company.lib.php | 16 ++++------------ htdocs/core/tpl/admin_extrafields_edit.tpl.php | 5 ++++- .../tpl/extrafields_list_array_fields.tpl.php | 15 +++++++++++++-- htdocs/core/tpl/extrafields_view.tpl.php | 2 +- htdocs/langs/en_US/admin.lang | 7 +++++-- htdocs/opensurvey/list.php | 15 +++------------ htdocs/projet/activity/perday.php | 11 ++++------- htdocs/projet/activity/permonth.php | 12 ++++-------- htdocs/projet/activity/perweek.php | 12 ++++-------- htdocs/public/ticket/list.php | 11 ++--------- 13 files changed, 47 insertions(+), 76 deletions(-) diff --git a/htdocs/admin/mails_senderprofile_list.php b/htdocs/admin/mails_senderprofile_list.php index 7bc4d49b6f5..155acb37f74 100644 --- a/htdocs/admin/mails_senderprofile_list.php +++ b/htdocs/admin/mails_senderprofile_list.php @@ -126,18 +126,8 @@ foreach ($object->fields as $key => $val) { } } // Extra fields -if (!empty($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label']) > 0) { - foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) { - if (!empty($extrafields->attributes[$object->table_element]['list'][$key])) { - $arrayfields["ef.".$key] = array( - 'label' => $extrafields->attributes[$object->table_element]['label'][$key], - 'checked' => (($extrafields->attributes[$object->table_element]['list'][$key] < 0) ? 0 : 1), - 'position' => $extrafields->attributes[$object->table_element]['pos'][$key], - 'enabled' => (abs($extrafields->attributes[$object->table_element]['list'][$key]) != 3 && $extrafields->attributes[$object->table_element]['perms'][$key]) - ); - } - } -} +include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_array_fields.tpl.php'; + $object->fields = dol_sort_array($object->fields, 'position'); $arrayfields = dol_sort_array($arrayfields, 'position'); diff --git a/htdocs/api/class/api_setup.class.php b/htdocs/api/class/api_setup.class.php index 2f0bcd86001..e32d9cadf37 100644 --- a/htdocs/api/class/api_setup.class.php +++ b/htdocs/api/class/api_setup.class.php @@ -1582,7 +1582,6 @@ class Setup extends DolibarrApi $pos = $request_data['pos']; $moreparams = array(); - dol_syslog(get_class($this).'::updateExtraField', LOG_DEBUG); if (0 > $extrafields->updateExtraField($attrname, $label, $type, (int) $pos, $size, $elementtype, (int) $unique, (int) $required, $default_value, $param, (int) $alwayseditable, $perms, $list, $help, $computed, (string) $entity, $langfile, (string) $enabled, (int) $totalizable, (int) $printable, $moreparams)) { throw new RestException(500, 'Error updating extrafield', array_merge(array($extrafields->errno), $extrafields->errors)); } diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index f55e290510a..5ffa87eba76 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -1046,7 +1046,7 @@ class ExtraFields $this->attributes[$tab->elementtype]['param'][$tab->name] = ($tab->param ? jsonOrUnserialize($tab->param) : ''); $this->attributes[$tab->elementtype]['pos'][$tab->name] = $tab->pos; $this->attributes[$tab->elementtype]['alwayseditable'][$tab->name] = $tab->alwayseditable; - $this->attributes[$tab->elementtype]['perms'][$tab->name] = ((is_null($tab->perms) || strlen($tab->perms) == 0) ? 1 : $tab->perms); + $this->attributes[$tab->elementtype]['perms'][$tab->name] = $tab->perms; $this->attributes[$tab->elementtype]['langfile'][$tab->name] = $tab->langs; $this->attributes[$tab->elementtype]['list'][$tab->name] = $tab->list; $this->attributes[$tab->elementtype]['printable'][$tab->name] = $tab->printable; diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php index 89f8e104ef2..b9e931c0c67 100644 --- a/htdocs/core/lib/company.lib.php +++ b/htdocs/core/lib/company.lib.php @@ -1303,18 +1303,10 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '', $showuserl 'u.user' => array('label' => "DolibarrLogin", 'checked' => '1', 'position' => 50, 'class' => 'center'), ); // Extra fields - if (!empty($extrafields->attributes[$contactstatic->table_element]['label']) && is_array($extrafields->attributes[$contactstatic->table_element]['label']) && count($extrafields->attributes[$contactstatic->table_element]['label'])) { - foreach ($extrafields->attributes[$contactstatic->table_element]['label'] as $key => $val) { - if (!empty($extrafields->attributes[$contactstatic->table_element]['list'][$key])) { - $arrayfields["ef.".$key] = array( - 'label' => $extrafields->attributes[$contactstatic->table_element]['label'][$key], - 'checked' => (((int) dol_eval($extrafields->attributes[$contactstatic->table_element]['list'][$key], 1, 1, '1') < 0) ? '0' : '1'), - 'position' => 1000 + $extrafields->attributes[$contactstatic->table_element]['pos'][$key], - 'enabled' => (string) (int) (abs((int) dol_eval($extrafields->attributes[$contactstatic->table_element]['list'][$key], 1)) != 3 && (int) dol_eval($extrafields->attributes[$contactstatic->table_element]['perms'][$key], 1, 1, '1')) - ); - } - } - } + $extrafieldsobjectkey = $contactstatic->table_element; + $extrafieldsobjectprefix = 'ef.'; + $extrafieldspositionoffset = 1000; + include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_array_fields.tpl.php'; // Initialize array of search criteria $search = array(); diff --git a/htdocs/core/tpl/admin_extrafields_edit.tpl.php b/htdocs/core/tpl/admin_extrafields_edit.tpl.php index e0bdd1a65a9..d7f36b80eb8 100644 --- a/htdocs/core/tpl/admin_extrafields_edit.tpl.php +++ b/htdocs/core/tpl/admin_extrafields_edit.tpl.php @@ -296,6 +296,9 @@ if (in_array($type, array_keys($typewecanchangeinto))) { textwithpicto($langs->trans("AlwaysEditable"), $langs->trans("EditableWhenDraftOnly")); ?>> + +textwithpicto($langs->trans("PermissionOnField"), $langs->trans("PermissionToEditField")); ?> + textwithpicto($langs->trans("Visibility"), $langs->trans("VisibleDesc").'

'.$langs->trans("ItCanBeAnExpression")); ?> @@ -326,7 +329,7 @@ if (in_array($type, array_keys($typewecanchangeinto))) { - trans("EnabledCondition"); ?> + textwithpicto($langs->trans("EnabledCondition"), $langs->trans("EnabledConditionHelp")); ?> diff --git a/htdocs/core/tpl/extrafields_list_array_fields.tpl.php b/htdocs/core/tpl/extrafields_list_array_fields.tpl.php index 522eeeaecbe..b7466890fb8 100644 --- a/htdocs/core/tpl/extrafields_list_array_fields.tpl.php +++ b/htdocs/core/tpl/extrafields_list_array_fields.tpl.php @@ -6,6 +6,13 @@ // So no output must be done. // TODO: Note, supposing $arrayfields is already set + +/** + * @var string $extrafieldsobjectkey + * @var string $extrafieldsobjectprefix + * @var int $extrafieldspositionoffset + */ + ' @phan-var-force array $arrayfields '; @@ -19,6 +26,9 @@ if (empty($conf) || !is_object($conf)) { if (empty($extrafieldsobjectkey) && is_object($object)) { $extrafieldsobjectkey = $object->table_element; } +if (empty($extrafieldspositionoffset)) { + $extrafieldspositionoffset = 0; +} // Loop to show all columns of extrafields from $obj, $extrafields and $db if (!empty($extrafieldsobjectkey)) { // $extrafieldsobject is the $object->table_element like 'societe', 'socpeople', ... @@ -37,8 +47,9 @@ if (!empty($extrafieldsobjectkey)) { // $extrafieldsobject is the $object->table 'label' => $extrafields->attributes[$extrafieldsobjectkey]['label'][$key], 'type' => $extrafields->attributes[$extrafieldsobjectkey]['type'][$key], 'checked' => (((int) dol_eval($extrafields->attributes[$extrafieldsobjectkey]['list'][$key], 1, 1, '1') <= 0) ? '0' : '1'), - 'position' => $extrafields->attributes[$extrafieldsobjectkey]['pos'][$key], - 'enabled' => (string) (int) (abs((int) dol_eval($extrafields->attributes[$extrafieldsobjectkey]['list'][$key], 1)) != 3 && (int) dol_eval($extrafields->attributes[$extrafieldsobjectkey]['perms'][$key], 1, 1, '1')), + 'position' => $extrafieldspositionoffset + $extrafields->attributes[$extrafieldsobjectkey]['pos'][$key], + 'perms' => ((dol_eval($extrafields->attributes[$extrafieldsobjectkey]['perms'][$key], 1, 1, '1') <= 0) ? '0' : '1'), + 'enabled' => (string) (int) (abs((int) dol_eval($extrafields->attributes[$extrafieldsobjectkey]['list'][$key], 1)) != 3), 'langfile' => $extrafields->attributes[$extrafieldsobjectkey]['langfile'][$key], 'help' => $extrafields->attributes[$extrafieldsobjectkey]['help'][$key], ); diff --git a/htdocs/core/tpl/extrafields_view.tpl.php b/htdocs/core/tpl/extrafields_view.tpl.php index c84d30f494d..9190417270f 100644 --- a/htdocs/core/tpl/extrafields_view.tpl.php +++ b/htdocs/core/tpl/extrafields_view.tpl.php @@ -91,7 +91,7 @@ if (empty($reshook) && !empty($object->table_element) && isset($extrafields->att $perms = 1; if ($perms && isset($extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra])) { - $perms = (int) dol_eval($extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra], 1, 1, '2'); + $perms = (int) dol_eval($extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra], 1, 1, '1'); } //print $tmpkeyextra.'-'.$enabled.'-'.$perms.'
'."\n"; diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index cafeff11d1d..0be7b59397a 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -1994,7 +1994,8 @@ ConfirmDeleteFiscalYear=Are you sure to delete this accounting period? ShowFiscalYear=Show accounting period ##### Assets ##### AssetNumberingModules=Assets numbering module -AlwaysEditable=Can always be edited +AlwaysEditable=Can be edited for all object status +PermissionOnField=Permission on field MAIN_APPLICATION_TITLE=Force visible name of application (warning: setting your own name here may break autofill login feature when using DoliDroid mobile application) NbMajMin=Minimum number of uppercase characters NbNumMin=Minimum number of numeric characters @@ -2381,7 +2382,8 @@ DashboardDisableBlockBank=Disable the thumb for banks DashboardDisableBlockAdherent=Disable the thumb for memberships DashboardDisableBlockExpenseReport=Disable the thumb for expense reports DashboardDisableBlockHoliday=Disable the thumb for leaves -EnabledCondition=Condition to have field enabled (if not enabled, visibility will always be off) +EnabledCondition=Condition to have field enabled +EnabledConditionHelp=if not enabled, visibility will always be off IfYouUseASecondTaxYouMustSetYouUseTheMainTax=If you want to use a second tax, you must enable also the first sales tax IfYouUseAThirdTaxYouMustSetYouUseTheMainTax=If you want to use a third tax, you must enable also the first sales tax LanguageAndPresentation=Language and presentation @@ -2458,6 +2460,7 @@ DefinedAPathForAntivirusCommandIntoSetup=Define a path for an antivirus program TriggerCodes=Triggerable events TriggerCodeInfo=Enter here the trigger code(s) that must generate a post of a web request (only external URL are allowed). You can enter several trigger codes separated by a comma. EditableWhenDraftOnly=If unchecked, the value can only be modified when object has a draft status +PermissionToEditField=Overwrite the permissions to edit the field. Keeping empty (default) will check the permission to create/update the object. CssOnEdit=CSS on edit pages CssOnView=CSS on view pages CssOnList=CSS on lists diff --git a/htdocs/opensurvey/list.php b/htdocs/opensurvey/list.php index 493d10157ab..3be31617af1 100644 --- a/htdocs/opensurvey/list.php +++ b/htdocs/opensurvey/list.php @@ -103,18 +103,9 @@ $arrayfields = array(); } } */ // Extra fields -if (isset($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label']) > 0) { - foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) { - if (!empty($extrafields->attributes[$object->table_element]['list'][$key])) { - $arrayfields["ef.".$key] = array( - 'label' => $extrafields->attributes[$object->table_element]['label'][$key], - 'checked' => (($extrafields->attributes[$object->table_element]['list'][$key] < 0) ? 0 : 1), - 'position' => $extrafields->attributes[$object->table_element]['pos'][$key], - 'enabled' => (abs($extrafields->attributes[$object->table_element]['list'][$key]) != 3 && $extrafields->attributes[$object->table_element]['perms'][$key]) - ); - } - } -} +include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_array_fields.tpl.php'; + + $object->fields = dol_sort_array($object->fields, 'position'); $arrayfields = dol_sort_array($arrayfields, 'position'); diff --git a/htdocs/projet/activity/perday.php b/htdocs/projet/activity/perday.php index 799f8796d04..db9ebf96000 100644 --- a/htdocs/projet/activity/perday.php +++ b/htdocs/projet/activity/perday.php @@ -147,13 +147,10 @@ $arrayfields['timeconsumed'] = array('label' => 'TimeConsumed', 'checked' => '1' ); */ // Extra fields -if (!empty($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label']) > 0) { - foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) { - if (!empty($extrafields->attributes[$object->table_element]['list'][$key])) { - $arrayfields["efpt.".$key] = array('label' => $extrafields->attributes[$object->table_element]['label'][$key], 'checked' => (($extrafields->attributes[$object->table_element]['list'][$key] < 0) ? '0' : '1'), 'position' => $extrafields->attributes[$object->table_element]['pos'][$key], 'enabled' => (string) (int) (abs((int) $extrafields->attributes[$object->table_element]['list'][$key]) != 3 && $extrafields->attributes[$object->table_element]['perms'][$key])); - } - } -} +$extrafieldsobjectkey = $object->table_element; +$extrafieldsobjectprefix = 'efpt.'; +include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_array_fields.tpl.php'; + $arrayfields = dol_sort_array($arrayfields, 'position'); diff --git a/htdocs/projet/activity/permonth.php b/htdocs/projet/activity/permonth.php index 2dd8305fae0..3296cce351a 100644 --- a/htdocs/projet/activity/permonth.php +++ b/htdocs/projet/activity/permonth.php @@ -145,15 +145,11 @@ $arrayfields['timeconsumed'] = array('label' => 'TimeConsumed', 'checked' => '1' // If $val['visible']==0, then we never show the field if (!empty($val['visible'])) $arrayfields['t.'.$key]=array('label'=>$val['label'], 'checked'=>(($val['visible']<0)?0:1), 'enabled'=>$val['enabled'], 'position'=>$val['position']); }*/ -// Definition of fields for list // Extra fields -if (!empty($extrafields->attributes['projet_task']['label']) && is_array($extrafields->attributes['projet_task']['label']) && count($extrafields->attributes['projet_task']['label']) > 0) { - foreach ($extrafields->attributes['projet_task']['label'] as $key => $val) { - if (!empty($extrafields->attributes['projet_task']['list'][$key])) { - $arrayfields["efpt.".$key] = array('label' => $extrafields->attributes['projet_task']['label'][$key], 'checked' => (($extrafields->attributes['projet_task']['list'][$key] < 0) ? '0' : '1'), 'position' => $extrafields->attributes['projet_task']['pos'][$key], 'enabled' => (string) (int) (abs((int) $extrafields->attributes['projet_task']['list'][$key]) != 3 && $extrafields->attributes['projet_task']['perms'][$key])); - } - } -} +$extrafieldsobjectkey = 'projet_task'; +$extrafieldsobjectprefix = 'efpt.'; +include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_array_fields.tpl.php'; + $arrayfields = dol_sort_array($arrayfields, 'position'); $search_array_options = array(); diff --git a/htdocs/projet/activity/perweek.php b/htdocs/projet/activity/perweek.php index 121ec925bc5..29959dee4cd 100644 --- a/htdocs/projet/activity/perweek.php +++ b/htdocs/projet/activity/perweek.php @@ -147,15 +147,11 @@ $arrayfields['timeconsumed'] = array('label' => 'TimeConsumed', 'checked' => '1' // If $val['visible']==0, then we never show the field if (!empty($val['visible'])) $arrayfields['t.'.$key]=array('label'=>$val['label'], 'checked'=>(($val['visible']<0)?0:1), 'enabled'=>$val['enabled'], 'position'=>$val['position']); }*/ -// Definition of fields for list // Extra fields -if (!empty($extrafields->attributes['projet_task']['label']) && is_array($extrafields->attributes['projet_task']['label']) && count($extrafields->attributes['projet_task']['label']) > 0) { - foreach ($extrafields->attributes['projet_task']['label'] as $key => $val) { - if (!empty($extrafields->attributes['projet_task']['list'][$key])) { - $arrayfields["efpt.".$key] = array('label' => $extrafields->attributes['projet_task']['label'][$key], 'checked' => (($extrafields->attributes['projet_task']['list'][$key] < 0) ? '0' : '1'), 'position' => $extrafields->attributes['projet_task']['pos'][$key], 'enabled' => (string) (int) (abs((int) $extrafields->attributes['projet_task']['list'][$key]) != 3 && $extrafields->attributes['projet_task']['perms'][$key])); - } - } -} +$extrafieldsobjectkey = 'projet_task'; +$extrafieldsobjectprefix = 'efpt.'; +include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_array_fields.tpl.php'; + $arrayfields = dol_sort_array($arrayfields, 'position'); $search_array_options = array(); diff --git a/htdocs/public/ticket/list.php b/htdocs/public/ticket/list.php index a4a3b4697a9..93ee14407ed 100644 --- a/htdocs/public/ticket/list.php +++ b/htdocs/public/ticket/list.php @@ -278,15 +278,8 @@ if ($action == "view_ticketlist") { } // Extra fields - if (isset($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) { - foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) { - if ($extrafields->attributes[$object->table_element]['type'][$key] != 'separate') { - $enabled = abs((int) dol_eval($extrafields->attributes[$object->table_element]['list'][$key], 1, 1, '2')); - $enabled = (($enabled == 0 || $enabled == 3) ? 0 : $enabled); - $arrayfields["ef.".$key] = array('label' => $extrafields->attributes[$object->table_element]['label'][$key], 'checked' => ($extrafields->attributes[$object->table_element]['list'][$key] < 0) ? '0' : '1', 'position' => $extrafields->attributes[$object->table_element]['pos'][$key], 'enabled' => (string) (int) ($enabled && $extrafields->attributes[$object->table_element]['perms'][$key])); - } - } - } + include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_array_fields.tpl.php'; + if (!empty($search_subject)) { $filter['t.subject'] = $search_subject; $param .= '&search_subject='.urlencode($search_subject);