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::Checkbox; 10 11use strict; 12use warnings; 13 14use Kernel::System::VariableCheck qw(:all); 15 16use parent qw(Kernel::System::SysConfig::BaseValueType); 17 18our @ObjectDependencies = ( 19 'Kernel::Language', 20 'Kernel::System::Log', 21); 22 23=head1 NAME 24 25Kernel::System::SysConfig::ValueType::Checkbox - System configuration check-box value type backed. 26 27=head1 PUBLIC INTERFACE 28 29=head2 new() 30 31Create an object. Do not use it directly, instead use: 32 33 use Kernel::System::ObjectManager; 34 local $Kernel::OM = Kernel::System::ObjectManager->new(); 35 my $ValueTypeObject = $Kernel::OM->Get('Kernel::System::SysConfig::ValueType::Checkbox'); 36 37=cut 38 39sub new { 40 my ( $Type, %Param ) = @_; 41 42 # Allocate new hash for object. 43 my $Self = {}; 44 bless( $Self, $Type ); 45 46 return $Self; 47} 48 49=head2 SettingEffectiveValueCheck() 50 51Check if provided EffectiveValue matches structure defined in XMLContentParsed. 52 53 my %Result = $ValueTypeObject->SettingEffectiveValueCheck( 54 XMLContentParsed => { 55 Value => [ 56 { 57 'Item' => [ 58 { 59 'Content' => '1', 60 'ValueType' => 'Checkbox', 61 }, 62 ], 63 }, 64 ], 65 }, 66 EffectiveValue => '1', 67 ); 68 69Result: 70 %Result = ( 71 EffectiveValue => 'open', # Note for Checkbox ValueTypes EffectiveValue is not changed. 72 Success => 1, 73 Error => undef, 74 ); 75 76=cut 77 78sub SettingEffectiveValueCheck { 79 my ( $Self, %Param ) = @_; 80 81 for my $Needed (qw(XMLContentParsed)) { 82 if ( !$Param{$Needed} ) { 83 $Kernel::OM->Get('Kernel::System::Log')->Log( 84 Priority => 'error', 85 Message => "Need $Needed!" 86 ); 87 88 return; 89 } 90 } 91 92 my %Result = ( 93 Success => 0, 94 ); 95 96 # Data should be scalar. 97 if ( ref $Param{EffectiveValue} ) { 98 $Result{Error} = 'EffectiveValue for Checkbox must be a scalar!'; 99 return %Result; 100 } 101 102 if ( $Param{EffectiveValue} && $Param{EffectiveValue} ne '1' ) { 103 $Result{Error} = 'EffectiveValue must be 0 or 1 for Checkbox!'; 104 return %Result; 105 } 106 107 $Result{Success} = 1; 108 $Result{EffectiveValue} = $Param{EffectiveValue}; 109 110 return %Result; 111} 112 113=head2 SettingRender() 114 115Extracts the effective value from a XML parsed setting. 116 117 my $SettingHTML = $ValueTypeObject->SettingRender( 118 Name => 'SettingName', 119 DefaultID => 123, # (required) 120 EffectiveValue => 'Product 6', 121 DefaultValue => 'Product 5', # (optional) 122 Class => 'My class' # (optional) 123 RW => 1, # (optional) Allow editing. Default 0. 124 Item => [ # (optional) XML parsed item 125 { 126 'ValueType' => 'Checkbox', 127 'Content' => '1', 128 'ValueRegex' => '', 129 }, 130 ], 131 IsArray => 1, # (optional) Item is part of the array 132 IsHash => 1, # (optional) Item is part of the hash 133 IDSuffix => 1, # (optional) Suffix will be added to the element ID 134 SkipEffectiveValueCheck => 1, # (optional) If enabled, system will not perform effective value check. 135 # Default: 1. 136 ); 137 138Returns: 139 140 $SettingHTML = '<div class "Field"...</div>'; 141 142=cut 143 144sub SettingRender { 145 my ( $Self, %Param ) = @_; 146 147 for my $Needed (qw(Name EffectiveValue)) { 148 if ( !defined $Param{$Needed} ) { 149 $Kernel::OM->Get('Kernel::System::Log')->Log( 150 Priority => 'error', 151 Message => "Need $Needed", 152 ); 153 return; 154 } 155 } 156 157 $Param{Class} //= ''; 158 $Param{DefaultValue} //= ''; 159 160 my $IDSuffix = $Param{IDSuffix} || ''; 161 162 my $LanguageObject = $Kernel::OM->Get('Kernel::Language'); 163 164 my $EffectiveValue = $Param{EffectiveValue}; 165 166 if ( 167 !defined $Param{EffectiveValue} 168 && $Param{Item}->[0]->{Content} 169 ) 170 { 171 $EffectiveValue = $Param{Item}->[0]->{Content}; 172 } 173 174 my %EffectiveValueCheck = ( 175 Success => 1, 176 ); 177 178 if ( !$Param{SkipEffectiveValueCheck} ) { 179 %EffectiveValueCheck = $Self->SettingEffectiveValueCheck( 180 EffectiveValue => $EffectiveValue, 181 XMLContentParsed => { 182 Value => [ 183 { 184 Item => $Param{Item}, 185 }, 186 ], 187 }, 188 ); 189 } 190 191 my $HTML = "<div class='SettingContent'>\n"; 192 $HTML 193 .= "<input class=\"$Param{Class}\" type=\"checkbox\" id=\"Checkbox_$Param{Name}$IDSuffix\" "; 194 $HTML .= "value=\"1\" "; 195 196 if ($EffectiveValue) { 197 $HTML .= "checked='checked' "; 198 } 199 200 if ( !$Param{RW} ) { 201 $HTML .= "disabled='disabled' "; 202 } 203 $HTML .= ">"; 204 205 # Add hidden input field with checkbox value 206 $HTML .= "<input type='hidden' name='$Param{Name}' id=\"$Param{Name}$IDSuffix\" "; 207 if ($EffectiveValue) { 208 $HTML .= "value='1' "; 209 } 210 else { 211 $HTML .= "value='0' "; 212 } 213 $HTML .= "/>\n"; 214 215 $HTML .= "<label for='Checkbox_$Param{Name}$IDSuffix' class='CheckboxLabel'>" 216 . $LanguageObject->Translate('Enabled') 217 . "</label>\n"; 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 = $Kernel::OM->Get('Kernel::Language')->Translate('Default'); 231 my $DefaultValueText = $Param{DefaultValue} 232 ? 233 $LanguageObject->Translate('Enabled') 234 : 235 $LanguageObject->Translate('Disabled'); 236 237 $HTML .= <<"EOF"; 238 <div class=\"WidgetMessage Bottom\"> 239 $DefaultText: $DefaultValueText 240 </div> 241EOF 242 } 243 244 return $HTML; 245} 246 247sub EffectiveValueCalculate { 248 my ( $Self, %Param ) = @_; 249 250 # Check needed stuff. 251 for my $Needed (qw(Name)) { 252 if ( !$Param{$Needed} ) { 253 $Kernel::OM->Get('Kernel::System::Log')->Log( 254 Priority => 'error', 255 Message => "Need $Needed!", 256 ); 257 return; 258 } 259 } 260 261 my $Result = '0'; 262 263 if ( $Param{ $Param{Name} } ) { 264 $Result = $Param{ $Param{Name} }; 265 } 266 267 return $Result; 268} 269 270=head2 AddItem() 271 272Generate HTML for new array/hash item. 273 274 my $HTML = $ValueTypeObject->AddItem( 275 Name => 'SettingName', (required) Name 276 DefaultItem => { (optional) DefaultItem hash, if available 277 Content => 'Value', 278 ValueType => 'Checkbox', 279 }, 280 IDSuffix => '_Array1', (optional) IDSuffix is needed for arrays and hashes. 281 ); 282 283Returns: 284 285 $HTML = "<input type="checkbox" checked="checked" name="SettingName" value="1" id="SettingName_Array1"> 286 <input type="hidden" value="1" name="SettingName"> 287 <label for="SettingName_Array1" class='CheckboxLabel'> 288 Enabled 289 </label>"; 290 291=cut 292 293sub AddItem { 294 my ( $Self, %Param ) = @_; 295 296 # Check needed stuff. 297 for my $Needed (qw(Name)) { 298 if ( !$Param{$Needed} ) { 299 $Kernel::OM->Get('Kernel::System::Log')->Log( 300 Priority => 'error', 301 Message => "Need $Needed!", 302 ); 303 return; 304 } 305 } 306 307 my $IDSuffix = $Param{IDSuffix} || ''; 308 my $Checked; 309 if ( $Param{DefaultItem} ) { 310 $Checked = $Param{DefaultItem}->{Content}; 311 } 312 313 my $Enabled = $Kernel::OM->Get('Kernel::Language')->Translate('Enabled'); 314 315 my $Result = "<input type=\"checkbox\" id=\"Checkbox_$Param{Name}$IDSuffix\" value=\"1\" "; 316 317 if ($Checked) { 318 $Result .= "checked='checked' "; 319 } 320 321 $Result .= "/><input type=\"hidden\" class=\"Entry\" id=\"$Param{Name}$IDSuffix\" name=\"$Param{Name}\" value=\""; 322 323 if ($Checked) { 324 $Result .= "1"; 325 } 326 else { 327 $Result .= "0"; 328 } 329 330 $Result .= "\" /><label for='Checkbox_$Param{Name}$IDSuffix' class='CheckboxLabel'>$Enabled</label>"; 331 332 return $Result; 333} 334 3351; 336 337=head1 TERMS AND CONDITIONS 338 339This software is part of the OTRS project (L<https://otrs.org/>). 340 341This software comes with ABSOLUTELY NO WARRANTY. For details, see 342the enclosed file COPYING for license information (GPL). If you 343did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>. 344 345=cut 346