1# --
2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
3# --
4# This software comes with ABSOLUTELY NO WARRANTY. For details, see
5# the enclosed file COPYING for license information (GPL). If you
6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
7# --
8
9package Kernel::System::CustomerCompany;
10
11use strict;
12use warnings;
13
14use Kernel::System::VariableCheck qw(:all);
15
16use parent qw(Kernel::System::EventHandler);
17
18our @ObjectDependencies = (
19    'Kernel::Config',
20    'Kernel::System::DynamicField',
21    'Kernel::System::DynamicField::Backend',
22    'Kernel::System::Encode',
23    'Kernel::System::Log',
24    'Kernel::System::Main',
25    'Kernel::System::ReferenceData',
26    'Kernel::System::Valid',
27);
28
29=head1 NAME
30
31Kernel::System::CustomerCompany - customer company lib
32
33=head1 DESCRIPTION
34
35All Customer functions. E.g. to add and update customer companies.
36
37=head1 PUBLIC INTERFACE
38
39=head2 new()
40
41Don't use the constructor directly, use the ObjectManager instead:
42
43    my $CustomerCompanyObject = $Kernel::OM->Get('Kernel::System::CustomerCompany');
44
45=cut
46
47sub new {
48    my ( $Type, %Param ) = @_;
49
50    # allocate new hash for object
51    my $Self = {};
52    bless( $Self, $Type );
53
54    # get needed objects
55    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
56    my $MainObject   = $Kernel::OM->Get('Kernel::System::Main');
57
58    # load customer company backend modules
59    SOURCE:
60    for my $Count ( '', 1 .. 10 ) {
61
62        next SOURCE if !$ConfigObject->Get("CustomerCompany$Count");
63
64        my $GenericModule = $ConfigObject->Get("CustomerCompany$Count")->{Module}
65            || 'Kernel::System::CustomerCompany::DB';
66        if ( !$MainObject->Require($GenericModule) ) {
67            $MainObject->Die("Can't load backend module $GenericModule! $@");
68        }
69        $Self->{"CustomerCompany$Count"} = $GenericModule->new(
70            Count              => $Count,
71            CustomerCompanyMap => $ConfigObject->Get("CustomerCompany$Count"),
72        );
73    }
74
75    # init of event handler
76    $Self->EventHandlerInit(
77        Config => 'CustomerCompany::EventModulePost',
78    );
79
80    return $Self;
81}
82
83=head2 CustomerCompanyAdd()
84
85add a new customer company
86
87    my $ID = $CustomerCompanyObject->CustomerCompanyAdd(
88        CustomerID              => 'example.com',
89        CustomerCompanyName     => 'New Customer Inc.',
90        CustomerCompanyStreet   => '5201 Blue Lagoon Drive',
91        CustomerCompanyZIP      => '33126',
92        CustomerCompanyCity     => 'Miami',
93        CustomerCompanyCountry  => 'USA',
94        CustomerCompanyURL      => 'http://www.example.org',
95        CustomerCompanyComment  => 'some comment',
96        ValidID                 => 1,
97        UserID                  => 123,
98    );
99
100NOTE: Actual fields accepted by this API call may differ based on
101CustomerCompany mapping in your system configuration.
102
103=cut
104
105sub CustomerCompanyAdd {
106    my ( $Self, %Param ) = @_;
107
108    # check data source
109    if ( !$Param{Source} ) {
110        $Param{Source} = 'CustomerCompany';
111    }
112
113    # check needed stuff
114    for (qw(CustomerID UserID)) {
115        if ( !$Param{$_} ) {
116            $Kernel::OM->Get('Kernel::System::Log')->Log(
117                Priority => 'error',
118                Message  => "Need $_!"
119            );
120            return;
121        }
122    }
123
124    # store customer company data
125    my $Result = $Self->{ $Param{Source} }->CustomerCompanyAdd(%Param);
126    return if !$Result;
127
128    # trigger event
129    $Self->EventHandler(
130        Event => 'CustomerCompanyAdd',
131        Data  => {
132            CustomerID => $Param{CustomerID},
133            NewData    => \%Param,
134        },
135        UserID => $Param{UserID},
136    );
137
138    return $Result;
139}
140
141=head2 CustomerCompanyGet()
142
143get customer company attributes
144
145    my %CustomerCompany = $CustomerCompanyObject->CustomerCompanyGet(
146        CustomerID => 123,
147    );
148
149Returns:
150
151    %CustomerCompany = (
152        'CustomerCompanyName'    => 'Customer Inc.',
153        'CustomerID'             => 'example.com',
154        'CustomerCompanyStreet'  => '5201 Blue Lagoon Drive',
155        'CustomerCompanyZIP'     => '33126',
156        'CustomerCompanyCity'    => 'Miami',
157        'CustomerCompanyCountry' => 'United States',
158        'CustomerCompanyURL'     => 'http://example.com',
159        'CustomerCompanyComment' => 'Some Comments',
160        'ValidID'                => '1',
161        'CreateTime'             => '2010-10-04 16:35:49',
162        'ChangeTime'             => '2010-10-04 16:36:12',
163    );
164
165NOTE: Actual fields returned by this API call may differ based on
166CustomerCompany mapping in your system configuration.
167
168=cut
169
170sub CustomerCompanyGet {
171    my ( $Self, %Param ) = @_;
172
173    # check needed stuff
174    if ( !$Param{CustomerID} ) {
175        $Kernel::OM->Get('Kernel::System::Log')->Log(
176            Priority => 'error',
177            Message  => "Need CustomerID!"
178        );
179        return;
180    }
181
182    # Fetch dynamic field configurations for CustomerCompany.
183    my $DynamicFieldConfigs = $Kernel::OM->Get('Kernel::System::DynamicField')->DynamicFieldListGet(
184        ObjectType => 'CustomerCompany',
185        Valid      => 1,
186    );
187
188    my %DynamicFieldLookup = map { $_->{Name} => $_ } @{$DynamicFieldConfigs};
189
190    # get needed objects
191    my $ConfigObject              = $Kernel::OM->Get('Kernel::Config');
192    my $DynamicFieldBackendObject = $Kernel::OM->Get('Kernel::System::DynamicField::Backend');
193
194    SOURCE:
195    for my $Count ( '', 1 .. 10 ) {
196
197        next SOURCE if !$Self->{"CustomerCompany$Count"};
198
199        my %Company = $Self->{"CustomerCompany$Count"}->CustomerCompanyGet( %Param, );
200        next SOURCE if !%Company;
201
202        # fetch dynamic field values
203        if ( IsArrayRefWithData( $Self->{"CustomerCompany$Count"}->{CustomerCompanyMap}->{Map} ) ) {
204            CUSTOMERCOMPANYFIELD:
205            for my $CustomerCompanyField ( @{ $Self->{"CustomerCompany$Count"}->{CustomerCompanyMap}->{Map} } ) {
206                next CUSTOMERCOMPANYFIELD if $CustomerCompanyField->[5] ne 'dynamic_field';
207                next CUSTOMERCOMPANYFIELD if !$DynamicFieldLookup{ $CustomerCompanyField->[2] };
208
209                my $Value = $DynamicFieldBackendObject->ValueGet(
210                    DynamicFieldConfig => $DynamicFieldLookup{ $CustomerCompanyField->[2] },
211                    ObjectName         => $Company{CustomerID},
212                );
213
214                $Company{ $CustomerCompanyField->[0] } = $Value;
215            }
216        }
217
218        # return company data
219        return (
220            %Company,
221            Source => "CustomerCompany$Count",
222            Config => $ConfigObject->Get("CustomerCompany$Count"),
223        );
224    }
225
226    return;
227}
228
229=head2 CustomerCompanyUpdate()
230
231update customer company attributes
232
233    $CustomerCompanyObject->CustomerCompanyUpdate(
234        CustomerCompanyID       => 'oldexample.com', # required for CustomerCompanyID-update
235        CustomerID              => 'example.com',
236        CustomerCompanyName     => 'New Customer Inc.',
237        CustomerCompanyStreet   => '5201 Blue Lagoon Drive',
238        CustomerCompanyZIP      => '33126',
239        CustomerCompanyLocation => 'Miami',
240        CustomerCompanyCountry  => 'USA',
241        CustomerCompanyURL      => 'http://example.com',
242        CustomerCompanyComment  => 'some comment',
243        ValidID                 => 1,
244        UserID                  => 123,
245    );
246
247=cut
248
249sub CustomerCompanyUpdate {
250    my ( $Self, %Param ) = @_;
251
252    $Param{CustomerCompanyID} ||= $Param{CustomerID};
253
254    # check needed stuff
255    if ( !$Param{CustomerCompanyID} ) {
256        $Kernel::OM->Get('Kernel::System::Log')->Log(
257            Priority => 'error',
258            Message  => "Need CustomerCompanyID or CustomerID!"
259        );
260        return;
261    }
262
263    # check if company exists
264    my %Company = $Self->CustomerCompanyGet( CustomerID => $Param{CustomerCompanyID} );
265    if ( !%Company ) {
266        $Kernel::OM->Get('Kernel::System::Log')->Log(
267            Priority => 'error',
268            Message  => "No such company '$Param{CustomerCompanyID}'!",
269        );
270        return;
271    }
272
273    my $Result = $Self->{ $Company{Source} }->CustomerCompanyUpdate(%Param);
274    return if !$Result;
275
276    # trigger event
277    $Self->EventHandler(
278        Event => 'CustomerCompanyUpdate',
279        Data  => {
280            CustomerID    => $Param{CustomerID},
281            OldCustomerID => $Param{CustomerCompanyID},
282            NewData       => \%Param,
283            OldData       => \%Company,
284        },
285        UserID => $Param{UserID},
286    );
287    return $Result;
288}
289
290=head2 CustomerCompanySourceList()
291
292return customer company source list
293
294    my %List = $CustomerCompanyObject->CustomerCompanySourceList(
295        ReadOnly => 0 # optional, 1 returns only RO backends, 0 returns writable, if not passed returns all backends
296    );
297
298=cut
299
300sub CustomerCompanySourceList {
301    my ( $Self, %Param ) = @_;
302
303    # get config object
304    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
305
306    my %Data;
307    SOURCE:
308    for my $Count ( '', 1 .. 10 ) {
309
310        next SOURCE if !$ConfigObject->Get("CustomerCompany$Count");
311
312        if ( defined $Param{ReadOnly} ) {
313            my $BackendConfig = $ConfigObject->Get("CustomerCompany$Count");
314            if ( $Param{ReadOnly} ) {
315                next SOURCE if !$BackendConfig->{ReadOnly};
316            }
317            else {
318                next SOURCE if $BackendConfig->{ReadOnly};
319            }
320        }
321
322        $Data{"CustomerCompany$Count"} = $ConfigObject->Get("CustomerCompany$Count")->{Name}
323            || "No Name $Count";
324    }
325
326    return %Data;
327}
328
329=head2 CustomerCompanyList()
330
331get list of customer companies.
332
333    my %List = $CustomerCompanyObject->CustomerCompanyList();
334
335    my %List = $CustomerCompanyObject->CustomerCompanyList(
336        Valid => 0,
337        Limit => 0,     # optional, override configured search result limit (0 means unlimited)
338    );
339
340    my %List = $CustomerCompanyObject->CustomerCompanyList(
341        Search => 'somecompany',
342    );
343
344Returns:
345
346    %List = {
347        'example.com' => 'example.com Customer Inc.',
348        'acme.com'    => 'acme.com Acme, Inc.'
349    };
350
351=cut
352
353sub CustomerCompanyList {
354    my ( $Self, %Param ) = @_;
355
356    # Get dynamic field object.
357    my $DynamicFieldObject = $Kernel::OM->Get('Kernel::System::DynamicField');
358
359    my $DynamicFieldConfigs = $DynamicFieldObject->DynamicFieldListGet(
360        ObjectType => 'CustomerCompany',
361        Valid      => 1,
362    );
363
364    my %DynamicFieldLookup = map { $_->{Name} => $_ } @{$DynamicFieldConfigs};
365
366    # Get dynamic field backend object.
367    my $DynamicFieldBackendObject = $Kernel::OM->Get('Kernel::System::DynamicField::Backend');
368
369    my %Data;
370    SOURCE:
371    for my $Count ( '', 1 .. 10 ) {
372
373        next SOURCE if !$Self->{"CustomerCompany$Count"};
374
375        # search dynamic field values, if configured
376        my $Map = $Self->{"CustomerCompany$Count"}->{CustomerCompanyMap}->{Map};
377        if ( IsArrayRefWithData($Map) ) {
378
379            # fetch dynamic field names that are configured in Map
380            # only these will be considered for any other search config
381            # [ 'DynamicField_Name_Y', undef, 'Name_Y', 0, 0, 'dynamic_field', undef, 0,],
382            my %DynamicFieldNames = map { $_->[2] => 1 } grep { $_->[5] eq 'dynamic_field' } @{$Map};
383
384            if (%DynamicFieldNames) {
385                my $FoundDynamicFieldObjectIDs;
386                my $SearchFields;
387                my $SearchParam;
388
389                # check which of the dynamic fields configured in Map are also
390                # configured in SearchFields
391
392                # param Search
393                if ( defined $Param{Search} && length $Param{Search} ) {
394                    $SearchFields
395                        = $Self->{"CustomerCompany$Count"}->{CustomerCompanyMap}->{CustomerCompanySearchFields};
396                    $SearchParam = $Param{Search};
397                }
398
399                # search dynamic field values
400                if ( IsArrayRefWithData($SearchFields) ) {
401                    my @SearchDynamicFieldNames = grep { exists $DynamicFieldNames{$_} } @{$SearchFields};
402
403                    my %FoundDynamicFieldObjectIDs;
404                    FIELDNAME:
405                    for my $FieldName (@SearchDynamicFieldNames) {
406
407                        my $DynamicFieldConfig = $DynamicFieldLookup{$FieldName};
408
409                        next FIELDNAME if !IsHashRefWithData($DynamicFieldConfig);
410
411                        my $DynamicFieldValues = $DynamicFieldBackendObject->ValueSearch(
412                            DynamicFieldConfig => $DynamicFieldConfig,
413                            Search             => $SearchParam,
414                        );
415
416                        if ( IsArrayRefWithData($DynamicFieldValues) ) {
417                            for my $DynamicFieldValue ( @{$DynamicFieldValues} ) {
418                                $FoundDynamicFieldObjectIDs{ $DynamicFieldValue->{ObjectID} } = 1;
419                            }
420                        }
421                    }
422
423                    $FoundDynamicFieldObjectIDs = [ keys %FoundDynamicFieldObjectIDs ];
424                }
425
426                # execute backend search for found object IDs
427                # this data is being merged with the following CustomerCompanyList call
428                if ( IsArrayRefWithData($FoundDynamicFieldObjectIDs) ) {
429
430                    my $ObjectNames = $DynamicFieldObject->ObjectMappingGet(
431                        ObjectID   => $FoundDynamicFieldObjectIDs,
432                        ObjectType => 'CustomerCompany',
433                    );
434
435                    my %SearchParam = %Param;
436                    delete $SearchParam{Search};
437                    my %CompanyList = $Self->{"CustomerCompany$Count"}->CustomerCompanyList(%SearchParam);
438
439                    OBJECTNAME:
440                    for my $ObjectName ( values %{$ObjectNames} ) {
441                        next OBJECTNAME if exists $Data{$ObjectName};
442
443                        if ( IsHashRefWithData( \%CompanyList ) && exists $CompanyList{$ObjectName} ) {
444                            %Data = (
445                                $ObjectName => $CompanyList{$ObjectName},
446                                %Data
447                            );
448                        }
449                    }
450                }
451            }
452        }
453
454        # get company list result of backend and merge it
455        my %SubData = $Self->{"CustomerCompany$Count"}->CustomerCompanyList(%Param);
456        %Data = ( %Data, %SubData );
457    }
458    return %Data;
459}
460
461=head2 CustomerCompanySearchDetail()
462
463To find customer companies in the system.
464
465The search criteria are logically AND connected.
466When a list is passed as criteria, the individual members are OR connected.
467When an undef or a reference to an empty array is passed, then the search criteria
468is ignored.
469
470Returns either a list, as an arrayref, or a count of found customer company ids.
471The count of results is returned when the parameter C<Result = 'COUNT'> is passed.
472
473    my $CustomerCompanyIDsRef = $CustomerCompanyObject->CustomerCompanySearchDetail(
474
475        # all search fields possible which are defined in CustomerCompany::EnhancedSearchFields
476        CustomerID          => 'example*',                                  # (optional)
477        CustomerCompanyName => 'Name*',                                     # (optional)
478
479        # array parameters are used with logical OR operator (all values are possible which
480        are defined in the config selection hash for the field)
481        CustomerCompanyCountry => [ 'Austria', 'Germany', ],                # (optional)
482
483        # DynamicFields
484        #   At least one operator must be specified. Operators will be connected with AND,
485        #       values in an operator with OR.
486        #   You can also pass more than one argument to an operator: ['value1', 'value2']
487        DynamicField_FieldNameX => {
488            Equals            => 123,
489            Like              => 'value*',                # "equals" operator with wildcard support
490            GreaterThan       => '2001-01-01 01:01:01',
491            GreaterThanEquals => '2001-01-01 01:01:01',
492            SmallerThan       => '2002-02-02 02:02:02',
493            SmallerThanEquals => '2002-02-02 02:02:02',
494        }
495
496        OrderBy => [ 'CustomerID', 'CustomerCompanyCountry' ],              # (optional)
497        # ignored if the result type is 'COUNT'
498        # default: [ 'CustomerID' ]
499        # (all search fields possible which are defined in
500        CustomerCompany::EnhancedSearchFields)
501
502        # Additional information for OrderBy:
503        # The OrderByDirection can be specified for each OrderBy attribute.
504        # The pairing is made by the array indices.
505
506        OrderByDirection => [ 'Down', 'Up' ],                               # (optional)
507        # ignored if the result type is 'COUNT'
508        # (Down | Up) Default: [ 'Down' ]
509
510        Result => 'ARRAY' || 'COUNT',                                       # (optional)
511        # default: ARRAY, returns an array of change ids
512        # COUNT returns a scalar with the number of found changes
513
514        Limit => 100,                                                       # (optional)
515        # ignored if the result type is 'COUNT'
516    );
517
518Returns:
519
520Result: 'ARRAY'
521
522    @CustomerIDs = ( 1, 2, 3 );
523
524Result: 'COUNT'
525
526    $CustomerIDs = 10;
527
528=cut
529
530sub CustomerCompanySearchDetail {
531    my ( $Self, %Param ) = @_;
532
533    # Get all general search fields (without a restriction to a source).
534    my @AllSearchFields = $Self->CustomerCompanySearchFields();
535
536    # Generate a hash with the customer company sources which must be searched.
537    my %SearchCustomerCompanySources;
538
539    SOURCE:
540    for my $Count ( '', 1 .. 10 ) {
541        next SOURCE if !$Self->{"CustomerCompany$Count"};
542
543        # Get the search fields for the current source.
544        my @SourceSearchFields = $Self->CustomerCompanySearchFields(
545            Source => "CustomerCompany$Count",
546        );
547        my %LookupSourceSearchFields = map { $_->{Name} => 1 } @SourceSearchFields;
548
549        # Check if all search param exists in the search fields from the current source.
550        SEARCHFIELD:
551        for my $SearchField (@AllSearchFields) {
552
553            next SEARCHFIELD if !$Param{ $SearchField->{Name} };
554
555            next SOURCE if !$LookupSourceSearchFields{ $SearchField->{Name} };
556        }
557        $SearchCustomerCompanySources{"CustomerCompany$Count"} = \@SourceSearchFields;
558    }
559
560    # Set the default behaviour for the return type.
561    $Param{Result} ||= 'ARRAY';
562
563    if ( $Param{Result} eq 'COUNT' ) {
564
565        my $IDsCount = 0;
566
567        SOURCE:
568        for my $Source ( sort keys %SearchCustomerCompanySources ) {
569            next SOURCE if !$Self->{$Source};
570
571            my $SubIDsCount = $Self->{$Source}->CustomerCompanySearchDetail(
572                %Param,
573                SearchFields => $SearchCustomerCompanySources{$Source},
574            );
575
576            return if !defined $SubIDsCount;
577
578            $IDsCount += $SubIDsCount || 0;
579        }
580        return $IDsCount;
581    }
582    else {
583
584        my @IDs;
585
586        my $ResultCount = 0;
587
588        SOURCE:
589        for my $Source ( sort keys %SearchCustomerCompanySources ) {
590            next SOURCE if !$Self->{$Source};
591
592            my $SubIDs = $Self->{$Source}->CustomerCompanySearchDetail(
593                %Param,
594                SearchFields => $SearchCustomerCompanySources{$Source},
595            );
596
597            return if !defined $SubIDs;
598
599            next SOURCE if !IsArrayRefWithData($SubIDs);
600
601            push @IDs, @{$SubIDs};
602
603            $ResultCount++;
604        }
605
606        # If we have more then one search results from diffrent sources, we need a resorting
607        #   and splice (for the limit) because of the merged single results.
608        if ( $ResultCount > 1 ) {
609
610            my @CustomerCompanyataList;
611
612            for my $ID (@IDs) {
613
614                my %CustomerCompanyData = $Self->CustomerCompanyGet(
615                    CustomerID => $ID,
616                );
617                push @CustomerCompanyataList, \%CustomerCompanyData;
618            }
619
620            my $OrderBy = 'CustomerID';
621            if ( IsArrayRefWithData( $Param{OrderBy} ) ) {
622                $OrderBy = $Param{OrderBy}->[0];
623            }
624
625            if ( IsArrayRefWithData( $Param{OrderByDirection} ) && $Param{OrderByDirection}->[0] eq 'Up' ) {
626                @CustomerCompanyataList
627                    = sort { lc( $a->{$OrderBy} ) cmp lc( $b->{$OrderBy} ) } @CustomerCompanyataList;
628            }
629            else {
630                @CustomerCompanyataList
631                    = sort { lc( $b->{$OrderBy} ) cmp lc( $a->{$OrderBy} ) } @CustomerCompanyataList;
632            }
633
634            if ( $Param{Limit} && scalar @CustomerCompanyataList > $Param{Limit} ) {
635                splice @CustomerCompanyataList, $Param{Limit};
636            }
637
638            @IDs = map { $_->{CustomerID} } @CustomerCompanyataList;
639        }
640
641        return \@IDs;
642    }
643}
644
645=head2 CustomerCompanySearchFields()
646
647Get a list of defined search fields (optional only the relevant fields for the given source).
648
649    my @SeachFields = $CustomerCompanyObject->CustomerCompanySearchFields(
650        Source => 'CustomerCompany', # optional, but important in the CustomerCompanySearchDetail to get the right database fields
651    );
652
653Returns an array of hash references.
654
655    @SeachFields = (
656        {
657            Name  => 'CustomerID',
658            Label => 'CustomerID',
659            Type  => 'Input',
660        },
661        {
662            Name           => 'CustomerCompanyCountry',
663            Label          => 'Country',
664            Type           => 'Selection',
665            SelectionsData => {
666                'Germany'        => 'Germany',
667                'United Kingdom' => 'United Kingdom',
668                'United States'  => 'United States',
669                ...
670            },
671        },
672        {
673            Name          => 'DynamicField_Branch',
674            Label         => '',
675            Type          => 'DynamicField',
676            DatabaseField => 'Branch',
677        },
678    );
679
680=cut
681
682sub CustomerCompanySearchFields {
683    my ( $Self, %Param ) = @_;
684
685    # Get the search fields from all customer company maps (merge from all maps together).
686    my @SearchFields;
687
688    my %SearchFieldsExists;
689
690    SOURCE:
691    for my $Count ( '', 1 .. 10 ) {
692        next SOURCE if !$Self->{"CustomerCompany$Count"};
693        next SOURCE if $Param{Source} && $Param{Source} ne "CustomerCompany$Count";
694
695        ENTRY:
696        for my $Entry ( @{ $Self->{"CustomerCompany$Count"}->{CustomerCompanyMap}->{Map} } ) {
697
698            my $SearchFieldName = $Entry->[0];
699
700            next ENTRY if $SearchFieldsExists{$SearchFieldName};
701
702            # Remeber the already collected search field name.
703            $SearchFieldsExists{$SearchFieldName} = 1;
704
705            my %FieldConfig = $Self->GetFieldConfig(
706                FieldName => $SearchFieldName,
707                Source    => $Param{Source},     # to get the right database field for the given source
708            );
709
710            next SEARCHFIELDNAME if !%FieldConfig;
711
712            my %SearchFieldData = (
713                %FieldConfig,
714                Name => $SearchFieldName,
715            );
716
717            my %SelectionsData = $Self->GetFieldSelections(
718                FieldName => $SearchFieldName,
719            );
720
721            if ( $SearchFieldData{StorageType} eq 'dynamic_field' ) {
722                $SearchFieldData{Type} = 'DynamicField';
723            }
724            elsif (%SelectionsData) {
725                $SearchFieldData{Type}           = 'Selection';
726                $SearchFieldData{SelectionsData} = \%SelectionsData;
727            }
728            else {
729                $SearchFieldData{Type} = 'Input';
730            }
731
732            push @SearchFields, \%SearchFieldData;
733        }
734    }
735
736    return @SearchFields;
737}
738
739=head2 GetFieldConfig()
740
741This function collect some field config information from the customer user map.
742
743    my %FieldConfig = $CustomerCompanyObject->GetFieldConfig(
744        FieldName => 'CustomerCompanyName',
745        Source    => 'CustomerCompany', # optional
746    );
747
748Returns some field config information:
749
750    my %FieldConfig = (
751        Label         => 'Name',
752        DatabaseField => 'name',
753        StorageType   => 'var',
754    );
755
756=cut
757
758sub GetFieldConfig {
759    my ( $Self, %Param ) = @_;
760
761    if ( !$Param{FieldName} ) {
762        $Kernel::OM->Get('Kernel::System::Log')->Log(
763            Priority => 'error',
764            Message  => "Need FieldName!"
765        );
766        return;
767    }
768
769    SOURCE:
770    for my $Count ( '', 1 .. 10 ) {
771        next SOURCE if !$Self->{"CustomerCompany$Count"};
772        next SOURCE if $Param{Source} && $Param{Source} ne "CustomerCompany$Count";
773
774        # Search the right field and return the label.
775        ENTRY:
776        for my $Entry ( @{ $Self->{"CustomerCompany$Count"}->{CustomerCompanyMap}->{Map} } ) {
777            next ENTRY if $Param{FieldName} ne $Entry->[0];
778
779            my %FieldConfig = (
780                Label         => $Entry->[1],
781                DatabaseField => $Entry->[2],
782                StorageType   => $Entry->[5],
783            );
784
785            return %FieldConfig;
786        }
787    }
788
789    return;
790}
791
792=head2 GetFieldSelections()
793
794This function collect the selections for the given field name, if the field has some selections.
795
796    my %SelectionsData = $CustomerCompanyObject->GetFieldSelections(
797        FieldName => 'CustomerCompanyCountry',
798    );
799
800Returns the selections for the given field name (merged from all sources) or a empty hash:
801
802    my %SelectionData = (
803        'Germany'        => 'Germany',
804        'United Kingdom' => 'United Kingdom',
805        'United States'  => 'United States',
806    );
807
808=cut
809
810sub GetFieldSelections {
811    my ( $Self, %Param ) = @_;
812
813    # check needed stuff
814    if ( !$Param{FieldName} ) {
815        $Kernel::OM->Get('Kernel::System::Log')->Log(
816            Priority => 'error',
817            Message  => "Need FieldName!"
818        );
819        return;
820    }
821
822    my %SelectionsData;
823
824    SOURCE:
825    for my $Count ( '', 1 .. 10 ) {
826        next SOURCE if !$Self->{"CustomerCompany$Count"};
827        next SOURCE if !$Self->{"CustomerCompany$Count"}->{CustomerCompanyMap}->{Selections}->{ $Param{FieldName} };
828
829        %SelectionsData = (
830            %SelectionsData,
831            %{ $Self->{"CustomerCompany$Count"}->{CustomerCompanyMap}->{Selections}->{ $Param{FieldName} } }
832        );
833    }
834
835    # Make sure the encoding stamp is set.
836    for my $Key ( sort keys %SelectionsData ) {
837        $SelectionsData{$Key} = $Kernel::OM->Get('Kernel::System::Encode')->EncodeInput( $SelectionsData{$Key} );
838    }
839
840    # Default handling for field 'CustomerCompanyCountry'.
841    if ( !%SelectionsData && $Param{FieldName} =~ /^CustomerCompanyCountry/i ) {
842        %SelectionsData = %{ $Kernel::OM->Get('Kernel::System::ReferenceData')->CountryList() };
843    }
844
845    # Default handling for field 'ValidID'.
846    elsif ( !%SelectionsData && $Param{FieldName} =~ /^ValidID/i ) {
847        %SelectionsData = $Kernel::OM->Get('Kernel::System::Valid')->ValidList();
848    }
849
850    return %SelectionsData;
851}
852
853sub DESTROY {
854    my $Self = shift;
855
856    # execute all transaction events
857    $Self->EventHandlerTransaction();
858
859    return 1;
860}
861
8621;
863
864=head1 TERMS AND CONDITIONS
865
866This software is part of the OTRS project (L<https://otrs.org/>).
867
868This software comes with ABSOLUTELY NO WARRANTY. For details, see
869the enclosed file COPYING for license information (GPL). If you
870did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
871
872=cut
873