View | Details | Raw Unified | Return to bug 11998
Collapse All | Expand All

(-)a/C4/Context.pm (-34 / +10 lines)
Lines 18-23 package C4::Context; Link Here
18
18
19
use strict;
19
use strict;
20
use warnings;
20
use warnings;
21
22
use Koha::Config::SystemPreference;
23
21
use vars qw($VERSION $AUTOLOAD $context @context_stack $servers $memcached $ismemcached);
24
use vars qw($VERSION $AUTOLOAD $context @context_stack $servers $memcached $ismemcached);
22
BEGIN {
25
BEGIN {
23
	if ($ENV{'HTTP_USER_AGENT'})	{
26
	if ($ENV{'HTTP_USER_AGENT'})	{
Lines 629-668 preference. Link Here
629
632
630
sub set_preference {
633
sub set_preference {
631
    my ( $self, $var, $value, $expl, $type, $options ) = @_;
634
    my ( $self, $var, $value, $expl, $type, $options ) = @_;
632
    $var = lc($var);
633
635
634
    my $dbh = C4::Context->dbh or return 0;
636
    $var = lc $var;
635
637
636
    my $db_type = $dbh->selectrow_array(
638
    my $set = Koha::Config::SystemPreference::set( $var, $value, $expl, $type, $options );
637
        "SELECT type FROM systempreferences WHERE variable = ?",
638
        {}, $var );
639
639
640
    $value = 0 if ( $db_type && $db_type eq 'YesNo' && $value eq '' );
640
    $syspref_cache->set_in_cache("syspref_$var", $value)
641
        if $use_syspref_cache and $set;
641
642
642
    my ( $query, @params, $logtype );
643
    return $set;
643
    if ( !defined($db_type) ) {
644
        $query = '
645
INSERT INTO systempreferences
646
    ( variable, value, explanation, type, options )
647
VALUES( ?, ?, ?, ?, ? )';
648
        @params = ( $var, $value, $expl, $type, $options );
649
        $logtype = 'ADD';
650
    }
651
    else {
652
        $query = '
653
INSERT INTO systempreferences
654
    ( variable, value )
655
VALUES( ?, ? )
656
ON DUPLICATE KEY UPDATE value = VALUES(value)';
657
        @params = ( $var, $value );
658
        $logtype = 'MODIFY';
659
    }
660
    my $sth = $dbh->prepare($query);
661
    if ( $sth->execute(@params) ) {
662
        $syspref_cache->set_in_cache("syspref_$var", $value) if $use_syspref_cache;
663
        logaction( $dbh, userenv(), 'SYSTEMPREFERENCE', $logtype, undef, $var . " | " . $value );
664
    }
665
    $sth->finish;
666
}
644
}
667
645
668
=head2 delete_preference
646
=head2 delete_preference
Lines 681-695 sub delete_preference { Link Here
681
    # We want to log the previous value
659
    # We want to log the previous value
682
    my $val = C4::Context->preference($var);
660
    my $val = C4::Context->preference($var);
683
    my $dbh = C4::Context->dbh or die "Unable to talk to the database";
661
    my $dbh = C4::Context->dbh or die "Unable to talk to the database";
684
    my $sth = $dbh->prepare("DELETE FROM systempreferences WHERE variable=?");
662
685
    my $res = $sth->execute($var);
663
    if ( Koha::Config::SystemPreference::delete($var) ) {
686
    if ($res) {
687
        $syspref_cache->clear_from_cache("syspref_$var") if $use_syspref_cache;
664
        $syspref_cache->clear_from_cache("syspref_$var") if $use_syspref_cache;
688
        logaction( $dbh, userenv(), 'SYSTEMPREFERENCE', 'DELETE', undef,
665
        logaction( $dbh, userenv(), 'SYSTEMPREFERENCE', 'DELETE', undef,
689
            $var . " | " . ( $val // 'undefined' ) );
666
            $var . " | " . ( $val // 'undefined' ) );
690
        return 1;
667
        return 1;
691
    }
668
    }
692
    warn "Unable to delete syspref: " . $sth->errstr;
693
    return 0;
669
    return 0;
694
}
670
}
695
# AUTOLOAD
671
# AUTOLOAD
(-)a/Koha/Config/SystemPreference.pm (-1 / +50 lines)
Line 0 Link Here
0
- 
1
package Koha::Config::SystemPreference;
2
3
use Koha::Database;
4
use C4::Log qw( logaction );
5
6
sub set {
7
    my ( $variable, $value, $expl, $type, $options ) = @_;
8
9
    my $dbh = C4::Context->dbh or return 0;
10
11
    my $pref = Koha::Database->new->schema->resultset('Systempreference')->find($variable);
12
13
    if ( $pref ) {
14
        $value = 0 if ( $pref->type && $pref->type eq 'YesNo' && $pref->type eq '' );
15
    }
16
17
    $pref = Koha::Database->new->schema->resultset('Systempreference')->update_or_new(
18
        {
19
            variable => $variable,
20
            value => $value,
21
            explanation => $expl,
22
            type => $type,
23
            options => $options,
24
        }
25
    );
26
27
    if ( $pref->in_storage ) {
28
        logaction('SYSTEMPREFERENCE', 'MODIFY', undef, $variable . ' | ' . $value);
29
        return 1;
30
    }
31
32
    my $inserted  = $pref->insert;
33
    logaction('SYSTEMPREFERENCE', 'ADD', undef, $variable . ' | ' . $value) if $inserted;
34
35
    return $inserted;
36
}
37
38
sub delete {
39
    my ( $variable ) = @_;
40
41
    my $deleted = Koha::Database->new->schema->resultset('Systempreference')->find($variable)->delete;
42
43
    return unless $deleted;
44
45
    logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
46
47
    return 1
48
}
49
50
1

Return to bug 11998