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

(-)a/C4/Context.pm (-16 / +60 lines)
Lines 103-108 use ZOOM; Link Here
103
use XML::Simple;
103
use XML::Simple;
104
use C4::Boolean;
104
use C4::Boolean;
105
use C4::Debug;
105
use C4::Debug;
106
use C4::Log qw/ logaction /;
106
use POSIX ();
107
use POSIX ();
107
use DateTime::TimeZone;
108
use DateTime::TimeZone;
108
use Module::Load::Conditional qw(can_load);
109
use Module::Load::Conditional qw(can_load);
Lines 624-660 sub clear_syspref_cache { Link Here
624
625
625
=head2 set_preference
626
=head2 set_preference
626
627
627
  C4::Context->set_preference( $variable, $value );
628
  C4::Context->set_preference( $variable, $value, [ $explanation, $type, $options ] );
628
629
629
This updates a preference's value both in the systempreferences table and in
630
This updates a preference's value both in the systempreferences table and in
630
the sysprefs cache.
631
the sysprefs cache. If the optional parameters are provided, then the query
632
becomes a create. It won't update the parameters (except value) for an existing
633
preference.
631
634
632
=cut
635
=cut
633
636
634
sub set_preference {
637
sub set_preference {
635
    my $self = shift;
638
    my ( $self, $var, $value, $expl, $type, $options ) = @_;
636
    my $var = lc(shift);
639
    $var = lc($var);
637
    my $value = shift;
638
640
639
    my $dbh = C4::Context->dbh or return 0;
641
    my $dbh = C4::Context->dbh or return 0;
640
642
641
    my $type = $dbh->selectrow_array( "SELECT type FROM systempreferences WHERE variable = ?", {}, $var );
643
    my $db_type = $dbh->selectrow_array(
642
644
        "SELECT type FROM systempreferences WHERE variable = ?",
643
    $value = 0 if ( $type && $type eq 'YesNo' && $value eq '' );
645
        {}, $var );
644
646
645
    my $sth = $dbh->prepare( "
647
    $value = 0 if ( $db_type && $db_type eq 'YesNo' && $value eq '' );
646
      INSERT INTO systempreferences
647
        ( variable, value )
648
        VALUES( ?, ? )
649
        ON DUPLICATE KEY UPDATE value = VALUES(value)
650
    " );
651
648
652
    if($sth->execute( $var, $value )) {
649
    my ( $query, @params, $logtype );
653
        $syspref_cache{$var} = [time, $value];
650
    if ( !defined($db_type) ) {
651
        $query = '
652
INSERT INTO systempreferences
653
    ( variable, value, explanation, type, options )
654
VALUES( ?, ?, ?, ?, ? )';
655
        @params = ( $var, $value, $expl, $type, $options );
656
        $logtype = 'ADD';
657
    }
658
    else {
659
        $query = '
660
INSERT INTO systempreferences
661
    ( variable, value )
662
VALUES( ?, ? )
663
ON DUPLICATE KEY UPDATE value = VALUES(value)';
664
        @params = ( $var, $value );
665
        $logtype = 'MODIFY';
666
    }
667
    my $sth = $dbh->prepare($query);
668
    if ( $sth->execute(@params) ) {
669
        $syspref_cache{$var} = [ time, $value ];
670
        logaction( 'SYSTEMPREFERENCE', $logtype, undef, $var . " | " . $value );
654
    }
671
    }
655
    $sth->finish;
672
    $sth->finish;
656
}
673
}
657
674
675
=head2 delete_preference
676
677
    C4::Context->delete_preference( $variable );
678
679
This deletes a system preference from the database. Returns a true value on
680
success. Failure means there was an issue with the database, not that there
681
was no syspref of the name.
682
683
=cut
684
685
sub delete_preference {
686
    my ( $self, $var ) = @_;
687
688
    # We want to log the previous value
689
    my $val = C4::Context->preference($var);
690
    my $dbh = C4::Context->dbh or die "Unable to talk to the database";
691
    my $sth = $dbh->prepare("DELETE FROM systempreferences WHERE variable=?");
692
    my $res = $sth->execute($var);
693
    if ($res) {
694
        delete $syspref_cache{$var};
695
        logaction( 'SYSTEMPREFERENCE', 'DELETE', undef,
696
            $var . " | " . ( $var // 'undefined' ) );
697
        return 1;
698
    }
699
    warn "Unable to delete syspref: " . $sth->errstr;
700
    return 0;
701
}
658
# AUTOLOAD
702
# AUTOLOAD
659
# This implements C4::Config->foo, and simply returns
703
# This implements C4::Config->foo, and simply returns
660
# C4::Context->config("foo"), as described in the documentation for
704
# C4::Context->config("foo"), as described in the documentation for
(-)a/admin/systempreferences.pl (-47 / +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
    if ( $sth->rows ) {
348
        unless ( C4::Context->config('demo') ) {
332
        C4::Context->set_preference( $variable, $value, $expl, $type, $options )
349
            my $sth = $dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
333
        unless C4::Context->config('demo');
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
    }
334
    }
360
    print $input->redirect("/cgi-bin/koha/admin/systempreferences.pl?tab=");
335
    print $input->redirect("/cgi-bin/koha/admin/systempreferences.pl?tab=");
361
    exit;
336
    exit;
362
################## DELETE_CONFIRM ##################################
337
################## DELETE_CONFIRM ##################################
363
    # called by default form, used to confirm deletion of data in DB
338
    # called by default form, used to confirm deletion of data in DB
364
} elsif ( $op eq 'delete_confirm' ) {
339
} elsif ( $op eq 'delete_confirm' ) {
365
    my $dbh = C4::Context->dbh;
340
    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(
341
    $template->param(
370
        searchfield => $searchfield,
342
        searchfield => $searchfield,
371
        Tvalue      => $data->{'value'},
343
        Tvalue      => $value,
372
    );
344
    );
373
345
374
    # END $OP eq DELETE_CONFIRM
346
    # END $OP eq DELETE_CONFIRM
375
################## DELETE_CONFIRMED ##################################
347
################## DELETE_CONFIRMED ##################################
376
    # called by delete_confirm, used to effectively confirm deletion of data in DB
348
    # called by delete_confirm, used to effectively confirm deletion of data in DB
377
} elsif ( $op eq 'delete_confirmed' ) {
349
} elsif ( $op eq 'delete_confirmed' ) {
378
    my $dbh = C4::Context->dbh;
350
    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
351
    # END $OP eq DELETE_CONFIRMED
385
################## DEFAULT ##################################
352
################## DEFAULT ##################################
386
} else {    # DEFAULT
353
} else {    # DEFAULT
387
- 

Return to bug 11998