Resynching Savannah
[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         
77 # Circulation
78         $tabsysprefs{maxoutstanding}="Circulation";
79         $tabsysprefs{maxreserves}="Circulation";
80         $tabsysprefs{noissuescharge}="Circulation";
81         $tabsysprefs{patronimages}="Circulation";
82         $tabsysprefs{printcirculationslips}="Circulation";
83         $tabsysprefs{ReturnBeforeExpiry}="Circulation";
84         $tabsysprefs{allowrenewalsbefore}="Circulation";
85         $tabsysprefs{defaultBranch}="Circulation";
86         $tabsysprefs{strictrenewals}="Circulation";
87 # Intranet
88         $tabsysprefs{TemplateEncoding}="Intranet";
89         $tabsysprefs{template}="Intranet";
90         $tabsysprefs{intranetstylesheet}="Intranet";
91         $tabsysprefs{IntranetNav}="Intranet";
92         $tabsysprefs{intranetcolorstylesheet}="Intranet";
93         $tabsysprefs{Activate_Log}="Intranet";
94         $tabsysprefs{allowrenewalsbefore}="Intranet";
95         
96         $tabsysprefs{zebrawait}="Intranet";
97         $tabsysprefs{retrieve_from}="Intranet";
98         $tabsysprefs{batchMode}="Intranet";
99         
100 # Members
101         $tabsysprefs{automembernum}="Members";
102         $tabsysprefs{checkdigit}="Members";
103         $tabsysprefs{NotifyBorrowerDeparture}="Members";
104 # OPAC
105         $tabsysprefs{AmazonAssocTag}="OPAC";
106         $tabsysprefs{AmazonContent}="OPAC";
107         $tabsysprefs{AmazonDevKey}="OPAC";
108         $tabsysprefs{AnonSuggestions}="OPAC";
109         $tabsysprefs{BiblioDefaultView}="OPAC";
110         $tabsysprefs{Disable_Dictionary}="OPAC";
111         $tabsysprefs{hidelostitems}="OPAC";
112         $tabsysprefs{LibraryName}="OPAC";
113         $tabsysprefs{opacbookbag}="OPAC";
114         $tabsysprefs{opaccolorstylesheet}="OPAC";
115         $tabsysprefs{opaccredits}="OPAC";
116         $tabsysprefs{opaclanguages}="OPAC";
117         $tabsysprefs{opaclanguagesdisplay}="OPAC";
118         $tabsysprefs{opaclargeimage}="OPAC";
119         $tabsysprefs{opaclayoutstylesheet}="OPAC";
120         $tabsysprefs{OpacNav}="OPAC";
121         $tabsysprefs{OpacPasswordChange}="OPAC";
122         $tabsysprefs{opacreadinghistory}="OPAC";
123         $tabsysprefs{opacsmallimage}="OPAC";
124         $tabsysprefs{opacstylesheet}="OPAC";
125         $tabsysprefs{opacthemes}="OPAC";
126         $tabsysprefs{opacuserlogin}="OPAC";
127         $tabsysprefs{SubscriptionHistory}="OPAC";
128         $tabsysprefs{suggestion}="OPAC";
129         $tabsysprefs{virtualshelves}="OPAC";
130         $tabsysprefs{opacheader}="OPAC";
131         $tabsysprefs{allowrenewsfromopac}="OPAC";
132
133 sub StringSearch  {
134         my ($env,$searchstring,$type)=@_;
135         my $dbh = C4::Context->dbh;
136         $searchstring=~ s/\'/\\\'/g;
137         my @data=split(' ',$searchstring);
138         my $count=@data;
139         my @results;
140         my $cnt=0;
141         if ($type){
142                 foreach my $syspref (sort keys %tabsysprefs){
143                         if ($tabsysprefs{$syspref} eq $type){
144                                 my $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
145                                 $sth->execute($syspref);
146                                 while (my $data=$sth->fetchrow_hashref){
147                                         $data->{value} =~ s/</&lt;/g;
148                                         $data->{value} =~ s/>/&lt;/g;
149                                         $data->{value}=substr($data->{value},0,100)."..." if length($data->{value}) >100;
150                                         push(@results,$data);
151                                         $cnt++;
152                                 }
153                                 $sth->finish;
154                         }
155                 }
156         } else {
157                 my $strsth ="Select variable,value,explanation,type,options from systempreferences where variable not in (";  
158                 foreach my $syspref (keys %tabsysprefs){
159                         $strsth .= $dbh->quote($syspref).",";
160                 }
161                 $strsth =~ s/,$/) /;
162                 $strsth .= " order by variable";
163                 #warn $strsth;
164                 my $sth=$dbh->prepare($strsth);
165                 $sth->execute();
166                 while (my $data=$sth->fetchrow_hashref){
167                         $data->{value}=substr($data->{value},0,100);
168                         push(@results,$data);
169                         $cnt++;
170                 }
171                 $sth->finish;
172         }
173         return ($cnt,\@results);
174 }
175
176 my $input = new CGI;
177 my $searchfield=$input->param('searchfield');
178 my $offset=$input->param('offset');
179 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
180
181 my ($template, $borrowernumber, $cookie)
182     = get_template_and_user({template_name => "admin/systempreferences.tmpl",
183                              query => $input,
184                              type => "intranet",
185                              authnotrequired => 0,
186                              flagsrequired => {parameters => 1},
187                              debug => 1,
188                              });
189 my $pagesize=100;
190 my $op = $input->param('op');
191 $searchfield=~ s/\,//g;
192
193 if ($op) {
194 $template->param(script_name => $script_name,
195                                                 $op              => 1); # we show only the TMPL_VAR names $op
196 } else {
197 $template->param(script_name => $script_name,
198                                                 else              => 1); # we show only the TMPL_VAR names $op
199 }
200
201 if ($op eq 'update_and_reedit') {
202         foreach ($input->param) {
203         }
204         my $value='';
205         if (my $currentorder=$input->param('currentorder')) {
206                 my @currentorder=split /\|/, $currentorder;
207                 my $orderchanged=0;
208                 foreach my $param ($input->param) {
209                         if ($param=~m#up-(\d+).x#) {
210                                 my $temp=$currentorder[$1];
211                                 $currentorder[$1]=$currentorder[$1-1];
212                                 $currentorder[$1-1]=$temp;
213                                 $orderchanged=1;
214                                 last;
215                         } elsif ($param=~m#down-(\d+).x#) {
216                                 my $temp=$currentorder[$1];
217                                 $currentorder[$1]=$currentorder[$1+1];
218                                 $currentorder[$1+1]=$temp;
219                                 $orderchanged=1;
220                                 last;
221                         }
222                 }
223                 $value=join ' ', @currentorder;
224                 if ($orderchanged) {
225                         $op='add_form';
226                         $template->param(script_name => $script_name,
227                                                         $op              => 1); # we show only the TMPL_VAR names $op
228                 } else {
229                         $op='';
230                         $searchfield='';
231                         $template->param(script_name => $script_name,
232                                                         else              => 1); # we show only the TMPL_VAR names $op
233                 }
234         }
235         my $dbh = C4::Context->dbh;
236         my $query="select * from systempreferences where variable=?";
237         my $sth=$dbh->prepare($query);
238         $sth->execute($input->param('variable'));
239         if ($sth->rows) {
240                 unless (C4::Context->config('demo') eq 1) {
241                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
242                         $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
243                         $sth->finish;
244                 }
245     } else {
246                 unless (C4::Context->config('demo') eq 1) {
247                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
248                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
249                         $sth->finish;
250                 }
251         }
252         $sth->finish;
253
254 }
255
256 ################## ADD_FORM ##################################
257 # called by default. Used to create form to add or  modify a record
258
259 if ($op eq 'add_form') {
260         #---- if primkey exists, it's a modify action, so read values to modify...
261         my $data;
262         if ($searchfield) {
263                 my $dbh = C4::Context->dbh;
264                 my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
265                 $sth->execute($searchfield);
266                 $data=$sth->fetchrow_hashref;
267                 $sth->finish;
268                 $template->param(modify => 1);
269         }
270
271         my @options;
272         foreach my $option (split(/\|/, $data->{'options'})) {
273                 my $selected='0';
274                 $option eq $data->{'value'} and $selected=1;
275                 push @options, { option => $option, selected => $selected };
276         }
277         if ($data->{'type'} eq 'Choice') {
278                 $template->param('type-choice' => 1);
279         } elsif ($data->{'type'} eq 'YesNo') {
280                 $template->param('type-yesno' => 1);
281                 $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
282                 ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
283         } elsif ($data->{'type'} eq 'Integer') {
284                 $template->param('type-free' => 1);
285                 $template->param('fieldlength' => $data->{'options'});
286         } elsif ($data->{'type'} eq 'Textarea') {
287                 $template->param('type-textarea' => 1);
288                 $data->{options} =~ /(.*)\|(.*)/;
289                 $template->param('cols' => $1, 'rows' => $2);;
290         } elsif ($data->{'type'} eq 'Float') {
291                 $template->param('type-free' => 1);
292                 $template->param('fieldlength' => $data->{'options'});
293         } elsif ($data->{'type'} eq 'Themes') {
294                 $template->param('type-choice' => 1);
295                 my $type='';
296                 ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
297                 @options=();
298                 my $currently_selected_themes;
299                 my $counter=0;
300                 foreach my $theme (split /\s+/, $data->{'value'}) {
301                     push @options, { option => $theme, counter => $counter };
302                     $currently_selected_themes->{$theme}=1;
303                     $counter++;
304                 }
305                 foreach my $theme (getallthemes($type)) {
306                         my $selected='0';
307                         next if $currently_selected_themes->{$theme};
308                         push @options, { option => $theme, counter => $counter };
309                         $counter++;
310                 }
311         } elsif ($data->{'type'} eq 'Languages') {
312                 $template->param('type-choice' => 1);
313                 my $type='';
314                 @options=();
315                 my $currently_selected_languages;
316                 my $counter=0;
317                 foreach my $language (split /\s+/, $data->{'value'}) {
318                     next if $language eq 'images';
319                     push @options, { option => $language, counter => $counter };
320                     $currently_selected_languages->{$language}=1;
321                     $counter++;
322                 }
323                 foreach my $language (getalllanguages()) {
324                         next if $language eq 'images';
325                         my $selected='0';
326                         next if $currently_selected_languages->{$language};
327                         push @options, { option => $language, counter => $counter };
328                         $counter++;
329                 }
330         } else {
331                 $template->param('type-free' => 1);
332                 $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
333         }
334         $template->param(explanation => $data->{'explanation'},
335                          value => $data->{'value'},
336                          type => $data->{'type'},
337                          options => \@options,
338                          preftype => $data->{'type'},
339                          prefoptions => $data->{'options'},
340                          searchfield => $searchfield);
341
342 ################## ADD_VALIDATE ##################################
343 # called by add_form, used to insert/modify data in DB
344 } elsif ($op eq 'add_validate') {
345         my $dbh = C4::Context->dbh;
346         my $sth=$dbh->prepare("select * from systempreferences where variable=?");
347         $sth->execute($input->param('variable'));
348         if ($sth->rows) {
349                 unless (C4::Context->config('demo') eq 1) {
350                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
351                         $sth->execute($input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
352                         $sth->finish;
353                 }
354         } else {
355                 unless (C4::Context->config('demo') eq 1) {
356                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
357                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
358                         $sth->finish;
359                 }
360         }
361         $sth->finish;
362         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl?tab=".$tabsysprefs{$input->param('variable')}."\"></html>";
363         exit;
364 ################## DELETE_CONFIRM ##################################
365 # called by default form, used to confirm deletion of data in DB
366 } elsif ($op eq 'delete_confirm') {
367         my $dbh = C4::Context->dbh;
368         my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
369         $sth->execute($searchfield);
370         my $data=$sth->fetchrow_hashref;
371         $sth->finish;
372         $template->param(searchfield => $searchfield,
373                                                         Tvalue => $data->{'value'},
374                                                         );
375
376                                                                                                         # END $OP eq DELETE_CONFIRM
377 ################## DELETE_CONFIRMED ##################################
378 # called by delete_confirm, used to effectively confirm deletion of data in DB
379 } elsif ($op eq 'delete_confirmed') {
380         my $dbh = C4::Context->dbh;
381         my $sth=$dbh->prepare("delete from systempreferences where variable=?");
382         $sth->execute($searchfield);
383         $sth->finish;
384                                                                                                         # END $OP eq DELETE_CONFIRMED
385 ################## DEFAULT ##################################
386 } else { # DEFAULT
387         #Adding tab management for system preferences
388         my $tab=$input->param('tab');
389         
390         my $env;
391         my ($count,$results)=StringSearch($env,$searchfield,$tab);
392         my $toggle=0;
393         my @loop_data = ();
394         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
395                 if ($toggle eq 0){
396                         $toggle=1;
397                 } else {
398                         $toggle=0;
399                 }
400                 my %row_data;  # get a fresh hash for the row data
401                 $row_data{variable} = $results->[$i]{'variable'};
402                 $row_data{value} = $results->[$i]{'value'};
403                 $row_data{explanation} = $results->[$i]{'explanation'};
404                 $row_data{toggle} = $toggle;
405                 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
406                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
407                 push(@loop_data, \%row_data);
408         }
409         $tab=($tab?$tab:"Others");
410         $template->param(loop => \@loop_data, $tab => 1);
411         if ($offset>0) {
412                 my $prevpage = $offset-$pagesize;
413                 $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
414         }
415         if ($offset+$pagesize<$count) {
416                 my $nextpage =$offset+$pagesize;
417                 $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
418         }
419 } #---- END $OP eq DEFAULT
420 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
421                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
422                 IntranetNav => C4::Context->preference("IntranetNav"),
423                 );
424 output_html_with_http_headers $input, $cookie, $template->output;