Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #3400: Added field type filtering to dynamic field list functions. #3872

Open
wants to merge 1 commit into
base: rel-11_0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions Kernel/System/DynamicField.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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} ) ) {
Expand All @@ -683,6 +700,8 @@ sub DynamicFieldList {
. $Valid
. '::ObjectType::'
. $ObjectType
. '::FieldType::'
. $FieldType
. '::Namespace::'
. $Namespace
. '::ResultType::'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down