New XML API
[koha.git] / admin / systempreferences.pl
1 #!/usr/bin/perl
2
3 #script to administer the systempref table
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # ALGO :
8 # this script use an $op to know what to do.
9 # if $op is empty or none of the above values,
10 #       - the default screen is build (with all records, or filtered datas).
11 #       - the   user can clic on add, modify or delete record.
12 # if $op=add_form
13 #       - if primkey exists, this is a modification,so we read the $primkey record
14 #       - builds the add/modify form
15 # if $op=add_validate
16 #       - the user has just send datas, so we create/modify the record
17 # if $op=delete_form
18 #       - we show the record having primkey=$primkey and ask for deletion validation form
19 # if $op=delete_confirm
20 #       - we delete the record having primkey=$primkey
21
22
23 # Copyright 2000-2002 Katipo Communications
24 #
25 # This file is part of Koha.
26 #
27 # Koha is free software; you can redistribute it and/or modify it under the
28 # terms of the GNU General Public License as published by the Free Software
29 # Foundation; either version 2 of the License, or (at your option) any later
30 # version.
31 #
32 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
33 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
34 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
35 #
36 # You should have received a copy of the GNU General Public License along with
37 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
38 # Suite 330, Boston, MA  02111-1307 USA
39
40 use strict;
41 use CGI;
42 use C4::Auth;
43 use C4::Context;
44 use C4::Koha;
45 use C4::Output;
46 use C4::Interface::CGI::Output;
47 use C4::Search;
48 use C4::Context;
49
50 my %tabsysprefs;
51 # Acquisitions
52         $tabsysprefs{acquisitions}="Acquisitions";
53         $tabsysprefs{gist}="Acquisitions";
54 # Admin
55         $tabsysprefs{dateformat}="Admin";
56         $tabsysprefs{insecure}="Admin";
57         $tabsysprefs{KohaAdmin}="Admin";
58 # Authorities
59         $tabsysprefs{authoritysep}="Authorities";
60 # Catalogue
61         $tabsysprefs{advancedMARCEditor}="Catalogue";
62         $tabsysprefs{autoBarcode}="Catalogue";
63         $tabsysprefs{hide_marc}="Catalogue";
64         $tabsysprefs{IntranetBiblioDefaultView} = "Catalogue";
65         $tabsysprefs{ISBD}="Catalogue";
66         $tabsysprefs{itemcallnumber}="Catalogue";
67         $tabsysprefs{LabelMARCView}="Catalogue";
68         $tabsysprefs{marc}="Catalogue";
69         $tabsysprefs{marcflavour}="Catalogue";
70         $tabsysprefs{serialsadditems}="Catalogue";
71         $tabsysprefs{sortbynonfiling}="Catalogue";
72         $tabsysprefs{MARCOrgCode}="Catalogue";
73         $tabsysprefs{z3950AuthorAuthFields}="Catalogue";
74         $tabsysprefs{z3950NormalizeAuthor}="Catalogue";
75         $tabsysprefs{SQLorZEBRA}="Catalogue";
76 # Circulation
77         $tabsysprefs{maxoutstanding}="Circulation";
78         $tabsysprefs{maxreserves}="Circulation";
79         $tabsysprefs{noissuescharge}="Circulation";
80         $tabsysprefs{patronimages}="Circulation";
81         $tabsysprefs{printcirculationslips}="Circulation";
82         $tabsysprefs{ReturnBeforeExpiry}="Circulation";
83         $tabsysprefs{allowrenewalsbefore}="Circulation";
84         $tabsysprefs{defaultBranch}="Circulation";
85         $tabsysprefs{strictrenewals}="Circulation";
86 # Intranet
87         $tabsysprefs{TemplateEncoding}="Intranet";
88         $tabsysprefs{template}="Intranet";
89         $tabsysprefs{intranetstylesheet}="Intranet";
90         $tabsysprefs{IntranetNav}="Intranet";
91         $tabsysprefs{intranetcolorstylesheet}="Intranet";
92         $tabsysprefs{Activate_Log}="Intranet";
93         $tabsysprefs{allowrenewalsbefore}="Intranet";
94         
95         $tabsysprefs{zebrawait}="Intranet";
96         $tabsysprefs{retrieve_from}="Intranet";
97         $tabsysprefs{batchMode}="Intranet";
98         
99 # Members
100         $tabsysprefs{automembernum}="Members";
101         $tabsysprefs{checkdigit}="Members";
102         $tabsysprefs{NotifyBorrowerDeparture}="Members";
103 # OPAC
104         $tabsysprefs{AmazonAssocTag}="OPAC";
105         $tabsysprefs{AmazonContent}="OPAC";
106         $tabsysprefs{AmazonDevKey}="OPAC";
107         $tabsysprefs{AnonSuggestions}="OPAC";
108         $tabsysprefs{BiblioDefaultView}="OPAC";
109         $tabsysprefs{Disable_Dictionary}="OPAC";
110         $tabsysprefs{hidelostitems}="OPAC";
111         $tabsysprefs{LibraryName}="OPAC";
112         $tabsysprefs{opacbookbag}="OPAC";
113         $tabsysprefs{opaccolorstylesheet}="OPAC";
114         $tabsysprefs{opaccredits}="OPAC";
115         $tabsysprefs{opaclanguages}="OPAC";
116         $tabsysprefs{opaclanguagesdisplay}="OPAC";
117         $tabsysprefs{opaclargeimage}="OPAC";
118         $tabsysprefs{opaclayoutstylesheet}="OPAC";
119         $tabsysprefs{OpacNav}="OPAC";
120         $tabsysprefs{OpacPasswordChange}="OPAC";
121         $tabsysprefs{opacreadinghistory}="OPAC";
122         $tabsysprefs{opacsmallimage}="OPAC";
123         $tabsysprefs{opacstylesheet}="OPAC";
124         $tabsysprefs{opacthemes}="OPAC";
125         $tabsysprefs{opacuserlogin}="OPAC";
126         $tabsysprefs{SubscriptionHistory}="OPAC";
127         $tabsysprefs{suggestion}="OPAC";
128         $tabsysprefs{virtualshelves}="OPAC";
129         $tabsysprefs{opacheader}="OPAC";
130         $tabsysprefs{allowrenewsfromopac}="OPAC";
131
132 sub StringSearch  {
133         my ($env,$searchstring,$type)=@_;
134         my $dbh = C4::Context->dbh;
135         $searchstring=~ s/\'/\\\'/g;
136         my @data=split(' ',$searchstring);
137         my $count=@data;
138         my @results;
139         my $cnt=0;
140         if ($type){
141                 foreach my $syspref (sort keys %tabsysprefs){
142                         if ($tabsysprefs{$syspref} eq $type){
143                                 my $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
144                                 $sth->execute($syspref);
145                                 while (my $data=$sth->fetchrow_hashref){
146                                         $data->{value} =~ s/</&lt;/g;
147                                         $data->{value} =~ s/>/&lt;/g;
148                                         $data->{value}=substr($data->{value},0,100)."..." if length($data->{value}) >100;
149                                         push(@results,$data);
150                                         $cnt++;
151                                 }
152                                 $sth->finish;
153                         }
154                 }
155         } else {
156                 my $strsth ="Select variable,value,explanation,type,options from systempreferences where variable not in (";  
157                 foreach my $syspref (keys %tabsysprefs){
158                         $strsth .= $dbh->quote($syspref).",";
159                 }
160                 $strsth =~ s/,$/) /;
161                 $strsth .= " order by variable";
162                 #warn $strsth;
163                 my $sth=$dbh->prepare($strsth);
164                 $sth->execute();
165                 while (my $data=$sth->fetchrow_hashref){
166                         $data->{value}=substr($data->{value},0,100);
167                         push(@results,$data);
168                         $cnt++;
169                 }
170                 $sth->finish;
171         }
172         return ($cnt,\@results);
173 }
174
175 my $input = new CGI;
176 my $searchfield=$input->param('searchfield');
177 my $offset=$input->param('offset');
178 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
179
180 my ($template, $borrowernumber, $cookie)
181     = get_template_and_user({template_name => "admin/systempreferences.tmpl",
182                              query => $input,
183                              type => "intranet",
184                              authnotrequired => 0,
185                              flagsrequired => {parameters => 1},
186                              debug => 1,
187                              });
188 my $pagesize=100;
189 my $op = $input->param('op');
190 $searchfield=~ s/\,//g;
191
192 if ($op) {
193 $template->param(script_name => $script_name,
194                                                 $op              => 1); # we show only the TMPL_VAR names $op
195 } else {
196 $template->param(script_name => $script_name,
197                                                 else              => 1); # we show only the TMPL_VAR names $op
198 }
199
200 if ($op eq 'update_and_reedit') {
201         foreach ($input->param) {
202         }
203         my $value='';
204         if (my $currentorder=$input->param('currentorder')) {
205                 my @currentorder=split /\|/, $currentorder;
206                 my $orderchanged=0;
207                 foreach my $param ($input->param) {
208                         if ($param=~m#up-(\d+).x#) {
209                                 my $temp=$currentorder[$1];
210                                 $currentorder[$1]=$currentorder[$1-1];
211                                 $currentorder[$1-1]=$temp;
212                                 $orderchanged=1;
213                                 last;
214                         } elsif ($param=~m#down-(\d+).x#) {
215                                 my $temp=$currentorder[$1];
216                                 $currentorder[$1]=$currentorder[$1+1];
217                                 $currentorder[$1+1]=$temp;
218                                 $orderchanged=1;
219                                 last;
220                         }
221                 }
222                 $value=join ' ', @currentorder;
223                 if ($orderchanged) {
224                         $op='add_form';
225                         $template->param(script_name => $script_name,
226                                                         $op              => 1); # we show only the TMPL_VAR names $op
227                 } else {
228                         $op='';
229                         $searchfield='';
230                         $template->param(script_name => $script_name,
231                                                         else              => 1); # we show only the TMPL_VAR names $op
232                 }
233         }
234         my $dbh = C4::Context->dbh;
235         my $query="select * from systempreferences where variable=?";
236         my $sth=$dbh->prepare($query);
237         $sth->execute($input->param('variable'));
238         if ($sth->rows) {
239                 unless (C4::Context->config('demo') eq 1) {
240                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
241                         $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
242                         $sth->finish;
243                 }
244     } else {
245                 unless (C4::Context->config('demo') eq 1) {
246                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
247                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
248                         $sth->finish;
249                 }
250         }
251         $sth->finish;
252
253 }
254
255 ################## ADD_FORM ##################################
256 # called by default. Used to create form to add or  modify a record
257
258 if ($op eq 'add_form') {
259         #---- if primkey exists, it's a modify action, so read values to modify...
260         my $data;
261         if ($searchfield) {
262                 my $dbh = C4::Context->dbh;
263                 my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
264                 $sth->execute($searchfield);
265                 $data=$sth->fetchrow_hashref;
266                 $sth->finish;
267                 $template->param(modify => 1);
268         }
269
270         my @options;
271         foreach my $option (split(/\|/, $data->{'options'})) {
272                 my $selected='0';
273                 $option eq $data->{'value'} and $selected=1;
274                 push @options, { option => $option, selected => $selected };
275         }
276         if ($data->{'type'} eq 'Choice') {
277                 $template->param('type-choice' => 1);
278         } elsif ($data->{'type'} eq 'YesNo') {
279                 $template->param('type-yesno' => 1);
280                 $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
281                 ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
282         } elsif ($data->{'type'} eq 'Integer') {
283                 $template->param('type-free' => 1);
284                 $template->param('fieldlength' => $data->{'options'});
285         } elsif ($data->{'type'} eq 'Textarea') {
286                 $template->param('type-textarea' => 1);
287                 $data->{options} =~ /(.*)\|(.*)/;
288                 $template->param('cols' => $1, 'rows' => $2);;
289         } elsif ($data->{'type'} eq 'Float') {
290                 $template->param('type-free' => 1);
291                 $template->param('fieldlength' => $data->{'options'});
292         } elsif ($data->{'type'} eq 'Themes') {
293                 $template->param('type-choice' => 1);
294                 my $type='';
295                 ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
296                 @options=();
297                 my $currently_selected_themes;
298                 my $counter=0;
299                 foreach my $theme (split /\s+/, $data->{'value'}) {
300                     push @options, { option => $theme, counter => $counter };
301                     $currently_selected_themes->{$theme}=1;
302                     $counter++;
303                 }
304                 foreach my $theme (getallthemes($type)) {
305                         my $selected='0';
306                         next if $currently_selected_themes->{$theme};
307                         push @options, { option => $theme, counter => $counter };
308                         $counter++;
309                 }
310         } elsif ($data->{'type'} eq 'Languages') {
311                 $template->param('type-choice' => 1);
312                 my $type='';
313                 @options=();
314                 my $currently_selected_languages;
315                 my $counter=0;
316                 foreach my $language (split /\s+/, $data->{'value'}) {
317                     next if $language eq 'images';
318                     push @options, { option => $language, counter => $counter };
319                     $currently_selected_languages->{$language}=1;
320                     $counter++;
321                 }
322                 foreach my $language (getalllanguages()) {
323                         next if $language eq 'images';
324                         my $selected='0';
325                         next if $currently_selected_languages->{$language};
326                         push @options, { option => $language, counter => $counter };
327                         $counter++;
328                 }
329         } else {
330                 $template->param('type-free' => 1);
331                 $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
332         }
333         $template->param(explanation => $data->{'explanation'},
334                          value => $data->{'value'},
335                          type => $data->{'type'},
336                          options => \@options,
337                          preftype => $data->{'type'},
338                          prefoptions => $data->{'options'},
339                          searchfield => $searchfield);
340
341 ################## ADD_VALIDATE ##################################
342 # called by add_form, used to insert/modify data in DB
343 } elsif ($op eq 'add_validate') {
344         my $dbh = C4::Context->dbh;
345         my $sth=$dbh->prepare("select * from systempreferences where variable=?");
346         $sth->execute($input->param('variable'));
347         if ($sth->rows) {
348                 unless (C4::Context->config('demo') eq 1) {
349                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
350                         $sth->execute($input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
351                         $sth->finish;
352                 }
353         } else {
354                 unless (C4::Context->config('demo') eq 1) {
355                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
356                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
357                         $sth->finish;
358                 }
359         }
360         $sth->finish;
361         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=".$tabsysprefs{$input->param('variable')}."\"></html>";
362         exit;
363 ################## DELETE_CONFIRM ##################################
364 # called by default form, used to confirm deletion of data in DB
365 } elsif ($op eq 'delete_confirm') {
366         my $dbh = C4::Context->dbh;
367         my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
368         $sth->execute($searchfield);
369         my $data=$sth->fetchrow_hashref;
370         $sth->finish;
371         $template->param(searchfield => $searchfield,
372                                                         Tvalue => $data->{'value'},
373                                                         );
374
375                                                                                                         # END $OP eq DELETE_CONFIRM
376 ################## DELETE_CONFIRMED ##################################
377 # called by delete_confirm, used to effectively confirm deletion of data in DB
378 } elsif ($op eq 'delete_confirmed') {
379         my $dbh = C4::Context->dbh;
380         my $sth=$dbh->prepare("delete from systempreferences where variable=?");
381         $sth->execute($searchfield);
382         $sth->finish;
383                                                                                                         # END $OP eq DELETE_CONFIRMED
384 ################## DEFAULT ##################################
385 } else { # DEFAULT
386         #Adding tab management for system preferences
387         my $tab=$input->param('tab');
388         
389         my $env;
390         my ($count,$results)=StringSearch($env,$searchfield,$tab);
391         my $toggle=0;
392         my @loop_data = ();
393         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
394                 if ($toggle eq 0){
395                         $toggle=1;
396                 } else {
397                         $toggle=0;
398                 }
399                 my %row_data;  # get a fresh hash for the row data
400                 $row_data{variable} = $results->[$i]{'variable'};
401                 $row_data{value} = $results->[$i]{'value'};
402                 $row_data{explanation} = $results->[$i]{'explanation'};
403                 $row_data{toggle} = $toggle;
404                 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
405                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
406                 push(@loop_data, \%row_data);
407         }
408         $tab=($tab?$tab:"Others");
409         $template->param(loop => \@loop_data, $tab => 1);
410         if ($offset>0) {
411                 my $prevpage = $offset-$pagesize;
412                 $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
413         }
414         if ($offset+$pagesize<$count) {
415                 my $nextpage =$offset+$pagesize;
416                 $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
417         }
418 } #---- END $OP eq DEFAULT
419 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
420                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
421                 IntranetNav => C4::Context->preference("IntranetNav"),
422                 );
423 output_html_with_http_headers $input, $cookie, $template->output;