From f2751f562d87ed401022979d7252eecf3656ab5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20H=C3=A4rter?= Date: Tue, 15 Oct 2024 08:11:36 +0200 Subject: [PATCH] Issue #3400: Added field type filtering to dynamic field list functions. --- Kernel/System/DynamicField.pm | 67 +++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/Kernel/System/DynamicField.pm b/Kernel/System/DynamicField.pm index b71b101f62..570d35d055 100644 --- a/Kernel/System/DynamicField.pm +++ b/Kernel/System/DynamicField.pm @@ -595,9 +595,17 @@ or Valid => 0, # optional, defaults to 1 # object type (optional) as STRING or as ARRAYREF + # The special object type 'All' places no restriction on the object type when + # it is passed as a single string. ObjectType => 'Ticket', ObjectType => ['Ticket', 'Article'], + # field type (optional) as STRING or as ARRAYREF + # The special field type 'All' places no restriction on the field type when + # it is passed as a single string. + FieldType => 'Dropdown', + FieldType => ['Dropdown', 'Text'], + ResultType => 'HASH', # optional, 'ARRAY' or 'HASH', defaults to 'ARRAY' FieldFilter => { # optional, only active fields (non 0) will be returned @@ -667,6 +675,15 @@ sub DynamicFieldList { $ObjectType = $Param{ObjectType}; } + # set cache key object type component depending on the FieldType parameter + my $FieldType = 'All'; + if ( IsArrayRefWithData( $Param{FieldType} ) ) { + $FieldType = join '_', sort @{ $Param{FieldType} }; + } + elsif ( IsStringWithData( $Param{FieldType} ) ) { + $FieldType = $Param{FieldType}; + } + # set cache key namespace component depending on the Namespace parameter my $Namespace = 'All'; if ( IsStringWithData( $Param{Namespace} ) ) { @@ -683,6 +700,8 @@ sub DynamicFieldList { . $Valid . '::ObjectType::' . $ObjectType + . '::FieldType::' + . $FieldType . '::Namespace::' . $Namespace . '::ResultType::' @@ -781,6 +800,20 @@ sub DynamicFieldList { } + if ( $Param{FieldType} ) { + + # differentiate whether we have an field type string or array + if ( IsStringWithData( $Param{FieldType} ) && $Param{FieldType} ne 'All' ) { + push @WhereClauses, 'field_type = ?'; + push @Bind, \$Param{FieldType}; + } + elsif ( IsArrayRefWithData( $Param{FieldType} ) ) { + push @WhereClauses, 'field_type IN (' . join( ', ', map {'?'} $Param{FieldType}->@* ) . ')'; + push @Bind, map { \$_ } $Param{FieldType}->@*; + } + + } + if ( $Param{Namespace} && $Param{Namespace} ne 'All' ) { # select all fields without a namespace @@ -904,12 +937,18 @@ Additional restrictions can be applied: my $List = $DynamicFieldObject->DynamicFieldListGet( Valid => 0, # optional, defaults to 1 - # object type (optional) as STRING or as ARRAYREF + # object type (optional) as STRING or as ARRAYREF # The special object type 'All' places no restriction on the object type when # it is passed as a single string. ObjectType => 'Ticket', ObjectType => ['Ticket', 'Article'], + # field type (optional) as STRING or as ARRAYREF + # The special field type 'All' places no restriction on the field type when + # it is passed as a single string. + FieldType => 'Dropdown', + FieldType => ['Dropdown', 'Text'], + # optional, filter by name of the dynamic field # only the fields where there the field name has a true value are returned FieldFilter => { @@ -973,12 +1012,25 @@ sub DynamicFieldListGet { $ObjectTypeCacheKey = $Param{ObjectType}; } + # set cache key field type component depending on the FieldType parameter + my @FieldTypes; + my $FieldTypeCacheKey = 'All'; + if ( IsArrayRefWithData( $Param{FieldType} ) ) { + @FieldTypes = sort $Param{FieldType}->@*; + $FieldTypeCacheKey = join '_', @FieldTypes; + } + elsif ( IsStringWithData( $Param{FieldType} ) ) { + @FieldTypes = $Param{FieldType} eq 'All' ? () : ( $Param{FieldType} ); + $FieldTypeCacheKey = $Param{FieldType}; + } + # get cache object my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache'); my $CacheKey = join '::', 'DynamicFieldListGet', Valid => $Valid, - ObjectType => $ObjectTypeCacheKey; + ObjectType => $ObjectTypeCacheKey, + FieldType => $FieldTypeCacheKey; my $Cache = $CacheObject->Get( Type => 'DynamicField', Key => $CacheKey, @@ -1038,6 +1090,17 @@ sub DynamicFieldListGet { push @Binds, $QueryCondition{Values}->@*; } + if (@FieldTypes) { + my %QueryCondition = $DBObject->QueryInCondition( + Key => 'field_type', + Values => \@FieldTypes, + BindMode => 1, + ); + + push @WhereClauses, $QueryCondition{SQL}; + push @Binds, $QueryCondition{Values}->@*; + } + my $WhereSQL = ''; if (@WhereClauses) { $WhereSQL = 'WHERE ' . join ' AND ', @WhereClauses;