<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># --
# Kernel/System/Service/PreferencesDB.pm - some user functions
# Copyright (C) 2001-2014 OTRS AG, http://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::System::Service::PreferencesDB;

use strict;
use warnings;

use Kernel::System::CacheInternal;

sub new {
    my ( $Type, %Param ) = @_;

    # allocate new hash for object
    my $Self = {};
    bless( $Self, $Type );

    # check needed objects
    for (qw(DBObject ConfigObject LogObject EncodeObject MainObject)) {
        $Self-&gt;{$_} = $Param{$_} || die "Got no $_!";
    }

    $Self-&gt;{CacheInternalObject} = Kernel::System::CacheInternal-&gt;new(
        %{$Self},
        Type =&gt; 'ServicePreferencesDB',
        TTL  =&gt; 60 * 60 * 24 * 20,
    );

    # preferences table data
    $Self-&gt;{PreferencesTable}          = 'service_preferences';
    $Self-&gt;{PreferencesTableKey}       = 'preferences_key';
    $Self-&gt;{PreferencesTableValue}     = 'preferences_value';
    $Self-&gt;{PreferencesTableServiceID} = 'service_id';

    # create cache prefix
    $Self-&gt;{CachePrefix} = 'ServicePreferencesDB'
        . $Self-&gt;{PreferencesTable}
        . $Self-&gt;{PreferencesTableKey}
        . $Self-&gt;{PreferencesTableValue}
        . $Self-&gt;{PreferencesTableServiceID};

    return $Self;
}

sub ServicePreferencesSet {
    my ( $Self, %Param ) = @_;

    # check needed stuff
    for (qw(ServiceID Key Value)) {
        if ( !defined $Param{$_} ) {
            $Self-&gt;{LogObject}-&gt;Log( Priority =&gt; 'error', Message =&gt; "Need $_!" );
            return;
        }
    }

    # delete old data
    return if !$Self-&gt;{DBObject}-&gt;Do(
        SQL =&gt; "DELETE FROM $Self-&gt;{PreferencesTable} WHERE "
            . "$Self-&gt;{PreferencesTableServiceID} = ? AND $Self-&gt;{PreferencesTableKey} = ?",
        Bind =&gt; [ \$Param{ServiceID}, \$Param{Key} ],
    );

    # insert new data
    return if !$Self-&gt;{DBObject}-&gt;Do(
        SQL =&gt; "INSERT INTO $Self-&gt;{PreferencesTable} ($Self-&gt;{PreferencesTableServiceID}, "
            . " $Self-&gt;{PreferencesTableKey}, $Self-&gt;{PreferencesTableValue}) "
            . " VALUES (?, ?, ?)",
        Bind =&gt; [ \$Param{ServiceID}, \$Param{Key}, \$Param{Value} ],
    );

    # delete cache
    $Self-&gt;{CacheInternalObject}-&gt;Delete(
        Key =&gt; $Self-&gt;{CachePrefix} . $Param{ServiceID},
    );

    return 1;
}

sub ServicePreferencesGet {
    my ( $Self, %Param ) = @_;

    # check needed stuff
    for (qw(ServiceID)) {
        if ( !$Param{$_} ) {
            $Self-&gt;{LogObject}-&gt;Log( Priority =&gt; 'error', Message =&gt; "Need $_!" );
            return;
        }
    }

    # check if service preferences are available
    return if !$Self-&gt;{ConfigObject}-&gt;Get('ServicePreferences');

    # read cache
    my $Cache = $Self-&gt;{CacheInternalObject}-&gt;Get(
        Key =&gt; $Self-&gt;{CachePrefix} . $Param{ServiceID},
    );
    return %{$Cache} if $Cache;

    # get preferences
    return if !$Self-&gt;{DBObject}-&gt;Prepare(
        SQL =&gt; "SELECT $Self-&gt;{PreferencesTableKey}, $Self-&gt;{PreferencesTableValue} "
            . " FROM $Self-&gt;{PreferencesTable} WHERE $Self-&gt;{PreferencesTableServiceID} = ?",
        Bind =&gt; [ \$Param{ServiceID} ],
    );

    my %Data;
    while ( my @Row = $Self-&gt;{DBObject}-&gt;FetchrowArray() ) {
        $Data{ $Row[0] } = $Row[1];
    }

    # set cache
    $Self-&gt;{CacheInternalObject}-&gt;Set(
        Key   =&gt; $Self-&gt;{CachePrefix} . $Param{ServiceID},
        Value =&gt; \%Data,
    );

    return %Data;
}

1;
</pre></body></html>