Merge commit 'kc/master'
[koha.git] / svc / config / systempreferences
1 #!/usr/bin/perl
2
3 # Copyright 2009 Jesse Weaver
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #
20
21 use strict;
22 use warnings;
23
24 use C4::Context;
25 use C4::Service;
26 use C4::Log;
27
28 =head1 NAME
29
30 svc/config/systempreferences - Web service for setting system preferences
31
32 =head1 SYNOPSIS
33
34   POST /svc/config/systempreferences/insecure
35   POST /svc/config/systempreferences/
36
37 =head1 DESCRIPTION
38
39 This service is used to set system preferences, either one at a time or in
40 batches.
41
42 =head1 METHODS
43
44 =cut
45
46 our ( $query, $response ) = C4::Service->init( parameters => 1 );
47
48 =head2 set_preference
49
50 =over 4
51
52 POST /svc/config/systempreferences/$preference
53
54 value=$value
55
56 =back
57
58 Used to set a single system preference.
59
60 =cut
61
62 sub set_preference {
63     my ( $preference ) = @_;
64
65     unless ( C4::Context->config('demo') ) {
66         my $value = join( ',', $query->param( 'value' ) );
67         C4::Context->set_preference( $preference, $value );
68         logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $preference . " | " . $value );
69     }
70
71     C4::Service->return_success( $response );
72 }
73
74 =head2 set_preferences
75
76 =over 4
77
78 POST /svc/config/systempreferences/
79
80 pref_$pref1=$value1&pref_$pref2=$value2
81
82 =back
83
84 Used to set several system preferences at once. Each preference you want to set
85 should be sent prefixed with pref. If you wanted to turn off the
86 virtualshelves syspref, for instance, you would POST the following:
87
88 pref_virtualshelves=0
89
90 =cut
91
92 sub set_preferences {
93     unless ( C4::Context->config( 'demo' ) ) {
94         foreach my $param ( $query->param() ) {
95             my ( $pref ) = ( $param =~ /pref_(.*)/ );
96
97             next if ( !defined( $pref ) );
98
99             my $value = join( ',', $query->param( $param ) );
100
101             C4::Context->set_preference( $pref, $value );
102             logaction( 'SYSTEMPREFERENCE', 'MODIFY', undef, $pref . " | " . $value );
103         }
104     }
105
106     C4::Service->return_success( $response );
107 }
108
109 C4::Service->dispatch(
110     [ 'POST /([A-Za-z0-9_-]+)', [ 'value' ], \&set_preference ],
111     [ 'POST /', [], \&set_preferences ],
112 );