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

(-)a/C4/Context.pm (-16 / +60 lines)
Lines 102-107 use ZOOM; Link Here
102
use XML::Simple;
102
use XML::Simple;
103
use C4::Boolean;
103
use C4::Boolean;
104
use C4::Debug;
104
use C4::Debug;
105
use C4::Log qw/ logaction /;
105
use POSIX ();
106
use POSIX ();
106
use DateTime::TimeZone;
107
use DateTime::TimeZone;
107
use Module::Load::Conditional qw(can_load);
108
use Module::Load::Conditional qw(can_load);
Lines 623-659 sub clear_syspref_cache { Link Here
623
624
624
=head2 set_preference
625
=head2 set_preference
625
626
626
  C4::Context->set_preference( $variable, $value );
627
  C4::Context->set_preference( $variable, $value, [ $explanation, $type, $options ] );
627
628
628
This updates a preference's value both in the systempreferences table and in
629
This updates a preference's value both in the systempreferences table and in
629
the sysprefs cache.
630
the sysprefs cache. If the optional parameters are provided, then the query
631
becomes a create. It won't update the parameters (except value) for an existing
632
preference.
630
633
631
=cut
634
=cut
632
635
633
sub set_preference {
636
sub set_preference {
634
    my $self = shift;
637
    my ( $self, $var, $value, $expl, $type, $options ) = @_;
635
    my $var = lc(shift);
638
    $var = lc($var);
636
    my $value = shift;
637
639
638
    my $dbh = C4::Context->dbh or return 0;
640
    my $dbh = C4::Context->dbh or return 0;
639
641
640
    my $type = $dbh->selectrow_array( "SELECT type FROM systempreferences WHERE variable = ?", {}, $var );
642
    my $db_type = $dbh->selectrow_array(
641
643
        "SELECT type FROM systempreferences WHERE variable = ?",
642
    $value = 0 if ( $type && $type eq 'YesNo' && $value eq '' );
644
        {}, $var );
643
645
644
    my $sth = $dbh->prepare( "
646
    $value = 0 if ( $db_type && $db_type eq 'YesNo' && $value eq '' );
645
      INSERT INTO systempreferences
646
        ( variable, value )
647
        VALUES( ?, ? )
648
        ON DUPLICATE KEY UPDATE value = VALUES(value)
649
    " );
650
647
651
    if($sth->execute( $var, $value )) {
648
    my ( $query, @params, $logtype );
652
        $syspref_cache{$var} = [time, $value];
649
    if ( !defined($db_type) ) {
650
        $query = '
651
INSERT INTO systempreferences
652
    ( variable, value, explanation, type, options )
653
VALUES( ?, ?, ?, ?, ? )';
654
        @params = ( $var, $value, $expl, $type, $options );
655
        $logtype = 'ADD';
656
    }
657
    else {
658
        $query = '
659
INSERT INTO systempreferences
660
    ( variable, value )
661
VALUES( ?, ? )
662
ON DUPLICATE KEY UPDATE value = VALUES(value)';
663
        @params = ( $var, $value );
664
        $logtype = 'MODIFY';
665
    }
666
    my $sth = $dbh->prepare($query);
667
    if ( $sth->execute(@params) ) {
668
        $syspref_cache{$var} = [ time, $value ];
669
        logaction( 'SYSTEMPREFERENCE', $logtype, undef, $var . " | " . $value );
653
    }
670
    }
654
    $sth->finish;
671
    $sth->finish;
655
}
672
}
656
673
674
=head2 delete_preference
675
676
    C4::Context->delete_preference( $variable );
677
678
This deletes a system preference from the database. Returns a true value on
679
success. Failure means there was an issue with the database, not that there
680
was no syspref of the name.
681
682
=cut
683
684
sub delete_preference {
685
    my ( $self, $var ) = @_;
686
687
    # We want to log the previous value
688
    my $val = C4::Context->preference($var);
689
    my $dbh = C4::Context->dbh or die "Unable to talk to the database";
690
    my $sth = $dbh->prepare("DELETE FROM systempreferences WHERE variable=?");
691
    my $res = $sth->execute($var);
692
    if ($res) {
693
        delete $syspref_cache{$var};
694
        logaction( 'SYSTEMPREFERENCE', 'DELETE', undef,
695
            $var . " | " . ( $var // 'undefined' ) );
696
        return 1;
697
    }
698
    warn "Unable to delete syspref: " . $sth->errstr;
699
    return 0;
700
}
657
# AUTOLOAD
701
# AUTOLOAD
658
# This implements C4::Config->foo, and simply returns
702
# This implements C4::Config->foo, and simply returns
659
# C4::Context->config("foo"), as described in the documentation for
703
# C4::Context->config("foo"), as described in the documentation for
(-)a/admin/systempreferences.pl (-49 / +13 lines)
Lines 50-56 use C4::Context; Link Here
50
use C4::Koha;
50
use C4::Koha;
51
use C4::Languages qw(getTranslatedLanguages);
51
use C4::Languages qw(getTranslatedLanguages);
52
use C4::ClassSource;
52
use C4::ClassSource;
53
use C4::Log;
54
use C4::Output;
53
use C4::Output;
55
use YAML::Syck qw( Dump LoadFile );
54
use YAML::Syck qw( Dump LoadFile );
56
55
Lines 269-292 if ( $op eq 'update_and_reedit' ) { Link Here
269
            );    # we show only the TMPL_VAR names $op
268
            );    # we show only the TMPL_VAR names $op
270
        }
269
        }
271
    }
