Bug 34794: Fix typo in recalls_to_pull.tt
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 #
20
21 use Modern::Perl;
22
23 use C4::Context;
24 use C4::Service;
25 use C4::Log;
26 use Koha::Token;
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/
35
36 =head1 DESCRIPTION
37
38 This service is used to set system preferences, either one at a time or in
39 batches.
40
41 =head1 METHODS
42
43 =cut
44
45 our ( $query, $response ) = C4::Service->init( parameters => 'manage_sysprefs' );
46
47 =head2 set_preference
48
49 =over 4
50
51 =item url path
52
53 POST /svc/config/systempreferences/$preference
54
55 =item url query
56
57 value=$value
58
59 =back
60
61 Used to set a single system preference.
62
63 =cut
64
65 sub set_preference {
66     my ( $preference ) = @_;
67     die "wrong_csrf_token\n" unless Koha::Token->new->check_csrf(
68         {
69             session_id => scalar $query->cookie('CGISESSID'),
70             token      => scalar $query->param('csrf_token'),
71         }
72     );
73
74     my $value = join( ',', $query->param( 'value' ) );
75     C4::Context->set_preference( $preference, $value );
76
77     C4::Service->return_success( $response );
78 }
79
80 =head2 get_preference
81
82 =over 4
83
84 =item url path
85
86 GET /svc/config/systempreferences/$preference
87
88 =item url query
89
90 preference=$pref_name
91
92 =back
93
94 Used to get a single system preference.
95
96 =cut
97
98 sub get_preference {
99     my $preference = scalar $query->param('pref');
100
101     my $value = C4::Context->preference( $preference );
102     $response->param( value => $value );
103
104     C4::Service->return_success( $response );
105 }
106
107
108
109 =head2 set_preferences
110
111 =over 4
112
113 =item url path
114
115 POST /svc/config/systempreferences/
116
117 =item url query
118
119 pref_$pref1=$value1&pref_$pref2=$value2
120
121 =back
122
123 Used to set several system preferences at once. Each preference you want to set
124 should be sent prefixed with pref. If you wanted to turn off the
125 virtualshelves syspref, for instance, you would POST the following:
126
127 pref_virtualshelves=0
128
129 =cut
130
131 sub set_preferences {
132     die "wrong_csrf_token\n" unless Koha::Token->new->check_csrf(
133         {
134             session_id => scalar $query->cookie('CGISESSID'),
135             token      => scalar $query->param('csrf_token'),
136         }
137     );
138
139     foreach my $param ( $query->param() ) {
140         my ( $pref ) = ( $param =~ /pref_(.*)/ );
141
142         next if ( !defined( $pref ) );
143
144         my $value = join( ',', $query->multi_param( $param ) );
145
146         C4::Context->set_preference( $pref, $value );
147     }
148
149     C4::Service->return_success( $response );
150 }
151
152 C4::Service->dispatch(
153     [ 'POST /([A-Za-z0-9_-]+)', [ 'value' ], \&set_preference ],
154     [ 'POST /', [], \&set_preferences ],
155     [ 'GET /', [ 'pref' ], \&get_preference],
156 );