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::DynamicField::Driver::Base;
10
11use strict;
12use warnings;
13
14use Kernel::System::VariableCheck qw(:all);
15
16our @ObjectDependencies = (
17    'Kernel::System::DynamicFieldValue',
18    'Kernel::System::Log',
19);
20
21=head1 NAME
22
23Kernel::System::DynamicField::Driver::Base - common fields backend functions
24
25=head1 PUBLIC INTERFACE
26
27=cut
28
29sub ValueIsDifferent {
30    my ( $Self, %Param ) = @_;
31
32    # special cases where the values are different but they should be reported as equals
33    return if !defined $Param{Value1} && ( defined $Param{Value2} && $Param{Value2} eq '' );
34    return if !defined $Param{Value2} && ( defined $Param{Value1} && $Param{Value1} eq '' );
35
36    # compare the results
37    return DataIsDifferent(
38        Data1 => \$Param{Value1},
39        Data2 => \$Param{Value2}
40    );
41}
42
43sub ValueDelete {
44    my ( $Self, %Param ) = @_;
45
46    my $Success = $Kernel::OM->Get('Kernel::System::DynamicFieldValue')->ValueDelete(
47        FieldID  => $Param{DynamicFieldConfig}->{ID},
48        ObjectID => $Param{ObjectID},
49        UserID   => $Param{UserID},
50    );
51
52    return $Success;
53}
54
55sub AllValuesDelete {
56    my ( $Self, %Param ) = @_;
57
58    my $Success = $Kernel::OM->Get('Kernel::System::DynamicFieldValue')->AllValuesDelete(
59        FieldID => $Param{DynamicFieldConfig}->{ID},
60        UserID  => $Param{UserID},
61    );
62
63    return $Success;
64}
65
66sub HasBehavior {
67    my ( $Self, %Param ) = @_;
68
69    # return fail if Behaviors hash does not exists
70    return if !IsHashRefWithData( $Self->{Behaviors} );
71
72    # return success if the dynamic field has the expected behavior
73    return IsPositiveInteger( $Self->{Behaviors}->{ $Param{Behavior} } );
74}
75
76sub SearchFieldPreferences {
77    my ( $Self, %Param ) = @_;
78
79    my @Preferences = (
80        {
81            Type        => '',
82            LabelSuffix => '',
83        },
84    );
85
86    return \@Preferences;
87}
88
89=head2 EditLabelRender()
90
91creates the label HTML to be used in edit masks.
92
93    my $LabelHTML = $BackendObject->EditLabelRender(
94        DynamicFieldConfig => $DynamicFieldConfig,      # complete config of the DynamicField
95        FieldName          => 'TheField',               # the value to be set on the 'for' attribute
96        AdditionalText     => 'Between'                 # other text to be placed next to FieldName
97        Mandatory          => 1,                        # 0 or 1,
98    );
99
100=cut
101
102sub EditLabelRender {
103    my ( $Self, %Param ) = @_;
104
105    # check needed stuff
106    for my $Needed (qw(DynamicFieldConfig FieldName)) {
107        if ( !$Param{$Needed} ) {
108            $Kernel::OM->Get('Kernel::System::Log')->Log(
109                Priority => 'error',
110                Message  => "Need $Needed!"
111            );
112            return;
113        }
114    }
115
116    # check DynamicFieldConfig (general)
117    if ( !IsHashRefWithData( $Param{DynamicFieldConfig} ) ) {
118        $Kernel::OM->Get('Kernel::System::Log')->Log(
119            Priority => 'error',
120            Message  => "The field configuration is invalid",
121        );
122        return;
123    }
124
125    # check DynamicFieldConfig (internally)
126    for my $Needed (qw(Label)) {
127        if ( !$Param{DynamicFieldConfig}->{$Needed} ) {
128            $Kernel::OM->Get('Kernel::System::Log')->Log(
129                Priority => 'error',
130                Message  => "Need $Needed in DynamicFieldConfig!"
131            );
132            return;
133        }
134    }
135
136    my $Name      = $Param{FieldName};
137    my $LabelText = $Param{DynamicFieldConfig}->{Label};
138
139    my $LabelID    = 'Label' . $Param{FieldName};
140    my $HTMLString = '';
141
142    if ( $Param{Mandatory} ) {
143
144        # opening tag
145        $HTMLString = <<"EOF";
146<label id="$LabelID" for="$Name" class="Mandatory">
147    <span class="Marker">*</span>
148EOF
149    }
150    else {
151
152        # opening tag
153        $HTMLString = <<"EOF";
154<label id="$LabelID" for="$Name">
155EOF
156    }
157
158    # text
159    $HTMLString .= $Param{LayoutObject}->Ascii2Html(
160        Text => $Param{LayoutObject}->{LanguageObject}->Translate("$LabelText")
161    );
162    if ( $Param{AdditionalText} ) {
163        $HTMLString .= " (";
164        $HTMLString .= $Param{LayoutObject}->Ascii2Html(
165            Text => $Param{LayoutObject}->{LanguageObject}->Translate("$Param{AdditionalText}")
166        );
167        $HTMLString .= ")";
168    }
169    $HTMLString .= ":\n";
170
171    # closing tag
172    $HTMLString .= <<"EOF";
173</label>
174EOF
175
176    return $HTMLString;
177}
178
179=head2 ValueSearch()
180
181Searches/fetches dynamic field value.
182
183    my $Value = $BackendObject->ValueSearch(
184        DynamicFieldConfig => $DynamicFieldConfig,      # complete config of the DynamicField
185        Search             => 'test',
186    );
187
188    Returns [
189        {
190            ID            => 437,
191            FieldID       => 23,
192            ObjectID      => 133,
193            ValueText     => 'some text',
194            ValueDateTime => '1977-12-12 12:00:00',
195            ValueInt      => 123,
196        },
197    ];
198
199=cut
200
201sub ValueSearch {
202    my ( $Self, %Param ) = @_;
203
204    # check mandatory parameters
205    if ( !IsHashRefWithData( $Param{DynamicFieldConfig} ) ) {
206        $Kernel::OM->Get('Kernel::System::Log')->Log(
207            Priority => 'error',
208            Message  => "Need DynamicFieldConfig!"
209        );
210        return;
211    }
212
213    my $SearchTerm = $Param{Search};
214    my $Operator   = 'Equals';
215    if ( $Self->HasBehavior( Behavior => 'IsLikeOperatorCapable' ) ) {
216        $SearchTerm = '%' . $Param{Search} . '%';
217        $Operator   = 'Like';
218    }
219
220    my $SearchSQL = $Self->SearchSQLGet(
221        DynamicFieldConfig => $Param{DynamicFieldConfig},
222        TableAlias         => 'dynamic_field_value',
223        SearchTerm         => $SearchTerm,
224        Operator           => $Operator,
225    );
226
227    if ( !defined $SearchSQL || !length $SearchSQL ) {
228        $Kernel::OM->Get('Kernel::System::Log')->Log(
229            Priority => 'error',
230            Message  => "Error generating search SQL!"
231        );
232        return;
233    }
234
235    my $Values = $Kernel::OM->Get('Kernel::System::DynamicFieldValue')->ValueSearch(
236        FieldID   => $Param{DynamicFieldConfig}->{ID},
237        Search    => $Param{Search},
238        SearchSQL => $SearchSQL,
239    );
240
241    return $Values;
242}
243
2441;
245
246=head1 TERMS AND CONDITIONS
247
248This software is part of the OTRS project (L<https://otrs.org/>).
249
250This software comes with ABSOLUTELY NO WARRANTY. For details, see
251the enclosed file COPYING for license information (GPL). If you
252did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
253
254=cut
255