270
    }
272
    my $dbh   = C4::Context->dbh;
271
    my $variable = $input->param('variable');
273
    my $query = "select * from systempreferences where variable=?";
272
    C4::Context->set_preference($variable, $value) unless C4::Context->config('demo');
274
    my $sth   = $dbh->prepare($query);
275
    $sth->execute( $input->param('variable') );
276
    if ( $sth->rows ) {
277
        unless ( C4::Context->config('demo') ) {
278
            my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
279
            $sth->execute( $value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions') );
280
            logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
281
        }
282
    } else {
283
        unless ( C4::Context->config('demo') ) {
284
            my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
285
            $sth->execute( $input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
286
            logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $input->param('value') );
287
        }
288
    }
289
290
}
273
}
291
274
292
################## ADD_FORM ##################################
275
################## ADD_FORM ##################################
Lines 315-327 if ( $op eq 'add_form' ) { Link Here
315
################## ADD_VALIDATE ##################################
298
################## ADD_VALIDATE ##################################
316
    # called by add_form, used to insert/modify data in DB
299
    # called by add_form, used to insert/modify data in DB
317
} elsif ( $op eq 'add_validate' ) {
300
} elsif ( $op eq 'add_validate' ) {
318
    my $dbh = C4::Context->dbh;
319
    my $sth = $dbh->prepare("select * from systempreferences where variable=?");
320
    $sth->execute( $input->param('variable') );
321
322
    # to handle multiple values
301
    # to handle multiple values
323
    my $value;
302
    my $value;
324
303
304
    my $variable = $input->param('variable');
305
    my $expl     = $input->param('explanation');
306
    my $type     = $input->param('preftype');
307
    my $options  = $input->param('prefoptions');
308
325
    # handle multiple value strings (separated by ',')
309
    # handle multiple value strings (separated by ',')
326
    my $params = $input->Vars;
310
    my $params = $input->Vars;
327
    if ( defined $params->{'value'} ) {
311
    if ( defined $params->{'value'} ) {
Lines 338-386 if ( $op eq 'add_form' ) { Link Here
338
        }
322
        }
339
    }
323
    }
340
324
341
    if ( $input->param('preftype') eq 'Upload' ) {
325
    if ( $type eq 'Upload' ) {
342
        my $lgtfh = $input->upload('value');
326
        my $lgtfh = $input->upload('value');
343
        $value = join '', <$lgtfh>;
327
        $value = join '', <$lgtfh>;
344
        $value = encode_base64($value);
328
        $value = encode_base64($value);
345
    }
329
    }
346
330
347
    if ( $sth->rows ) {
331
    C4::Context->set_preference( $variable, $value, $expl, $type, $options )
348
        unless ( C4::Context->config('demo') ) {
332
      unless C4::Context->config('demo');
349
            my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
350
            $sth->execute( $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable') );
351
            logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $input->param('variable') . " | " . $value );
352
        }
353
    } else {
354
        unless ( C4::Context->config('demo') ) {
355
            my $sth = $dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
356
            $sth->execute( $input->param('variable'), $value, $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions') );
357
            logaction( 'SYSTEMPREFERENCE', 'ADD', undef, $input->param('variable') . " | " . $value );
358
        }
359
    }
360
    print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=\"></html>";
333
    print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=\"></html>";
361
    exit;
334
    exit;
362
################## DELETE_CONFIRM ##################################
335
################## DELETE_CONFIRM ##################################
363
    # called by default form, used to confirm deletion of data in DB
336
    # called by default form, used to confirm deletion of data in DB
364
} elsif ( $op eq 'delete_confirm' ) {
337
} elsif ( $op eq 'delete_confirm' ) {
365
    my $dbh = C4::Context->dbh;
338
    my $value = C4::Context->preference($searchfield);
366
    my $sth = $dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
367
    $sth->execute($searchfield);
368
    my $data = $sth->fetchrow_hashref;
369
    $template->param(
339
    $template->param(
370
        searchfield => $searchfield,
340
        searchfield => $searchfield,
371
        Tvalue      => $data->{'value'},
341
        Tvalue      => $value,
372
    );
342
    );
373
343
374
    # END $OP eq DELETE_CONFIRM
344
    # END $OP eq DELETE_CONFIRM
375
################## DELETE_CONFIRMED ##################################
345
################## DELETE_CONFIRMED ##################################
376
    # called by delete_confirm, used to effectively confirm deletion of data in DB
346
    # called by delete_confirm, used to effectively confirm deletion of data in DB
377
} elsif ( $op eq 'delete_confirmed' ) {
347
} elsif ( $op eq 'delete_confirmed' ) {
378
    my $dbh = C4::Context->dbh;
348
    C4::Context->delete_preference($searchfield);
379
    my $sth = $dbh->prepare("delete from systempreferences where variable=?");
380
    $sth->execute($searchfield);
381
    my $logstring = $searchfield . " | " . $Tvalue;
382
    logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring );
383
384
    # END $OP eq DELETE_CONFIRMED
349
    # END $OP eq DELETE_CONFIRMED
385
################## DEFAULT ##################################
350
################## DEFAULT ##################################
386
} else {    # DEFAULT
351
} else {    # DEFAULT
387
- 

Return to bug 11998