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