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::SysConfig::ValueType::TimeZone;
10## nofilter(TidyAll::Plugin::OTRS::Perl::LayoutObject)
11
12use strict;
13use warnings;
14
15use Kernel::System::VariableCheck qw(:all);
16
17use parent qw(Kernel::System::SysConfig::BaseValueType);
18
19our @ObjectDependencies = (
20    'Kernel::Language',
21    'Kernel::Output::HTML::Layout',
22    'Kernel::System::Log',
23);
24
25=head1 NAME
26
27Kernel::System::SysConfig::ValueType::TimeZone - System configuration time-zone value type backed.
28
29=head1 PUBLIC INTERFACE
30
31=head2 new()
32
33Create an object. Do not use it directly, instead use:
34
35    use Kernel::System::ObjectManager;
36    local $Kernel::OM = Kernel::System::ObjectManager->new();
37    my $ValueTypeObject = $Kernel::OM->Get('Kernel::System::SysConfig::ValueType::TimeZone');
38
39=cut
40
41sub new {
42    my ( $Type, %Param ) = @_;
43
44    # Allocate new hash for object.
45    my $Self = {};
46    bless( $Self, $Type );
47
48    return $Self;
49}
50
51=head2 SettingEffectiveValueCheck()
52
53Check if provided EffectiveValue matches structure defined in XMLContentParsed.
54
55    my %Result = $ValueTypeObject->SettingEffectiveValueCheck(
56        XMLContentParsed => {
57            Value => [
58                {
59                    'Item' => [
60                        {
61                            'Content'   => 'UTC',
62                            'ValueType' => 'TimeZone',
63                        },
64                    ],
65                },
66            ],
67        },
68        EffectiveValue => 'UTC',
69    );
70
71Result:
72    %Result = (
73        EffectiveValue => 'UTC',    # Note for common TimeZone EffectiveValue is not changed.
74        Success => 1,
75        Error   => undef,
76    );
77
78=cut
79
80sub SettingEffectiveValueCheck {
81    my ( $Self, %Param ) = @_;
82
83    for my $Needed (qw(XMLContentParsed)) {
84        if ( !$Param{$Needed} ) {
85            $Kernel::OM->Get('Kernel::System::Log')->Log(
86                Priority => 'error',
87                Message  => "Need $Needed!"
88            );
89
90            return;
91        }
92    }
93
94    my %Result = (
95        Success => 0,
96    );
97
98    # Data should be scalar.
99    if ( ref $Param{EffectiveValue} ) {
100        $Result{Error} = 'EffectiveValue for TimeZone must be a scalar!';
101        return %Result;
102    }
103
104    my $DateTimeObject = $Kernel::OM->Create(
105        'Kernel::System::DateTime',
106    );
107    my $TimeZones = $DateTimeObject->TimeZoneList();
108    if ( !grep { $Param{EffectiveValue} eq $_ } @{$TimeZones} ) {
109        $Result{Error} = "$Param{EffectiveValue} is not valid time zone!";
110        return %Result;
111    }
112
113    $Result{Success}        = 1;
114    $Result{EffectiveValue} = $Param{EffectiveValue};
115
116    return %Result;
117}
118
119=head2 SettingRender()
120
121Extracts the effective value from a XML parsed setting.
122
123    my $SettingHTML = $ValueTypeObject->SettingRender(
124        Name           => 'SettingName',
125        EffectiveValue => 'UTC',            # (optional)
126        DefaultValue   => 'UTC',            # (optional)
127        Class          => 'My class'        # (optional)
128        RW             => 1,                # (optional) Allow editing. Default 0.
129        Item           => [                 # (optional) XML parsed item
130            {
131                'ValueType' => 'TimeZone',
132                'Content' => 'UTC',
133            },
134        ],
135        IsArray  => 1,                      # (optional) Item is part of the array
136        IsHash   => 1,                      # (optional) Item is part of the hash
137        IDSuffix => 1,                      # (optional) Suffix will be added to the element ID
138        SkipEffectiveValueCheck => 1,       # (optional) If enabled, system will not perform effective value check.
139                                            #            Default: 1.
140    );
141
142Returns:
143
144    $SettingHTML = '<div class "Field"...</div>';
145
146=cut
147
148sub SettingRender {
149    my ( $Self, %Param ) = @_;
150
151    if ( !defined $Param{Name} ) {
152        $Kernel::OM->Get('Kernel::System::Log')->Log(
153            Priority => 'error',
154            Message  => "Need Name",
155        );
156        return;
157    }
158
159    $Param{Class}        //= '';
160    $Param{DefaultValue} //= '';
161    $Param{IDSuffix}     //= '';
162
163    my $LanguageObject = $Kernel::OM->Get('Kernel::Language');
164
165    my $DateTimeObject = $Kernel::OM->Create(
166        'Kernel::System::DateTime',
167    );
168    my $TimeZones = $DateTimeObject->TimeZoneList();
169
170    my $EffectiveValue = $Param{EffectiveValue};
171    if (
172        !defined $EffectiveValue
173        && $Param{Item}
174        && $Param{Item}->[0]->{Content}
175        )
176    {
177        $EffectiveValue = $Param{Item}->[0]->{Content};
178    }
179
180    # When displaying diff between current and old value, it can happen that value is missing
181    #    since it was renamed, or removed. In this case, we need to add this "old" value also.
182    if (
183        $EffectiveValue
184        && !grep { $_ eq $EffectiveValue } @{$TimeZones}
185        )
186    {
187        push @{$TimeZones}, $EffectiveValue;
188    }
189
190    my %EffectiveValueCheck = (
191        Success => 1,
192    );
193
194    if ( !$Param{SkipEffectiveValueCheck} ) {
195        %EffectiveValueCheck = $Self->SettingEffectiveValueCheck(
196            EffectiveValue   => $EffectiveValue,
197            XMLContentParsed => {
198                Value => [
199                    {
200                        Item => $Param{Item},
201                    },
202                ],
203            },
204        );
205    }
206
207    my $HTML = "<div class='SettingContent'>\n";
208    $HTML .= $Kernel::OM->Get('Kernel::Output::HTML::Layout')->BuildSelection(
209        Data          => $TimeZones,
210        Name          => $Param{Name},
211        ID            => $Param{Name} . $Param{IDSuffix},
212        Disabled      => $Param{RW} ? 0 : 1,
213        SelectedValue => $EffectiveValue,
214        Title         => $Param{Name},
215        OptionTitle   => 1,
216        Class         => "$Param{Class} Modernize",
217    );
218
219    if ( !$EffectiveValueCheck{Success} ) {
220        my $Message = $LanguageObject->Translate("Value is not correct! Please, consider updating this field.");
221
222        $HTML .= $Param{IsValid} ? "<div class='BadEffectiveValue'>\n" : "<div>\n";
223        $HTML .= "<p>* $Message</p>\n";
224        $HTML .= "</div>\n";
225    }
226
227    $HTML .= "</div>\n";
228
229    if ( !$Param{IsArray} && !$Param{IsHash} ) {
230        my $DefaultText = $LanguageObject->Translate('Default');
231
232        $HTML .= <<"EOF";
233                                <div class=\"WidgetMessage Bottom\">
234                                    $DefaultText: $Param{DefaultValue}
235                                </div>
236EOF
237    }
238
239    return $HTML;
240}
241
242=head2 AddItem()
243
244Generate HTML for new array/hash item.
245
246    my $HTML = $ValueTypeObject->AddItem(
247        Name           => 'SettingName',    (required) Name
248        DefaultItem    => {                 (required) DefaultItem hash
249            'ValueType' => 'TimeZone',
250            'Content' => 'UTC'
251        },
252        IDSuffix       => '_Array1',        (optional) IDSuffix is needed for arrays and hashes.
253    );
254
255Returns:
256
257    $HTML = '<select class="Modernize" id="SettingName" name="SettingName" title="SettingName">
258        ...
259        </select>';
260
261=cut
262
263sub AddItem {
264    my ( $Self, %Param ) = @_;
265
266    # Check needed stuff.
267    for my $Needed (qw(Name DefaultItem)) {
268        if ( !$Param{$Needed} ) {
269            $Kernel::OM->Get('Kernel::System::Log')->Log(
270                Priority => 'error',
271                Message  => "Need $Needed!",
272            );
273            return;
274        }
275    }
276
277    $Param{Class}        //= '';
278    $Param{DefaultValue} //= '';
279    $Param{IDSuffix}     //= '';
280
281    my $DateTimeObject = $Kernel::OM->Create(
282        'Kernel::System::DateTime',
283    );
284    my $TimeZones = $DateTimeObject->TimeZoneList();
285
286    my $Result = $Kernel::OM->Get('Kernel::Output::HTML::Layout')->BuildSelection(
287        Data          => $TimeZones,
288        Name          => $Param{Name},
289        ID            => $Param{Name} . $Param{IDSuffix},
290        SelectedValue => $Param{DefaultItem}->{Content},
291        Title         => $Param{Name},
292        OptionTitle   => 1,
293        Class         => "$Param{Class} Modernize Entry",
294    );
295
296    return $Result;
297}
298
2991;
300
301=head1 TERMS AND CONDITIONS
302
303This software is part of the OTRS project (L<https://otrs.org/>).
304
305This software comes with ABSOLUTELY NO WARRANTY. For details, see
306the enclosed file COPYING for license information (GPL). If you
307did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
308
309=cut
310