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

(-)a/C4/Context.pm (-87 / +3 lines)
Lines 41-49 use File::Spec; Link Here
41
use List::MoreUtils qw(any);
41
use List::MoreUtils qw(any);
42
42
43
use Koha::Caches;
43
use Koha::Caches;
44
use Koha::Config::SysPref;
45
use Koha::Config::SysPrefs;
46
use Koha::Config;
44
use Koha::Config;
45
use Koha::Database;
47
use Koha;
46
use Koha;
48
47
49
=head1 NAME
48
=head1 NAME
Lines 300-306 with this method. Link Here
300
299
301
=cut
300
=cut
302
301
303
my $use_syspref_cache = 1;
302
our $use_syspref_cache = 1;
304
sub preference {
303
sub preference {
305
    my $self = shift;
304
    my $self = shift;
306
    my $var  = shift;    # The system preference to return
305
    my $var  = shift;    # The system preference to return
Lines 316-324 sub preference { Link Here
316
        return $cached_var if defined $cached_var;
315
        return $cached_var if defined $cached_var;
317
    }
316
    }
318
317
319
    my $syspref;
318
    my ($value) = $self->dbh()->selectrow_array("SELECT value FROM systempreferences WHERE variable like '$var'");
320
    eval { $syspref = Koha::Config::SysPrefs->find( lc $var ) };
321
    my $value = $syspref ? $syspref->value() : undef;
322
319
323
    if ( $use_syspref_cache ) {
320
    if ( $use_syspref_cache ) {
324
        my $syspref_cache = Koha::Caches->get_instance('syspref');
321
        my $syspref_cache = Koha::Caches->get_instance('syspref');
Lines 413-499 sub clear_syspref_cache { Link Here
413
    $syspref_cache->flush_all;
410
    $syspref_cache->flush_all;
414
}
411
}
415
412
416
=head2 set_preference
417
418
  C4::Context->set_preference( $variable, $value, [ $explanation, $type, $options ] );
419
420
This updates a preference's value both in the systempreferences table and in
421
the sysprefs cache. If the optional parameters are provided, then the query
422
becomes a create. It won't update the parameters (except value) for an existing
423
preference.
424
425
=cut
426
427
sub set_preference {
428
    my ( $self, $variable, $value, $explanation, $type, $options ) = @_;
429
430
    my $variable_case = $variable;
431
    $variable = lc $variable;
432
433
    my $syspref = Koha::Config::SysPrefs->find($variable);
434
    $type =
435
        $type    ? $type
436
      : $syspref ? $syspref->type
437
      :            undef;
438
439
    $value = 0 if ( $type && $type eq 'YesNo' && $value eq '' );
440
441
    # force explicit protocol on OPACBaseURL
442
    if ( $variable eq 'opacbaseurl' && $value && substr( $value, 0, 4 ) !~ /http/ ) {
443
        $value = 'http://' . $value;
444
    }
445
446
    if ($syspref) {
447
        $syspref->set(
448
            {   ( defined $value ? ( value       => $value )       : () ),
449
                ( $explanation   ? ( explanation => $explanation ) : () ),
450
                ( $type          ? ( type        => $type )        : () ),
451
                ( $options       ? ( options     => $options )     : () ),
452
            }
453
        )->store;
454
    } else {
455
        $syspref = Koha::Config::SysPref->new(
456
            {   variable    => $variable_case,
457
                value       => $value,
458
                explanation => $explanation || undef,
459
                type        => $type,
460
                options     => $options || undef,
461
            }
462
        )->store();
463
    }
464
465
    if ( $use_syspref_cache ) {
466
        my $syspref_cache = Koha::Caches->get_instance('syspref');
467
        $syspref_cache->set_in_cache( "syspref_$variable", $value );
468
    }
469
470
    return $syspref;
471
}
472
473
=head2 delete_preference
474
475
    C4::Context->delete_preference( $variable );
476
477
This deletes a system preference from the database. Returns a true value on
478
success. Failure means there was an issue with the database, not that there
479
was no syspref of the name.
480
481
=cut
482
483
sub delete_preference {
484
    my ( $self, $var ) = @_;
485
486
    if ( Koha::Config::SysPrefs->find( $var )->delete ) {
487
        if ( $use_syspref_cache ) {
488
            my $syspref_cache = Koha::Caches->get_instance('syspref');
489
            $syspref_cache->clear_from_cache("syspref_$var");
490
        }
491
492
        return 1;
493
    }
494
    return 0;
495
}
496
497
=head2 csv_delimiter
413
=head2 csv_delimiter
498
414
499
    $delimiter = C4::Context->csv_delimiter;
415
    $delimiter = C4::Context->csv_delimiter;
(-)a/Koha/Config/SysPrefs.pm (-3 / +84 lines)
Lines 19-27 package Koha::Config::SysPrefs; Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use C4::Context;
22
23
23
use Koha::Database;
24
use Koha::Caches;
24
25
use Koha::Config::SysPref;
25
use Koha::Config::SysPref;
26
26
27
use base qw(Koha::Objects);
27
use base qw(Koha::Objects);
Lines 36-41 Koha::Config::SysPrefs - Koha System Preference object set class Link Here
36
36
37
=cut
37
=cut
38
38
39
=head2 set_preference
40
41
  Koha::Config::SysPrefs->set_preference( $variable, $value, [ $explanation, $type, $options ] );
42
43
This updates a preference's value both in the systempreferences table and in
44
the sysprefs cache. If the optional parameters are provided, then the query
45
becomes a create. It won't update the parameters (except value) for an existing
46
preference.
47
48
=cut
49
50
sub set_preference {
51
    my ( $self, $variable, $value, $explanation, $type, $options ) = @_;
52
53
    my $variable_case = $variable;
54
    $variable = lc $variable;
55
56
    my $syspref = Koha::Config::SysPrefs->find($variable);
57
    $type =
58
        $type    ? $type
59
      : $syspref ? $syspref->type
60
      :            undef;
61
62
    $value = 0 if ( $type && $type eq 'YesNo' && $value eq '' );
63
64
    # force explicit protocol on OPACBaseURL
65
    if ( $variable eq 'opacbaseurl' && $value && substr( $value, 0, 4 ) !~ /http/ ) {
66
        $value = 'http://' . $value;
67
    }
68
69
    if ($syspref) {
70
        $syspref->set(
71
            {   ( defined $value ? ( value       => $value )       : () ),
72
                ( $explanation   ? ( explanation => $explanation ) : () ),
73
                ( $type          ? ( type        => $type )        : () ),
74
                ( $options       ? ( options     => $options )     : () ),
75
            }
76
        )->store;
77
    } else {
78
        $syspref = Koha::Config::SysPref->new(
79
            {   variable    => $variable_case,
80
                value       => $value,
81
                explanation => $explanation || undef,
82
                type        => $type,
83
                options     => $options || undef,
84
            }
85
        )->store();
86
    }
87
88
    if ( $C4::Context::use_syspref_cache ) {
89
        my $syspref_cache = Koha::Caches->get_instance('syspref');
90
        $syspref_cache->set_in_cache( "syspref_$variable", $value );
91
    }
92
93
    return $syspref;
94
}
95
96
=head2 delete_preference
97
98
    Koha::Config::SysPrefs->delete_preference( $variable );
99
100
This deletes a system preference from the database. Returns a true value on
101
success. Failure means there was an issue with the database, not that there
102
was no syspref of the name.
103
104
=cut
105
106
sub delete_preference {
107
    my ( $self, $var ) = @_;
108
109
    if ( Koha::Config::SysPrefs->find( $var )->delete ) {
110
        if ( $C4::Context::use_syspref_cache ) {
111
            my $syspref_cache = Koha::Caches->get_instance('syspref');
112
            $syspref_cache->clear_from_cache("syspref_$var");
113
        }
114
        return 1;
115
    }
116
    return 0;
117
}
118
119
120
39
=head3 type
121
=head3 type
40
122
41
=cut
123
=cut
42
- 

Return to bug 35250